This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
170 lines (149 loc) · 5.25 KB
/
app.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
""" main app for wetterdienst-ui """
import os
import dash
import pandas as pd
import plotly.graph_objects as go
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
from wetterdienst.dwd.observations import (
DWDObservationStations,
DWDObservationValues,
DWDObservationPeriod,
DWDObservationResolution,
DWDObservationParameterSet,
)
from wetterdienst.exceptions import InvalidParameterCombination
from ui.layouts.observations_germany import dashboard_layout
from ui.plotting.figure import default_figure
app = dash.Dash(
__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}]
)
server = app.server
app.layout = dashboard_layout()
OBSERVATION_VALUES_PARAMETER_COLUMN = "PARAMETER"
OBSERVATION_VALUES_VALUE_COLUMN = "VALUE"
OBSERVATION_VALUES_DATE_COLUMN = "DATE"
@app.callback(
Output("hidden-div-metadata", "children"),
[
Input("select-parameter", "value"),
Input("select-time-resolution", "value"),
Input("select-period-type", "value"),
],
)
def update_meta_data(parameter, time_resolution, period_type):
"""
Function to update the metadata according to
the selection of the dropdowns
It stores MetaData behind a hidden div on the front-end
"""
try:
meta_data = DWDObservationStations(
parameter=DWDObservationParameterSet(parameter),
resolution=DWDObservationResolution(time_resolution),
period=DWDObservationPeriod(period_type),
).all()
except InvalidParameterCombination:
raise PreventUpdate
return meta_data.to_json(date_format="iso", orient="split")
@app.callback(
Output("graph1", "figure"),
[Input("select-variable", "value")],
[State("hidden-div", "children")],
)
def make_graph(variable, jsonified_data):
""" takes hidden data to show up the central plot """
climate_data = pd.read_json(jsonified_data, orient="split")
fig = default_figure(climate_data, variable)
fig.update_layout(
margin=go.layout.Margin(
l=0, # left margin
r=0, # right margin
b=0, # bottom margin
t=0, # top margin
)
)
return fig
@app.callback(
Output("hidden-div", "children"),
[Input("select-weather-stations", "value")],
[
State("select-parameter", "value"),
State("select-time-resolution", "value"),
State("select-period-type", "value"),
],
)
def update_data(
station_id: int, parameter: str, time_resolution: str, period_type: str
):
""" stores selected data behind a hidden div box to share with other callbacks """
climate_data = DWDObservationValues(
station_id=station_id,
parameter=DWDObservationParameterSet(parameter),
resolution=DWDObservationResolution(time_resolution),
period=DWDObservationPeriod(period_type),
humanize_parameters=True,
).all()
climate_data = climate_data.dropna(axis=0)
climate_data.VALUE = climate_data.VALUE.astype(float)
climate_data = climate_data.pivot_table(
values=OBSERVATION_VALUES_VALUE_COLUMN,
columns=OBSERVATION_VALUES_PARAMETER_COLUMN,
index=OBSERVATION_VALUES_DATE_COLUMN,
)
return climate_data.to_json(date_format="iso", orient="split")
@app.callback(Output("select-variable", "options"), [Input("hidden-div", "children")])
def update_variable_drop_down(jsonified_data):
""" Depending on the selection the variable drop_down is adapted """
climate_data = pd.read_json(jsonified_data, orient="split")
return [{"label": column, "value": column} for column in climate_data.columns]
@app.callback(
Output("select-weather-stations", "options"),
[Input("hidden-div-metadata", "children")],
)
def update_weather_stations_dropdown(jsonified_data):
""" Depending on the selection the variable drop_down is adapted """
meta_data = pd.read_json(jsonified_data, orient="split")
return [
{"label": name, "value": station_id}
for name, station_id in zip(meta_data.STATION_NAME, meta_data.STATION_ID)
]
@app.callback(Output("sites-map", "figure"), [Input("hidden-div-metadata", "children")])
def update_systems_map(jsonified_data):
meta_data = pd.read_json(jsonified_data, orient="split")
fig = go.Figure(
go.Scattermapbox(
lat=meta_data.LATITUDE,
lon=meta_data.LONGITUDE,
mode="markers",
marker=go.scattermapbox.Marker(size=10),
text=[
f"{name} <br>Station Height: {altitude}m <br>Id: {station_id}"
for name, altitude, station_id in zip(
meta_data.STATION_NAME,
meta_data.HEIGHT,
meta_data.STATION_ID,
)
],
)
)
fig.update_layout(
hovermode="closest",
mapbox=dict(
bearing=0,
center=go.layout.mapbox.Center(lat=50, lon=10),
style="open-street-map",
pitch=0,
zoom=5,
),
margin=go.layout.Margin(
l=0,
r=0,
b=0,
t=0,
),
)
return fig
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8050))
server.run(host="0.0.0.0", port=port, processes=4)