forked from Gab0/japonicus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
184 lines (167 loc) · 5.71 KB
/
web.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import pandas as pd
import os
import flask
import dash
from dash.dependencies import Input, Output, Event
import dash_core_components as dcc
import dash_html_components as html
from flask_caching import Cache
from promoterz.statistics import statisticsNames
import Settings
gsettings = Settings.getSettings()['Global']
settings = Settings.getSettings()['bayesian']
def load_evolution_logs(filename=None):
FileList = os.listdir(gsettings["save_dir"])
filename = os.path.join(gsettings["save_dir"], filename)
df = pd.read_csv(filename, names=columns)
return df
def update_graph(GraphName, Statistics):
print('Loading')
ID = [s for s in GraphName if s.isdigit()]
'''
try:
df = load_evolution_logs(filename="evolution_gen_Locale%s.csv" % ''.join(ID))
except:
print("Failure to read evolution data.")
return None
'''
df = pd.DataFrame(Statistics)
annotations = []
for W in range(len(df['dateRange'])):
DR = df['dateRange'][W]
if DR != None:
annotations.append(
{
'xref': 'axis',
'yref': 'paper',
'xanchor': 'left',
'yanchor': 'bottom',
'font': {'family': 'Arial', 'size': 12, 'color': 'rgb(37,37,37)'},
'x': W,
'y': 1 if not len(annotations) %
2 else 0.93, # avoid label overlap;
'text': DR,
}
)
colorSequence = [
(188, 189, 34),
(100, 11, 182),
(186, 3, 34),
(45, 111, 45),
(66, 128, 66),
(128, 66, 66),
]
statNames = [
'avg', 'std', 'min', 'max', 'evaluationScore', 'evaluationScoreOnSecondary'
]
DATA = [
{
'x': df['id'],
'y': df[statNames[S]],
'type': 'line',
'name': statisticsNames[statNames[S]],
'line': {'color': 'rgb%s' % str(colorSequence[S])},
}
for S in range(len(statNames))
]
fig = {
'data': [
{
'x': [0, df["id"]],
'y': [0],
'type': 'line',
'name': 'markzero',
'line': {'color': 'rgb(0,0,0)'},
}
] +
DATA,
'layout': {'title': 'Evolution at %s' % GraphName, 'annotations': annotations},
}
return fig
def newGraphic(name):
G = dcc.Graph(id=name)
G.Active = True
return G
def run_server():
# Setup the app
server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server, csrf_protect=False)
app.scripts.config.serve_locally = False
dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-finance-1.28.0.min.js'
# Add caching
cache = Cache(app.server, config={'CACHE_TYPE': 'simple'})
timeout = 60 * 60 # 1 hour
# Controls
app.update_graph = update_graph
# Layout
app.GraphicList = []
app.newGraphic = lambda name: app.GraphicList.append(newGraphic(name))
app.layout = html.Div(
[
html.Div(
[
html.H2(
'japonicus Evolution Statistics',
style={'padding-top': '20', 'text-align': 'center'},
),
html.Div(
[
dcc.Interval(id='my-interval'),
dcc.RadioItems(
id='set-time',
value=5000,
options=[
{'label': 'Every 60 seconds', 'value': 60000},
{'label': 'Every 15 seconds', 'value': 15000},
{
'label': 'Every hour', 'value': 60 * 60 * 1000
}, # or just every hour
],
),
]
),
html.Div(id='display-time'),
]
),
html.Div(id='Graphs'),
],
style={'width': '1100', 'margin-left': 'auto', 'margin-right': 'auto', 'font-family': 'overpass', 'background-color': '#F3F3F3'},
# Traces>Color
)
app.config['suppress_callback_exceptions'] = True
@app.callback(
Output('display-time', 'children'), events=[Event('my-interval', 'interval')]
)
def display_time():
return str(datetime.datetime.now())
@app.callback(Output('my-interval', 'interval'), [Input('set-time', 'value')])
def update_interval(value):
return value
@cache.memoize(timeout=timeout)
@app.callback(
Output('Graphs', 'children'), events=[Event('my-interval', 'interval')]
)
def updateGraphs():
'''
for F in range(len(app.GraphicList)):
if app.GraphicList[F].Active:
app.GraphicList[F].__setattr__('figure', update_graph(app.GraphicList[F].id))
'''
return app.GraphicList
# External css
external_css = [
"https://fonts.googleapis.com/css?family=Overpass:400,400i,700,700i",
"https://cdn.rawgit.com/plotly/dash-app-stylesheets/c6a126a684eaaa94a708d41d6ceb32b28ac78583/dash-technical-charting.css",
]
for css in external_css:
app.css.append_css({"external_url": css})
# Run the Dash app
if __name__ == '__main__':
app.server.run(debug=True, host='0.0.0.0')
else: # this way it integrates with main interface without child procs across pipes,
return app
if __name__ == '__main__':
run_server()