import numpy as np
import pandas as pd
import holoviews as hv
import hvplot.pandas
import panel as pn
hv.extension('bokeh')
pn.config.sizing_mode="stretch_width"
def make_ts_data(n_timesteps):
data = pd.DataFrame(
{
"a": np.random.normal(size=(n_timesteps,)),
"b": np.random.normal(size=(n_timesteps,)),
"c": np.random.normal(size=(n_timesteps,)),
},
index=pd.Index(np.arange(n_timesteps), name="time", )
)
return data
ts_data = make_ts_data(1000)
ts_data.head()
a | b | c | |
---|---|---|---|
time | |||
0 | 0.984334 | 0.493209 | 0.170402 |
1 | -0.610320 | 0.251799 | 0.145121 |
2 | 0.556030 | -1.805431 | 0.464559 |
3 | -0.686167 | 0.465423 | -0.042672 |
4 | 0.212212 | -2.609381 | -0.011463 |
HEIGHT=300
plot_a = ts_data.hvplot(y="a", responsive=True, height=HEIGHT)
plot_b = ts_data.hvplot(y="b", responsive=True, height=HEIGHT)
plot_c = ts_data.hvplot(y="c", responsive=True, height=HEIGHT)
plot_a + plot_b + plot_c
def get_image(frame):
return hv.Image(np.random.normal(size=(100, 100))).opts(height=HEIGHT, responsive=True)
get_image(100)
c:\repos\private\panel-extensions-template\.venv\lib\site-packages\holoviews\plotting\util.py:685: MatplotlibDeprecationWarning: The global colormaps dictionary is no longer considered public API. [cmap for cmap in cm.cmap_d if not
def get_vline(frame):
return hv.VLine(frame).opts(color="red")
get_vline(0.5)
app_bar = pn.Row(
pn.pane.Markdown("## TimeSeries Image Analysis - POC", style={"color": "white"}, width=500, sizing_mode="fixed", margin=(10,5,10,15)),
pn.Spacer(),
pn.pane.PNG("http://holoviews.org/_static/logo.png", height=50, sizing_mode="fixed", align="center"),
pn.pane.PNG("https://panel.holoviz.org/_static/logo_horizontal.png", height=50, sizing_mode="fixed", align="center"),
background="black",
)
app_bar
frame_slider = pn.widgets.IntSlider(name="Time", value=25, start=0, end=999)
@pn.depends(frame=frame_slider)
def image(frame):
return get_image(frame)
@pn.depends(frame=frame_slider)
def vline(frame):
return get_vline(frame)
vline_dmap = hv.DynamicMap(vline)
img_dmap = hv.DynamicMap(image)
plots = ((plot_a + plot_b + plot_c) * vline_dmap).cols(1)
app = pn.Column(
app_bar,
pn.Spacer(height=10),
frame_slider,
pn.Row(
plots,
pn.Column(
pn.Spacer(height=20),
img_dmap,
),
),
)
app.servable()
c:\repos\private\panel-extensions-template\.venv\lib\site-packages\holoviews\plotting\util.py:685: MatplotlibDeprecationWarning: The global colormaps dictionary is no longer considered public API. [cmap for cmap in cm.cmap_d if not
You are now ready to serve you app to your users via panel serve NAME_OF_NOTEBOOK.ipynb
The plots = ((plot_a + plot_b + plot_c) * vline_dmap).cols(1)
line is essential for making the app fast and responsive. Initially the plot_a + plot_b + plot_c
where regenerated everytime the frame_slider.value
was changed. That made the app slower because it had to recalculate and retransfer much more data.