Python+flask編寫一個簡單實用的自動排班系統(tǒng)
這是一個基于Flask和PyQt的排班系統(tǒng),可以將Web界面嵌入到桌面應(yīng)用程序中。
系統(tǒng)界面

功能特點
- 讀取員工信息和現(xiàn)有排班表
- 自動生成排班表
- 美觀的Web界面
- 獨立的桌面應(yīng)用程序
整體架構(gòu)
系統(tǒng)采用前后端分離的架構(gòu)設(shè)計,通過 PyQt5 的 WebEngine 組件將 Web 界面嵌入到桌面應(yīng)用中。
├── 桌面應(yīng)用層 (PyQt5)
│ └── WebEngine 視圖
├── Web 層 (Flask)
│ ├── 路由控制
│ └── 業(yè)務(wù)邏輯
└── 數(shù)據(jù)層
├── CSV 數(shù)據(jù)文件
└── Excel 導(dǎo)出
核心模塊
主程序模塊 (main.py)
- 負責初始化 PyQt5 應(yīng)用
- 集成 Flask 服務(wù)器
- 管理主窗口和 Web 視圖
后端服務(wù)模塊 (app.py)
- 提供 RESTful API
- 處理排班算法
- 管理數(shù)據(jù)導(dǎo)入導(dǎo)出
前端界面模塊 (templates/index.html)
- 員工列表管理
- 排班表顯示
- 用戶交互處理
核心代碼:main.py
import sys
import time
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
from flask import Flask
import threading
import os
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("排班系統(tǒng)")
self.setGeometry(100, 100, 1200, 800)
# 創(chuàng)建中心部件
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# 創(chuàng)建Web視圖
self.web_view = QWebEngineView()
layout.addWidget(self.web_view)
# 啟動Flask服務(wù)器
self.start_flask_server()
# 等待服務(wù)器啟動后加載頁面
time.sleep(1) # 給服務(wù)器一點啟動時間
self.web_view.setUrl(QUrl("http://127.0.0.1:3863"))
def start_flask_server(self):
# 在新線程中啟動Flask服務(wù)器
threading.Thread(target=self.run_flask, daemon=True).start()
def run_flask(self):
from app import app
app.run(host='127.0.0.1', port=3863)
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main() 核心代碼:app.py
from flask import Flask, render_template, request, jsonify, send_file
import pandas as pd
from datetime import datetime, timedelta
import calendar
import json
import numpy as np
import os
app = Flask(__name__)
# 班次定義
SHIFTS = {
'白班': 'D',
'晚班': 'N',
'休息': 'R'
}
# 讀取員工數(shù)據(jù)
def load_employee_data():
try:
df = pd.read_csv('Employee.csv', encoding='utf-8')
# 只返回員工姓名列
return pd.DataFrame({'name': df["Employee'sName"]})
except Exception as e:
print(f"Error loading employee data: {e}")
return pd.DataFrame({'name': []})
# 讀取排班表
def load_schedule():
try:
df = pd.read_excel('客戶服務(wù)部排班表20250301-20250331.xls')
return df
except Exception as e:
print(f"Error loading schedule: {e}")
return pd.DataFrame()
def get_month_calendar(year, month):
cal = calendar.monthcalendar(year, month)
return cal
def generate_monthly_schedule(employees, year, month):
num_days = calendar.monthrange(year, month)[1]
num_employees = len(employees)
# 將employees列表轉(zhuǎn)換為numpy數(shù)組
employees_array = np.array(employees)
# 創(chuàng)建排班表
schedule = pd.DataFrame(index=employees, columns=range(1, num_days + 1))
schedule.fillna('R', inplace=True) # 默認全部休息
# 為每一天分配班次
for day in range(1, num_days + 1):
# 確保每天有足夠的白班和晚班
day_employees = employees_array.copy()
np.random.shuffle(day_employees)
# 分配白班(約40%的員工)
day_shifts = int(num_employees * 0.4)
schedule.loc[day_employees[:day_shifts], day] = 'D'
# 分配晚班(約30%的員工)
night_shifts = int(num_employees * 0.3)
schedule.loc[day_employees[day_shifts:day_shifts+night_shifts], day] = 'N'
# 確保每周至少休息兩天
for employee in employees:
for week in range(0, num_days, 7):
week_schedule = schedule.loc[employee, week+1:min(week+7, num_days)]
rest_days = (week_schedule == 'R').sum()
if rest_days < 2:
work_days = list(week_schedule[week_schedule != 'R'].index)
if work_days: # 確保有工作日可以調(diào)整
np.random.shuffle(work_days)
for i in range(min(2-rest_days, len(work_days))):
schedule.loc[employee, work_days[i]] = 'R'
return schedule
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/employees')
def get_employees():
df = load_employee_data()
return jsonify(df.to_dict('records'))
@app.route('/api/calendar/<int:year>/<int:month>')
def get_calendar(year, month):
cal = get_month_calendar(year, month)
return jsonify(cal)
@app.route('/api/generate_schedule', methods=['POST'])
def generate_schedule():
try:
data = request.get_json()
year = data.get('year', 2025)
month = data.get('month', 1)
selected_employees = data.get('employees', [])
if not selected_employees:
return jsonify({"status": "error", "message": "請選擇員工"})
schedule = generate_monthly_schedule(selected_employees, year, month)
# 將DataFrame轉(zhuǎn)換為字典格式
schedule_dict = {}
for employee in selected_employees:
schedule_dict[employee] = schedule.loc[employee].to_dict()
return jsonify({
"status": "success",
"schedule": schedule_dict,
"message": "排班表生成成功"
})
except Exception as e:
return jsonify({"status": "error", "message": str(e)})
@app.route('/api/export_schedule', methods=['POST'])
def export_schedule():
try:
data = request.get_json()
year = data.get('year', 2025)
month = data.get('month', 1)
schedule_data = data.get('schedule', {})
# 創(chuàng)建新的排班表
df = pd.DataFrame.from_dict(schedule_data, orient='index')
# 設(shè)置列名為日期
df.columns = [str(i) for i in range(1, len(df.columns) + 1)]
# 重置索引,將員工名稱作為一列
df.reset_index(inplace=True)
df.rename(columns={'index': '姓名'}, inplace=True)
# 保存文件
output_file = f'客戶服務(wù)部排班表{year}{month:02d}01-{year}{month:02d}{calendar.monthrange(year, month)[1]}.xlsx'
# 使用 openpyxl 引擎保存為 xlsx 格式
df.to_excel(output_file, index=False, engine='openpyxl')
# 返回文件下載路徑
return send_file(
output_file,
as_attachment=True,
download_name=output_file,
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
except Exception as e:
print(f"Export error: {str(e)}") # 添加錯誤日志
return jsonify({"status": "error", "message": f"導(dǎo)出失敗: {str(e)}"})
if __name__ == '__main__':
app.run(host='127.0.0.1', port=3863, debug=True) 到此這篇關(guān)于Python+flask編寫一個簡單實用的自動排班系統(tǒng)的文章就介紹到這了,更多相關(guān)Python flask自動排班系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中try excpet BaseException(異常處理捕獲)的使用
本文主要介紹了Python中try excpet BaseException(異常處理捕獲)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
基于并發(fā)服務(wù)器幾種實現(xiàn)方法(總結(jié))
下面小編就為大家分享一篇基于并發(fā)服務(wù)器幾種實現(xiàn)方法(總結(jié)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Python3.6基于正則實現(xiàn)的計算器示例【無優(yōu)化簡單注釋版】
這篇文章主要介紹了Python3.6基于正則實現(xiàn)的計算器,涉及Python基于正則表達式的算術(shù)式遍歷、查找及數(shù)學運算相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
python光學仿真面向?qū)ο蠊鈱W元件類的實現(xiàn)
這篇文章主要為大家介紹了python光學仿真面向?qū)ο蠊鈱W元件類的實現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10
Python深度學習pytorch神經(jīng)網(wǎng)絡(luò)圖像卷積運算詳解
這篇文章主要介紹了Python深度學習關(guān)于pytorch神經(jīng)網(wǎng)絡(luò)圖像卷積的運算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
Python根據(jù)文件名批量轉(zhuǎn)移圖片的方法
今天小編就為大家分享一篇Python根據(jù)文件名批量轉(zhuǎn)移圖片的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

