{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import panel as pn\n", "import numpy as np\n", "import holoviews as hv\n", "\n", "pn.extension(sizing_mode=\"stretch_width\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example demonstrates how to dynamically add and remove plots from a layout (in this case a Column) using the ``pop`` and ``append`` methods (requires a live Python server):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider = pn.widgets.IntSlider(name='N', start=0, end=5)\n", "\n", "column = pn.Column('This column will be populated with N Plots', sizing_mode=\"stretch_width\")\n", "\n", "def set_plots(event):\n", " nplots = event.new-event.old\n", " for i in range(abs(nplots)):\n", " if nplots < 0:\n", " column.pop(len(column)-1)\n", " elif nplots > 0:\n", " column.append(hv.Scatter(np.random.randn(1000, 2)).opts(responsive=True, height=300))\n", " \n", " column[0]=f'This column will be populated with {event.new} Plots'\n", " \n", "slider.param.watch(set_plots, 'value')\n", "slider.value = 1\n", "\n", "pn.Row(pn.Column(slider, sizing_mode=\"fixed\", width=300), column, sizing_mode=\"stretch_width\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## App\n", "\n", "Lets wrap it into nice template that can be served via `panel serve dynamic_plot_layout.ipynb`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "description = \"This example demonstrates **how to dynamically add and remove plots** from a layout (in this case a Column) using the ``pop`` and ``append`` methods.\"\n", "pn.template.FastListTemplate(site=\"Panel\", title=\"Dynamic Plot Layout\", sidebar=[slider], main=[description, column]).servable();" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }