Flask - Calling python function on button OnClick event

share link

by vsasikalabe dot icon Updated: Mar 9, 2023

technology logo
technology logo

Solution Kit Solution Kit  

We call the run() method to start the Flask application. The server reloads itself when the code changes. However, we should restart the execution manually for each change in the code. We can enable the debug support to avoid this inconvenience. The print option will display the output in the terminal, and the return statement will do the same process on the browser side. 

Set the debug property of the application object to True to enable the Debug mode. It must be set before running or passing the debug parameter to the run() method. A user can remember application URLs by the Modern web frameworks routing technique. Without having to navigate from the home page, accessing the desired page directly is useful. The route() in Flask holds the URL to a function. 

The background process is happening without any refreshing by redirecting the process directly. Flask is based on the Jinja2 template engine. An HTML file can be rendered by the render_template() function instead of returning hardcode HTML from the function. The getJSON() method gets JSON data using a GET request.   

Here is an example of how to call the Python function on the button Onclick event using Flask: