Unable to save as embed HTML for python Panel button parameterized action

Hi @imroy26 and welcome! :slight_smile:

What you can do with Panel is to save a snapshot of an app (its current state, what it looks like now basically) with the save method. With the embed parameter set to True, Panel will go through each one of your widgets, change their value, and save the output. You can explicitly define which states you want to the embed_states parameter that accepts a dict of widget states (Deploy and Export — Panel 0.12.4 documentation). If you don’t define which states you want to save, I believe Panel will try to be reasonable and not save all the states (with just a few widgets there could already be millions of states to save!), instead it will just save a sample.

I believe that what you experience here isn’t a bug. The button widget executes a function, that could be doing any kind of thing. It may for instance clear some in-memory data. This is the kind of thing that you probably wouldn’t like save to record.

For the simple case you posted, there’s a solution though that relies on Javascript links. A Button widget has a on_click method to attach a Python callback and a js_on_click method to attache a Javascript callback. In the example below I attach a simple Javascript callback that will, each time the button is clicked, increment the value of the number widget.

button = pn.widgets.Button(name='Click here!', button_type='primary')
number = pn.widgets.Spinner(name='Number', disabled=True)

button.js_on_click(args={'target': number}, code='target.value = target.value + 1')

app = pn.Column(
    button,
    number,
    '**Click the button** to trigger an update in the output.'
)
app.save('app.html')

You can read more about linking here: Links — Panel 0.12.4 documentation

It’s true that the docs could better describe the limitations of the save method.

1 Like