How to force JSONEditor to redraw after the underlying dict is updated


Is it possible to force JSONEditor to redraw the moment its underlying dict is updated. For example, with the following code

class JSONEditorDemo(param.Parameterized):
    config = param.Dict(default={"data": 1}, doc="config")

    def panel(self):
        return pn.widgets.JSONEditor.from_param(self.param.config)

demo = JSONEditorDemo()

it will show a JSONEditor in the current cell.

If some other process modified the underlying dict, e.g. demo.config["data"]=0, is it possible to force the existing widget to update its content to show the updated value?

I would not say it is a pretty solution, but this will work.

The class will create a new editor every time the config changes. This is done inside a method that is wrapped in a pn.panel to be able to update every time config changes.

The demo.param.trigger("config") is needed because updating a value inside a dictionary does not update trigger param.depends. Alternative it could have been done with demo.config = {'data': 0}

import panel as pn
import param


class JSONEditorDemo(pn.viewable.Viewer):
    config = param.Dict(default={"data": 1}, doc="config")

    @param.depends("config", on_init=True)
    def _update_editor(self):
        self.editor = pn.widgets.JSONEditor.from_param(self.param.config)
        return self.editor

    def __panel__(self):
        return self._update_editor

demo = JSONEditorDemo()
demo.config["data"] = 0

1 Like

Thanks for the solution :grin:. It seems like Panel cannot tell if a dictionary is updated in the backend, so a manual trigger is necessary in this case.

1 Like