Execute Python script via JavaScript?

I have a home server in my home network and would like to start/execute a Python file via JavaScript (unless there is a better way) from the website I have set up there.

Does anyone know how this can be achieved?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
18 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

(a) You can implement the server-side web application with Python. A microframework like Bottle, Flask or CherryPy is enough. You should either configure the web server accordingly (for Apache, for example, the mod_wsgi-Module) or you generally exchange the web server (for example for gunicorn).

(b) If you have already developed a server-side web application with a technology such as PHP, Node.js, or the like, the first option would of course be a little unfavorable, as you do not want to re-implement everything. As a rule, server-side web technologies have the possibility to send system commands to the OS via function and thus also to start Python scripts (if a Python interpreter is installed). In PHP, for example, the shell_exec-Function, Node.js has child_process– Module with the exec– Function.

(c) If you tend to call the Python script as described in the previous paragraph, you can also ask whether your Python script is really so Python-specific (for example, certain libraries that are only available in Python) or whether it could not be implemented with PHP/Node.js/… .

As regards the trigger that triggers the execution should be sufficient in the simplest case a GET request. You can use a simple link:

Start Python script

The target address refers to a request handler/script that executes the Python script/python code.

You can also fire GET requests with JavaScript. Either you let the target address load directly from the browser:

location.href = "/start";

or you send an AJAX request:

fetch("/start");
regex9
1 year ago
Reply to  FrozenArmy
  1. Does the system command generally work when you manually call it in the console?
  2. With the exec– Function (instead of shell_exec) you can also get the outputs/returns of the script more easily. Before the function it would be convenient to allow the output of all error messages and warnings temporarily.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
regex9
1 year ago

In the shell command, specify a correct path for the Python interpreter.

regex9
1 year ago

I still assume that the Python interpreter cannot be found about your claim. If the command is executed, it will now be in the directories to which the environment variable LD_LIBRARY_PATH shows, unsuccessful after /bin/python3 searched.

regex9
1 year ago

The Python interpreter is not in /usr/bin and python?

Otherwise, this return is not meaningful. Use exec or let you write the error message into the output channel by

2>&1

to the shell command.

regex9
1 year ago

What does the shell script return? There must be returns that have been executed by the system. Let’s do this var_dump output.

KarlRanseierIII
1 year ago

What does the Python file mean to start directly? So why do you need JS or vice versa, why should the python part be in Python?

RegExp’s answer is quite detailed, but there are still things missing:

When it comes to performing the script and letting it display something, even as SSI, when interacting, the python script can be executed as CGI (et. al.).

For servers like Apache there are also mod_python.

Finally, you can perform other executeables in (almost) any language, so also the python script. If it is convenient, both components can also exchange/communate data via IPC.

So there are always a whole range of ways (Aja, Python can also set the whole HTTP daemon if you like it better, with framework will be more convenient).

alexthenr14
1 year ago

It is possible, e.g. https://pyscript.net/

But it would be better to make it server-side

How this works there are enough tutorials on Youtube

alexthenr14
1 year ago
Reply to  FrozenArmy

Search for “python server website”

Python can also be used instead of PHP or NodeJS but is not so widespread