[Python] How to dynamically render tables in Taipy app

Discussão em 'Python' iniciado por Stack, Junho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Taipy application where I want to, when selected a value from the drop-down component, to dynamically render tables.

    I have data frame that I load from csv file with columns: partnerName, regionName, userId, groupName, depositAmount, withdrawalAmount, totalRevenue.
    My drop-down component will contain values: partnerName, regionName, userId, groupName. After each selection of a value from a drop-down, the value will be removed and the table will be rendered, along with the other tables that are already rendered, for previously selected values.

    For each selected value from drop-down, the original (default) data frame will be grouped by that value and all numeric columns will be aggregated. The table should have data from this aggregated data frame.

    My idea so far was to save each of those aggregated data frames to a list and then iterate through that list and display it in a table using partial from Taipy. When tried to use index

    for i, _ in enumerate(state.tables_data):
    with builder.part("card"):
    builder.text(f"Filter applied: {state.filter_display_texts}", class_name="h2")
    builder.table(data=state.tables_data, page_size=10)


    , it always says that


    > WARNING:root:
    --- 1 warning(s) were found for page 'TaiPy_partials_0' ---
    - Warning 1: table.data property should be bound.
    -------------------------------------------------------------

    return attrgetter(name)(gui._get_data_scope())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'types.SimpleNamespace' object has no attribute ''​

    and if I try to iterate by data frame it says:


    --- 2 warning(s) were found for page 'TaiPy_partials_0' ---

    • Warning 1: table.data property should be bound.
    • Warning 2: Can't find Data Accessor for type <class 'str'>.
    ​

    and an error


    AttributeError: 'types.SimpleNamespace' object has no attribute ''

    _warn(f"Decoding Message has failed: {message}", e)
    ​

    I am using: Python v3.12.0 Taipy v4.0.3 Pandas v2.2.2

    My code so far:

    from taipy.gui import Gui
    import taipy.gui.builder as builder
    import pandas as pd
    from datetime import date

    # Load data
    data = pd.read_csv('my_data.csv')
    data.fillna(0, inplace=True)

    # Initialize variables
    selected_partners = None
    partners = data['partnerName'].unique().tolist()

    starting = date(2025, 1, 1)
    ending = date(2025, 5, 1)
    dates = [starting, ending]

    selected_filter = None
    filters = [ 'partnerName', 'regionName', 'userId', 'groupName' ]

    filter_display_texts = []
    tables_data = []

    def load_data(state):
    print(state.selected_partners)
    print(state.dates)
    state.data = data[data['partnerName'].isin(state.selected_partners)] if state.selected_partners else data

    def filter_category(state):
    if not state.selected_filter:
    return

    print('Filtering data by:', state.selected_filter)
    state.filter_display_texts.append(state.selected_filter)
    value = state.data.groupby(state.selected_filter)[['depositAmount', 'withdrawalAmount', 'totalRevenue']].sum().reset_index()
    state.tables_data.append(value)

    with builder.Page() as table_data:
    with builder.layout("1"):
    for val in state.tables_data:
    with builder.part("card"):
    builder.table(value=val, class_name="h2")

    tables_partial.update_content(state, table_data)
    state.filters = [f for f in state.filters if f != state.selected_filter]

    def reset_state(state):
    pass

    with builder.Page() as page:
    builder.date_range(dates="{dates}", label_start="Start Date", label_end="End Date")
    builder.selector(value="{selected_partners}", lov="{partners}", dropdown=True, multiple=True, filter=True, label="Select region (if none all will be displayed)")
    builder.button(label="Submit", on_action=load_data)
    builder.button(label="Reset", on_action=reset_state)
    builder.html("br")

    builder.selector(value='{selected_filter}', lov="{filters}", dropdown=True, label='Select filter', on_change=filter_category)
    builder.part(partial="{tables_partial}")

    gui = Gui(page=page)
    tables_partial = gui.add_partial("")
    gui.run(title="Analytics Dashboard", dark_mode=True, use_reloader=True, port=5001, debug=True)

    Continue reading...

Compartilhe esta Página