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.

DeepSea
  • 305
  • 2
  • 15
  • 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](https://jdhao.github.io/2020/06/07/asyncio_inside_flask/). – Mike Williamson Aug 06 '20 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. – DeepSea Aug 06 '20 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. – Mike Williamson Aug 06 '20 at 20:51

1 Answers1

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.

Irfan wani
  • 4,084
  • 2
  • 19
  • 34