0

I have a project where I save a lot of data in the sqlite3 database. I want to create a simple application in Flask - displaying appropriate data from the database. New records are added to the database every few minutes - I want this application to refresh automatically. I prepared some information about it and found information that it can be done somehow using socketio Can flask framework send real-time data from server to client browser? or using AJAX. I tried to do something like this: or Python Flask date update real-time but I didn't manage to do it.

here's the code I'm using right now:

from flask import Flask, request, render_template, session, redirect
import numpy as np
import pandas as pd
import sqlite3


con = sqlite3.connect(r'path')

app = Flask(__name__)

df = pd.read_sql('select * from table1', con)
df1 = pd.read_sql('select * from table2', con)


@app.route('/', methods=("POST", "GET"))
def html_table():
    return render_template('app.html',
                           tables=[df.to_html(classes='data1'),df1.to_html(classes='data2')],
                           titles=df.columns.values)

if __name__ == '__main__':
    app.run()

Template:


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Title</title>
  </head>

  <body>

    {% for table in tables %}
            {{titles[loop.index]}}
            {{ table|safe }}
    {% endfor %}

</body>
</html>

in this case, everything is displayed, but it doesn't work in real time.
I'm asking for some instructions or some advice on how to do it? Can I use the links I posted here somehow? This is my first application in Flask so I don't quite know how to achieve such a thing.

3
  • I am not sure what you mean by "real time", but can't you just make all of the calls async? Database calls are notoriously slow due to network connections; you have sqlite, so it is different, but if you want the app to be responsive (which is what I will assume you mean by real time), then you should use this async await paradigm. Commented Aug 6, 2020 at 20:20
  • @MikeWilliamson I want these selects from the database to be displayed asynchronously in the application - if only some new record appears in the database, it will be displayed without reloading the whole application - currently , new records are not displayed until I rerun the whole application. Commented Aug 6, 2020 at 20:27
  • Then you will need to make your actions "bite-sized" and somehow cache differences. Right now you have pd.read_sql, which grabs the entire query at once. That is OK; it is outside of the route. But you also have df.to_html in your route. This means that it needs to be called every time the page is hit. You should do all of that to_html outside of the route, then maybe have some clever diff to a cache to see when it changes. That might help. Commented Aug 6, 2020 at 20:51

1 Answer 1

1

If i got your question what you want to do is load the data without loading the page. As you mentioned AJAX and socket.io, AJAX helps in getting the data on the same page but it requires reloading.It get different data on the same route without going to another route.Socketio will help you in achieving that.As it is used commonly in real time chat apps that is why we dont need to refresh the page to see the messages.In flask, use flask_socketio library to use it and you also need Javascript and by incorporating these two, you can do what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.