我做这个项目写了比较多的python和html文件。所以,可能得分几次发文。
先创建Flask项目,然后,在下面构建目录和文件如下: 创建名为MongoDB_data_export的python文件,文件内容如下所示:
import pymongo
import re
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["Car"]
collection = db["options"]
def export():
result = []
options_data_ls = collection.find({})
for options_data in options_data_ls:
del options_data['_id']
s = '{}'.format(options_data)
reg = '{(.*?)}'
ls = re.findall(reg,s)[0]
result.append(ls)
return result
if __name__ == '__main__':
export()
在Flask框架中创建路由,如下所示:
"""
#encoding="utf-8"
Author:Mr.Pan_学狂
前端和后端结合,
需要连接MongoDB和mysql,
MongoDB负责做数据展示和数据搜索(数据查询)
mysql负责存储和调度用户数据
用pyecharts做数据可视化
"""
from flask import Flask,request,redirect
from flask import render_template
import user_mysql as user
import MongoDB_data_export as Mde
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route("/info_show")
def car_show():
options_ls = Mde.export()
return render_template("info.html",d=options_ls)
@app.route("/search",methods=['GET','POST'])
def car_search():
if request.method == 'POST':
car_name = request.form.get('车名')
car_ls = Mde.export()
data = []
for car in car_ls:
if car_name in car:
data.append(car)
return render_template("search.html",d=data)
return render_template("search.html")
@app.route("/data_show")
def data_show():
return "数据可视化页面"
@app.route("/login",methods=["GET","POST"])
def login():
if request.method == "POST":
name = request.form.get('用户名')
pwd = request.form.get('密码')
tup = (name,pwd)
sql = "select * from user;"
data = user.query(sql)
if tup in data:
return redirect('info_show')
else:
return "<h1>用户名或密码错误</h1><br><a href='http://127.0.0.1:5000/login'><h3>返回登录页面</h3></a>"
return render_template('login.html')
@app.route("/registration",methods=["POST","GET"])
def user_regist():
dt = {}
if request.method == "POST":
username = request.form.get('用户名')
password = request.form.get('密码')
sql = "select * from user;"
data = user.query(sql)
for d in data:
dt[d[0]] = d[1]
if username == '':
return "<h1>用户名不能为空!!</h1><br><a href='http://127.0.0.1:5000/registration'><h3>返回注册页面</h3></a>"
if password == '':
return "<h1>密码不能为空!!</h1><br><a href='http://127.0.0.1:5000/registration'><h3>返回注册页面</h3></a>"
if username in dt.keys():
return "<h1>此用户已存在!!</h1><br><a href='http://127.0.0.1:5000/registration'><h3>返回注册页面</h3></a>"
else:
name = "'"+username+"'"
pwd = "'"+password+"'"
sql2 = "insert into user(name,pwd)values({},{})".format(name,pwd)
user.insert_data(sql2)
return render_template('user_registration.html')
@app.route("/history")
def user_visit():
return render_template('visit.html')
@app.route("/Author")
def introduce():
return render_template('Author.html')
if __name__ == '__main__':
app.run(debug=True)
下几篇文章会把路由对应的html文件公布出来,我可视化还没有搞,先搞可视化,然后再把剩下的内容发上去。我马上做完这个项目就搞机器学习了,因为我的毕设是深度学习的题目,我得马上学会,时间不是很多,项目可能做的不够完善。大家见谅。要是需要项目的源代码,可以与我联系(QQ:2143883550或者是163邮箱:python_jack@163.com)。
最后,感谢大家前来观看鄙人的文章,文中或有诸多不妥之处,还望指出和海涵。
|