added auto height checkbox, made state of this setting part of the grid key

This commit is contained in:
2022-06-05 17:27:58 +02:00
parent b72078d17c
commit 4dfe0c1c1b
2 changed files with 21 additions and 6 deletions

View File

@ -33,12 +33,22 @@ df = restapi.data
download(df) download(df)
auto_height = st.sidebar.checkbox(
"Auto Height",
value=False,
key="auto_height"
)
height = st.sidebar.slider( height = st.sidebar.slider(
"Grid Height", "Grid Height",
min_value=100, max_value=1500, value=500, step=10, min_value=100, max_value=1500, value=500, step=10,
disabled=auto_height,
key="grid_height" key="grid_height"
) )
if auto_height:
height = "auto"
# if df in restapi changed, we need to reload it into aggrid. # if df in restapi changed, we need to reload it into aggrid.
# if df in restapi did not change, we do not reload to ensure changes in the browser persist. # if df in restapi did not change, we do not reload to ensure changes in the browser persist.

View File

@ -1,7 +1,7 @@
from st_aggrid import AgGrid, GridOptionsBuilder from st_aggrid import AgGrid, GridOptionsBuilder
def aggrid(df, reload_data=False, height=500): def aggrid(df, reload_data=False, height="auto"):
df = df[::-1] # display in reversed chronological order df = df[::-1] # display in reversed chronological order
gob = GridOptionsBuilder.from_dataframe( gob = GridOptionsBuilder.from_dataframe(
@ -13,7 +13,9 @@ def aggrid(df, reload_data=False, height=500):
sortable=True sortable=True
) )
# gob.configure_auto_height(True) enable_auto_height = (height == "auto")
gob.configure_auto_height(enable_auto_height)
go = gob.build() go = gob.build()
response = AgGrid( response = AgGrid(
@ -23,7 +25,7 @@ def aggrid(df, reload_data=False, height=500):
theme="streamlit", theme="streamlit",
fit_columns_on_grid_load=True, fit_columns_on_grid_load=True,
reload_data=reload_data, reload_data=reload_data,
key=make_key(df) key=make_key(df, enable_auto_height)
) )
df = response.get("data", df) df = response.get("data", df)
@ -33,12 +35,15 @@ def aggrid(df, reload_data=False, height=500):
def make_key(df): def make_key(df, enable_auto_height):
""" """
encode the dataframe's column names into a key, encode the dataframe's column names into a key,
this triggers a hard reload (like F5) of the AgGrid if the columns change as well as the state of the auto height setting,
this triggers a hard reload (like F5) of the AgGrid if the columns/setting change
""" """
return "stand:" + "+".join(str(col) for col in df.columns) items = list(df.columns)
items.append(enable_auto_height)
return "stand:" + "+".join(str(i) for i in items)