当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 2

luenay/flask_dengji
暂停

forked from ghc/flask_dengji
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
extensions.py 13.06 KB
一键复制 编辑 原始数据 按行查看 历史
ghc 提交于 2020-01-08 18:00 . 01.08 更新考试系统
# coding=utf-8
# 中转一下 flask-sqlalchemy下的SQLAlchemy需要flask的实例app 模型类需要SQLAlchemy的实例对象
# 解决循环引用
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from pyecharts.charts import Line, Map, Pie, Bar
import pyecharts.options as opts
from pyecharts.globals import ThemeType
import json
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_cache import Cache
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from flask_cors import CORS
import os
import pandas as pd
from flask_session import Session
from flask_login import LoginManager, login_required, current_user, AnonymousUserMixin
from datetime import timedelta
import random
user_socket_dict = {}
user_dict = {}
exam_time_dic = {}
# 匿名的用户类 如果未注册登录 current_user 创建的为该类对象 为完成默认图片的读取
class AnonymousUser(AnonymousUserMixin):
def __init__(self):
self.name = '匿名用户'
def get_name(self):
return '匿名用户'
def get_bg_img(self):
return '/static/img/user_bg/default.jpg?random={}'.format(random.random())
# 读取ip机号对应表
ipDict = json.load(open('ip.json', 'r', encoding='utf-8'))
# 读取学生-学号对应表
xuehaoDict = json.load(open('xuehao.json', 'r', encoding='utf-8'))
# 学号和班级对应表
class_xuehao = json.load(open('学号+班级.json', 'r', encoding='gbk'))
createVar = locals()
# 获取今日课表目录
tablelst = os.listdir('./class_table')
# 循环处理每个教师的课表
for i in range(11):
# 读取表格 对数据进行处理
df = pd.read_excel('./课表.xlsx', index_col=[0, 1], sheet_name=i)
df.index.set_names(['节次', '时间'], inplace=True)
df = pd.DataFrame(df.values, index=['12', '34', '56', '78', '910'], columns=[
'1', '2', '3', '4', '5'])
# 创建变量 如df1211 df1210
createVar['df' + tablelst[i][-8:-4]] = df # type:pd.DataFrame
del df
liujiL = open('./留级.txt', 'r', encoding='utf-8').readlines()
liujiL = [i.strip() for i in liujiL]
lunbol = os.listdir('./static/img/lunbo/')
lunbo_dongman = os.listdir('./static/img/lunbo_dongman/')
yyf = json.load(open('yiyan.json', 'r', encoding='utf-8'))
weatherf = open('weather.json', 'r', encoding='utf-8')
# 初始化
db = SQLAlchemy()
bootstrap = Bootstrap()
moment = Moment()
cache = Cache(with_jinja2_ext=False)
limiter = Limiter(key_func=get_remote_address)
# r'/*' 是通配符,让本服务器所有的URL 都允许跨域请求
cors = CORS(resources=r'/*')
session = Session()
# 初始化实例
login_manager = LoginManager()
# 验证失败跳转的界面
login_manager.login_view = 'login_blue.login'
# 闪烁信息级别
login_manager.login_message_category = 'info'
# 用户重定向到登录页面时闪出的消息
login_manager.login_message = '请先登录'
# cookie 的默认有效期
login_manager.remember_cookie_duration = timedelta(days=1)
# 制定匿名用户类
login_manager.anonymous_user = AnonymousUser
def init_app(app):
# flask-sqlalchemy初始化
db.init_app(app)
moment.init_app(app)
bootstrap.init_app(app)
cache.init_app(app, config={'CACHE_TYPE': 'simple'})
limiter.init_app(app)
cors.init_app(app)
session.init_app(app)
login_manager.init_app(app)
def yanggang():
bar = Bar(init_opts=opts.InitOpts(width="550px", height="500px", theme=ThemeType.ESSOS))
bar.add_xaxis(
['铁道工程1824', '铁道工程1823', '城轨机电1933', '城轨机电1936', '城轨机电1934', '城轨机电1828', '城轨机电1932', '城轨车辆1810', '城轨机电1830',
'城轨机电1935'])
bar.add_yaxis('男', [57, 56, 51, 51, 50, 49, 49, 49, 48, 48])
bar.add_yaxis('女', [1, 0, 2, 2, 2, 2, 2, 1, 2, 2])
bar.set_global_opts(title_opts=opts.TitleOpts(title='十大阳刚班级'),
xaxis_opts=opts.AxisOpts(axislabel_opts={"rotate": 35}))
return bar
def yinrou():
bar = Bar(init_opts=opts.InitOpts(width="550px", height="500px", theme=ThemeType.MACARONS))
bar.add_xaxis(
['高铁乘务1812', '高铁乘务1809', '高铁乘务1810', '高铁乘务1811', '高铁乘务1917', '高铁乘务1915', '高铁乘务1916', '高铁乘务1913', '高铁乘务1914',
'物流管理1838'])
bar.add_yaxis('男', [7, 6, 6, 7, 9, 11, 8, 10, 10, 24])
bar.add_yaxis('女', [40, 39, 39, 39, 37, 36, 36, 35, 35, 34])
bar.set_global_opts(title_opts=opts.TitleOpts(title='十大阴柔班级'),
xaxis_opts=opts.AxisOpts(axislabel_opts={"rotate": 35}))
return bar
def get_china_map():
map = Map(init_opts=opts.InitOpts(width='1024px', height='768px', theme=ThemeType.LIGHT))
map.add("人数",
data_pair=[['陕西', 7723], ['河南', 81], ['青海', 61], ['山东', 54], ['甘肃', 47], ['山西', 46], ['河北', 38], ['四川', 31],
['浙江', 19], ['贵州', 18], ['湖北', 16], ['湖南', 15], ['新疆', 74], ['吉林', 14], ['安徽', 10], ['广东', 11],
['宁夏', 53], ['江苏', 7], ['海南', 7], ['福建', 6], ['内蒙古', 23], ['黑龙江', 2], ['重庆', 3], ['北京', 1],
['内蒙', 1], ['重庆市', 2], ['天津', 1], ['广西', 2]], maptype='china')
map.set_global_opts(title_opts=opts.TitleOpts(title="2018与2019级学生居住地分布", subtitle='共有8366条有效数据'),
visualmap_opts=opts.VisualMapOpts(is_show=False))
return map
def get_shanxi_map():
map = Map(init_opts=opts.InitOpts(width='1024px', height='768px', theme=ThemeType.LIGHT))
map.add("人数",
data_pair=[['西安市', 1736], ['咸阳市', 1491], ['渭南市', 1344], ['宝鸡市', 1105], ['商洛市', 571], ['榆林市', 373],
['汉中市', 368], ['延安市', 349], ['安康市', 196], ['铜川市', 188]], maptype='陕西')
map.set_global_opts(title_opts=opts.TitleOpts(title="2018与2019级陕西学生居住地分布", subtitle='共有7723条有效数据'),
visualmap_opts=opts.VisualMapOpts(max_=2000, pos_bottom='100px'))
return map
def get_sex_pie():
pie = Pie(init_opts=opts.InitOpts(width='550px', height='550px', theme=ThemeType.LIGHT))
pie.add('人数', data_pair=[['男', 6025], ['女', 2357]])
pie.set_global_opts(title_opts=opts.TitleOpts(title="2018与2019级男女比例", subtitle='共有8382条有效数据'))
return pie
def get_xueyuan_pie():
pie = Pie(init_opts=opts.InitOpts(width='550px', height='550px', theme=ThemeType.LIGHT))
pie.add('人数',
data_pair=[['轨道交通学院', 2655], ['公路与铁道工程学院', 1927], ['建筑与测绘工程学院', 1253], ['汽车工程学院', 955], ['经济管理学院', 833],
['交通信息学院', 759]])
pie.set_global_opts(title_opts=opts.TitleOpts(title="2018与2019级学院人数占比", subtitle='共有8382条有效数据'),
legend_opts=opts.LegendOpts(pos_left='300px'))
return pie
def get_year_bar():
pie = Pie(init_opts=opts.InitOpts(width='550px', height='550px', theme=ThemeType.LIGHT))
pie.add('年龄', data_pair=[['19岁', 3348], ['18岁', 2550], ['20岁', 1565], ['17岁', 414], ['21岁', 379], ['22岁', 70],
['16岁', 31], ['23岁', 15], ['15岁', 5], ['24岁', 3], ['38岁', 1], ['14岁', 1]])
pie.set_global_opts(title_opts=opts.TitleOpts(title="2018与2019级学院年龄占比", subtitle='共有8382条有效数据'),
legend_opts=opts.LegendOpts(pos_left='300px'))
return pie
def get_class_time():
nowdate = datetime.now()
dongji = datetime.strptime('%s-10-01' % datetime.now().strftime('%Y'), '%Y-%m-%d')
xiaji = datetime.strptime('%s-05-01' % datetime.now().strftime('%Y'), '%Y-%m-%d')
if dongji >= nowdate >= xiaji:
# 上课时间全部提前十分钟准备
first_on = '07:50'
first_off = '09:50'
second_on = '10:10'
second_off = '12:10'
third_on = '14:20'
third_off = '16:20'
fourth_on = '16:30'
fourth_off = '18:30'
fifth_on = '19:30'
fifth_off = '21:30'
else:
first_on = '07:50'
first_off = '09:50'
second_on = '10:10'
second_off = '12:10'
third_on = '13:50'
third_off = '15:50'
fourth_on = '16:00'
fourth_off = '18:00'
fifth_on = '18:55'
fifth_off = '21:00'
return first_on, first_off, second_on, second_off, third_on, third_off, fourth_on, fourth_off, fifth_on, fifth_off
first_on, first_off, second_on, second_off, third_on, third_off, fourth_on, fourth_off, fifth_on, fifth_off = get_class_time()
def check_teach_time(time):
if first_on <= time <= first_off:
return '12'
elif second_on <= time <= second_off:
return '34'
elif third_on <= time <= third_off:
return '56'
elif fourth_on <= time <= fourth_off:
return '78'
elif fifth_on <= time <= fifth_off:
return '910'
else:
return False
def is_teach(time: str):
"""
判断时间是否在上课时间内
:param time:
:return: True or False and message
"""
# or second_on <= time <= second_off or third_on <= time <= third_off or fourth_on <= time <= fourth_off or
# fifth_on <= time <= fifth_off
if time < first_on:
return False, '1 2节上课时间为%s - %s' % (first_on, first_off)
elif first_on <= time <= first_off:
return True, ''
elif first_off < time < second_on:
return False, '3 4节上课时间为%s - %s' % (second_on, second_off)
elif second_on <= time <= second_off:
return True, ''
elif second_off < time < third_on:
return False, '5 6节上课时间为%s - %s' % (third_on, third_off)
elif third_on <= time <= third_off:
return True, ''
elif third_off < time < fourth_on:
return False, '7 8节上课时间为%s - %s' % (fourth_on, fourth_off)
elif fourth_on <= time <= fourth_off:
return True, ''
elif fourth_off < time < fifth_on:
return False, '9 10节上课时间为%s - %s' % (fifth_on, fifth_off)
elif fifth_on <= time <= fifth_off:
return True, ''
else:
return False, '太晚了,不在上课时间'
def weather():
d = json.load(open('weather.json', 'r', encoding='utf-8'))
data = d['data']['forecast']
date = [i['ymd'][-5:] for i in data]
high = [int(i['high'].strip('高温').strip('℃').strip()) for i in data]
low = [int(i['low'].strip('低温').strip('℃').strip()) for i in data]
line = Line(init_opts=opts.InitOpts(width='700px', height='350px', theme=ThemeType.LIGHT))
line.add_xaxis(date) # 设置x轴数据
line.add_yaxis("高温", high, is_smooth=True) # 设置y轴数据
line.add_yaxis("低温", low, is_smooth=True) # 设置y轴数据
line.set_global_opts(tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-20)), # 设置x轴标签旋转角度
yaxis_opts=opts.AxisOpts(name="温度/单位:摄氏度"), # 设置y轴名称
title_opts=opts.TitleOpts(title="西安未来15天温度")) # 设置图表标题
shidu = d['data']['shidu']
pm25 = d['data']['pm25']
quality = d['data']['quality']
ganmao = d['data']['ganmao']
aqi = d['data']['forecast'][0]['aqi']
hight = d['data']['forecast'][0]['high'].strip('高温')
lowt = d['data']['forecast'][0]['low'].strip('低温')
wendu = lowt + ' ~ ' + hight
weather_type = d['data']['forecast'][0]['type']
notice = d['data']['forecast'][0]['notice']
ymd = d['data']['forecast'][0]['ymd']
return line.render_embed(), notice, weather_type, wendu, shidu, pm25, quality, aqi, ganmao, ymd
# def gen(camera):
# while True:
# frame = camera.get_frame()
# # 使用generator函数输出视频流, 每次请求输出的content类型是image/jpeg
# yield (b'--frame\r\n'
# b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
#
#
# class VideoCamera(object):
# def __init__(self):
# # 通过opencv获取实时视频流
# self.video = cv2.VideoCapture("rtsp://admin:jsjzx@51101@172.29.99.6:554/cam/realmonitor?channel=8&subtype=1")
# # self.face_engine = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
#
# def __del__(self):
# self.video.release()
#
# def get_frame(self):
# success, image = self.video.read()
# # faces = self.face_engine.detectMultiScale(image, scaleFactor=1.3, minNeighbors=5)
# # 对每一张脸,进行如下操作
# # for (x, y, w, h) in faces:
# # # 画出人脸框,蓝色(BGR色彩体系),画笔宽度为2
# # image = cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 1)
# # 因为opencv读取的图片并非jpeg格式,因此要用motion JPEG模式需要先将图片转码成jpg格式图片
# ret, jpeg = cv2.imencode('.jpg', image)
# return jpeg.tobytes()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/luenay/flask_dengji.git
git@gitee.com:luenay/flask_dengji.git
luenay
flask_dengji
flask_dengji
master

搜索帮助