Hi!
I am working with streamlit-aggrid. It is a fantastic tool.
I think I have to ask this question to @PablocFonseca. Is it possible to know what variables and conditions are used when filtering a dataframe?
Let me suppose I have a dataframe with age column.
I display it with streamlit-aggrid.
After that, I filter people whose age is greater than 18.
Is it possible that, apart form getting the data, to obtain a list with the conditions used? In this case, I would obtain something like: [(age, greater than, 18)]
Hi @pachoning and @Miguel_Gonzalez ,
After more than an hour of searching and testing, I found a solution from here.
Please see my code below. Hope this could be a help for you.
PS: Refer here to know how gridOptions.api works.
# import streamlit as st
import pandas as pd
from st_aggrid import GridOptionsBuilder, AgGrid, JsCode
# Test data is copy from www.ag-grid.com
# In order to minimize the coding time, please allow me to show it in such a weird format.
data = {
"Michael Phelps": 27,
"Natalie Coughlin": 25,
"Aleksey Nemov": 24,
"Alicia Coutts": 24,
"Missy Franklin": 17,
"Ryan Lochte": 27,
"Allison Schmitt": 22,
"Natalie Coughlin": 21,
"Ian Thorpe": 17,
"Dara Torres": 33,
"Cindy Klassen": 26,
}
df = pd.DataFrame({"name": data.keys(), "age": data.values()})
defaultColDef = {
"filter": True,
"resizable": True,
"sortable": True,
}
# refer from https://www.ag-grid.com/javascript-data-grid/filter-api/#example-filter-api-readonly
# This is the keypoint.
"""
If you have a component that you wish to work on data once it's ready
(calculate the sum of a column for example)
then you'll need to hook into either the gridReady
or the firstDataRendered events.
"""
onFirstDataRendered = JsCode("""
function onFirstDataRendered(parmas) {
# console.log('The grid is now ready');
var ageFilterComponent = parmas.api.getFilterInstance('age');
ageFilterComponent.setModel({
type: 'greaterThan',
filter: 18,
filterTo: null,
});
parmas.api.onFilterChanged();
}
""")
options = {
"rowSelection": "multiple",
"rowMultiSelectWithClick": True,
# "sideBar": ["columns", 'filters'],
"enableRangeSelection": True,
"onFirstDataRendered": onFirstDataRendered
}
options_builder = GridOptionsBuilder.from_dataframe(df)
options_builder.configure_default_column(**defaultColDef)
options_builder.configure_grid_options(**options)
grid_options = options_builder.build()
grid_table = AgGrid(df, grid_options, allow_unsafe_jscode=True)
Hello!
I have tried to adapt this code to get the column where a filter was applied, but without success!
I think it’s related to onFilterChanged, but I don’t know exactly how to do it… Does anyone know? Thanks
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.