PablocFonseca / streamlit-aggrid

Implementation of Ag-Grid component for Streamlit
https://pypi.org/project/streamlit-aggrid/
MIT License
1.06k stars 202 forks source link

When the number of rows in table is small and `reload_data=True`, the selection doesn't show up in UI #39

Closed smolendawid closed 6 months ago

smolendawid commented 3 years ago

When the number of rows in the table is small and reload_data=True, the selection doesn't show up

Minimal example to reproduce:

import streamlit as st
from st_aggrid import GridOptionsBuilder, AgGrid
import pandas as pd

csv_url = (
    "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"
)
df = pd.read_csv(csv_url, sep=";")
df = df.head(28)  # HERE!!! when set to 29, it's working

st.title("Not showing selection example")

gb1 = GridOptionsBuilder.from_dataframe(df)
gb1.configure_selection(selection_mode="multiple", use_checkbox=True)
gridOptions = gb1.build()
response = AgGrid(
    df,
    gridOptions=gridOptions,
    width="100%",
    update_mode="SELECTION_CHANGED",
    reload_data=True,
    key='example',
)

print(response['selected_rows'])

The row that is selected is printed but you can't see it in the UI

PablocFonseca commented 2 years ago

I was able to make your example work by setting reload_data=False (the default value). Why did you need to set this to true?

smolendawid commented 2 years ago

That's a good point, I didn't mention it. Unfortunately, I need to set reload_data=True because I have some checkboxes/sliders that filter the dataframe (for example I need to be able to select show only rows where A=='a' checkbox that filters out some rows from dataframe df and the table reflects it). If I want to be able to filter the table, I need to have reload_data=True.

I was trying to create a workaround - I set reload_data=False and added some state to understand the table shape. When data was filtered and shape changed, I deleted the table component with del operator and created it again. Unfortunately it didn't work, the table is somehow cached and del operator doesn't really destroy it

PablocFonseca commented 2 years ago

Looks like you're using reload_data to "prevent" grid to reload between streamlit refreshs. If this is the case, try setting a fixed key instead. By design streamlit's components will not redraw if key is fixed.

thunderbug1 commented 2 years ago

@smolendawid, a workaround for your issue is to set the "key" argument of the AgGrid widget depending on your filter settings. For example you could just append your settings to a string. In this way it will not reuse the old one and create a new table when the settings change.

smolendawid commented 2 years ago

This workaround works, congrats you figured it out!

@PablocFonseca my issue is not a 'question' I think.