how to select filter rows using streamlit ag-grid?

Viewed 1021

I'm using streamlit Ag-Grid to show a table. I'm allowing the user to filter using the filter option.

table filter

When the user filters any of the columns it works as expected.

I now want to allow the user to download only the filtered rows.

I know that this feature is still in the pipeline, however if I can allow the user to check all the filtered rows by clicking on a 'check all rows' option it will work for now.

I know that this option is avaliable, I just can't seem to make it show up.

Can you guys help me? Thank you

gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(enablePivot=True, enableValue=True, enableRowGroup=True)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gb.configure_side_bar()
gridoptions = gb.build()

response = AgGrid(
    df,
    gridOptions=gridoptions,
    enable_enterprise_modules=True,
    update_mode=GridUpdateMode.MODEL_CHANGED,
    data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
    fit_columns_on_grid_load=False,
    header_checkbox_selection_filtered_only=True,
    use_checkbox=True)
2 Answers

Filtered rows will be in the response. The response is a dictionary with keys data and selected_rows. selected_rows is a list of dict, we just convert this to a pandas df and further convert it to csv for download.

Code
from st_aggrid import AgGrid, GridOptionsBuilder, GridUpdateMode, DataReturnMode
import pandas as pd
import streamlit as st


data = {'cpu': ['Intel Core i7-12700K', 'Intel Core i9-12900K',
                'Intel Core i9-10850K', 'Intel Core i5-11400F'],
        'price': [350, 560, 300, 160]}


@st.cache
def convert_df(df):
    # IMPORTANT: Cache the conversion to prevent computation on every rerun
    return df.to_csv(index=False).encode('utf-8')


df = pd.DataFrame(data)

gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(enablePivot=True, enableValue=True, enableRowGroup=True)
gb.configure_selection(selection_mode="multiple", use_checkbox=True)
gb.configure_side_bar()
gridoptions = gb.build()

response = AgGrid(
    df,
    height=200,
    gridOptions=gridoptions,
    enable_enterprise_modules=True,
    update_mode=GridUpdateMode.MODEL_CHANGED,
    data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
    fit_columns_on_grid_load=False,
    header_checkbox_selection_filtered_only=True,
    use_checkbox=True)

# st.write(type(response))
# st.write(response.keys())

v = response['selected_rows']
if v:
    st.write('Selected rows')
    st.dataframe(v)
    dfs = pd.DataFrame(v)
    csv = convert_df(dfs)

    st.download_button(
        label="Download data as CSV",
        data=csv,
        file_name='selected.csv',
        mime='text/csv',
    )
Sample output

enter image description here

csv output
cpu,price
Intel Core i7-12700K,350
Intel Core i9-10850K,300

I was able to get the filtered rows out by using response['data'], together with the same parameters in the first answer

Related