Flask Button run Python without refreshing page?

前端 未结 3 455
攒了一身酷
攒了一身酷 2020-12-31 16:42

I am just getting started into python and flask (for the raspberry pi). I want a web application that would execute some python code to pan and tilt a camera and display a v

3条回答
  •  感动是毒
    2020-12-31 17:15

    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.

    
    
    
    
    //button
    

    Test

    Here when you press the button Test simple in the console you can see "Hello" is displaying without any refreshing.

提交回复
热议问题