I have a Flask project and need to load a SQLite table into an HTML table. I need the table to be loaded very frequently in order to get real time information of what's in the table; however, I do not want to refresh the entire page in order to do this.
Right now I am using an ajax call every five seconds; the call is successfully going through every five seconds (I know this because I put a print hello statement in the Flask method it is calling to see if it's being called and it is, but my HTML table does not load the updated database unless I refresh the whole page. So the ajax call goes through, but the HTML is not rendering with the new database information (I purposely change the database state to test this). If I refresh the page using the browser's reload button, however, then the HTML table will update and show the new contents of the SQlite table.
Here is my HTML code:
<table class="table table-responsive table-hover">
<thead>
<tr>
<th>Incident #</th>
<th>Username</th>
<th>Description</th>
<th>Resolve Incident</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td>{{item[2]}}</td>
<td>
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#ModalResolveForm">Resolve Ticket</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
Here is my ajax call:
function loadDatabase(){
window.setInterval(function(){
$.ajax({
url: "/queue",
type: "GET"
});
}, 5000);
}
And here is my Flask views.py method:
@app.route('/queue', methods=['GET', 'POST'])
def addQueue():
conn = sqlite3.connect('snow_db.db')
cur = conn.cursor()
cur.execute("SELECT username, description, incident FROM tickets")
items = cur.fetchall()
print("hello")
# return jsonify({'array' : items})
return render_template("queue.html", items = items)
As you can see from the commented out code in the Flask function, I have also tried returning the items in Json format and actually was able to load the HTML table from this Json; however, I had the same issue of the table not loading the updated Json/sqlite data unless I did an actual page refresh.
All your help is appreciated as I have been trying tirelessly to figure out this problem, but cannot. Thank you!