Estimated reading time: 3 minutes
Are you developing a website and looking to execute some Python script directly from the website?
Here we take you through how to achieve this using Python Flask, the scenarios we demonstrate are as follows:
In both scenarios above you will be presented with pop-up boxes like below, but the python code can be changed to whatever you like.
The code you will need to run this is split into two. (A) The Index.HTML logic and (B) the app.py logic
The code for the INDEX.HTML page
In this instance, the app.py python script holds the commands that need to be executed.
Note the page load event goes to the route in app.py, namely ” @app.route(‘/’)”
The code for the APP.PY Python script
Here we are linking to the index.html page and taking commands from it and executing those requests.
In this scenario, the message boxes are rendered by using the win32api package.
The app is using the flask package to run the website.
The powerful thing here is you can start customising this logic to do what you like, examples include:
(A) Return graphs to the webpage.
(B) Process data received and return statistics on the data.
(C) Validate data received in Python and return a response. An example here could be a user logging into a database.