Вопрос

I'm building a Joomla 3 web site but I have the need to customize quite a few pages. I know I can use PHP with Joomla, but is it also possible to use Python with it? Specifically, I'm looking to use CherryPy to write some custom pieces of code but I want them to be displayed in native Joomla pages (not just iFrames). Is this possible?

Это было полезно?

Решение

PHP execution of Python "scripts"

This will work for scripts, which do stuff and return the output, not for 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 to access a Python/CherryPy served website

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

cherrypy.quickstart(HelloWorld())

This starts a http://localhost:8080 and you should see Hello world!.

Now you could access CherryPy's output by accessing it at it's localhost:port. Not good performance-wise, but works.

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

Joomla + Ajax to access a Pyhton/CherryPy served website

An alternative solution would be, to not use PHP to fetch the content, but to do the fetch from client-side. Basically, you would use an Ajax-Request to the CherryPy served website, to fetch it's content and embed it into the dom of the Joomla served page.

 // 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);
    }
 });

Другие советы

Probably the most efficient way of constructing pages from different sources (Joomla and CherryPy) is probably using Edge Side Includes .

Basically you run a Joomla instance that serves the documents that contain ESI tags. You run a CherryPy instance that serves the pieces you want to put on the location of the esi tags. And you run a reverse proxy that supports ESI like varnish to stitch it all together.

That may look like a lot of moving parts, but it will be responsive like... something that is really responsive. Tune the caching parts and you relieve your Joomla and database from a lot of work.

The exec way from the other answers will work for small python scripts that print something, but will not work for CherryPy.

You can execute it through a php file. Try this:

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

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.

Extracted from here.

More info : Unable to put Python code to Joomla

Before considering Python

  • What are you wanting to customize? (perhaps some clever Javascript or a Joomla extension already exists)

  • Is the Joomla-way not a better solution for your problem, given the fact that you're using Joomla? (change the template, or the view-templates of the modules and component in particular)

    i.o.w.: do you understand Joomla enough to know that you need something else? See below:

If Python is still the way to go:

  • Does your hosting support Python?

  • Should you reconsider your choice of CMS?

I like your choice of CherryPy.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top