请教一些问题。
我做了一个网页,网页需要实时获取数据库的数据并返回显示到网页上,而数据库部分会在后台被另一个程序实时修改。
请问应该用什么方法来完成呢?
我现在写的代码如下:
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部分:
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部分:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>人群管理系统</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% if 0 == stop %}
<meta http-equiv="refresh" content="2;url={{ url_for('index') }}">
{% endif %}
<style type="test/css">
td{font-size:280%;width:50%;}
td>ali{text-align:center:}
img{width:40%;}
p{text-align:center;}
</style>
</head>
<body>
<table id="info" width=100%>
<tr>
<th style="align:center;font-size:320%" colspan="2">人群管理系统</th>
</tr>
<!-- <tr></tr>-->
{% for num in id_area %}
<tr>
<td><img src="{{ url_for('static', filename='logo.png')}}" width="200"></td>
<!-- <td style="text-align:center;"></td>-->
<td>地点:<p style="text-align:center;">{{ name_area[num-1] }}</p>人数:<p style="text-align:center;">
{{ people_count[num-1] }}</p> 时间:<p style="text-align:center;">{{ time_area[num-1] }}</p>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>