我目前正在使用AgGrid表显示数据的实时指标 Jmeter 板。为了使数据保持最新,我希望每15秒自动重新加载一次。我尝试使用st_autorefresh
来实现这一点,但我遇到了AgGrid表在每次页面更新后丢失选定行的问题,即使使用pre_selected_rows
也是如此。
你们中有没有人对这个问题有经验,知道如何解决它?
下面是示例存储库的链接:
https://github.com/andrewssobral/streamlit-example
下面是正在运行的应用程序的链接:
https://andrewssobral-streamlit-example-streamlit-app-txrvx1.streamlit.app/
import numpy as np
import pandas as pd
import streamlit as st
from streamlit_autorefresh import st_autorefresh
from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode, JsCode
if 'pre_selected_rows' not in st.session_state:
st.session_state.pre_selected_rows = []
df = pd.DataFrame(columns=['ID', 'STATUS'])
df['ID'] = [1, 2, 3]
df['STATUS'] = np.random.randint(0,100,size=(3))
st.write(df)
# get pre-selected rows from session state
pre_selected_rows = st.session_state.pre_selected_rows
st.write("pre_selected_rows: ", st.session_state.pre_selected_rows)
# use the pre-selected rows when building the grid options
gb = GridOptionsBuilder.from_dataframe(df)
gb.configure_default_column(editable=True, wrapText=True, autoHeight=True)
gb.configure_column('ID', minWidth=80, maxWidth=80, type=["numericColumn","numberColumnFilter"], sortable=True, sort='desc', checkboxSelection=True, headerCheckboxSelection=True)
gb.configure_column('STATUS', minWidth=100, maxWidth=100)
gb.configure_pagination(paginationAutoPageSize=False, paginationPageSize=3)
gb.configure_side_bar()
gb.configure_selection('multiple', pre_selected_rows=pre_selected_rows, use_checkbox=True)
gb_grid_options = gb.build()
# render the grid and get the selected rows
grid_return = AgGrid(
df,
gridOptions = gb_grid_options,
key = 'ID',
reload_data = True,
data_return_mode = DataReturnMode.AS_INPUT,
update_mode = GridUpdateMode.MODEL_CHANGED, # GridUpdateMode.SELECTION_CHANGED or GridUpdateMode.VALUE_CHANGED or
allow_unsafe_jscode = True,
fit_columns_on_grid_load = False,
enable_enterprise_modules = False,
height = 320,
width = '100%',
theme = "streamlit"
)
selected_rows = grid_return["selected_rows"]
# save the row indexes of the selected rows in the session state
pre_selected_rows = []
for selected_row in selected_rows:
pre_selected_rows.append(selected_row['_selectedRowNodeInfo']['nodeRowIndex'])
st.session_state.pre_selected_rows = pre_selected_rows
# print the selected rows
st.write("Selected rows: ", selected_rows)
st_autorefresh(interval=((1*15*1000)), key="dataframerefresh")
1条答案
按热度按时间fcwjkofz1#
不幸的是,如果你定期刷新页面,没有一个好的方法来完成这个任务,如果页面实际上没有重新加载(你只是重新加载数据),你可以把用户选择的行存储在session state中。
问题是,如果当前会话结束/页面完全重新加载,则存储在会话状态中的任何内容都将丢失。在这种情况下,最好将所选行存储在数据库中(可以简单到Google Sheet),并在页面加载时检索该数据,以便可以为用户正确显示选定的行。我可能希望将选定的行与某种类型的用户ID(如电子邮件地址)关联起来-您可以使用st.experimental_user来实现此目的,因为它看起来像是您在使用Streamlit Community Cloud。