-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (54 loc) · 1.99 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
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from random import random, randint
from time import sleep
from threading import Thread, Event
from scraper import Scraper
import datetime as dt
CANVAS_X = 1000
CANVAS_Y = 1000
COLOR_LIMIT = 250
app = Flask(__name__)
socketio = SocketIO(app)
thread = Thread()
thread_stop_event = Event()
@app.route('/')
def hello():
return render_template('visualisation.html', name='Test')
class StreamThread(Thread):
# This class grabs our data from the Scraper class, puts it in a queue for delivery.
def __init__(self):
self.time_delay = 5
self.selection = 10
super(StreamThread, self).__init__()
def text_generator(self):
# Generate a list first
while not thread_stop_event.isSet():
text = Scraper('George Floyd', self.selection, dt.date.today() - dt.timedelta(weeks=2)).get_text()[randint(0, self.selection-1)]
data = {'text': text,
'number_1': round(random() * CANVAS_X, 3),
'number_2': round(random() * CANVAS_Y, 3),
'c_1': round(random() * COLOR_LIMIT, 3),
'c_2': round(random() * COLOR_LIMIT, 3),
'c_3': round(random() * COLOR_LIMIT, 3)}
socketio.emit('textoutput', data, namespace='/test')
sleep(self.time_delay)
def position_generator(self):
pass
def run(self):
self.text_generator()
@socketio.on('connect', namespace='/test')
def test_connect():
# need visibility of the global thread object
global thread
print('Client connected')
#Start the random stream generator thread only if the thread has not been started before.
if not thread.isAlive():
print("Starting Thread")
thread = StreamThread()
thread.start()
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app)