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

Enable User Column Filters #98

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions st_aggrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def AgGrid(
reload_data:bool=False,
theme:str='light',
custom_css=None,
default_column_filters: typing.Optional[str] = None,
key: typing.Any=None,
**default_column_parameters) -> typing.Dict:
"""Reders a DataFrame using AgGrid.
Expand Down Expand Up @@ -126,6 +127,10 @@ def AgGrid(
custom_css (dict, optional):
Custom CSS rules to be added to the component's iframe.

default_column_filters : str, optional
Default column filters to apply on instantiation. Requires enterprise license.
Defaults to None.

key : typing.Any, optional
Streamlits key argument. Check streamlit's documentation.
Defaults to None.
Expand All @@ -146,6 +151,7 @@ def AgGrid(
response = {}
response["data"] = dataframe
response["selected_rows"] = []
response["column_filters"] = None

#basic numpy types of dataframe
frame_dtypes = dict(zip(dataframe.columns, (t.kind for t in dataframe.dtypes)))
Expand Down Expand Up @@ -225,7 +231,8 @@ def cast_to_serializable(value):
reload_data=reload_data,
theme=theme,
custom_css=custom_css,
key=key
default_column_filters=default_column_filters,
key=key,
)

except components.components.MarshallComponentException as ex:
Expand All @@ -250,7 +257,7 @@ def cast_to_serializable(value):

text_columns = [k for k,v in original_types.items() if v in ['O','S','U']]
if text_columns:
frame.loc[:,text_columns.keys()] = frame.loc[:,text_columns.keys()].astype(str)
frame.loc[:,text_columns] = frame.loc[:,text_columns].astype(str)

date_columns = [k for k,v in original_types.items() if v == "M"]
if date_columns:
Expand All @@ -268,5 +275,6 @@ def cast_to_timedelta(s):

response["data"] = frame
response["selected_rows"] = component_value["selectedRows"]
response["column_filters"] = component_value["columnFilters"]

return response
13 changes: 11 additions & 2 deletions st_aggrid/frontend/src/AgGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AgGrid extends StreamlitComponentBase<State> {
private allowUnsafeJsCode: boolean = false
private fitColumnsOnGridLoad: boolean = false
private gridOptions: any
private defaultColumnFilters: string

constructor(props: any) {
super(props)
Expand All @@ -89,6 +90,7 @@ class AgGrid extends StreamlitComponentBase<State> {
this.manualUpdateRequested = (this.props.args.update_mode === 1)
this.allowUnsafeJsCode = this.props.args.allow_unsafe_jscode
this.fitColumnsOnGridLoad = this.props.args.fit_columns_on_grid_load
this.defaultColumnFilters = this.props.args.default_column_filters

this.columnFormaters = {
columnTypes: {
Expand Down Expand Up @@ -203,7 +205,13 @@ class AgGrid extends StreamlitComponentBase<State> {
this.columnApi = event.columnApi

this.setUpdateMode()
this.api.addEventListener('firstDataRendered', (e: any) => this.fitColumns())
this.api.addEventListener('firstDataRendered', (e: any) => {
this.fitColumns()
if (this.defaultColumnFilters) {
let filters = JSON.parse(this.defaultColumnFilters)
this.api.setFilterModel(filters)
}
})

this.api.setRowData(this.state.rowData)

Expand Down Expand Up @@ -270,7 +278,8 @@ class AgGrid extends StreamlitComponentBase<State> {
let returnValue = {
originalDtypes: this.frameDtypes,
rowData: returnData,
selectedRows: this.api.getSelectedRows()
selectedRows: this.api.getSelectedRows(),
columnFilters: JSON.stringify(this.api.getFilterModel())
}

Streamlit.setComponentValue(returnValue)
Expand Down