Skip to content

Commit

Permalink
Add str support to dropdown (#529)
Browse files Browse the repository at this point in the history
This small PR adds str column support to `drop_down` and `multi_select` widgets, along with `charts.bar`, which already supports str columns directly since 23.08

```python
import cudf
from cuxfilter import charts, DataFrame

cux_df = DataFrame.from_dataframe(cudf.DataFrame({'val': ['A', 'B', 'C', 'D', 'E']}))
multi_select = charts.multi_select('val')

d = cux_df.dashboard([multi_select])
# View the individual multi_select chart part of the dashboard d
multi_select.view(height=200)
```
![image](https://github.com/rapidsai/cuxfilter/assets/20476096/5768f5ca-8915-410f-9d10-065b89cc94df)

cc @exactlyallan

Authors:
  - Ajay Thorve (https://github.com/AjayThorve)

Approvers:
  - Allan (https://github.com/exactlyallan)

URL: #529
  • Loading branch information
AjayThorve authored Aug 16, 2023
1 parent 3cc7705 commit bc073d7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
Binary file not shown.
16 changes: 8 additions & 8 deletions docs/source/user_guide/charts/widgets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ Example
import cudf
from cuxfilter import charts, DataFrame

cux_df = DataFrame.from_dataframe(cudf.DataFrame({'key': [0, 1, 2, 3, 4], 'val':[float(i + 10) for i in range(5)]}))
drop_down = charts.drop_down('val')
cux_df = DataFrame.from_dataframe(cudf.DataFrame({'val': ['A', 'B', 'C', 'D', 'E']}))
multi_select = charts.drop_down('val')

d = cux_df.dashboard([drop_down])
#view the individual drop_down chart part of the dashboard d
drop_down.view()
d = cux_df.dashboard([multi_select])
# View the individual multi_select chart part of the dashboard d
multi_select.view(height=200)


Multiselect
Expand All @@ -118,12 +118,12 @@ Example
import cudf
from cuxfilter import charts, DataFrame

cux_df = DataFrame.from_dataframe(cudf.DataFrame({'key': [0, 1, 2, 3, 4], 'val':[float(i + 10) for i in range(5)]}))
cux_df = DataFrame.from_dataframe(cudf.DataFrame({'val': ['A', 'B', 'C', 'D', 'E']}))
multi_select = charts.multi_select('val')

d = cux_df.dashboard([multi_select])
#view the individual multi_select chart part of the dashboard d
multi_select.view()
# View the individual multi_select chart part of the dashboard d
multi_select.view(height=200)


Number Chart
Expand Down
2 changes: 1 addition & 1 deletion python/cuxfilter/charts/core/core_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _repr_mimebundle_(self, include=None, exclude=None):
return None

def view(self, width=400, height=10):
return pn.panel(self.chart, width=width, height=10)
return pn.Column(self.chart, width=width, height=height)

def get_dashboard_view(self):
return pn.panel(self.chart, sizing_mode="stretch_width")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def cb(bounds, x_selection, y_selection):
dashboard_cls._query_str_dict,
dashboard_cls._query_local_variables_dict,
)
print("hello")
# reload all charts with new queried data (cudf.DataFrame only)
dashboard_cls._reload_charts()

Expand Down
12 changes: 6 additions & 6 deletions python/cuxfilter/charts/panel_widgets/panel_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def range_slider(
----------
x: str
column name from gpu dataframe
column name from gpu dataframe, dtype should be int or float
data_points: int, default None
when None, it means no custom number of bins are provided and
Expand Down Expand Up @@ -64,7 +64,7 @@ def date_range_slider(
----------
x: str
column name from gpu dataframe
column name from gpu dataframe, dtype should be datetime
data_points: int, default None
when None, it means no custom number of bins are provided and
Expand Down Expand Up @@ -99,7 +99,7 @@ def int_slider(x, data_points=None, step_size=1, **params):
----------
x: str
column name from gpu dataframe
column name from gpu dataframe, dtype should be int
data_points: int, default None
when None, it means no custom number of bins are provided and
Expand Down Expand Up @@ -129,7 +129,7 @@ def float_slider(x, data_points=None, step_size=None, **params):
----------
x: str
column name from gpu dataframe
column name from gpu dataframe, dtype should be float
data_points: int, default None
when None, it means no custom number of bins are provided and
Expand Down Expand Up @@ -165,7 +165,7 @@ def drop_down(x, **params):
----------
x: str
column name from gpu dataframe
column name from gpu dataframe, dtype [str, int, float]
data_points: int, default number of unique values
Expand All @@ -190,7 +190,7 @@ def multi_select(x, **params):
----------
x: str
column name from gpu dataframe
column name from gpu dataframe, dtype [str, int, float]
data_points: int, default number of unique values
Expand Down
31 changes: 17 additions & 14 deletions python/cuxfilter/charts/panel_widgets/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def compute_query_dict(self, query_str_dict, query_local_variables_dict):


class MultiChoice(BaseWidget):
value = None
source = None

def initiate_chart(self, dashboard_cls):
"""
Expand All @@ -328,16 +328,11 @@ def initiate_chart(self, dashboard_cls):
self.min_value, self.max_value = get_min_max(
dashboard_cls._cuxfilter_df.data, self.x
)

if self.stride is None:
if self.max_value < 1 and self.stride_type == int:
self.stride_type = float
self.stride = self.stride_type(1)

self.source = dashboard_cls._cuxfilter_df.data[self.x].reset_index(
drop=True
)
self.calc_list_of_values(dashboard_cls._cuxfilter_df.data)

self.generate_widget()

self.add_events(dashboard_cls)

def calc_list_of_values(self, data):
Expand Down Expand Up @@ -398,13 +393,21 @@ def compute_query_dict(self, query_str_dict, query_local_variables_dict):
query_dict:
reference to dashboard.__cls__.query_dict
"""
if len(self.chart.value) == 0 or self.chart.value == [""]:
if len(self.chart.value) == 0:
query_str_dict.pop(self.name, None)
elif len(self.chart.value) == 1:
query_str_dict[self.name] = f"{self.x}=={self.chart.value[0]}"
else:
indices_string = ",".join(map(str, self.chart.value))
query_str_dict[self.name] = f"{self.x} in ({indices_string})"
df_module = (
cudf if isinstance(self.source, cudf.Series) else dask_cudf
)

if self.source.dtype == "object":
query_str_dict[self.name] = df_module.DataFrame(
self.source.str.contains("|".join(self.chart.value))
)
else:
query_str_dict[self.name] = df_module.DataFrame(
self.source.isin(self.chart.value)
)

def apply_theme(self, theme):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/cuxfilter/tests/charts/core/test_core_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_view(self, chart, _chart):
bw = BaseWidget("test_x")
bw.chart = chart

assert str(bw.view()) == str(pn.panel(_chart, width=400, height=10))
assert str(bw.view()) == str(pn.Column(_chart, width=400, height=10))

def test_add_event(self):
bw = BaseWidget("test_x")
Expand Down

0 comments on commit bc073d7

Please sign in to comment.