This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
78 lines (58 loc) · 1.94 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from flask import Flask, render_template, jsonify
from os import listdir
from os.path import join, abspath, dirname
from typing import List
from flask_socketio import SocketIO
from random import choice
ROOT_DIR = dirname(abspath(__file__))
STATIC_DIR = join(ROOT_DIR, 'static')
app = Flask(__name__)
socketio = SocketIO(app)
students = None
class Student:
def __init__(self, student_id: int, name: str, major: str, gender: str,
image_path: str):
self.id = student_id
self.name = name
self.major = major
self.image_path = image_path
self.gender = gender
@app.route('/')
def show(**kwargs):
return render_template('show.html', students=students,
async_mode=socketio.async_mode, **kwargs)
@app.route('/settings')
def settings(**kwargs):
return render_template('settings.html', students=students,
async_mode=socketio.async_mode, **kwargs)
@socketio.on('start')
def start():
print('start')
socketio.emit('start')
@socketio.on('stop')
def stop(message):
print('stop')
socketio.emit('stop', {'winner': message['winner']})
@socketio.on('students')
def students():
print('students')
socketio.emit('students', {'students': students})
@socketio.on('connect')
def on_connect():
print('Client connected')
@socketio.on('disconnect')
def on_disconnect():
print('Client disconnected')
if __name__ == '__main__':
"""
Image file names are expected to have the form
<name> "_" [<name> "_"]* <major> "." <suffix>
"""
images: List[str] = listdir(join(STATIC_DIR, 'students'))
students = list()
for index, image in enumerate(images):
*name, major, gender = image.split('.')[0].split('_')
student = Student(index, ' '.join(name), major, gender,
join('static', 'students', image))
students.append(vars(student))
socketio.run(app, port=5000, debug=True)