当前位置: 首页 > news >正文

显示数据库信息及发送Ajax请求

framework.py:

"""web框架的职责专门负责处理动态资源请求"""
import json
import time
import pymysql
route_list=[]
#定义带有参数的装饰器
def route(path):def decorator(func):#装饰器执行的时候就需要把路由添加到路由列表里route_list.append((path, func))def inner():result=func();return resultreturn innerreturn decorator# 获取首页数据
@route("/index.html")
def index():# 状态信息status = "200 OK"# 响应头信息response_header = [("Server", "PWS/1.1")]# 1.打开指定模板文件,读取模板文件中的数据with open("template/index.html","r",encoding='utf-8') as file:file_data=file.read()# 2.查询数据库,模板里面的模板变量替换成以后从数据库里查询的数据conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",database="stock_db",charset="utf8")#获取游标cursor=conn.cursor()#准备sqlsql="select * from info;"cursor.execute(sql)#获取查询结果result=cursor.fetchall()print(result)cursor.close()# web框架处理后的数据# 遍历每一条数据,完成数据的tr标签封装data=""for row in result:data += """<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>""" % rowresponse_body=file_data.replace("{%content%}",data)# 这里返回的是元组return status, response_header, response_body#获取个人中心数据接口
@route("/center_data.html")
def center_data():#从数据库把数据查询处理,然后把查询处理的数据转成json数据#创建连接对象conn=pymysql.connect(host="localhost",port=3306,user="root",password="123456",database="stock_db",charset="utf8")cursor=conn.cursor()sql="select i.code, i.short, i.chg, i.turnover, i.price, i.highs, f.note_info from info i inner join focus f on i.id = f.info_id"cursor.execute(sql)result=cursor.fetchall()print(result)#把元组转成列表字典center_data_list=[{"code":row[0],"short": row[1],"chg": row[2],"turnover": row[3],"price": str(row[4]),"highs":str( row[5]),"note_info":row[6]}for row in result]print(center_data_list)#ensure_ascii=False在控制台能显示中文json_str=json.dumps(center_data_list,ensure_ascii=False)cursor.close()# 状态信息status = "200 OK"# 响应头信息,指定编码格式,因为没有模板文件response_header = [("Server", "PWS/1.1"),("Content-Type","text/html;charset=utf-8")]response_body=json_strreturn status, response_header, response_body#获取个人中心数据
@route("/center.html")
def center():# 状态信息status = "200 OK"# 响应头信息response_header = [("Server", "PWS/1.1")]# 1.打开指定模板文件,读取模板文件中的数据with open("template/center.html","r",encoding='utf-8') as file:file_data=file.read()# 2.查询数据库,模板里面的模板变量替换成以后从数据库里查询的数据# web框架处理后的数据# 获取当前时间,模拟数据库内容# data = time.ctime()response_body=file_data.replace("{%content%}"," ")# 这里返回的是元组return status, response_header, response_body# 处理没有找到的动态资源
def not_found():# 状态信息status = "404 Not Found"# 响应头信息response_header = [("Server", "PWS/1.1")]# web框架处理后的数据data = "not found"# 这里返回的是元组return status, response_header, data# 处理动态资源请求
def handle_request(env):# 获取动态的请求资源路径request_path = env["request_path"]print("动态资源请求的地址:", request_path)# 判断请求的动态资源路径,选择指定的函数处理对应的动态资源请求for path, func in route_list:if request_path == path:result = func()return resultelse:result = not_found()return result# if request_path == "/index.html":#     # 获取首页数据#     result = index()#     # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用#     return result# elif request_path=="/center.html":#    #个人中心#     result=center()#     return result# else:#     # 没有动态资源数据, 返回404状态信息#     result = not_found()#     # 把处理后的结果返回给web服务器使用,让web服务器拼接响应报文时使用#     return result
if __name__=="__main__":center_data()

