Param#

Download this notebook from GitHub (right-click to download).


import panel as pn
import param

pn.extension()

The panel.Param pane allows customizing the widgets, layout and style of the parameters of a param.Parametrized Class.

Parameters:#

The basic parameters are:

  • object (param.parameterized.Parameters): The param attribute of a param.Parameterized Class

  • parameters (List[str]): A list identifying the subset of parameters to include in the Pane.

  • widgets (Dict): A Dictionary specifying which parameters and widgets to use for a given parameter. You can also specify widget attributes.

The more advanced parameters which give you more control are:

  • default_layout (ClassSelector): A layout like Column, Row, etc. or a Custom GridBox.

  • display_threshold (float): Parameters with precedence below this value are not displayed.

  • expand (bool): Whether parameterized subobjects are expanded or collapsed on instantiation.

  • expand_button (bool): Whether to add buttons to expand and collapse sub-objects.

  • expand_layout (layout): Layout to expand sub-objects into.

  • name (str): The title of the pane.

  • show_labels (bool): Whether or not to show labels

  • show_name (bool): Whether or not to show the name of the Parameterized Class

For more layout and styling related parameters see the customization user guide.

For an alternative example of using panel.Param see the parameters user guide.


Model building#

Let’s build a model of a cycling Athlete and her PowerCurve.

The PowerCurve is a recording of her maximum power output in Watt per kg for fixed durations of time.

import datetime
import pandas as pd
import hvplot.pandas

DATE_BOUNDS = (datetime.date(1900, 1, 1), datetime.datetime.now().date())

class PowerCurve(param.Parameterized):
    ten_sec = param.Number(1079)
    ten_sec_date = param.Date(datetime.date(2018, 8, 21), bounds=DATE_BOUNDS)
    one_min = param.Number(684)
    one_min_date = param.Date(datetime.date(2017, 8, 31), bounds=DATE_BOUNDS)
    ten_min = param.Number(419)
    ten_min_date = param.Date(datetime.date(2017, 9, 22), bounds=DATE_BOUNDS)
    twenty_min = param.Number(398)
    twenty_min_date = param.Date(datetime.date(2017, 9, 22), bounds=DATE_BOUNDS)
    one_hour = param.Number(319)
    one_hour_date = param.Date(datetime.date(2017, 8, 6), bounds=DATE_BOUNDS)
    
    @param.depends("ten_sec", "one_min", "ten_min", "twenty_min", "one_hour")
    def plot(self):
        data = {
            "duration": [10 / 60, 1, 10, 20, 60],
            "power": [self.ten_sec, self.one_min, self.ten_min, self.twenty_min, self.one_hour],
        }
        dataframe = pd.DataFrame(data)
        line_plot = dataframe.hvplot.line(
            x="duration", y="power", line_color="#007BFF", line_width=3, responsive=True,
        )
        scatter_plot = dataframe.hvplot.scatter(
            x="duration", y="power", marker="o", size=6, color="#007BFF", responsive=True
        )
        fig = line_plot * scatter_plot
        gridstyle = {"grid_line_color": "black", "grid_line_width": 0.1}
        fig = fig.opts(
            min_height=400,
            toolbar=None,
            yticks=list(range(0, 1600, 200)),
            ylim=(0, 1500),
            gridstyle=gridstyle,
            show_grid=True,
        )
        return fig

class Athlete(param.Parameterized):
    name_ = param.String("P.A. Nelson")
    birthday = param.Date(datetime.date(1976, 9, 17), bounds=DATE_BOUNDS)
    weight = param.Number(default=82, bounds=(20,300))
    power_curve = param.ClassSelector(class_=PowerCurve, default=PowerCurve())
    
athlete = Athlete()