你能在Joomla中展示Python Web代码吗?

5

我正在建设一个Joomla 3网站,但我需要定制相当多的页面。我知道我可以使用PHP来与Joomla搭配使用,但是是否也可以使用Python呢?具体而言,我想使用CherryPy编写一些自定义代码片段,但我希望它们能显示在本地的Joomla页面上(而不仅仅是iFrames)。这可行吗?


1
你已经有Joomla网站了吗?如果你不喜欢PHP而喜欢Python(我不怪你),为什么不使用一个用Python编写的CMS呢? - Chris Wesseling
5个回答

1

构建来自不同源(Joomla和CherryPy)页面最有效的方式可能是使用边缘端包含Edge Side Includes

基本上,您运行一个Joomla实例,用于提供包含ESI标记的文档。您运行一个CherryPy实例,用于提供要放置在esi标记位置上的部件。然后您运行一个支持ESI的反向代理,如varnish,将它们全部组合在一起。

这可能看起来有很多移动部分,但响应速度会像...非常快。调整缓存部分,可以减轻Joomla和数据库的工作负担。

其他答案中的exec方法适用于打印内容的小型Python脚本,但不适用于CherryPy。


这里有一些与此相关的讨论 https://groups.google.com/forum/#!topic/joomla-dev-framework/-3TU_ab5M3A - Elin

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
 ?>

使用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访问Python/CherryPy服务网站

另一个解决方案是,不使用PHP来获取内容,而是从客户端进行获取。基本上,您将使用Ajax请求到CherryPy服务的网站,获取其内容并嵌入到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);
    }
 });

0

在考虑Python之前

  • 你想要自定义什么?(也许已经存在一些聪明的Javascript或Joomla扩展)

  • 考虑到你正在使用Joomla,难道Joomla的方式不是解决你的问题的更好方法吗?(更改模板或特定模块和组件的视图模板)

    换句话说:你是否足够了解Joomla,知道你需要其他东西?请参见下文:

如果Python仍然是正确的选择:

  • 你的主机支持Python吗?

  • 你应该重新考虑你的CMS选择吗?

我喜欢你选择CherryPy。


0

It's possible, but not very efficient.

You can execute a Python script from PHP using the exec() function: PHP Code:

exec("python /path/to/python-script.py");

There are also a variety of similar PHP functions that can be used to accomplish the same thing with minor differences to the way input and output are handled (passthru, system, proc_open, backticks).

The Python script will be executed using its command line interface - not using CGI (or similar) interface as you would have if the web server were directly executing the Python script. This means that the Python script will not have access to information about the HTTP request - GET/POST values, the client's IP address, the page URL, etc. You could pass this information from PHP to Python using command line parameters, a pipe, a temporary file or some other form of inter-process communication, but you need to pass each piece of required information explicitly.

The reason this is inefficient is because every call to exec will spawn a whole new process for the Python script. That's a fairly expensive operation to do on every HTTP request (this is why servers like Apache and interfaces like Fast-CGI re-use child processes and threads instead of creating new ones). Additionally, if you have more than one call to exec, every single one is going to spawn a new process.

摘自这里

更多信息:无法将Python代码放入Joomla


似乎“无法将Python代码放入Joomla”更多是关于将Python Web应用程序移植到Joomla中。 - Chris Wesseling

0

您可以通过一个php文件来执行它。尝试这样做:

exec("python/path/your-script.py");  

非常有趣。这也适用于输出到像CherryPy这样的Web框架的Python脚本吗?或者我该如何使Python脚本输出Web项? - Unknown Coder
@JimBeam 这仅适用于打印输出并退出的进程。正如您所怀疑的那样,CherryPy是一个长时间运行的进程,不会打印您的Web内容。 - Chris Wesseling

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接