Flask+AJAX+Jquery+JINJA to dynamically update HTML Table

后端 未结 1 1261
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 10:33

I want to display the Port status dynamically. I don\'t want to reload the page to see a new value. I know how to get the Port status in Python(using uiApi()).

相关标签:
1条回答
  • 2021-01-03 11:20

    You will need two things : A route for an AJAX request that will return your data formatted as JSON (should be quite easy to do with Flask's jsonify function).

    Once you have that route setup, you need to call it using an AJAX call. Using jQuery it feels painless (but you can do it with vanilla JS too).

    <script>
        $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
        (function(){
            $.getJSON(
                $SCRIPT_ROOT+"/_stuff", // Your AJAX route here
                function(data) {
                    // Update the value in your table here
                }
            );
            setTimeout(arguments.callee, 10000);
        })();
    </script>
    

    In the snippet above you will need to specify the path of your AJAX route, and do things with the data value. For a quick test you can simply console.log(data) and check if the returned data are good.

    Note that the above snippets uses an anonymous function, and will fetch your data every 10 seconds (10000 ms).

    I hope that will help

    Documentation :
    Ajax with Flask
    jQuery.getJSON

    0 讨论(0)
提交回复
热议问题