0

My problem is, How to auto refresh browser when pandas dataframe will update. ? What is the easiest way ? I am practicing python for last 6 months and new in Flask and This is my first project. Please help.

This is my table.html file

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title> Table </title>          
        </head>
        <body>
            <div align="center">
                <table>
                    <h1>
                    <!--Displaying the converted table-->
                        {% for table in tables %}
                        <h2>{{titles[loop.index]}}</h2>                         
                        {{ table|safe }}
                        {% endfor %}    
                    </h1>
                </table>
            </div>
        </body>
    </html>

This is my main.py file

    import threading
    from flask import Flask, render_template
    from datetime import datetime
    import time as tmm
    from feed_to_dash import getData

    df = getData()

    def get_Data():
     global df
     while 1<2:
        t1=tmm.time()
        df = getData()
        
        print('Execution time for getData in seconds: ' + str((tmm.time() - t1)))
        tmm.sleep(1)
    
    threading.Thread(target=get_Data).start() 

    app = Flask(__name__)

    @app.route('/')
    @app.route('/table')
    def table():
        return render_template('table.html', tables=[df.to_html()], titles=[''])


    if __name__ == "__main__":
        app.run(host="localhost", port=int("5000"))
        get_Data()
user12647598
  • 21
  • 1
  • 3
  • This might relate to https://stackoverflow.com/questions/60115833/how-do-i-refresh-browser-from-server-side-with-node-js. That question is about node.js and express, but it can adapted for flask and python. – Michael M. Aug 13 '22 at 22:19
  • Sockets, server sent events and polling a route with `setInterval` are typical ways. – ggorlen Aug 13 '22 at 23:39

0 Answers0