Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,60 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import pandas as pd
|
3 |
+
import panel as pn
|
4 |
+
import tropycal.tracks as tracks
|
5 |
|
6 |
+
pn.extension("perspective") # 1.
|
7 |
+
|
8 |
+
def update_name_options(year):
|
9 |
+
names = sorted(basin_df.query(f"year == {year}")["name"].unique())
|
10 |
+
name_select.options = names
|
11 |
+
if not name_select.value or name_select.value not in names:
|
12 |
+
name_select.value = names[0] if names else None
|
13 |
+
|
14 |
+
def update_storm_preview(name, year):
|
15 |
+
try:
|
16 |
+
storm_preview.loading = True
|
17 |
+
storm_id = track_dataset.get_storm_id((name, year))
|
18 |
+
axes = track_dataset.plot_storm(storm_id)
|
19 |
+
plt.close(axes.figure)
|
20 |
+
storm_preview.object = axes.figure
|
21 |
+
storm_table.object = track_dataset.get_storm(storm_id).to_dataframe() # 4.
|
22 |
+
finally:
|
23 |
+
storm_preview.loading = False
|
24 |
+
|
25 |
+
@pn.cache
|
26 |
+
def initialize_data():
|
27 |
+
track_dataset = tracks.TrackDataset(basin="both", source="hurdat")
|
28 |
+
basin_df = pd.DataFrame.from_dict(track_dataset.data, orient="index")
|
29 |
+
return track_dataset, basin_df
|
30 |
+
|
31 |
+
# Instantiate Widgets & Pane
|
32 |
+
year_slider = pn.widgets.IntSlider(
|
33 |
+
name="Year", value=1950, start=1950, end=2022, step=1
|
34 |
+
)
|
35 |
+
name_select = pn.widgets.Select(name="Name")
|
36 |
+
storm_preview = pn.pane.Matplotlib(plt.figure(), sizing_mode="stretch_both")
|
37 |
+
storm_table = pn.pane.Perspective(editable=False, sizing_mode="stretch_both") # 2.
|
38 |
+
|
39 |
+
# Layout the app
|
40 |
+
sidebar = pn.Column(year_slider, name_select)
|
41 |
+
main = pn.Tabs(("Preview", storm_preview), ("Table", storm_table)) # 3.
|
42 |
+
|
43 |
+
template = pn.template.FastListTemplate(
|
44 |
+
sidebar=sidebar,
|
45 |
+
main=main,
|
46 |
+
title="HURDAT Tracks Viewer"
|
47 |
+
)
|
48 |
+
|
49 |
+
# Initialize data
|
50 |
+
track_dataset, basin_df = initialize_data()
|
51 |
+
|
52 |
+
# Define Callbacks
|
53 |
+
pn.bind(update_name_options, year=year_slider.param.value_throttled, watch=True)
|
54 |
+
pn.bind(update_storm_preview, name=name_select, year=year_slider.param.value_throttled, watch=True)
|
55 |
+
|
56 |
+
# Pre-populate values
|
57 |
+
year_slider.param.trigger("value_throttled")
|
58 |
+
|
59 |
+
# Serve the app
|
60 |
+
template.show()
|