Last active
March 15, 2022 20:43
-
-
Save MarcSkovMadsen/8fe223a15eb1e1e155c8ba9148a89be3 to your computer and use it in GitHub Desktop.
Panel - Getting Started .py Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from matplotlib.figure import Figure | |
data_url = "https://cdn.jsdelivr.net/gh/holoviz/panel@master/examples/assets/occupancy.csv" | |
data = pd.read_csv(data_url, parse_dates=["date"]).set_index("date") | |
primary_color = "#0072B5" | |
secondary_color = "#94EA84" | |
def mpl_plot(avg, highlight): | |
fig = Figure(figsize=(10,5)) | |
ax = fig.add_subplot() | |
avg.plot(ax=ax, c=primary_color) | |
if len(highlight): | |
highlight.plot(style="o", ax=ax, c=secondary_color) | |
return fig | |
def find_outliers(variable="Temperature", window=20, sigma=10, view_fn=mpl_plot): | |
avg = data[variable].rolling(window=window).mean() | |
residual = data[variable] - avg | |
std = residual.rolling(window=window).std() | |
outliers = np.abs(residual) > std * sigma | |
return view_fn(avg, avg[outliers]) | |
# Panel | |
import panel as pn | |
pn.extension(sizing_mode="stretch_width", template="fast") | |
# Define labels and widgets | |
pn.pane.Markdown("Variable").servable(area="sidebar") | |
variable = pn.widgets.RadioBoxGroup( | |
name="Variable", value="Temperature", options=list(data.columns), margin=(-10, 5, 10, 10) | |
).servable(area="sidebar") | |
window = pn.widgets.IntSlider(name="Window", value=20, start=1, end=60).servable(area="sidebar") | |
# Make your functions interactive, i.e. react to changes in widget values | |
ifind_outliers = pn.bind(find_outliers, variable, window, 10) | |
# Layout the interactive functions | |
pn.panel(ifind_outliers, sizing_mode="scale_both").servable() | |
# Configure the template | |
pn.state.template.param.update( | |
site="Panel", title="Getting Started Example", | |
accent_base_color=primary_color, header_background=primary_color, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
panel>=0.12.6 | |
param>=1.12.0 | |
pandas | |
matplotlib |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Panel - Getting Started App
Introduction
This example shows the components of a Panel App in action: widgets, reactive functions, layouts, panes, and templates
Installation
Run the app
Please note the
--autoreload
provides hot reload when developing. Don't use it for deployment.The app is available at http://localhost:5006/panel_getting_started
Learn more
To get started with Panel check out the Panel Getting Started Guide. To get or provide help check out the community forum. For more inspiration check out my site awesome-panel.org.
Support us
If you want to support us please give us a ⭐ on Github (Panel, Awesome-Panel)
Promo Video
panel-getting-started-speedup.mp4