How do i make parameter readonly for user input but writeable in code?

I have a parametrized class and some of the parameters should never be changed by a user. They should be shown only. But the code should be able to update them.

How do I enable that?

Example

If the user clicks the below button the TypeError: Constant parameter 'mean_365d_30d_rolling' cannot be modified error is shown. How do I avoid that and allow the code to change the value?

image

import panel as pn
import param


def update(features):
    features.mean_365d_30d_rolling += 1


class SpreadFeatures(param.Parameterized):
    mean_365d_30d_rolling = param.Number(1.0, constant=True)
    std_365_30d_rolling = param.Number(1.0)
    action = param.Action(update)


features = SpreadFeatures()
pn.Param(features).servable()

I’d certainly like to make this easier somehow but currently you can do this:

def update(features):
    with param.edit_constant(features):
        features.mean_365d_30d_rolling += 1

It works. Thanks.

And I think the api for it is OK. Would not spend time on changing it. But maybe it should be documented somewhere?

1 Like

Hi,

I also got stumped by this. Would it not be nice with a setting for having a parameter writable from code but displayed as read only? Because this method gives a flickering effect as well.

I realize that my usecase is a little odd perhaps, but I want to display i.e. current position of a motor and have a field for setting target position beside it. In that case it would have been nice with something like pn.widgets.FloatDisplay or something. Have I missed something?

1 Like