Reactive Parameters
Welcome to the world of reactive parameters in Panel! In this section, we'll delve into the powerful capabilities offered by Parameters in the HoloViz ecosystem.
Understanding Parameters
Panel and other projects in the HoloViz ecosystem build on the foundation provided by Param. Param offers a robust framework for adding validation, documentation, and interactivity to projects. Similar to projects like Pydantic in some aspects, but Param focuses on providing APIs that simplify the expression of complex dependencies, reactivity, and UI interactions.
In this section, we won't delve into the inner workings of Param but rather focus on understanding the fundamentals. By the end of this section, we will:
Understand the difference between the Parameter value and the Parameter object.
Know how to use Parameter objects, bound functions, and reactive expressions as proxies or references for their current value.
Exploring Parameters
Parameters serve as objects expressing the semantics of a value of an attribute on an object, encompassing not only Python types but also broader semantics.
Let's define a simple Parameterized
class with a value
Parameter:
Now, let's create an instance and examine its current value:
We can also inspect its Parameter instance:
The Utility of Parameters
Parameters act as references to underlying values, making them invaluable for enhancing UI interactivity.
Parameters imbue validation, documentation, and interactivity into Panel, with most Panel components built as Parameterized
classes with Parameters.
For instance, consider the value
Parameter of the Panel TextInput
widget:
To explore all parameters on a Parameterized
class, we inspect the .param
namespace:
Utilizing Parameters
Let's now delve into practical usage scenarios.
Accessing Parameter Values
Consider working with a TextInput
widget:
We can access its current value:
Additionally, we can access the Parameter
instance, acting as a proxy or reference for the value:
Setting Parameter Values
We can set it to a new value:
For setting multiple parameter values, use the .param.update
method:
Or use it as a context manager for temporary value setting:
Validation
Parameters perform validation, raising errors for invalid assignments:
Parameters as References
Parameters serve as proxies for underlying Parameter values, facilitating interactive declarations.
Consider a simple example using the TextInput
widget:
Observe how changes in TextInput
are automatically reflected in the Markdown output.
This also works when using it for other parameters, e.g. we can add a switch to toggle the visibility of some component:
Many parameters that accept a container such as a dictionary
or list
can also resolve references when they are nested, e.g. if we declare a styles
dictionary one of the values can be a widget:
Notice that we passed in the widget object directly instead of the .param.value
. This is possible because widgets are treated as a proxy of their value
parameter just like a Parameter
is treated as a proxy for current value.
Transforming Parameters
Transforming parameters allows for complex value processing pipelines, offering immense flexibility.
For instance, we can create a reactive format string, filling in values based on widget input:
Similarly, we can make a DataFrame reactive:
Please explore the potential by replacing 2
with an instance of an IntSlider
.
:::{dropdown} Solution
:::
Now let's get a little more complex and write a whole pipeline that selects the desired columns, samples a number of random rows, and then applies some custom styling highlighting the rows with the highest value:
As you can see, the Pandas code is identical to what you might have written if you were working with a regular DataFrame. However, now you can use widgets and even complex expressions as inputs.
Try replacing pn.pane.DataFrame
with pn.panel
to display an interactive component with widgets.
:::{dropdown} Solution
:::
Exercise: Scale the Font Size
Write a small app where you can scale the font-size
of a Markdown
pane with another widget, e.g. an IntSlider
. The font-size
can be set using the styles
parameter.
:::{hint}
The
styles
parameter only accepts dictionaries of strings butIntSlider
returnsint
types.The
font-size
value should be a string value in pixels, eg."15px"
is a validfont-size
.
:::
:::{dropdown} Solution 1: Reactive String Formatting
:::
:::{dropdown} Solution 2: Reactive Function
:::
::::{dropdown} Solution 3: Bound Function
:::{note}
pn.bind
is the predecessor of pn.rx
. We recommend using pn.rx
over pn.bind
as it's much more flexible and efficient. We include this example because you will find lots of examples in the Panel documentation and in the Panel community using pn.bind
.
:::
::::
Recommended Reading
To harness the full potential of Param and Reactive Parameters in Panel, we recommend studying the Param User Guide and the ReactiveExpr
reference guide.
Recap
Param serves as the backbone of Panel, providing a robust framework for adding validation, documentation, and interactivity. By grasping the fundamentals discussed here, you'll be well-equipped to leverage Panel's interactivity and reactivity effectively.
Now, we should be able to:
Clearly understand the distinction between Parameter values and Parameter objects.
Use Parameter objects, bound functions, and expressions as proxies or references for their current value.