-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
104 lines (92 loc) · 4.49 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit
import json
import time
import random
import threading
# Required for server-side emit() to work
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dreamchaser'
socketio = SocketIO(app)
@app.route("/")
def index():
title = "Example Chart"
return render_template("index.html", title=title)
def produce_chart_data():
while True:
# Sleep for random duration to prove async working
time.sleep(0.1)
# Get some data from source and emit to clients when recieved
data = get_some_data()
socketio.emit('new-chart-data', data)
print("Emit data")
def get_some_data():
data = {
"series": [
{
"name": 'Data 1',
"data": [
{"x": 143034652600, "y": random.random()*10+70},
{"x": 143134652600, "y": random.random()*10+70},
{"x": 143234652600, "y": random.random()*10+70},
{"x": 143334652600, "y": random.random()*10+70},
{"x": 143434652600, "y": random.random()*10+70},
{"x": 143534652600, "y": random.random()*10+70},
{"x": 143634652600, "y": random.random()*10+70},
{"x": 143734652600, "y": random.random()*10+70},
{"x": 143834652600, "y": random.random()*10+70},
{"x": 143934652600, "y": random.random()*10+70}
]
}, {
"name": 'Data 2',
"data": [
{"x": 143034652600, "y": random.random()*10+40},
{"x": 143134652600, "y": random.random()*10+40},
{"x": 143234652600, "y": random.random()*10+40},
{"x": 143334652600, "y": random.random()*10+40},
{"x": 143434652600, "y": random.random()*10+40},
{"x": 143534652600, "y": random.random()*10+40},
{"x": 143634652600, "y": random.random()*10+40},
{"x": 143734652600, "y": random.random()*10+40},
{"x": 143834652600, "y": random.random()*10+40},
{"x": 143934652600, "y": random.random()*10+40}
]
}, {
"name": 'Data 3',
"data": [
{"x": 143034652600, "y": random.random()*10+25},
{"x": 143134652600, "y": random.random()*10+25},
{"x": 143234652600, "y": random.random()*10+25},
{"x": 143334652600, "y": random.random()*10+25},
{"x": 143434652600, "y": random.random()*10+25},
{"x": 143534652600, "y": random.random()*10+25},
{"x": 143634652600, "y": random.random()*10+25},
{"x": 143734652600, "y": random.random()*10+25},
{"x": 143834652600, "y": random.random()*10+25},
{"x": 143934652600, "y": random.random()*10+25}
]
}, {
"name": 'Data 3',
"data": [
{"x": 143034652600, "y": random.random()*10+25},
{"x": 143134652600, "y": random.random()*10+25},
{"x": 143234652600, "y": random.random()*10+25},
{"x": 143334652600, "y": random.random()*10+25},
{"x": 143434652600, "y": random.random()*10+25},
{"x": 143534652600, "y": random.random()*10+25},
{"x": 143634652600, "y": random.random()*10+25},
{"x": 143734652600, "y": random.random()*10+25},
{"x": 143834652600, "y": random.random()*10+25},
{"x": 143934652600, "y": random.random()*10+25}
]
}
]}
return data
if __name__ == '__main__':
t = threading.Thread(target=produce_chart_data)
t.start()
PORT = json.load(open('config.json'))["PORT"]
print("Running on localhost:"+str(PORT))
socketio.run(app, host='0.0.0.0', port=PORT)