center.html

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>个人中心 - 个人选股系统 V5.87</title><link href="/css/bootstrap.min.css" rel="stylesheet"><script src="/js/jquery-1.12.4.min.js"></script><script src="/js/bootstrap.min.js"></script><script>$(document).ready(function(){// 发送ajax请求,获取个人中心数据$.get("center_data.html",function (data) {// ajax 成功回调函数// 获取table标签var $table = $(".table");// 如果指定了返回数据的解析方式是json,那么data就是一个js对象for(var i = 0; i < data.length; i++){// 根据下标获取每一个个人中心数据js对象var oCenterData = data[i];// 封装后的每一个tr标签var oTr = '<tr>' +'<td>'+ oCenterData.code +'</td>' +'<td>'+ oCenterData.short +'</td>' +'<td>'+ oCenterData.chg +'</td>' +'<td>'+ oCenterData.turnover +'</td>' +'<td>'+ oCenterData.price +'</td>' +'<td>'+ oCenterData.highs +'</td>' +'<td>'+ oCenterData.note_info +'</td>' +'<td><a type="button" class="btn btn-default btn-xs" href="/update/000007.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a></td>' +'<td><input type="button" value="删除" id="toDel" name="toDel" systemidvaule="000007"></td>' +'</tr>'// 给table标签追加每一行tr标签$table.append(oTr)}}, "json");$("input[name='toDel']").each(function(){var currentAdd = $(this);currentAdd.click(function(){code = $(this).attr("systemIdVaule");alert("/del/" + code + ".html");$.get("/del/" + code + ".html", function(data, status){alert("数据: " + data + "\n状态: " + status);});window.location.reload()});});});</script>
</head><body>
<div class="navbar navbar-inverse navbar-static-top "><div class="container"><div class="navbar-header"><button class="navbar-toggle" data-toggle="collapse" data-target="#mymenu"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a href="#" class="navbar-brand">选股系统</a></div><div class="collapse navbar-collapse" id="mymenu"><ul class="nav navbar-nav"><li ><a href="/index.html">股票信息</a></li><li class="active"><a href="">个人中心</a></li></ul></div></div>
</div>
<div class="container"><div class="container-fluid"><table class="table table-hover"><tr><th>股票代码</th><th>股票简称</th><th>涨跌幅</th><th>换手率</th><th>最新价(元)</th><th>前期高点</th><th style="color:red">备注信息</th><th>修改备注</th><th>del</th></tr>{%content%}</table></div>
</div>
</body>
</html>            

 运行结果:

相关文章:

  • 【iOS】JSONModel源码阅读笔记
  • 【线性代数】第七章-二次型
  • Python闯LeetCode--第3题:无重复字符的最长子串
  • Node.js和npm的安装及配置
  • Centos系统yum安装mysql数据库
  • 将自己md文件发布到自己的博客园实现文件的持久化存储
  • FreeRTOS简单内核实现6 优先级
  • 小白跟做江科大32单片机之定时器输出比较
  • day38-39| 509. 斐波那契数 70. 爬楼梯 746. 使用最小花费爬楼梯 62.不同路径 343. 整数拆分 96.不同的二叉搜索树
  • 数据结构--力扣104,110 二叉树相关(C
  • springboot+shiro+jwt 兼容session和token
  • mac m芯片安装win11遇坑
  • MySQL 之 JSON 支持(三)—— JSON 函数
  • Centos7.9部署单节点K8S环境
  • 计算机网络之网络层知识总结
  • (三)从jvm层面了解线程的启动和停止
  • 【React系列】如何构建React应用程序
  • golang 发送GET和POST示例
  • HTTP--网络协议分层,http历史(二)
  • Javascript基础之Array数组API
  • Python打包系统简单入门
  • Webpack 4 学习01(基础配置)
  • WebSocket使用
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 构建二叉树进行数值数组的去重及优化
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 温故知新之javascript面向对象
  • 一个JAVA程序员成长之路分享
  • 因为阿里,他们成了“杭漂”
  • 怎样选择前端框架
  • 树莓派用上kodexplorer也能玩成私有网盘
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (四)Controller接口控制器详解(三)
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)使用VMware vSphere标准交换机设置网络连接
  • .Net 6.0 处理跨域的方式
  • .net Stream篇(六)
  • .net 受管制代码
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .NET项目中存在多个web.config文件时的加载顺序
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • @modelattribute注解用postman测试怎么传参_接口测试之问题挖掘
  • []Telit UC864E 拨号上网
  • [100天算法】-目标和(day 79)
  • [Android 13]Input系列--获取触摸窗口
  • [android] 切换界面的通用处理
  • [Angularjs]asp.net mvc+angularjs+web api单页应用
  • [BJDCTF2020]The mystery of ip
  • [Codeforces] combinatorics (R1600) Part.2