<jupyter_start><jupyter_text>The Easiest Way to Create an Interactive Dashboard in PythonThis notebook supports the blog post**The Easiest Way to Create an Interactive Dashboard in Python. Turn Pandas pipelines into adashboard using hvplot `.interactive`**by *<NAME>* and *<NAME>*.![Data App](assets/easy-dataframe-dashboards.gif)Source: https://github.com/sophiamyang/hvplot_interactive  Import and configure packagesPlease note that in **Colab** you will need to `!pip install panel hvplot` and add `comms='colab'` to `pn.extension`.<jupyter_code># !pip install panel==0.12.6 hvplot==0.7.3
import panel as pn

pn.extension('tabulator', sizing_mode="stretch_width")
# pn.extension('tabulator', sizing_mode="stretch_width", comms="colab")
import hvplot.pandas
import holoviews as hv

hv.extension('bokeh')<jupyter_output><empty_output><jupyter_text>Define function to determine environment<jupyter_code>def environment():
    try:
        get_ipython()
        return "notebook"
    except:
        return "server"
environment()<jupyter_output><empty_output><jupyter_text>Define Color Palette<jupyter_code>PALETTE = ["#ff6f69", "#ffcc5c", "#88d8b0", ]
pn.Row(
    pn.layout.HSpacer(height=50, background=PALETTE[0]),
    pn.layout.HSpacer(height=50, background=PALETTE[1]),
    pn.layout.HSpacer(height=50, background=PALETTE[2]),
)<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>from bokeh.sampledata.autompg import autompg_clean as df
df.head()<jupyter_output><empty_output><jupyter_text>Define DataFrame Pipeline<jupyter_code>(
    df[
        (df.cyl == 4) & 
        (df.mfr.isin(['ford','chevrolet']))
    ]
    .groupby(['origin', 'cyl', 'mfr', 'yr'])['hp'].mean()
    .to_frame()
    .reset_index()
    .sort_values(by='yr')
).head(1)<jupyter_output><empty_output><jupyter_text>Make DataFrame Pipeline Interactive<jupyter_code>idf = df.interactive()<jupyter_output><empty_output><jupyter_text>Define [Panel widgets](https://panel.holoviz.org/reference/index.htmlwidgets)<jupyter_code>cylinders = pn.widgets.IntSlider(name='Cylinders', start=4, end=8, step=2)
mfr = pn.widgets.ToggleGroup(
    name='MFR',
    options=['ford', 'chevrolet', 'honda', 'toyota', 'audi'], 
    value=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],
    button_type='success')
yaxis = pn.widgets.RadioButtonGroup(
    name='Y axis', 
    options=['hp', 'weight'],
    button_type='success'
)<jupyter_output><empty_output><jupyter_text>Combine pipeline and widgets<jupyter_code>ipipeline = (
    idf[
        (idf.cyl == cylinders) & 
        (idf.mfr.isin(mfr))
    ]
    .groupby(['origin', 'mpg'])[yaxis].mean()
    .to_frame()
    .reset_index()
    .sort_values(by='mpg')  
    .reset_index(drop=True)
)
ipipeline.head()<jupyter_output><empty_output><jupyter_text>Pipe to Table<jupyter_code>if environment()=="server":
   theme="fast"
else:
   theme="simple"
itable = ipipeline.pipe(pn.widgets.Tabulator, pagination='remote', page_size=10, theme=theme)
itable<jupyter_output><empty_output><jupyter_text>Check out the [Tabulator Reference Guide](https://panel.holoviz.org/reference/widgets/Tabulator.html) for more inspiration.  Pipe to hvplot<jupyter_code>ihvplot = ipipeline.hvplot(x='mpg', y=yaxis, by='origin', color=PALETTE, line_width=6, height=400)
ihvplot<jupyter_output><empty_output><jupyter_text>Layout using TemplateHere we use the [FastListTemplate](https://panel.holoviz.org/reference/templates/FastListTemplate.htmltemplates-gallery-fastlisttemplate).<jupyter_code>template = pn.template.FastListTemplate(
    title='Interactive DataFrame Dashboards with hvplot .interactive', 
    sidebar=[cylinders, 'Manufacturers', mfr, 'Y axis' , yaxis],
    main=[ihvplot.panel(), itable.panel()],
    accent_base_color="#88d8b0",
    header_background="#88d8b0",
)
# template.show()
template.servable();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1515860634425377, 'fraction_numerical': 0.011180447217888715, 'mean_word_length': 6.1638733705772815, 'pattern_counts': {'":': 1, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '298126', 'license': 'MIT', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1379, 'n_words': 226, 'path': 'hvplot_interactive.ipynb', 'repo_name': 'felipsoarez/hvplot_interactive', 'size': '3861'}