param icon indicating copy to clipboard operation
param copied to clipboard

Add context manager to temporarily set parameter(s)

Open philippjfr opened this issue 2 years ago • 5 comments

In my code I very frequently find myself wanting to temporarily set some parameter and then reset it to its original value. For this purpose it would be super helpful to have a context manager that handles this for me without me having to take care of storing the old value, adding error handling and then resetting the value.

This is particularly useful in cases where you want to configure something while some longer running task is occurring. Prime examples include setting loading parameter in Panel:

with pane.param.set(loading=True):
    ... # Do some expensive computation
    pane.object = new_pane

or temporarily overriding the styling of some component:

with pane.param.set(styles={'border': '1px solid yellow'}):
    ...

Additionally it can be useful when temporarily overriding some configuration option(s):

with pn.config.param.set(resource_mode='inline'):
    ...

with pn.config.param.set(design='bootstrap'):
    ...

I personally like .param.set as the API as we are temporarily setting parameter values as opposed to .param.update which permanently updates the values. I am open to other suggestions though.

philippjfr avatar Feb 10 '23 09:02 philippjfr

I think this is useful and don't see why it wouldn't be included in param. The only argument against that I can think of is that we want to minimize the API surface on the param namespace object (though I think this would be more useful than many things currently on there!)

jlstevens avatar Feb 10 '23 09:02 jlstevens

Would there be any special handling for read-only/constant parameters? E.g. should it operate the same way or warn?

jlstevens avatar Feb 10 '23 09:02 jlstevens

Frankly I thought we already had this. :-)

jbednar avatar Feb 10 '23 13:02 jbednar

Ah, what I was remembering was with param.edit_constant(pane.param.loading):. So maybe this should be a standalone function with param.edit(pane.param.loading) to avoid clashes with parameters named set.

jbednar avatar Feb 10 '23 13:02 jbednar

I like not having to import it though :/

philippjfr avatar Feb 10 '23 13:02 philippjfr