-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi-range sliders for DataFilterExtension (#340)
Closes #339 ### Change list - Add `MultiRangeSlider`, a subclass of `ipywidgets.VBox` to connect multiple float range sliders - Add to API docs
- Loading branch information
1 parent
949b272
commit 2ba95c9
Showing
6 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# lonboard.controls | ||
|
||
::: lonboard.controls.MultiRangeSlider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from functools import partial | ||
from typing import Sequence | ||
|
||
import traitlets | ||
from ipywidgets import FloatRangeSlider | ||
from ipywidgets.widgets.trait_types import TypedTuple | ||
|
||
# Import from source to allow mkdocstrings to link to base class | ||
from ipywidgets.widgets.widget_box import VBox | ||
|
||
|
||
class MultiRangeSlider(VBox): | ||
"""A widget for multiple ranged sliders. | ||
This is designed to be used with the | ||
[DataFilterExtension][lonboard.experimental.DataFilterExtension] when you want to | ||
filter on 2 to 4 columns on the same time. | ||
If you have only a single filter, use an ipywidgets | ||
[FloatRangeSlider][ipywidgets.widgets.widget_float.FloatRangeSlider] directly. | ||
# Example | ||
```py | ||
from ipywidgets import FloatRangeSlider | ||
slider1 = FloatRangeSlider( | ||
value=(2, 5), | ||
min=0, | ||
max=10, | ||
step=0.1, | ||
description="First slider: " | ||
) | ||
slider2 = FloatRangeSlider( | ||
value=(30, 40), | ||
min=0, | ||
max=50, | ||
step=1, | ||
description="Second slider: " | ||
) | ||
multi_slider = MultiRangeSlider([slider1, slider2]) | ||
multi_slider | ||
``` | ||
Then to propagate updates to a rendered layer, call | ||
[jsdlink][ipywidgets.widgets.widget_link.jsdlink] to connect the two widgets. | ||
```py | ||
from ipywidgets import jsdlink | ||
jsdlink( | ||
(multi_slider, "value"), | ||
(layer, "filter_range") | ||
) | ||
``` | ||
As you change the slider, the `filter_range` value on the layer class should be | ||
updated. | ||
""" | ||
|
||
# We use a tuple to force reassignment to update the list | ||
# This is because list mutations do not get propagated as events | ||
# https://github.com/jupyter-widgets/ipywidgets/blob/b2531796d414b0970f18050d6819d932417b9953/python/ipywidgets/ipywidgets/widgets/widget_box.py#L52-L54 | ||
value = TypedTuple(trait=TypedTuple(trait=traitlets.Float())).tag(sync=True) | ||
|
||
def __init__(self, children: Sequence[FloatRangeSlider], **kwargs): | ||
# We manage a list of lists to match what deck.gl expects for the | ||
# DataFilterExtension | ||
def callback(change, *, i: int): | ||
value = list(self.value) | ||
value[i] = change["new"] | ||
self.set_trait("value", value) | ||
self.send_state("value") | ||
|
||
initial_values = [] | ||
for i, child in enumerate(children): | ||
func = partial(callback, i=i) | ||
child.observe(func, "value") | ||
initial_values.append(child.value) | ||
|
||
super().__init__(children, value=initial_values, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "lonboard" | ||
version = "0.5.0" | ||
version = "0.6.0-beta.1" | ||
description = "Python library for fast, interactive geospatial vector data visualization in Jupyter." | ||
authors = ["Kyle Barron <[email protected]>"] | ||
license = "MIT" | ||
|