[Python] Python shiny express dynamic filter options

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I have a dataframe of the following format:

    [​IMG]

    I want two dropdown menus where I can filter the dataframe for column 1 and column 2. I want the options for the dropdown to be dynamic in that they reflect the option selected in the other dropdown menu.

    E.g. if I have selected Column1 = "A" then only "1" should show up in the dropdown for Column2.

    from shiny import reactive
    from shiny.express import input, render, ui
    import pandas as pd

    from data import master_df

    @reactive.calc
    def filter_data():

    filtered_df = master_df.copy()

    if(input.Column1() is not None):
    filtered_df = filtered_df[filtered_df['Column1'] == input.Column1()]

    if(input.Column2() is not None):
    filtered_df = filtered_df[filtered_df['Column2'] == input.Column2()]

    return filtered_df

    ui.input_selectize("Column1", "Select Column1", filter_data()['Column1'].tolist())

    ui.input_selectize("Column2", "Select Column2", filter_data()['Column2'].tolist())


    @render.data_frame
    def render_data():
    return filter_data()


    I have tried the above but I'm getting a "No current reactive context" error.

    Continue reading...

Compartilhe esta Página