您现在的位置是:首页 > 技术教程 正文

Python Flask Web + PyQt 前后端分离的项目—学习成绩可视化分析系统

admin 阅读: 2024-03-28
后台-插件-广告管理-内容页头部广告(手机)

简介

使用工具:

Python,PyQt ,Flask ,MySQL

注:制作重点在网页端,因此网页端的功能更全

WEB界面展示:

系统登录分为管理员,老师,学生3部分

管理员统一管理所有的账号信息以及登录信息

老师管理,添加,修改班级,学生的成绩信息

学生只能查看成绩信息,不能做出修改

 

 

PYQT界面展示:

 

数据库创建:

项目目录

Project-

       - PYQT                                      # 存放软件端的代码文件(运行login.py启动程序)

                   

       - static                                          # 存放静态资源(图片等)

       - templates                                   # 存放网页端的代码

                   

       - app.py                                        # 启动网页端系统

       - student.sql                                 # 数据库文件

代码

简单放一个登录的代码

  1. from flask import Flask, jsonify, render_template, request, redirect, url_for, session
  2. import mysql.connector
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import matplotlib
  6. matplotlib.use('agg')
  7. from matplotlib.font_manager import FontProperties
  8. from io import BytesIO
  9. import base64
  10. from flask import render_template_string
  11. import os
  12. from datetime import datetime
  13. from collections import defaultdict
  14. # 预设字体格式,并传给rc方法
  15. font = {'family': 'SimHei', "size": 12}
  16. matplotlib.rc('font', **font) # 一次定义终身使用
  17. # Set font properties for Chinese characters
  18. font_prop = FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf', size=12)
  19. app = Flask(__name__)
  20. app.secret_key = 'your_secret_key' # Change this to a secure secret key
  21. # Replace these placeholders with your database connection details
  22. DB_HOST = 'localhost'
  23. DB_USER = 'root'
  24. DB_PASSWORD = '123456'
  25. DB_DATABASE = 'Student'
  26. def connect_to_database():
  27. try:
  28. connection = mysql.connector.connect(
  29. host=DB_HOST,
  30. user=DB_USER,
  31. password=DB_PASSWORD,
  32. database=DB_DATABASE
  33. )
  34. return connection
  35. except mysql.connector.Error as err:
  36. print(f"Error: {err}")
  37. return None
  38. def save_login_record(connection, role, account):
  39. try:
  40. cursor = connection.cursor()
  41. login_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  42. query = f"INSERT INTO login_record (role, account, login_time) VALUES ('{role}', '{account}', '{login_time}')"
  43. cursor.execute(query)
  44. connection.commit()
  45. except mysql.connector.Error as err:
  46. print(f"Error: {err}")
  47. finally:
  48. cursor.close()
  49. def check_admin_login(connection, table_name, account, password, role):
  50. try:
  51. if role == '管理员':
  52. table_name = 'admin'
  53. cursor = connection.cursor()
  54. print(table_name,account,password,role)
  55. query = f"SELECT * FROM {table_name} WHERE Mainid='{account}' AND password='{password}'"
  56. # Implement your database query logic here
  57. cursor.execute(query)
  58. result = cursor.fetchone()
  59. elif role == '老师':
  60. table_name = 'admin_teachers'
  61. # Check administrator login
  62. cursor = connection.cursor()
  63. print(table_name,account,password,role)
  64. query = f"SELECT * FROM {table_name} WHERE Teacherid='{account}' AND password='{password}'"
  65. # Implement your database query logic here
  66. cursor.execute(query)
  67. result = cursor.fetchone()
  68. elif role == '学生':
  69. table_name = 'admin_students'
  70. # Check administrator login
  71. cursor = connection.cursor()
  72. print(table_name,account,password,role)
  73. query = f"SELECT * FROM {table_name} WHERE Studentid='{account}' AND password='{password}'"
  74. # Implement your database query logic here
  75. cursor.execute(query)
  76. result = cursor.fetchone()
  77. if result:
  78. # Save login record in data_8
  79. save_login_record(connection, role, account)
  80. return bool(result)
  81. except mysql.connector.Error as err:
  82. print(f"Error: {err}")
  83. return False
  84. finally:
  85. cursor.close()
  86. @app.route('/')
  87. def index():
  88. return render_template('login.html')
  89. @app.route('/login', methods=['POST'])
  90. def login():
  91. account = request.form['account']
  92. password = request.form['password']
  93. role = request.form['role']
  94. connection = connect_to_database()
  95. if connection is None:
  96. return render_template('login_failed.html', message='Failed to connect to the database.')
  97. table_name = get_table_name(role)
  98. if check_admin_login(connection, table_name, account, password, role):
  99. session['role'] = role
  100. if role == '学生':
  101. session['account'] = account
  102. print("===================",session['account'])
  103. return redirect(url_for('admin_dashboard'))
  104. else:
  105. return render_template('login_failed.html', message='Invalid username or password.')
  106. @app.route('/admin/dashboard')
  107. def admin_dashboard():
  108. role = session.get('role')
  109. if role == '管理员':
  110. data = fetch_admin_data()
  111. data_1 = fetch_admin_teacher_data()
  112. data_2 = fetch_admin_student_data()
  113. data_8 = fetch_login_records()
  114. data_10 = populate_tree_model_10()
  115. return render_template('admin_dashboard.html', role=role, data=data, data_1=data_1, data_2=data_2, data_8 = data_8, data_10 = data_10)
  116. elif role == '老师':
  117. data_2 = fetch_admin_student_data()
  118. data_3 = populate_tree_model_2()
  119. data_4 = populate_tree_model_6()
  120. data_5 = populate_tree_model_4()
  121. data_6 = populate_tree_model_7()
  122. data_7 = populate_tree_model_5()
  123. data_9 = populate_tree_model_9()
  124. scatter_plot_files = show_images()
  125. data_10 = populate_tree_model_10()
  126. return render_template('teacher_dashboard.html',scatter_plot_files = scatter_plot_files, role=role, data_2=data_2, data_3=data_3, data_4=data_4, data_5=data_5, data_6=data_6, data_7=data_7, data_9=data_9, data_10 = data_10)
  127. elif role == '学生':
  128. account = session.get('account')
  129. data_3 = populate_tree_model_2()
  130. data_4 = populate_tree_model_6()
  131. data_5 = populate_tree_model_4()
  132. data_6 = populate_tree_model_7()
  133. data_7 = populate_tree_model_5()
  134. data_8 = show_info()
  135. print("===================----------------",session['account'])
  136. return render_template('student_dashboard.html', studentID=account, data_3=data_3, data_4=data_4, data_5=data_5, data_6=data_6, data_7=data_7, data_8=data_8)
  137. else:
  138. return redirect(url_for('index'))
  139. # Add routes for other pages as needed
  140. def get_table_name(role):
  141. # Implement logic to determine the table name based on the role
  142. if role == '管理员':
  143. return 'admin'
  144. elif role == '老师':
  145. return 'admin_teachers'
  146. elif role == '学生':
  147. return 'admin_students'
  148. else:
  149. return None
  150. def fetch_login_records():
  151. data_8 = []
  152. try:
  153. connection = connect_to_database()
  154. if connection is None:
  155. return []
  156. cursor = connection.cursor()
  157. query = "SELECT * FROM login_record"
  158. cursor.execute(query)
  159. login_records = cursor.fetchall()
  160. # Convert rows to a list of dictionaries
  161. for row in login_records:
  162. data_8.append({
  163. 'role': row[0],
  164. 'account': row[1],
  165. 'login_time': row[2]
  166. })
  167. return data_8
  168. except mysql.connector.Error as err:
  169. print(f"Error: {err}")
  170. return []
  171. finally:
  172. cursor.close()
  173. if connection:
  174. connection.close()
  175. def fetch_admin_data():
  176. data = []
  177. try:
  178. # Connect to the MySQL database
  179. connection = mysql.connector.connect(
  180. host='localhost',
  181. user='root',
  182. password='123456',
  183. database='Student'
  184. )
  185. cursor = connection.cursor()
  186. # Execute a query to fetch data from the admin table
  187. query = "SELECT Mainid, Username, Password, Name FROM admin"
  188. cursor.execute(query)
  189. # Fetch all rows from the result
  190. rows = cursor.fetchall()
  191. # Convert rows to a list of dictionaries
  192. for row in rows:
  193. data.append({
  194. 'Mainid': row[0],
  195. 'Username': row[1],
  196. 'Password': row[2],
  197. 'Name': row[3]
  198. })
  199. except mysql.connector.Error as err:
  200. print(f"Error: {err}")
  201. finally:
  202. # Close the cursor and connection
  203. cursor.close()
  204. connection.close()
  205. return data

百度云链接:

链接:https://pan.baidu.com/s/13HtbUm0Wwd0RT_cY61M57A?pwd=o102 
提取码:o102 
--来自百度网盘超级会员V5的分享

系统可能还存在某些不完善的地方,欢迎讨论

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索