-
Notifications
You must be signed in to change notification settings - Fork 4
/
usage.py
206 lines (186 loc) · 5.55 KB
/
usage.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Example usage of DashWtgViewer component"""
import json
from dash import Dash, html, Input, Output, State, no_update
import dash_bootstrap_components as dbc
from pydantic import BaseModel
from pathlib import Path
from dash_wtgviewer.DashWtgviewer import DashWtgviewer
from dash_wtgviewer.model import Model
from dash_wtgviewer.model.results import Results
def load_valid_model(json_path: str, model: BaseModel) -> dict:
data = json.load(open(Path(json_path).resolve(), "r"))
valid = model.parse_obj(data)
return json.loads(valid.json())
# external CSS stylesheets
external_stylesheets = [
{
"href": "https://unpkg.com/[email protected]/dist/leaflet.css",
"rel": "stylesheet",
"integrity": "sha256-sA+zWATbFveLLNqWO2gtiw3HL/lh1giY/Inf1BJ0z14=",
"crossorigin": "",
},
{
"href": "https://unpkg.com/[email protected]/dist/leaflet.js",
"rel": "stylesheet",
"integrity": "sha256-o9N1jGDZrf5tS+Ft4gbIK7mYMipq9lqpVJ91xHSyKhg=",
"crossorigin": "",
},
]
app = Dash(external_stylesheets=external_stylesheets)
app.layout = html.Div(
[
DashWtgviewer(
id="viewer",
model=load_valid_model("assets/ea1_model.json", Model),
show_map=False,
environment=False,
tooltip=False,
stats=False,
map={
"center": {"id": "center", "lat": 52.29733, "lng": 2.35038},
"turbines": {
"positions": json.load(open("assets/ea1_turbines.json", "r"))
},
"boundary": {
"positions": json.load(open("assets/ea1_boundary.json", "r"))
},
},
),
html.Div(
[
dbc.Switch(
id="toggle_map",
label="map",
value=False,
),
dbc.Switch(
id="toggle_environment",
label="environment",
value=False,
),
dbc.Switch(
id="toggle_tooltip",
label="tooltip",
value=False,
),
dbc.Switch(
id="toggle_stats",
label="stats",
value=False,
),
dbc.Switch(
id="toggle_results",
label="results",
value=False,
),
dbc.Card(
dbc.CardBody(
[
html.H6("Change colorscale limits:", className="card-subtitle"),
dbc.InputGroup(
[
dbc.Input(id="minimum", placeholder="minimum", type="number"),
dbc.Input(id="maximum", placeholder="maximum", type="number")
]
),
dbc.Button("Done", id="change_colorscale")
]
),
id="input_colorscale_limits",
style={
"display": "none",
"position": "center"
}
)
],
style={
"zIndex": "100",
"right": "0px",
"top": "0px",
"margin": "20px",
"position": "absolute",
"display": "block",
},
),
],
style={"width": "100vw", "height": "100vh"},
)
@app.callback(
Output("viewer", "colorscale"),
Input("change_colorscale", "n_clicks"),
State("minimum", "value"),
State("maximum", "value"),
prevent_initial_call=True,
)
def set_min_max(n, min, max):
return {
"visible": True,
"limits": {
"min": min,
"max": max
}
}
@app.callback(
Output("input_colorscale_limits", "style"),
Input("viewer", "colorscale_clicked"),
prevent_initial_call=True,
)
def show_min_max(show_viewer):
if show_viewer:
return {"display": "block"}
return {"display": "none"}
@app.callback(
Output("viewer", "stats"),
Input("toggle_stats", "value"),
prevent_initial_call=True,
)
def toggle_map(toggle):
return toggle
@app.callback(
Output("viewer", "tooltip"),
Input("toggle_tooltip", "value"),
prevent_initial_call=True,
)
def toggle_map(toggle):
return toggle
@app.callback(
Output("viewer", "results"),
Input("toggle_results", "value"),
prevent_initial_call=True,
)
def toggle_results(toggle):
if toggle:
return load_valid_model("assets/ea1_results.json", Results)
else:
return {}
@app.callback(
Output("viewer", "environment"),
Input("toggle_environment", "value"),
prevent_initial_call=True,
)
def toggle_map(toggle):
return toggle
@app.callback(
Output("viewer", "show_map"),
Input("toggle_map", "value"),
State("viewer", "show_map"),
prevent_initial_call=True,
)
def toggle_map(toggle, show_map):
if show_map != toggle:
return toggle
else:
return no_update
@app.callback(
Output("toggle_map", "value"),
Input("viewer", "show_map"),
State("toggle_map", "value"),
prevent_initial_call=True,
)
def monitor_map(show_map, toggle):
if show_map != toggle:
return show_map
else:
return no_update
if __name__ == "__main__":
app.run_server(debug=False, port=8080)