欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

MongoDB小项目,第二部分数据导出

时间:2023-06-03

我做这个项目写了比较多的python和html文件。所以,可能得分几次发文。

先创建Flask项目,然后,在下面构建目录和文件如下:

创建名为MongoDB_data_export的python文件,文件内容如下所示:

#encoding="utf-8"#Author:Mr.Pan_学狂#从MongoDB中导出数据import pymongoimport reclient = 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: # if '_id' in options_data: del options_data['_id'] s = '{}'.format(options_data) reg = '{(.*?)}' ls = re.findall(reg,s)[0] result.append(ls) # print(ls) # print(result) return resultdef data_show(): suv_count = 0 comment_count = 0 machine_count = 0 bsx_count = 0 options_data_ls = collection.find({}) ls = [] # all_length = len(options_data_ls) for options_data in options_data_ls: del options_data['_id'] ls.append(options_data) # print(options_data) if '级 别' in options_data: if 'SUV' in options_data['级 别']: suv_count += 1 if '用户评分 ' in options_data: if '暂无' in options_data['用户评分 ']: comment_count+=1 if '发 动 机' in options_data: machine_count += 1 if '变 速 箱' in options_data: bsx_count += 1 all_length = len(ls) other_count = all_length - suv_count print('其他级别:',round(other_count/all_length,2)) suv_glv = round(suv_count/all_length,2) * 100 No_suv_glv = round(other_count/all_length,2) * 100 print('SUV:',round(suv_count/all_length,2)) have_comment = all_length - comment_count print('有用户评分:',round(have_comment/all_length,2)) have_comment_glv = round(have_comment/all_length,2) * 100 print('无用户评分:',round(comment_count/all_length,2)) No_comment_glv = round(comment_count/all_length,2) * 100 No_machine = all_length-machine_count print('电动机:',round(No_machine/all_length,2)) print('发动机:',round(machine_count/all_length,2)) machine_glv = round(machine_count/all_length,2) * 100 No_machine_glv = round(No_machine/all_length,2) * 100 No_bsx = all_length-bsx_count print('变速箱:',round(bsx_count/all_length,2)) print('非变速箱:',round(No_bsx/all_length,2)) bsx_glv = round(bsx_count/all_length,2) * 100 No_bsx_glv = round(No_bsx/all_length,2) * 100 return suv_glv,No_suv_glv,have_comment_glv,No_comment_glv,machine_glv,No_machine_glv,bsx_glv,No_bsx_glvif __name__ == '__main__': data_show()

在Flask框架中创建路由,如下所示:

"""#encoding="utf-8"Author:Mr.Pan_学狂前端和后端结合,需要连接MongoDB和mysql,MongoDB负责做数据展示和数据搜索(数据查询)mysql负责存储和调度用户数据用pyecharts做数据可视化"""from flask import Flask,request,redirectfrom flask import render_templateimport user_mysql as userimport MongoDB_data_export as Mdeapp = Flask(__name__)@app.route('/')def index(): return render_template("index.html")@app.route("/info_show")#全部数据展示def car_show(): options_ls = Mde.export() # print(options) 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(): # data_ls = [] 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 "用户名或密码错误
返回登录页面" 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) # print(data) for d in data: dt[d[0]] = d[1] if username == '': return "用户名不能为空!!
返回注册页面" if password == '': return "密码不能为空!!
返回注册页面" if username in dt.keys(): return "此用户已存在!!
返回注册页面" 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)。

最后,感谢大家前来观看鄙人的文章,文中或有诸多不妥之处,还望指出和海涵。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。