From 4dfe0c1c1b9b66ebcfb665476a87963fc539dd06 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Sun, 5 Jun 2022 17:27:58 +0200 Subject: [PATCH] added auto height checkbox, made state of this setting part of the grid key --- stand/webapp.py | 10 ++++++++++ stand/widgets/aggrid.py | 17 +++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/stand/webapp.py b/stand/webapp.py index af34abf..53428b4 100644 --- a/stand/webapp.py +++ b/stand/webapp.py @@ -33,12 +33,22 @@ df = restapi.data download(df) +auto_height = st.sidebar.checkbox( + "Auto Height", + value=False, + key="auto_height" +) + height = st.sidebar.slider( "Grid Height", min_value=100, max_value=1500, value=500, step=10, + disabled=auto_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 did not change, we do not reload to ensure changes in the browser persist. diff --git a/stand/widgets/aggrid.py b/stand/widgets/aggrid.py index 7b8229d..48c72bd 100644 --- a/stand/widgets/aggrid.py +++ b/stand/widgets/aggrid.py @@ -1,7 +1,7 @@ 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 gob = GridOptionsBuilder.from_dataframe( @@ -13,7 +13,9 @@ def aggrid(df, reload_data=False, height=500): sortable=True ) -# gob.configure_auto_height(True) + enable_auto_height = (height == "auto") + gob.configure_auto_height(enable_auto_height) + go = gob.build() response = AgGrid( @@ -23,7 +25,7 @@ def aggrid(df, reload_data=False, height=500): theme="streamlit", fit_columns_on_grid_load=True, reload_data=reload_data, - key=make_key(df) + key=make_key(df, enable_auto_height) ) 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, - 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)