作者:笑不评j | 来源:互联网 | 2023-09-25 15:19
请教一些问题。
我做了一个网页,网页需要实时获取数据库的数据并返回显示到网页上,而数据库部分会在后台被另一个程序实时修改。
请问应该用什么方法来完成呢?
我现在写的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| from flask import Flask,render_template,request
# import pymysql
from wtforms import form,Form
from wtforms.fields import simple
from DPT import DPT
app=Flask(__name__)
app.release=True
# app.config["SQLALCHEMY_DATABASE_URI"]='mysql://root:yidaimingjvn2017@127.0.0.1/icms'
# db=SQLAlchemy(app)
dpt = DPT()
dpt.icms_connect()
id_area=dpt.id()
name_area=dpt.area_name()
time_area=dpt.area_time()
people_count=dpt.area_count()
@app.route('/',methods=["GET","POST"])
def index():
stop = 0
dpt.icms_close()
dpt.icms_connect()
return render_template("index.html",
id_area=id_area,
name_area=name_area,
time_area=time_area,
people_count=people_count,
form=form
)
if __name__ == '__main__':
app.run(host="127.0.0.1", port="80") |
DPT.py部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import pymysql
pymysql.install_as_MySQLdb()
class DPT:
def __init__(self):
self.conn= None
def icms_connect(self):
self.conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="yidaimingjvn2017", db="icms" ,autocommit=True)
def icms_close(self):
self.cursor.close()
self.conn.close()
def sql_coursor(self):
self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
self.cursor.execute("select id,name,create_time from icms_address")
self.count_cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
self.count_cursor.execute("select count from icms_crowd_count")
def id(self):
"""获取记录条数"""
self.sql_coursor()
self.id_area = []
for self.area in self.cursor.fetchall():
self.id_area.append(self.area["id"])
return self.id_area
def area_name(self):
self.sql_coursor()
self.name_area = []
for self.name in self.cursor.fetchall():
self.name_area.append(self.name["name"])
return self.name_area
def area_time(self):
self.sql_coursor()
self.time_area = []
for self.time in self.cursor.fetchall():
self.time_area.append(self.time["create_time"])
return self.time_area
def area_count(self):
self.sql_coursor()
self.people_count = []
for self.count in self.count_cursor.fetchall():
self.people_count.append(self.count["count"])
return self.people_count
# dpt=DPT()
# dpt.icms_connect()
# while 1:
# print(dpt.area_count()) |
HTML部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| {% if 0 == stop %} {% endif %}
人群管理系统 |
---|
{% for num in id_area %}
|
地点: {{ name_area[num-1] }} 人数:
{{ people_count[num-1] }} 时间:{{ time_area[num-1] }}
|
|
{% endfor %}
|