Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions doc/how_to/state/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,44 @@ Lets try to serve it as an app
pn.Param(settings).servable()
```

### Sync a list of Parameterized objects

URL query parameters can only contain serializable values. To synchronize a
list of `Parameterized` objects, define how the list parameter converts its
items to and from plain Python values:

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


class CurveList(param.List):
class_ = Curve

@classmethod
def serialize(cls, value):
return [{"product": curve.product} for curve in value]

@classmethod
def deserialize(cls, value):
return [Curve(**curve) for curve in value]


class CurveCollection(param.Parameterized):
curves = CurveList(default=[])


collection = CurveCollection(curves=[Curve(product="A")])
pn.state.location.sync(collection, ["curves"])

pn.Param(collection).servable()
```

Changing `collection.curves` now updates the URL with a JSON representation,
and loading that URL reconstructs the `Curve` objects. Keep synchronized data
small and non-sensitive because query strings have practical length limits and
are commonly retained in browser history and server logs.

<video muted controls loop poster="../../_static/images/location_example_app.png" style="max-height: 400px; max-width: 100%;">
<source src="https://assets.holoviz.org/panel/how_to/state/sync_url.mp4" type="video/mp4">
Your browser does not support the video tag.
Expand Down
Loading