holoviz/panel

Describe how to sync the url/ location to a list of Parameterized objects

Open

#5,753 opened on Oct 27, 2023

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Python (615 forks)auto 404
good first issuetype: docstype: enhancement

Repository metrics

Stars
 (5,722 stars)
PR merge metrics
 (PR metrics pending)

Description

I've just contributed https://github.com/holoviz/panel/pull/5752. I was looking at this because I want to sync with a list of Parameterized classes.

If I can figure out how to get something like the below example working, I would like to contribute it to the docs.


import param
import panel as pn

class Curve(param.Parameterized):
    product = param.String()

class CurveCollection(param.Parameterized):
    value = param.List(class_=Curve)

    add = param.Event()
    index = param.Integer()

    @param.depends("add", watch=True)
    def _add_curve(self):
        self.value = self.value + [Curve(product=str(self.index))]
        self.index+=1

collection=CurveCollection()
pn.state.location.sync(collection, parameters=["value"])

pn.Column(
    collection.param.add, collection.param.value
).servable()

Currently when you click the add button you get

  File "/home/jovyan/repos/mt-quant-frontend/.venv/lib/python3.10/site-packages/panel/io/location.py", line 194, in _update_query
    val = json.dumps(val)
  File "/opt/conda/lib/python3.10/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/opt/conda/lib/python3.10/json/encoder.py", line 199, in encode

    chunks = self.iterencode(o, _one_shot=True)
  File "/opt/conda/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/opt/conda/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Curve is not JSON serializable

Contributor guide