Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Location sync serialization #1792

Merged
merged 5 commits into from
Nov 17, 2020
Merged
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
37 changes: 24 additions & 13 deletions panel/io/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Defines the Location widget which allows changing the href of the window.
"""

import json
import urllib.parse as urlparse

import param
Expand Down Expand Up @@ -89,33 +90,32 @@ def _update_synced(self, event=None):
if k not in mapping:
continue
pname = mapping[k]
if isinstance(v, str) and v.startswith('"') and v.endswith('"'):
v = v[1:-1]
else:
try:
v = p.param[pname].deserialize(v)
except Exception:
pass
try:
v = p.param[pname].deserialize(v)
except Exception:
pass
mapped[pname] = v
p.param.set_param(**mapped)

def _update_query(self, *events, query=None):
if self._syncing:
return
query = query or {}
serialized = query or {}
for e in events:
matches = [ps for o, ps, _ in self._synced if o in (e.cls, e.obj)]
if not matches:
continue
owner = e.cls if e.obj is None else e.obj
try:
val = owner.param.serialize_value(e.name)
val = owner.param[e.name].serialize(e.new)
except Exception:
val = e.new
query[matches[0][e.name]] = val
if not isinstance(val, str):
val = json.dumps(val)
serialized[matches[0][e.name]] = val
self._syncing = True
try:
self.update_query(**{k: v for k, v in query.items() if v is not None})
self.update_query(**{k: v for k, v in serialized.items() if v is not None})
finally:
self._syncing = False

Expand Down Expand Up @@ -150,8 +150,19 @@ def sync(self, parameterized, parameters=None):
watcher = parameterized.param.watch(self._update_query, list(parameters))
self._synced.append((parameterized, parameters, watcher))
self._update_synced()
self._update_query(query={v: getattr(parameterized, k)
for k, v in parameters.items()})
query = {}
for p, name in parameters.items():
v = getattr(parameterized, p)
if v is None:
continue
try:
parameterized.param[p].serialize(v)
except Exception:
pass
if not isinstance(v, str):
v = json.dumps(v)
query[name] = v
self._update_query(query=query)

def unsync(self, parameterized, parameters=None):
"""
Expand Down