How to execute python scripts from react-js?
Question:
I have a use case where I need to execute a local python script from the browser and display the output returned.
I have written my web app using react-js.
My current solution is to launch a local instance of a jupyter notebook (assuming the client is already running it) and make the user execute the prefilled first cell, but I want to be able to do this directly from the browser.
Answers:
web pages cannot run arbitrary OS commands (such as executing a python scripts) from the browser – due to security reasons
a server has to provide an API for the web page to call (note that Jupyter Notebook is using it’s own server to execute commands specified in a browser – python code is NOT executed directly in the browser)
there are many ways how to start a python web server, I personally recommend Flask: http://flask.pocoo.org/
Update 2022: there is now a project to run Python in the browser, PyScript, which might be worth checking for some of the use cases
If you wish to program in Python but have the code execute in browser, you need either:
A) to run some kind of python program on client machine (separate from browser) which would listen and allow browser to connect to it using some kind of Api (Rest etc);
Or B) transpile your Python to client side Javascript which you inject into the web page client views. There are several tools that support it, such as https://www.transcrypt.org or http://pyjs.org
Or C) use libraries which allow client side Python in browser, such as http://www.skulpt.org (basically similar to B) but does it transparently)
I have a use case where I need to execute a local python script from the browser and display the output returned.
I have written my web app using react-js.
My current solution is to launch a local instance of a jupyter notebook (assuming the client is already running it) and make the user execute the prefilled first cell, but I want to be able to do this directly from the browser.
web pages cannot run arbitrary OS commands (such as executing a python scripts) from the browser – due to security reasons
a server has to provide an API for the web page to call (note that Jupyter Notebook is using it’s own server to execute commands specified in a browser – python code is NOT executed directly in the browser)
there are many ways how to start a python web server, I personally recommend Flask: http://flask.pocoo.org/
Update 2022: there is now a project to run Python in the browser, PyScript, which might be worth checking for some of the use cases
If you wish to program in Python but have the code execute in browser, you need either:
A) to run some kind of python program on client machine (separate from browser) which would listen and allow browser to connect to it using some kind of Api (Rest etc);
Or B) transpile your Python to client side Javascript which you inject into the web page client views. There are several tools that support it, such as https://www.transcrypt.org or http://pyjs.org
Or C) use libraries which allow client side Python in browser, such as http://www.skulpt.org (basically similar to B) but does it transparently)