{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The `ReactTemplate` is a simple extension of the basic template that uses the [react-grid-layout](https://react-grid-layout.github.io/react-grid-layout/examples/0-showcase.html) to provide a drag-and-drop interface for resizing and rearranging components on a grid. It is a grid-like variant where the `main` area acts like a grid-like container, unlike the list-like templates such as `VanillaTemplate` and `MaterialTemplate`.\n", "\n", "## Basic Templates\n", "\n", "For a large variety of use cases we do not need complete control over the exact layout of each individual component on the page, as could be achieved with a [custom template](../../user_guide/Templates.ipynb), we just want to achieve a more polished look and feel. For these cases Panel ships with a number of default templates, which are defined by declaring four main content areas on the page, which can be populated as desired:\n", "\n", "* **`header`**: The header area of the HTML page\n", "* **`sidebar`**: A collapsible sidebar\n", "* **`main`**: The main area of the application\n", "* **`modal`**: A modal area which can be opened and closed from Python\n", "\n", "These four areas behave very similarly to other Panel layout components and have list-like semantics. The `ReactTemplate` in particular however is an exception to this rule as the `main` area behaves like panel `GridSpec` object. Unlike regular layout components however, the contents of the areas is fixed once rendered. If you need a dynamic layout you should therefore insert a regular Panel layout component (e.g. a `Column` or `Row`) and modify it in place once added to one of the content areas. \n", "\n", "Templates can allow for us to quickly and easily create web apps for displaying our data. Panel comes with a default Template, and includes multiple Templates that extend the default which add some customization for a better display.\n", "\n", "#### Parameters:\n", "\n", "In addition to the four different areas we can populate the `ReactTemplate` declares a few variables to configure the layout:\n", "\n", "* **`cols`** (dict): Number of columns in the grid for different display sizes (`default={'lg': 12, 'md': 10, 'sm': 6, 'xs': 4, 'xxs': 2}`)\n", "* **`breakpoints`** (dict): Sizes in pixels for various layouts (`default={'lg': 1200, 'md': 996, 'sm': 768, 'xs': 480, 'xxs': 0}`)\n", "* **`row_height`** (int, default=150): Height per row in the grid\n", "* **`dimensions`** (dict): Minimum/Maximum sizes of cells in grid units (`default={'minW': 0, 'maxW': 'Infinity', 'minH': 0, 'maxH': 'Infinity'}`)\n", "* **`prevent_collision`** (bool, default=False): Prevent collisions between grid items.\n", "* **`save_layout`** {bool, default=False): Save layout changes to localStorage.\n", "\n", "These parameters control the responsive resizing in different layouts. The `ReactTemplate` also exposes the same parameters as other templates:\n", "\n", "* **`busy_indicator`** (BooleanIndicator): Visual indicator of application busy state.\n", "* **`collapsed_sidebar`** (str, `default=False`): Whether the sidebar (if present) is initially collapsed.\n", "* **`header_background`** (str): Optional header background color override.\n", "* **`header_color`** (str): Optional header text color override.\n", "* **`initial_sidebar_state`** (str, `default='expanded'`): Whether the side (if present) is expanded or collapsed initially.\n", "* **`logo`** (str): URI of logo to add to the header (if local file, logo is base64 encoded as URI).\n", "* **`site`** (str): Name of the site. Will be shown in the header. Default is '', i.e. not shown.\n", "* **`site_url`** (str): Url of the site and logo. Default is \"/\".\n", "* **`title`** (str): A title to show in the header.\n", "* **`theme`** (Theme): A Theme class (available in `panel.template.theme`)\n", "* **`sidebar_width`** (int): The width of the sidebar in pixels. Default is 350.\n", "\n", "________" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case we are using the `ReactTemplate`, built on [react-grid-layout](https://github.com/STRML/react-grid-layout), which provides a responsive, resizable, draggable grid layout. Here is an example of how you can set up a display using this template:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "import hvplot.pandas\n", "import numpy as np\n", "import panel as pn\n", "import pandas as pd\n", "\n", "xs = np.linspace(0, np.pi)\n", "\n", "freq = pn.widgets.FloatSlider(name=\"Frequency\", start=0, end=10, value=2)\n", "phase = pn.widgets.FloatSlider(name=\"Phase\", start=0, end=np.pi)\n", "\n", "def sine(freq, phase):\n", " return pd.DataFrame(dict(y=np.sin(xs*freq+phase)), index=xs)\n", "\n", "def cosine(freq, phase):\n", " return pd.DataFrame(dict(y=np.cos(xs*freq+phase)), index=xs)\n", "\n", "dfi_sine = hvplot.bind(sine, freq, phase).interactive()\n", "dfi_cosine = hvplot.bind(cosine, freq, phase).interactive()\n", "\n", "plot_opts = dict(responsive=True, min_height=400)\n", "\n", "# Instantiate the template with widgets displayed in the sidebar\n", "template = pn.template.ReactTemplate(\n", " title='ReactTemplate',\n", " sidebar=[freq, phase],\n", ")\n", "# Populate the main area with plots, to demonstrate the grid-like API\n", "template.main[:3, :6] = pn.Card(dfi_sine.hvplot(**plot_opts).output(), title='Sine')\n", "template.main[:3, 6:] = pn.Card(dfi_cosine.hvplot(**plot_opts).output(), title='Cosine')\n", "template.servable();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the `row_height=150` this will result in the two `Card` objects filling 4 rows each totalling 600 pixels and each taking up 6 columns, which resize responsively to fill the screen and reflow when working on a smaller screen. When hovering of the top-left corner of each card a draggable handle will allow dragging the components around while a resize handle will show up at the bottom-right corner." ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Each built-in template comes with a *light* (default) and *dark* theme. The theme can be set when instantiating the template with the `theme` parameter, or [globally](../../how_to/styling/themes.md).\n", "\n", "

ReactTemplate with DefaultTheme

\n", "\n", "
\n", "

ReactTemplate with DarkTheme

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":::{tip}\n", "Built-in templates don't render necessarily well in a notebook as their styling can badly interact with the notebook built-in styling. You can disable rendering the output of a cell using `;`, as done above. For development purposes, the app can be quickly rendered in another tab by replacing `.servable()` with `.show()`. Alternatively, the [JupyterLab Preview](../../how_to/notebook/jupyterlabpreview.md) can be used to display objects marked with `.servable()` in a new JupyterLab tab, circumventing any potential styling issue.\n", ":::" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }