php - 你能在 Joomla 中显示 python 网页代码吗?

标签 php python joomla cherrypy joomla3.0

我正在构建一个 Joomla 3 网站,但我需要自定义很多页面。我知道我可以将 PHP 与 Joomla 一起使用,但是否也可以将 Python 与它一起使用?具体来说,我希望使用 CherryPy 编写一些自定义代码,但我希望它们显示在 native Joomla 页面(不仅仅是 iFrame)中。这可能吗?

最佳答案

Python“脚本”的 PHP 执行

这适用于执行操作并返回输出的脚本,不适用于 CherryPy。

 <?php
    // execute your Python script from PHP
    $command = escapeshellcmd('myPythonScript.py');
    $output = shell_exec($command);

    echo $output;

    // take response content to embed it into the page
 ?>

PHP 访问 Python/CherryPy 服务的网站

import cherrypy
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

这会启动一个 http://localhost:8080,您应该会看到 Hello world!

现在您可以通过在 localhost:port 访问 CherryPy 的输出来访问它。 性能不佳,但有效。

<?php
    $output = file_get_contents('http://localhost:8080/');
    echo $output;
?>

Joomla + Ajax 访问 Pyhton/CherryPy 服务的网站

另一种解决方案是不使用 PHP 来获取内容,而是从客户端进行获取。基本上,您将对 CherryPy 服务的网站使用 Ajax 请求,以获取其内容并将其嵌入到 Joomla 服务页面的 dom 中。

 // add jQuery Ajax reqeust from your Joomla page to CherryPy
 $.ajax({
    url: "https://localhost:8080/", // <-- access the 2nd served website
    type: 'GET',
    success: function(res) {
      //console.log(res);
      alert(res);
      $("#someElement").html(res);
    }
 });

关于php - 你能在 Joomla 中显示 python 网页代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22512321/

相关文章:

mysql - 以编程方式将 Joomla 文章创建到多个数据库表中

python - 如何测试是否已导入一个 python 模块?

php Extract 对此图像的最佳猜测来自谷歌图像搜索?

php - 从 MySQL 中的文本区域插入多行/列

php - 使用php使用图像代码在img标签中显示图像

python - 如何使用 Flask 和 Python 重定向到基于实时视频流的特定模板?

python - 将 Pandas DF 加载到 Big Query 失败

html - 不可见的 HTML 表单

css - 如何使 Joomla 主要内容和模块列动态匹配高度?

php - 在 Laravel 中的 MySQL 中插入 JSON 对象