簡介
使用工具:
Python,PyQt ,F(xiàn)lask ,MySQL
注:制作重點在網(wǎng)頁端,因此網(wǎng)頁端的功能更全
WEB界面展示:
系統(tǒng)登錄分為管理員,老師,學生3部分
管理員統(tǒng)一管理所有的賬號信息以及登錄信息
老師管理,添加,修改班級,學生的成績信息
學生只能查看成績信息,不能做出修改
?
?
PYQT界面展示:
?
數(shù)據(jù)庫創(chuàng)建:
項目目錄
Project-
? ? ? ?- PYQT? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 存放軟件端的代碼文件(運行l(wèi)ogin.py啟動程序)
? ? ? ? ? ? ? ? ? ?
? ? ? ?- static? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 存放靜態(tài)資源(圖片等)
? ? ? ?-?templates ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 存放網(wǎng)頁端的代碼
? ? ? ? ? ? ? ? ? ?
? ? ? ?- app.py? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 啟動網(wǎng)頁端系統(tǒng)
? ? ? ?-?student.sql ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 數(shù)據(jù)庫文件
代碼
簡單放一個登錄的代碼
from flask import Flask, jsonify, render_template, request, redirect, url_for, session
import mysql.connector
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib
matplotlib.use('agg')
from matplotlib.font_manager import FontProperties
from io import BytesIO
import base64
from flask import render_template_string
import os
from datetime import datetime
from collections import defaultdict
# 預(yù)設(shè)字體格式,并傳給rc方法
font = {'family': 'SimHei', "size": 12}
matplotlib.rc('font', **font) # 一次定義終身使用
# Set font properties for Chinese characters
font_prop = FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf', size=12)
app = Flask(__name__)
app.secret_key = 'your_secret_key' # Change this to a secure secret key
# Replace these placeholders with your database connection details
DB_HOST = 'localhost'
DB_USER = 'root'
DB_PASSWORD = '123456'
DB_DATABASE = 'Student'
def connect_to_database():
try:
connection = mysql.connector.connect(
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_DATABASE
)
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def save_login_record(connection, role, account):
try:
cursor = connection.cursor()
login_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
query = f"INSERT INTO login_record (role, account, login_time) VALUES ('{role}', '{account}', '{login_time}')"
cursor.execute(query)
connection.commit()
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
cursor.close()
def check_admin_login(connection, table_name, account, password, role):
try:
if role == '管理員':
table_name = 'admin'
cursor = connection.cursor()
print(table_name,account,password,role)
query = f"SELECT * FROM {table_name} WHERE Mainid='{account}' AND password='{password}'"
# Implement your database query logic here
cursor.execute(query)
result = cursor.fetchone()
elif role == '老師':
table_name = 'admin_teachers'
# Check administrator login
cursor = connection.cursor()
print(table_name,account,password,role)
query = f"SELECT * FROM {table_name} WHERE Teacherid='{account}' AND password='{password}'"
# Implement your database query logic here
cursor.execute(query)
result = cursor.fetchone()
elif role == '學生':
table_name = 'admin_students'
# Check administrator login
cursor = connection.cursor()
print(table_name,account,password,role)
query = f"SELECT * FROM {table_name} WHERE Studentid='{account}' AND password='{password}'"
# Implement your database query logic here
cursor.execute(query)
result = cursor.fetchone()
if result:
# Save login record in data_8
save_login_record(connection, role, account)
return bool(result)
except mysql.connector.Error as err:
print(f"Error: {err}")
return False
finally:
cursor.close()
@app.route('/')
def index():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
account = request.form['account']
password = request.form['password']
role = request.form['role']
connection = connect_to_database()
if connection is None:
return render_template('login_failed.html', message='Failed to connect to the database.')
table_name = get_table_name(role)
if check_admin_login(connection, table_name, account, password, role):
session['role'] = role
if role == '學生':
session['account'] = account
print("===================",session['account'])
return redirect(url_for('admin_dashboard'))
else:
return render_template('login_failed.html', message='Invalid username or password.')
@app.route('/admin/dashboard')
def admin_dashboard():
role = session.get('role')
if role == '管理員':
data = fetch_admin_data()
data_1 = fetch_admin_teacher_data()
data_2 = fetch_admin_student_data()
data_8 = fetch_login_records()
data_10 = populate_tree_model_10()
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)
elif role == '老師':
data_2 = fetch_admin_student_data()
data_3 = populate_tree_model_2()
data_4 = populate_tree_model_6()
data_5 = populate_tree_model_4()
data_6 = populate_tree_model_7()
data_7 = populate_tree_model_5()
data_9 = populate_tree_model_9()
scatter_plot_files = show_images()
data_10 = populate_tree_model_10()
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)
elif role == '學生':
account = session.get('account')
data_3 = populate_tree_model_2()
data_4 = populate_tree_model_6()
data_5 = populate_tree_model_4()
data_6 = populate_tree_model_7()
data_7 = populate_tree_model_5()
data_8 = show_info()
print("===================----------------",session['account'])
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)
else:
return redirect(url_for('index'))
# Add routes for other pages as needed
def get_table_name(role):
# Implement logic to determine the table name based on the role
if role == '管理員':
return 'admin'
elif role == '老師':
return 'admin_teachers'
elif role == '學生':
return 'admin_students'
else:
return None
def fetch_login_records():
data_8 = []
try:
connection = connect_to_database()
if connection is None:
return []
cursor = connection.cursor()
query = "SELECT * FROM login_record"
cursor.execute(query)
login_records = cursor.fetchall()
# Convert rows to a list of dictionaries
for row in login_records:
data_8.append({
'role': row[0],
'account': row[1],
'login_time': row[2]
})
return data_8
except mysql.connector.Error as err:
print(f"Error: {err}")
return []
finally:
cursor.close()
if connection:
connection.close()
def fetch_admin_data():
data = []
try:
# Connect to the MySQL database
connection = mysql.connector.connect(
host='localhost',
user='root',
password='123456',
database='Student'
)
cursor = connection.cursor()
# Execute a query to fetch data from the admin table
query = "SELECT Mainid, Username, Password, Name FROM admin"
cursor.execute(query)
# Fetch all rows from the result
rows = cursor.fetchall()
# Convert rows to a list of dictionaries
for row in rows:
data.append({
'Mainid': row[0],
'Username': row[1],
'Password': row[2],
'Name': row[3]
})
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
cursor.close()
connection.close()
return data
百度云鏈接:
鏈接:https://pan.baidu.com/s/13HtbUm0Wwd0RT_cY61M57A?pwd=o102?
提取碼:o102?
--來自百度網(wǎng)盤超級會員V5的分享文章來源:http://www.zghlxwxcb.cn/news/detail-830713.html
系統(tǒng)可能還存在某些不完善的地方,歡迎討論文章來源地址http://www.zghlxwxcb.cn/news/detail-830713.html
到了這里,關(guān)于Python Flask Web + PyQt 前后端分離的項目—學習成績可視化分析系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!