如何在streamlit-aggrid中获取组选择的详细信息

问题描述 投票:0回答:1

我正在使用 streamlit 扩展 streamlit-aggrid 并且我有一个带有行组的可选表。选择分组行时,我无法收集所选行的所有详细信息。

这是一个可运行的

issue_example.py

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

#_ Input data
df = pd.DataFrame({
    'Category': ['Fruit', 'Fruit', 'Vegetable', 'Vegetable'],
    'Items': ['Apple', 'Banana', 'Tomato', 'Carrots'],
    'Price': [1.04, 1.15, 1.74, 1.5]})

#_ Ag Grid table
st.markdown('# Issue: how to get group selection?')
st.write("Try selecting an aggregate, and then an atomic record")

grid_options = {
    "columnDefs":   [
                        {"field": "Category", "rowGroup": True, "hide": True},
                        {"field": "Items"},
                        {"field": "Price"},
                    ],
    "rowSelection": "single",
    }

#_ Playing with response
response = AgGrid(
    df,
    grid_options,
    columns_auto_size_mode=ColumnsAutoSizeMode.FIT_ALL_COLUMNS_TO_VIEW,
)

if response['selected_rows']:

    selection=response['selected_rows'][0]

    st.write("Current selection is provided as a nested dictionary, requesting `['selected_rows'][0]` value of AgGrid response:")
    st.write(selection)

    if "Items" in selection:
        st.markdown('#### Good!')
        Category = selection['Category']
        Item = selection['Items']
        Price = selection['Price']
        st.write(f"We know everything about current selection: you picked a `{Category}` called `{Item}`, with price `{Price}`!")
    else:
        st.markdown('#### Bad!')
        nodeId = response['selected_rows'][0]['_selectedRowNodeInfo']['nodeId']
        st.write(f"All we know is that a node with Id `{nodeId}` is selected.\n\r How do we know if you're looking for a `Fruit` or a `Vegetable`?")

当使用

streamlit run issue_example.py
运行上面的代码并选择
Fruit
组行时,AgGrid 的响应是一个字典,其中不包含有关
Fruit
组中的行详细信息的信息。它甚至没有告诉我我选择了
Fruit
。我需要有一种方法来知道当我选择水果时,里面选择的行是
Apple
Banana

查看正在运行的 streamlit 应用程序的屏幕截图:

python ag-grid streamlit
1个回答
0
投票

您似乎需要添加以下内容(基于 this section of the docs)以启用选择多行/选择组的子项:

options.configure_selection(selection_mode="multiple", rowMultiSelectWithClick=True, groupSelectsChildren=True, groupSelectsFiltered=True)

您可能还想在将数据传递给

streamlit-aggrid
之前将其转换为 Pandas 数据框:

df = pd.DataFrame({
        'Category': ['Fruit', 'Fruit', 'Vegetable', 'Vegetable'],
        'Items': ['Apple', 'Banana', 'Tomato', 'Carrots'],
        'Price': [1.04, 1.15, 1.74, 1.5]})
df = pd.DataFrame(df)
options = GridOptionsBuilder.from_dataframe(df, enableRowGroup=True, enableValue=True, enablePivot=True)
© www.soinside.com 2019 - 2024. All rights reserved.