Rx Marks the Spot: A Prescription for Reactive Python

In this blog post, I’ll walkthrough:

  1. how to use rx
  2. converting pn.bind into rx expressions
  3. example use cases

And along the way, highlight pitfalls when using rx.

TLDR:

A lambda is to a Python function as rx is to pn.bind.

import panel as pn
pn.extension()

def update_button_name(subject):
    return f"Add {subject}"

select = pn.widgets.Select(value="Biology", options=["Biology", "Chemistry", "Physics"])
button = pn.widgets.Button(
    name=pn.bind(update_button_name, subject=select.param.value),
    button_type="primary"
)

pn.Row(select, button).servable()

After:

import panel as pn
pn.extension()

select = pn.widgets.Select(value="Biology", options=["Biology", "Chemistry", "Physics"])
button = pn.widgets.Button(
    name="Add " + select.param.value.rx(),
    button_type="primary"
)

pn.Row(select, button).servable()
1 Like