-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_modules.py
476 lines (407 loc) · 15.5 KB
/
app_modules.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import streamlit as st
import base64
from bokeh.io import export_svgs, export_png
import os
import io
from deepmip_modules import get_csv_data
def init_widgets_single_site(page):
# initialise user input widgets
with st.form(key="my_form"):
# some settings that depend on the current page
if page == "extract_data":
st.info(
"""
Select the present-day location of your site and the variable you are interested in.
"""
)
button_label = "GET MODEL DATA"
elif page == "plot_local":
st.info(
"""
Select the present-day location of your site and the variable you are interested in.
"""
)
button_label = "UPDATE FIGURES"
elif page == "plot_map":
st.info(
"""
Select the present-day location of your site and an optional label.
"""
)
button_label = "UPDATE MAPS"
col1a, col1b, col1c, col1d = st.columns(4)
with col1a:
# use default value if input has not been defined before
if "modern_lat" not in st.session_state:
st.session_state.modern_lat = 52.4
modern_lat = st.number_input(
label="modern latitude of site",
min_value=-90.0,
max_value=90.0,
step=1.0,
format="%.1f",
key="modern_lat",
)
with col1b:
if "modern_lon" not in st.session_state:
st.session_state.modern_lon = 11.8
modern_lon = st.number_input(
label="modern longitude of site",
min_value=-180.0,
max_value=180.0,
step=1.0,
format="%.1f",
key="modern_lon",
)
with col1c:
variable_list = [
"Near-Surface Air Temperature",
"Sea Surface Temperature",
"Precipitation",
]
if "user_variable" not in st.session_state:
st.session_state.user_variable = "Near-Surface Air Temperature"
user_variable = st.selectbox(
label="variable",
options=variable_list,
key="user_variable",
)
with col1d:
if "user_site_name" not in st.session_state:
st.session_state.user_site_name = "Store Bælt (Denmark) "
user_site_name = st.text_input(
label="OPTIONAL: name of site",
key="user_site_name",
)
# add optional proxy comparison to user input
if page == "plot_local":
col2a, col2b, col2c = st.columns(3)
with col2a:
st.write("compare to proxy?")
if "proxy_check" in st.session_state:
proxy_check = st.checkbox(label=" ", key="proxy_check")
else:
proxy_check = st.checkbox(label=" ", key="proxy_check", value=True)
with col2b:
if "proxy_mean" not in st.session_state:
st.session_state.proxy_mean = 26.6
proxy_mean = st.number_input(
label="proxy mean",
step=1.0,
format="%.1f",
key="proxy_mean",
)
with col2c:
if "proxy_std" not in st.session_state:
st.session_state.proxy_std = 2.85
proxy_std = st.number_input(
label="proxy uncertainty",
step=1.0,
format="%.1f",
key="proxy_std",
)
submit_button = st.form_submit_button(
label=button_label, use_container_width=True, type="primary"
)
if page == "extract_data":
return (modern_lat, modern_lon, user_variable, user_site_name)
elif page == "plot_local":
return (
modern_lat,
modern_lon,
user_variable,
user_site_name,
proxy_check,
proxy_mean,
proxy_std,
)
elif page == "plot_map":
return (modern_lat, modern_lon, user_site_name)
def reset_csv_data():
# delete previous input from session state
if "csv_input" in st.session_state:
del st.session_state["csv_input"]
if "site_name" in st.session_state:
del st.session_state["site_name"]
def init_widgets_multi_site(page):
# some settings that depend on the current page
if page == "extract_data":
csv_proxy_flag = False
csv_placeholder = "name, modern latitude, modern longitude"
button_label = "GET MODEL DATA"
elif page == "plot_local":
csv_proxy_flag = True
csv_placeholder = "name, modern latitude, modern longitude, proxy mean (OPTIONAL), proxy uncertainty (OPTIONAL)"
button_label = "UPDATE FIGURES"
elif page == "plot_map":
csv_proxy_flag = False
csv_placeholder = "name, modern latitude, modern longitude"
button_label = "UPDATE MAPS"
st.info(
"""
Enter the names and present-day locations of your sites in the text box below.
Or you can use the dropdown menu below to pick a selection of sites from the DeepMIP compilation.
"""
)
template_list = [
"Enter your own data",
"DeepMIP marine proxies (latest Paleocene)",
"DeepMIP marine proxies (Paleocene–Eocene Thermal Maximum)",
"DeepMIP marine proxies (early Eocene Climatic Optimum)",
"DeepMIP marine proxies (all time periods)",
"DeepMIP terrestrial proxies (latest Paleocene)",
"DeepMIP terrestrial proxies (Paleocene–Eocene Thermal Maximum)",
"DeepMIP terrestrial proxies (early Eocene Climatic Optimum)",
"DeepMIP terrestrial proxies (all time periods)",
"DeepMIP marine+terrestrial proxies (latest Paleocene)",
"DeepMIP marine+terrestrial proxies (Paleocene–Eocene Thermal Maximum)",
"DeepMIP marine+terrestrial proxies (early Eocene Climatic Optimum)",
"DeepMIP marine+terrestrial proxies (all time periods)",
]
csv_choice = st.selectbox(
label="Choose a template from the DeepMIP proxy compilation or enter your own data:",
options=template_list,
key="csv_choice",
on_change=reset_csv_data,
)
if "csv_input" not in st.session_state:
st.session_state.csv_input = get_csv_data(csv_choice, csv_proxy_flag)
# initialise user input widgets
with st.form(key="my_form"):
csv_input = st.text_area(
label="Enter your data below (CSV format)",
# value=csv_data,
placeholder=csv_placeholder,
height=200,
key="csv_input",
)
variable_list = [
"Near-Surface Air Temperature",
"Sea Surface Temperature",
"Precipitation",
]
user_variable = st.selectbox(
label="variable",
options=variable_list,
key="user_variable",
)
submit_button = st.form_submit_button(
label=button_label, use_container_width=True, type="primary"
)
if page == "plot_map":
return (csv_choice, csv_input)
else:
return (csv_choice, csv_input, user_variable)
def get_base64(bin_file):
with open(bin_file, "rb") as f:
data = f.read()
return base64.b64encode(data).decode()
def add_logo():
bin_str = get_base64("img/deepmip_logo.png")
# bin_str = get_base64('deepmip_banner2.jpg')
page_bg_img = (
"""
<style>
[data-testid="stSidebarNav"] {
background-image: url("data:image/png;base64,%s");
background-repeat: no-repeat;
padding-top: 115px;
background-position: 70px 30px;
background-size: 150px 150px;
}
[data-testid="stSidebarNav"]::before {
content: "DeepMIP-Eocene Database";
margin-left: 22px;
margin-top: 20px;
font-size:22px;
position: relative;
top: 90px;
}
</style>
"""
% bin_str
)
st.markdown(page_bg_img, unsafe_allow_html=True)
def init_sidebar():
add_logo()
css = """
<style>
[data-testid='stSidebarNav'] > ul {
min-height: 330px;
}
</style>
"""
st.markdown(css, unsafe_allow_html=True)
st.sidebar.success("How to Get Started")
st.sidebar.markdown(
"""
1. ☝️ Use the navigation above to select the page of your choice.
2. Select the present-day location of your site(s) and the variable you are
interested in.
3. Optionally, you can add a proxy estimate for quick comparison with
the model data.
4. Click the red button below the form and wait for the results to update.
"""
)
st.sidebar.subheader("Links")
st.sidebar.markdown(
"Get the code: [](https://github.com/sebsteinig/deepmip_database_app)",
unsafe_allow_html=True,
)
st.sidebar.markdown(
# "Get in touch: [](https://twitter.com/sebsteinig)",
"Get in touch: [](mailto:[email protected])",
unsafe_allow_html=True,
)
st.sidebar.markdown(
# "Get more info: [www.deepmip.org](https://www.deepmip.org)",
"Get more info: [](https://www.deepmip.org)",
unsafe_allow_html=True,
)
def delete_figures(file1, file2, file3):
for file in [file1, file2, file3]:
if os.path.exists(file):
os.remove(file)
def customDownloadButton_JPG_PNG_PDF(figure, filename):
if "single_map" not in st.session_state:
# if st.session_state.get('single_map') != True:
button_map = st.button(
"Download Map", type="primary", use_container_width=True, key=filename
)
st.session_state["single_map"] = button_map # Saved the state
print(st.session_state["single_map"])
st.stop()
if st.session_state["single_map"] == True:
progress_bar = st.progress(0)
progress_bar.progress(1 / 3, "Creating JPG")
fn3 = filename + ".jpg"
img3 = io.BytesIO()
figure.savefig(img3, format="jpg", dpi=200)
progress_bar.progress(2 / 3, "Creating PNG")
fn = filename + ".png"
img = io.BytesIO()
figure.savefig(img, format="png", dpi=300)
progress_bar.progress(3 / 3, "Creating PDF")
fn2 = filename + ".pdf"
img2 = io.BytesIO()
figure.savefig(img2, format="pdf")
progress_bar.empty()
col1, col2, col3 = st.columns(3)
with col1:
st.download_button(
label="Download JPG",
data=img3,
file_name=fn3,
mime="image/jpg",
use_container_width=True,
)
with col2:
st.download_button(
label="Download PNG",
data=img,
file_name=fn,
mime="image/png",
use_container_width=True,
)
with col3:
st.download_button(
label="Download PDF",
data=img2,
file_name=fn2,
mime="image/pdf",
use_container_width=True,
)
def customDownloadButton_JPG_PNG(figure, filename):
if "model_maps" not in st.session_state:
# if st.session_state.get('model_maps') != True:
button_models = st.button(
"Download Model Maps",
type="primary",
use_container_width=True,
key=filename,
)
st.session_state["model_maps"] = button_models # Saved the state
if st.session_state["model_maps"] == True:
progress_bar = st.progress(0)
progress_bar.progress(1 / 2, "Creating JPG")
fn3 = filename + ".jpg"
img3 = io.BytesIO()
figure.savefig(img3, format="jpg", dpi=200)
progress_bar.progress(1 / 2, "Creating PNG")
fn = filename + ".png"
img = io.BytesIO()
figure.savefig(img, format="png", dpi=300)
progress_bar.empty()
col1, col2 = st.columns(2)
with col1:
st.download_button(
label="Download JPG",
data=img3,
file_name=fn3,
mime="image/jpg",
use_container_width=True,
)
with col2:
st.download_button(
label="Download PNG",
data=img,
file_name=fn,
mime="image/png",
use_container_width=True,
)
def customDownloadButton2(render1, render2, filename1, filename2):
if st.button("Download Figures", type="primary", use_container_width=True):
progress_bar = st.progress(0)
progress_bar.progress(1 / 2, "Exporting PNGs")
export_png(render1, filename=filename1 + ".png")
export_png(render2, filename=filename2 + ".png")
progress_bar.progress(2 / 2, "Exporting SVGs")
render1.output_backend = "svg"
render2.output_backend = "svg"
export_svgs(render1, filename=filename1 + ".svg")
export_svgs(render2, filename=filename2 + ".svg")
progress_bar.empty()
col1, col2 = st.columns(2)
with col1:
with open(filename1 + ".png", "rb") as file:
st.download_button(
label="Download Figure 1 as PNG",
data=file,
file_name=filename1 + ".png",
mime="image/",
use_container_width=True,
on_click=delete_figures,
args=(filename1 + ".svg", filename2 + ".png", filename2 + ".svg"),
)
with open(filename1 + ".svg", "rb") as file:
st.download_button(
label="Download Figure 1 as SVG",
data=file,
file_name=filename1 + ".svg",
mime="image/svg",
use_container_width=True,
on_click=delete_figures,
args=(filename1 + ".png", filename2 + ".png", filename2 + ".svg"),
)
col3, col4 = st.columns(2)
with col2:
with open(filename2 + ".png", "rb") as file:
st.download_button(
label="Download Figure 2 as PNG",
data=file,
file_name=filename2 + ".png",
mime="image/",
use_container_width=True,
on_click=delete_figures,
args=(filename1 + ".png", filename1 + ".svg", filename2 + ".svg"),
)
with open(filename2 + ".svg", "rb") as file:
st.download_button(
label="Download Figure 2 as SVG",
data=file,
file_name=filename2 + ".svg",
mime="image/svg",
use_container_width=True,
on_click=delete_figures,
args=(filename1 + ".png", filename1 + ".svg", filename2 + ".png"),
)