# 根据不同的路径返回不同的网页
# 让我们的入口函数读起来像目录
# 一个功能一个函数
import re
import pymysql
from pymysql import connect
# 定义一个空的字典
url_dict = dict()
# flask核心功能 就是路由功能
# 路由功能 完成
# 路由功能的功能就是用来控制当前的网页是否展示
def set_url(url):
def set_fun(func):
def call_fun(*args, **kwargs):
print("添加权限")
return func(*args, **kwargs)
print(call_fun) # 函数的引用
print(url) # 函数对应的地址
# 添加到我们的字典中
url_dict[url] = call_fun
return call_fun
return set_fun
# 如果if超过三个以上,我们可以考虑使用字典
def application(file_path):
# 响应头
head_stauts = "HTTP/1.1 200 OK\r\n"
# 定义一个url的字典
# url_dict = {"/index.html": index, "/center.html": center, "/login.html": login}
print("自动生成的字典:", url_dict)
try:
# 根据不同的地址去字典获取相应的函数引用
fun = url_dict[file_path]
# 得到相应体
body = fun()
except Exception as e:
print("异常:", e)
head_stauts = "HTTP/1.1 404 not found\r\n"
body = "not page is show"
return head_stauts, body
################################################################上面全是框架的代码#################################
# 获取前端代码,展示页面
@set_url('/index.html')
def index():
with open('./templates/index.html','r') as f:
content = f.read()
# return content
# 链接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
# curser对象
cs = conn.cursor()
# 执行sql语句
cs.execute("""select * from info;""")
# 获取数据
data = cs.fetchall()
# 关闭
cs.close()
conn.close()
# 处理数据
# 总字符串
table_str = ""
# 数据标签
row_str = """
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="000007">
</td>
</tr>
"""
# 处理数据
for temp in data:
# print(temp)
# 替换标签内的数据,合成总字符串
table_str += row_str % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7])
# 将前段留下的content替换成总字符串
content_new = re.sub(r'{%content%}',table_str,content)
# print(content_new)
return content_new
# 获取前端代码,展示页面
@set_url('/center.html')
def center():
with open('./templates/center.html','r') as f:
content = f.read()
# return content
# 链接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='mysql', database='stock_db', charset='utf8')
# curser对象
cs = conn.cursor()
# 执行sql语句
cs.execute("""select info.code,info.short,info.chg,info.turnover,info.price,info.highs,focus.note_info from info inner join focus on info.id = focus.info_id;""")
# 获取数据
data = cs.fetchall()
# 关闭
cs.close()
conn.close()
# 处理数据
# 总字符串
table_str = ""
# 数据标签
row_str = """
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<a type="button" class="btn btn-default btn-xs" href="/update/300268.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
</td>
<td>
<input type="button" value="删除" id="toDel" name="toDel" systemidvaule="300268">
</td>
</tr>
"""
# 处理数据
for temp in data:
# print(temp)
# 替换标签内的数据,合成总字符串
table_str += row_str % (temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6])
# 将前段留下的content替换成总字符串
content_new = re.sub(r'{%content%}',table_str,content)
print(content_new)
return content_new
# 测试用
if __name__ == '__main__':
center()