python-3.x 是否可以使用行索引以外的方法访问selected_rows.data?

px9o7tmv  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(117)

我一直在努力创建一个过滤器,让selected_rows显示在另一个表中。

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output, State
from dash_table import DataTable

#Define sample dataframes with the same columns
df1 = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Value': [1, 2, 3], 'tipo': ['Direita','Esquerda','Direita']})
df2 = pd.DataFrame({'Category': ['D', 'E', 'F'], 'Value': [5, 6, 7], 'tipo': ['Esquerda','Direita','Esquerda']})

lista_filtros = df1['tipo'].unique()

# Define Dash app
app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True

# Define dropdown options
dropdown_options = [{'label': 'Dataframe 1', 'value': 'df1'},
                    {'label': 'Dataframe 2', 'value': 'df2'}]

# Define layout
app.layout = html.Div([
    dcc.Dropdown(
        id='dropdown',
        options=dropdown_options,
        value='df1'
    ),
    dcc.Checklist(
        id='type-checklist',
        options=[{'label': i, 'value': i} for i in lista_filtros] + [{'label': 'All', 'value': 'All'}],
        value=['All']
    ),
    html.Br(),
    DataTable(
        id='table',
        row_selectable='multi',
        selected_rows=[],
        page_size=15
    ),
    html.Button('Submit', id='submit-button', n_clicks=0),
    dcc.Store(id='selected-rows', storage_type='session'),
    html.Br(),
    html.Div([
        DataTable(
            id='selected-data',
            columns=[{'name': 'Category', 'id': 'Category'}, {'name': 'Value', 'id': 'Value'}],
            page_size=5,
            row_deletable= True,
        )
    ], id='selected-data-container', style={'display': 'none'})
])

# Define callback to update table based on dropdown selection and type checklist
@app.callback(
    Output('table', 'data'),
    [Input('dropdown', 'value'), Input('type-checklist', 'value')]
)
def update_table(selected_value, selected_type):
    if selected_value == 'df1':
        df = df1
    elif selected_value == 'df2':
        df = df2
    else:
        return []

    all_selected = 'All' in selected_type
    if all_selected:
        return df.to_dict('records')
    else:
        filtered_df = df[df['tipo'].isin(selected_type)]
        # preserve original order of rows
        filtered_df = filtered_df.set_index('tipo')
        filtered_df = filtered_df.loc[selected_type]
        filtered_df = filtered_df.reset_index()
        return filtered_df.to_dict('records')

# Define callback to store selected rows in dcc.Store
@app.callback(
    Output('selected-rows', 'data'),
    [Input('submit-button', 'n_clicks')],
    [State('table', 'selected_rows')]
)
def store_selected_rows(n_clicks, selected_rows):
    if selected_rows:
        return selected_rows

# Define callback to update selected data table visibility and content
@app.callback(
    [Output('selected-data-container', 'style'),
     Output('selected-data', 'columns'),
     Output('selected-data', 'data')],
    [Input('selected-rows', 'data')],
    [State('selected-data', 'data'),
     State('dropdown', 'value')]
)
def update_selected_data(selected_rows, stored_data, selected_value):
    if selected_rows:
        # get selected rows from selected dataframe
        if selected_value == 'df1':
            selected_df = df1.iloc[selected_rows]
        elif selected_value == 'df2':
            selected_df = df2.iloc[selected_rows]
        else:
            selected_df = pd.DataFrame()
        # add new selected data to stored data
        if stored_data is not None:
            stored_df = pd.DataFrame(stored_data)
            stored_df = stored_df.append(selected_df, ignore_index=True)
            stored_data = stored_df.to_dict('records')
        else:
            stored_data = selected_df.to_dict('records')
        # get columns for selected data table
        selected_columns = [{'name': i, 'id': i} for i in selected_df.columns]
        # return updated values
        return {'display': 'block'}, selected_columns, stored_data
    else:
        # return default values
        return {'display': 'none'}, [], None

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)

