Add context manager to temporarily set parameter(s)
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.
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!)
Would there be any special handling for read-only/constant parameters? E.g. should it operate the same way or warn?
Frankly I thought we already had this. :-)
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.
I like not having to import it though :/