-
Notifications
You must be signed in to change notification settings - Fork 1
/
dash_test.py
107 lines (92 loc) · 3.7 KB
/
dash_test.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
105
106
# -*- coding: utf-8 -*-
import apsw
import dash
import dash_core_components as dcc
import dash_html_components as html
import datetime
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import time
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
def get_start_of_today(now):
"""
Returns unix epoch of the start of the day, based on input 'now'
"""
start_of_today = datetime.datetime.fromtimestamp(now, datetime.timezone.utc)\
.replace(hour=0, minute=0, second=0, microsecond=0)
start_of_today = start_of_today.timestamp()
return start_of_today
# Create the app
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# The graph to be given to the app
graph = dcc.Graph(id="num_streams")
app.layout = html.Div(style={"background-color": "#222222",
"color": "#EEEEEE", "width": 1400},
children=[html.H1(children="LBRYnomics Interactive"),
html.P(children="This is just a test!"),
graph, dcc.Interval(
id='interval-component',
interval=60*1000, # in milliseconds
n_intervals=0
)])
# Multiple components can update everytime interval gets fired.
@app.callback(Output('num_streams', 'figure'),
[Input('interval-component', 'n_intervals')])
def update_data(n):
# Get recent data from database
conn = apsw.Connection("db/lbrynomics.db")
db = conn.cursor()
ts = []
ys = []
now = time.time()
begin = get_start_of_today(now)
for row in db.execute("SELECT time, num_streams FROM measurements\
WHERE time >= ?;", (begin, )):
ts.append(row[0])
ys.append(row[1])
ts, ys = np.array(ts), np.array(ys)
ts = (ts - begin)/3600.0
data = {}
if len(ts) <= 1:
data = {"ts": [1, 2], "ys": [1, 2], "changes": [0]}
ys = ys - ys[0]
changes = np.diff(ys)
conn.close()
data["ts"] = ts
data["ys"] = ys
data["changes"] = changes
print("Refreshed data.", flush=True)
fig = make_subplots(rows=2, cols=1)
fig.update_layout(
{
"title": "Publications on LBRY today",
"plot_bgcolor": "#222222",
"paper_bgcolor": "#222222",
"xaxis": {"title": "Hours since UTC midnight"},
"yaxis": {"title": "Number of publications"},
"font": {"color": "#EEEEEE"},
"width": 1400, "height": 1400
}
)
line = go.Scatter(x=data["ts"], y=data["ys"], mode="lines+markers",
marker_color="blue", name="Cumulative")
fig.add_trace(line, row=1, col=1)
bar = go.Bar(x=data["ts"][1:], y=data["changes"],
marker_color="orange", name="5 minute change")
fig.add_trace(bar, row=2, col=1)
# fig.update_layout(data=[{"type": "line", "x": data["ts"], "y": data["ys"]}],
# row=1, col=1)
"""
"data": [{"x": data["ts"], "y": data["ys"], "type": "line",
"mode": "lines+markers",
"name": "Cumulative"},
{"x": data["ts"][1:], "y": data["changes"],
"type": "bar",
"name": "5 minute change"}]
})
"""
return fig
if __name__ == '__main__':
app.run_server(host="0.0.0.0")