File size: 8,306 Bytes
b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 4e078cc b1f8461 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
{
"cells": [
{
"cell_type": "markdown",
"id": "040c997b",
"metadata": {},
"source": [
"# The Easiest Way to Create an Interactive Dashboard in Python\n",
"\n",
"This notebook is an updated version of the original notebook supporting the blog post\n",
"\n",
"**[The Easiest Way to Create an Interactive Dashboard in Python](https://towardsdatascience.com/the-easiest-way-to-create-an-interactive-dashboard-in-python-77440f2511d1)**. Turn Pandas pipelines into a\n",
"dashboard using [`param.rx`](https://param.holoviz.org/user_guide/Reactive_Expressions.html) and [panel.ReactiveExpr](https://panel.holoviz.org/reference/panes/ReactiveExpr.html).\n",
"\n",
"by *Sophia Yang* and *Marc Skov Madsen*.\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"id": "abf60b58",
"metadata": {},
"source": [
"## Import and configure packages\n",
"\n",
"Please note that in **Colab** you will need to `!pip install panel hvplot`. In VS Code notebooks you will need to install `!pip install panel hvplot jupyter_bokeh`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5cb553bf",
"metadata": {},
"outputs": [],
"source": [
"# !pip install panel==1.3.6 hvplot==0.9.1 # colab\n",
"# !pip install panel==1.3.6 hvplot==0.9.1 jupyter_bokeh==3.0.7 # vscode"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f1112af",
"metadata": {},
"outputs": [],
"source": [
"import param\n",
"import panel as pn\n",
"\n",
"pn.extension('tabulator', sizing_mode=\"stretch_width\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5adb11a6",
"metadata": {},
"outputs": [],
"source": [
"import hvplot.pandas\n",
"import holoviews as hv\n",
"\n",
"hv.extension('bokeh')"
]
},
{
"cell_type": "markdown",
"id": "ef4ccd06",
"metadata": {},
"source": [
"## Define Color Palette"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce8c5f82",
"metadata": {},
"outputs": [],
"source": [
"PALETTE = [\"#ff6f69\", \"#ffcc5c\", \"#88d8b0\", ]\n",
"pn.Row(\n",
" pn.layout.HSpacer(height=50, styles={\"background\": PALETTE[0]}),\n",
" pn.layout.HSpacer(height=50, styles={\"background\": PALETTE[1]}),\n",
" pn.layout.HSpacer(height=50, styles={\"background\": PALETTE[2]}),\n",
")"
]
},
{
"cell_type": "markdown",
"id": "7698a74c",
"metadata": {},
"source": [
"## Load Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09d60a44",
"metadata": {},
"outputs": [],
"source": [
"from bokeh.sampledata.autompg import autompg_clean as df\n",
"df.head(3)"
]
},
{
"cell_type": "markdown",
"id": "1bfed521",
"metadata": {},
"source": [
"## Define DataFrame Pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a42b6e5",
"metadata": {},
"outputs": [],
"source": [
"def get_data(df, cylinders=4, mfr=['ford','chevrolet'], yaxis=\"hp\"):\n",
" return (\n",
" df[\n",
" (df.cyl == cylinders) & \n",
" (df.mfr.isin(mfr))\n",
" ]\n",
" .groupby(['origin', 'mpg'])[yaxis].mean()\n",
" .to_frame()\n",
" .reset_index()\n",
" .sort_values(by='mpg')\n",
" )\n",
"\n",
"get_data(df).head(3)"
]
},
{
"cell_type": "markdown",
"id": "f928f0ba",
"metadata": {},
"source": [
"## Make DataFrame Pipeline Interactive"
]
},
{
"cell_type": "markdown",
"id": "835b20da",
"metadata": {},
"source": [
"Define [Panel widgets](https://panel.holoviz.org/reference/index.html#widgets)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "956fa985",
"metadata": {},
"outputs": [],
"source": [
"cylinders = pn.widgets.IntSlider(name='Cylinders', start=4, end=8, step=2)\n",
"mfr = pn.widgets.ToggleGroup(\n",
" name='MFR',\n",
" options=['ford', 'chevrolet', 'honda', 'toyota', 'audi'], \n",
" value=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],\n",
" button_type='primary', button_style=\"outline\")\n",
"yaxis = pn.widgets.RadioButtonGroup(\n",
" name='Y axis', \n",
" options=['hp', 'weight'],\n",
" button_type='primary', button_style=\"outline\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "743aa619",
"metadata": {},
"source": [
"Combine pipeline and widgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8883956",
"metadata": {},
"outputs": [],
"source": [
"ipipeline=param.rx(get_data)(df, cylinders, mfr, yaxis)\n",
"\n",
"ipipeline.head()"
]
},
{
"cell_type": "markdown",
"id": "bff77abf",
"metadata": {},
"source": [
"## Pipe to Tabulator"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d262cb39",
"metadata": {},
"outputs": [],
"source": [
"itable = pn.widgets.Tabulator(ipipeline, pagination='remote', page_size=10)\n",
"itable"
]
},
{
"cell_type": "markdown",
"id": "b21344a1",
"metadata": {},
"source": [
"Check out the [Tabulator Reference Guide](https://panel.holoviz.org/reference/widgets/Tabulator.html) for more inspiration."
]
},
{
"cell_type": "markdown",
"id": "246398c8",
"metadata": {},
"source": [
"## Pipe to hvPlot and HoloViews"
]
},
{
"cell_type": "markdown",
"id": "1fcc805e",
"metadata": {},
"source": [
"First we will create the interactive plot with [hvPlot](https://hvplot.holoviz.org/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24423cca",
"metadata": {},
"outputs": [],
"source": [
"ihvplot = ipipeline.hvplot(x='mpg', y=yaxis, by='origin', color=PALETTE, line_width=6, height=400, responsive=True)\n",
"ihvplot"
]
},
{
"cell_type": "markdown",
"id": "a2b5271e",
"metadata": {},
"source": [
"The we will put it in a HoloViews pane"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b958a6e1",
"metadata": {},
"outputs": [],
"source": [
"iplot = pn.pane.HoloViews(ihvplot)\n",
"iplot"
]
},
{
"cell_type": "markdown",
"id": "0c943ca4",
"metadata": {},
"source": [
"Check out the [HoloViews Reference Guide](https://panel.holoviz.org/reference/panes/HoloViews.html) for more inspiration"
]
},
{
"cell_type": "markdown",
"id": "345db224",
"metadata": {},
"source": [
"## Layout using Template\n",
"\n",
"Here we use the [FastListTemplate](https://panel.holoviz.org/reference/templates/FastListTemplate.html#templates-gallery-fastlisttemplate)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d224e0e7",
"metadata": {},
"outputs": [],
"source": [
"template = pn.template.FastListTemplate(\n",
" title='The easiest way to create a dashboard', \n",
" sidebar=[cylinders, 'Manufacturers', mfr, 'Y axis' , yaxis],\n",
" main=[iplot, itable],\n",
" accent_base_color=\"#88d8b0\",\n",
" header_background=\"#88d8b0\",\n",
" # main_layout=None, # Use this if you want a gray sidebar and white main area\n",
")\n",
"template.servable(); # Add semicolon because templates don't render well in a notebook"
]
},
{
"cell_type": "markdown",
"id": "8831435d",
"metadata": {},
"source": [
"To *serve the notebook* run `panel serve the_easiest_way_to_create_dashboard.ipynb`."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|