我正在使用Flask设置GUI服务器。每当使用特定的Sqlite3数据库进行更改时,我都会使用一个调用函数的API,然后需要将更改后的值添加到HTML页面上的表中。
我尝试学习JQuery,但这似乎需要来自客户端的get请求。我需要将服务器端数据库中添加或更改的所有数据附加到客户端页面上的表中。
<tbody>
{% for data in table_data %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ data[0] }}</td>
<td>{{ data[1] }}</td>
<td>{{ data[2] }}</td>
</tr>
{% endfor %}
</tbody>
def get_data(cursor_obj, table_name, previous_data):
"When called will all the data from the db and will compair it to
the old data and return the data to be appended, which I then need
to add to the table."
cursor_obj.execute("SELECT * FROM "+str(table_name))
data = cursor_obj.fetchall()
for old in previous_data:
index = data.index(old)
data.del(index)
return data
答案 0 :(得分:0)
没有任何客户端请求,这将很复杂。 sqlite数据库更改的信息不能仅仅通过魔术从服务器传送到客户端。
但是,除了定期从客户端轮询服务器以检查信息是否已更改之外,还有其他解决方案:您可以保持服务器到客户端的连接处于打开状态,并在此推送通知。
在网络上实现此功能最简单的方法是使用“服务器发送事件”。
您可以检查How to implement server push in Flask framework?以获得更多信息