你能在Joomla中显示python web代码吗?

Unk*_*der 5 php python joomla cherrypy joomla3.0

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

Jen*_*och 1

PHP 执行 Python“脚本”

这适用于执行操作并返回输出的脚本,但不适用于 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
 ?>
Run Code Online (Sandbox Code Playgroud)

PHP 访问 Python/CherryPy 服务的网站

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

cherrypy.quickstart(HelloWorld())
Run Code Online (Sandbox Code Playgroud)

这将启动 ahttp://localhost:8080并且您应该看到Hello world!.

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

<?php
    $output = file_get_contents('http://localhost:8080/');
    echo $output;
?>
Run Code Online (Sandbox Code Playgroud)

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);
    }
 });
Run Code Online (Sandbox Code Playgroud)