Skip to content

Commit

Permalink
Sync WidgetFilter with url (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored Oct 31, 2023
1 parent 9582c8f commit 592e75b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lumen/filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ class Filter(MultiTypeComponent):

def __init__(self, **params):
super().__init__(**params)
if state.config and state.config.sync_with_url and self.sync_with_url and pn.state.location:
if self._sync_with_url:
pn.state.location.sync(self, {'value': self.field}, on_error=self._url_sync_error)

@property
def _sync_with_url(self) -> bool:
return bool(state.config and state.config.sync_with_url and self.sync_with_url and pn.state.location)

@classproperty
def _required_keys(cls) -> List[str | Tuple[str, ...]]: # type: ignore
return ['field'] if cls._requires_field else []
Expand Down Expand Up @@ -313,8 +317,11 @@ def __init__(self, **params):
self.widget.name = self.label
self.widget.visible = self.visible
self.widget.disabled = self.disabled
val = self.value
self.widget.link(self, bidirectional=True, value='value', visible='visible', disabled='disabled')
if self.default is not None:
if val is not None:
self.widget.value = val
elif self.default is not None:
self.widget.value = self.default

@classmethod
Expand Down
18 changes: 18 additions & 0 deletions lumen/tests/sample_dashboard/sync_query_filters_default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
config:
sync_with_url: true
sources:
test:
type: 'file'
files: ['../sources/test.csv']
targets:
- title: "Test"
source: test
filters:
- type: widget
field: A
- type: widget
field: C
default: ['foo1']
views:
- table: test
type: test
31 changes: 31 additions & 0 deletions lumen/tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,37 @@ def test_dashboard_with_url_sync_filters(set_root, document):
pn.state.location.search = '?A=%5B0.3%2C+0.8%5D&C=%5B%22foo1%22%2C+%22foo2%22%2C+%22foo3%22%5D'
assert f2.value == ['foo1', 'foo2', 'foo3']

def test_dashboard_with_url_sync_filters_with_default(set_root, document):
root = pathlib.Path(__file__).parent / 'sample_dashboard'
set_root(str(root))
dashboard = Dashboard(str(root / 'sync_query_filters_default.yaml'))
dashboard._render_dashboard()
layout = dashboard.layouts[0]
f1, f2 = list(layout._pipelines.values())[0].filters
f1.value = (0.1, 0.7)
assert pn.state.location.search == '?C=%5B%27foo1%27%5D&A=%5B0.1%2C+0.7%5D'
pn.state.location.search = '?A=%5B0.3%2C+0.8%5D'
assert f1.value == (0.3, 0.8)
assert f2.value == ['foo1']
assert f1.widget.value == (0.3, 0.8)
assert f2.widget.value == ['foo1']
f2.value = ['foo1', 'foo2']
assert pn.state.location.search == '?A=%5B0.3%2C+0.8%5D&C=%5B%22foo1%22%2C+%22foo2%22%5D'
pn.state.location.search = '?A=%5B0.3%2C+0.8%5D&C=%5B%22foo1%22%2C+%22foo2%22%2C+%22foo3%22%5D'
assert f2.value == ['foo1', 'foo2', 'foo3']
assert f2.widget.value == ['foo1', 'foo2', 'foo3']

def test_dashboard_with_url_sync_filters_with_overwritten_default(set_root, document):
root = pathlib.Path(__file__).parent / 'sample_dashboard'
set_root(str(root))
dashboard = Dashboard(str(root / 'sync_query_filters_default.yaml'))
dashboard._render_dashboard()
layout = dashboard.layouts[0]
f1, f2 = list(layout._pipelines.values())[0].filters
f1.value = (0.1, 0.7)
f2.value = [] # overwriting default with empty list
assert pn.state.location.search == '?C=%5B%5D&A=%5B0.1%2C+0.7%5D'
assert f2.widget.value == []

@sql_available
def test_dashboard_with_sql_source_and_transforms(set_root, document, mixed_df_object_type):
Expand Down
2 changes: 1 addition & 1 deletion lumen/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __panel__(self) -> Viewable:
return pn.panel(pn.bind(lambda e: self.panel, self.param.rerender))

def _update_loading(self, event):
if self._panel is not None:
if hasattr(self._panel, 'loading'):
with immediate_dispatch():
self._panel.loading = event.new

Expand Down

0 comments on commit 592e75b

Please sign in to comment.