Skip to content

Commit

Permalink
Ensure location.unsync unsets query params (#3806)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Sep 5, 2022
1 parent ccf12a9 commit c2aeb39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 14 additions & 9 deletions panel/io/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,20 @@ def unsync(self, parameterized: param.Parameterized, parameters: Optional[List[s
ptype = type(parameterized)
raise ValueError(f"Cannot unsync {ptype} object since it "
"was never synced in the first place.")
synced = []
synced, unsynced = [], []
for p, params, watcher, on_error in self._synced:
if parameterized is p:
parameterized.param.unwatch(watcher)
if parameters is not None:
new_params = {p: q for p, q in params.items()
if p not in parameters}
new_watcher = parameterized.param.watch(watcher.fn, list(new_params))
synced.append((p, new_params, new_watcher, on_error))
else:
if parameterized is not p:
synced.append((p, params, watcher, on_error))
continue
parameterized.param.unwatch(watcher)
if parameters is None:
unsynced.extend(list(params.values()))
else:
unsynced.extend([q for p, q in params.items() if p in parameters])
new_params = {p: q for p, q in params.items()
if p not in parameters}
new_watcher = parameterized.param.watch(watcher.fn, list(new_params))
synced.append((p, new_params, new_watcher, on_error))
self._synced = synced
query = {k: v for k, v in self.query_params.items() if k not in unsynced}
self.search = '?' + urlparse.urlencode(query) if query else ''
10 changes: 10 additions & 0 deletions panel/tests/io/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ def test_location_sync_query_init(location):
assert location.search == "?integer=1&string=abc"
location.unsync(p)
assert location._synced == []
assert location.search == ""

def test_location_unsync(location):
p = SyncParameterized(integer=1, string='abc')
location.sync(p)
assert location.search == "?integer=1&string=abc"
location.unsync(p)
assert location.search == ""
location.update_query(integer=2, string='def')
assert p.integer == 1
assert p.string == "abc"
Expand All @@ -55,6 +57,7 @@ def test_location_unsync_partial(location):
location.sync(p)
assert location.search == "?integer=1&string=abc"
location.unsync(p, ['string'])
assert location.search == "?integer=1"
location.update_query(integer=2, string='def')
assert p.integer == 2
assert p.string == "abc"
Expand All @@ -75,6 +78,7 @@ def test_location_sync_query_init_rename(location):
assert location.search == "?int=1&str=abc"
location.unsync(p)
assert location._synced == []
assert location.search == ""

def test_location_sync_query(location):
p = SyncParameterized()
Expand All @@ -83,6 +87,7 @@ def test_location_sync_query(location):
assert location.search == "?integer=2"
location.unsync(p)
assert location._synced == []
assert location.search == ""

def test_location_sync_param_init(location):
p = SyncParameterized()
Expand All @@ -92,6 +97,7 @@ def test_location_sync_param_init(location):
assert p.string == "abc"
location.unsync(p)
assert location._synced == []
assert location.search == ""

def test_location_sync_on_error(location):
p = SyncParameterized(string='abc')
Expand All @@ -103,6 +109,7 @@ def on_error(change):
assert changes == [{'integer': 'a'}]
location.unsync(p)
assert location._synced == []
assert location.search == ""

def test_location_sync_param_init_partial(location):
p = SyncParameterized()
Expand All @@ -112,6 +119,7 @@ def test_location_sync_param_init_partial(location):
assert p.string is None
location.unsync(p)
assert location._synced == []
assert location.search == "?string=abc"

def test_location_sync_param_init_rename(location):
p = SyncParameterized()
Expand All @@ -121,6 +129,7 @@ def test_location_sync_param_init_rename(location):
assert p.string == 'abc'
location.unsync(p)
assert location._synced == []
assert location.search == ""

def test_location_sync_param_update(location):
p = SyncParameterized()
Expand All @@ -130,3 +139,4 @@ def test_location_sync_param_update(location):
assert p.string == "abc"
location.unsync(p)
assert location._synced == []
assert location.search == ""

0 comments on commit c2aeb39

Please sign in to comment.