Flask - Calling python function on button OnClick event


Solution 1:

You can simply do this with help of AJAX... Here is a example which calls a python function which prints hello without redirecting or refreshing the page.


In app.py put below code segment.

#rendering the HTML page which has the button
@app.route('/json')
def json():
    return render_template('json.html')

#background process happening without any refreshing
@app.route('/background_process_test')
def background_process_test():
    print ("Hello")
    return ("nothing")

And your json.html page should look like below.