Skip to content

Commit

Permalink
Merge pull request #8 from danhooke/ploomber_v1
Browse files Browse the repository at this point in the history
Ploomber v1
  • Loading branch information
danhooke authored Mar 13, 2024
2 parents b4b70ca + bebb593 commit 6933a8f
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 5 deletions.
162 changes: 157 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ def make_ds_combined(l_name):
return ds


def get_scenario(temp):
temp_ds = xr.Dataset()
for file in glob.glob("data/*"):
ds = xr.open_dataset(file)
if temp in ds.threshold.values:
ds = ds.sel(threshold=temp)
temp_ds = xr.merge(
[temp_ds, ds["score"].rename(ds.attrs["short_name"])], compat="override"
)
ds.close()
else:
ds.close()
return temp_ds


info = dfs.get_info()


Expand Down Expand Up @@ -198,12 +213,13 @@ def get_ind_text(ind):
+ "<span style='font-weight: 400; font-size: 16px'>"
+ ind_info["description"]
+ "</span>"
+ "\n"
+ "<br>"
+ "<span style='font-weight: 600; font-size: 16px'>Unit: </span>"
+ "<span style='font-weight: 400; font-size: 16px'>"
+ ind_info["unit"]
+ "</span>"
)
ind_desc = pn.pane.HTML(ind_desc, sizing_mode="stretch_width")
return ind_desc


Expand Down Expand Up @@ -289,6 +305,120 @@ def make_score_map_test(ind, t):
return layout


temp_box = pnw.MultiChoice(
name="Temperatures (select multiple):",
value=[1.2],
options=[1.2, 1.5, 2.0, 2.5, 3.0, 3.5],
)


def make_indepth_dboard(ind, temps):
ds = make_ds_combined(ind)
ind_info = get_plot_info(ind)
indepth_col = pn.Column()
for temp in temps:
score_map = (
ds["score"]
.sel(threshold=temp)
.hvplot(
x="Longitude",
y="Latitude",
xlabel="",
ylabel="",
# xticks=[-180],
width=600,
cmap="magma_r",
clim=(0, 6),
title=ds.attrs["long_name"]
+ " risk score at "
# + str(temp)
+ "°C (Data: Werning et al. 2023)",
)
.hist()
)
abs_map = (
ds["abs"]
.sel(threshold=temp)
.hvplot(
x="Longitude",
y="Latitude",
xlabel="",
ylabel="",
width=600,
cmap=ind_info["ind_cmap"],
clim=(ind_info["ind_min"], ind_info["ind_max"]),
title=ds.attrs["long_name"]
+ " absolute values at "
# + str(temp)
+ "°C (Data: Werning et al. 2023)",
)
.hist()
)
diff_map = (
ds["difference"]
.sel(
threshold=temp,
# stats="mean"
)
.hvplot(
x="Longitude",
y="Latitude",
xlabel="",
ylabel="",
cmap=ind_info["diff_cmap"],
clim=(ind_info["diff_min"], ind_info["diff_max"]),
width=600,
title=ds.attrs["long_name"]
+ " difference at "
# + str(temp)
+ "°C (Data: Werning et al. 2023)",
)
.hist()
)
temp_row = pn.Column(
pn.layout.Divider(),
pn.pane.Markdown(f"### Temperature: {str(temp)}°C"),
pn.Row(
abs_map,
diff_map,
score_map,
# styles={"background": "#f0f3f6"},
sizing_mode="stretch_width",
styles={
"background": "#f0f3f6",
"padding": "0px",
},
),
)
indepth_col.append(temp_row)
return indepth_col


def make_scenario_dboard(temp):
ds = get_scenario(temp)
scenario_col = pn.Column()
for var in ds.data_vars:
var_map = (
ds[var]
.hvplot(
x="Longitude",
y="Latitude",
xlabel="",
ylabel="",
width=600,
cmap="magma_r",
clim=(0, 6),
title=var
+ " risk score at "
+ str(temp)
+ "°C (Data: Werning et al. 2023)",
)
.hist()
)
scenario_col.append(var_map)
return scenario_col


slider_style = {
"background": "#f0f3f6",
"padding": "10px",
Expand Down Expand Up @@ -329,6 +459,26 @@ def make_score_map_test(ind, t):
# ind_unit = ind_info["unit"]
simple_map_test = pn.bind(make_score_map_test, ind=input_ticker, t=slider)
ind_desc_sidebar = pn.bind(get_ind_text, ind=input_ticker)
indepth_dboard = pn.bind(make_indepth_dboard, ind=input_ticker, temps=temp_box)
scenario_dboard = pn.bind(make_scenario_dboard, temp=slider)

tabs = pn.Tabs(
(
"Single Indicator Overview",
pn.Column(pn.Row(input_ticker, slider, ind_desc_sidebar), simple_map_test),
),
(
"Indicator In Depth",
pn.Column(pn.Row(input_ticker, temp_box, ind_desc_sidebar), indepth_dboard),
),
# (
# "Scenario",
# pn.Column(
# pn.Row(slider),
# ),
# ), # scenario_dboard
dynamic=True,
)

template = pn.template.FastListTemplate(
title="Climate Impact Maps",
Expand All @@ -338,9 +488,10 @@ def make_score_map_test(ind, t):
background_color="#f0f3f6",
)
template.sidebar.append(data_short)
template.sidebar.append(input_ticker)
template.sidebar.append(slider)
template.sidebar.append(ind_desc_sidebar)
# template.sidebar.append(tabs)
# template.sidebar.append(input_ticker)
# template.sidebar.append(slider)
# template.sidebar.append(ind_desc_sidebar)
template.sidebar.append(atd)

template.main.append(
Expand All @@ -357,7 +508,8 @@ def make_score_map_test(ind, t):
# ),
# input_ticker,
# slider,
simple_map_test,
tabs,
# simple_map_test,
)
)

Expand Down
Empty file removed panel_test.py
Empty file.

0 comments on commit 6933a8f

Please sign in to comment.