Jmeter 板工作得很好,直到我为我的“selected_type”检查表选择了“all”以外的参数。
这是因为“selected_rows.data”参数返回给我的索引有问题。
例如,如果我使用过滤器“right”,我将看到的是“category = B”,它是我的“id = table”在过滤后的索引0。
但是,当我按下“提交”按钮时,我的函数在过滤之前使用原始表中的索引0,因此我收到数据“Category = A”。
这是一个Python问题,而不是Dash问题,但是回调的使用使我很难解决,当我试图在“updated_selected_data”函数中重新创建一个新的过滤 Dataframe 时,导致了各种错误。
我只需要我的表准确地反映我在前一个表中看到的数据,这可能可以很容易地使用索引以外的任何参数来完成。有什么想法可以解决这个问题吗?

ozxc1zmp

ozxc1zmp1#

我认为您不需要使用dcc.store来返回selected_rows,您应该更改:

html.Div([
        DataTable(
            id='selected-data',
            columns=[{'name': 'Category', 'id': 'Category'}, {'name': 'Value', 'id': 'Value'}],
            page_size=5,
            row_deletable= True,
        )
    ], id='selected-data-container', style={'display': 'none'})
])

致:

html.Div(id='selected-data-container')

然后向该DIv返回新的DataTable。
完整代码:

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output, State
from dash_table import DataTable

#Define sample dataframes with the same columns
df1 = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Value': [1, 2, 3], 'tipo': ['Direita','Esquerda','Direita']})
df2 = pd.DataFrame({'Category': ['D', 'E', 'F'], 'Value': [5, 6, 7], 'tipo': ['Esquerda','Direita','Esquerda']})

lista_filtros = df1['tipo'].unique()

# Define Dash app
app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True

# Define dropdown options
dropdown_options = [{'label': 'Dataframe 1', 'value': 'df1'},
                    {'label': 'Dataframe 2', 'value': 'df2'}]

# Define layout
app.layout = html.Div([
    dcc.Dropdown(
        id='dropdown',
        options=dropdown_options,
        value='df1'
    ),
    dcc.Checklist(
        id='type-checklist',
        options=[{'label': i, 'value': i} for i in lista_filtros] + [{'label': 'All', 'value': 'All'}],
        value=['All']
    ),
    html.Br(),
    DataTable(
        id='table',
        row_selectable='multi',
        selected_rows=[],
        page_size=15
    ),
    html.Button('Submit', id='submit-button', n_clicks=0),
    dcc.Store(id='selected-rows', storage_type='session'),
    html.Br(),
    html.Div(id='selected-data-container')
])

# Define callback to update table based on dropdown selection and type checklist
@app.callback(
    Output('table', 'data'),
    [Input('dropdown', 'value'), 
     Input('type-checklist', 'value')]
)
def update_table(selected_value, selected_type):
    if selected_value == 'df1':
        df = df1
    elif selected_value == 'df2':
        df = df2
    else:
        return []

    all_selected = 'All' in selected_type
    if all_selected:
        return df.to_dict('records')
    else:
        filtered_df = df[df['tipo'].isin(selected_type)]
        # preserve original order of rows
        filtered_df = filtered_df.set_index('tipo')
        filtered_df = filtered_df.loc[selected_type]
        filtered_df = filtered_df.reset_index()
        return filtered_df.to_dict('records')
@app.callback(
    Output('selected-data-container', 'children'),
    [Input('submit-button', "n_clicks"),
     State('dropdown', 'value'),
     State('table', 'selected_rows')]
)
def update_bar(n_clicks, selected_value, slctd_rows):
    if selected_value == 'df1':
        df = df1
        df_filterd = df1[df1.index.isin(slctd_rows)]
    elif selected_value == 'df2':
        df = df2
        df_filterd = df2[df2.index.isin(slctd_rows)]
    return html.Div([
        DataTable(
        id='table2',
        data=df_filterd.to_dict('records')
    ),
    ])
    
# Run the app
if __name__ == '__main__':
    app.run_server(debug=False)

第一节第一节第一节第一节第一次
请检查这是否是您需要的。

相关问题