简体   繁体   English

Streamlit 如何使用 session state 和 Aggrid 在切换页面后保持选择?

[英]Streamlit how to use session state with Aggrid to keep the selection even after switching pages?

This is potentially an easy one, but I just can't figure out how to do it.这可能很简单,但我就是不知道该怎么做。 Here's a simple reproducible code example.这是一个简单的可重现代码示例。 How to use session state to keep the tickbox selection, even after switching pages (you will need to create a page folder to include multi pages)?如何使用 session state 保持复选框选择,即使在切换页面后也是如此(您需要创建一个页面文件夹以包含多个页面)?

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


data = {
    "calories": [420, 380, 390],
    "duration": [50, 40, 45],
    "random1": [5, 12, 1],
    "random2": [230, 23, 1]
}

df = pd.DataFrame(data)

gb = GridOptionsBuilder.from_dataframe(df[["calories", "duration"]])
gb.configure_selection(selection_mode="single", use_checkbox=True)
gb.configure_side_bar()
gridOptions = gb.build()

data = AgGrid(df,
              gridOptions=gridOptions,
              enable_enterprise_modules=True,
              allow_unsafe_jscode=True,
              update_mode=GridUpdateMode.SELECTION_CHANGED,
              columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS)

selected_rows = data["selected_rows"]

if len(selected_rows) != 0:
    selected_rows[0]

For example, when I select the tickbox, and after I switch to page 2, then back to test page, the tickbox selection still remains.比如我select勾选框,切换到第2页,再回到测试页,勾选框的选择依然存在。 在此处输入图像描述

The method configure_selection has a parameter pre_selected_rows used to pre-select a row.方法configure_selection有一个参数pre_selected_rows用于预选一行。 Once the user selected a row, we will save this row's index in session state variable so we can use it to restore the previous selected row when coming from a different page.一旦用户选择了一行,我们就会将这一行的索引保存在 session state 变量中,这样我们就可以在来自不同页面时使用它来恢复之前选择的行。

I revise your code a bit to make it work.我稍微修改了您的代码以使其工作。

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


if 'sid' not in st.session_state:
    st.session_state.sid = None


data = {
    "calories": [420, 380, 390],
    "duration": [50, 40, 45],
    "random1": [5, 12, 1],
    "random2": [230, 23, 1]
}

df = pd.DataFrame(data)

gb = GridOptionsBuilder.from_dataframe(df[["calories", "duration"]])

# Add pre_selected_rows param.
gb.configure_selection(selection_mode="single", use_checkbox=True,
                       pre_selected_rows=[st.session_state.sid])
gb.configure_side_bar()
gridOptions = gb.build()

# Add key.
data = AgGrid(df,
              gridOptions=gridOptions,
              enable_enterprise_modules=True,
              allow_unsafe_jscode=True,
              update_mode=GridUpdateMode.SELECTION_CHANGED,
              columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS,
              key='mykey')

selected_rows = data["selected_rows"]

# Save the row index of the selected row.
if len(selected_rows):
    ind = selected_rows[0]['_selectedRowNodeInfo']['nodeRowIndex']
    st.session_state.sid = ind

if len(selected_rows) != 0:
    selected_rows[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM