Loop to Produce x Dash Tables

Hello! I am trying to dynamically output x amount of data tables based on a date chosen in a date picker. I have the data printing correctly, but once I put the dash table into the loop I am only getting the first split of the data in a table. For more context, I am working with PayPal payout data where there are multiple payouts a day based on when orders were placed. For example, orders placed on Friday, Saturday, and Sunday are paid out on Monday but in separate deposits.

Based on this, I feel like the problem is with using dash_table.DataTable in a loop. Has anyone done something like this before? TYIA!

@app.callback(
	Output('paypal_tables', 'children'),
  	[Input("date_picker", "date")]
)
def update_paypal_tables(payout_date):
    if payout_date is not None:
        payout_date = pd.to_datetime(payout_date, format='%Y-%m-%d')
        paypal_data = grab_paypal_payout_data()
        filtered_data = paypal_data[paypal_data['Payout_Date'] == payout_date]
        filtered_data = filtered_data.drop(columns=['Payout_Date'])
        payouts_list = [d for _, d in filtered_data.groupby(['order_date'])]
        for df in payouts_list:
            df = df.sort_values('product_group')
            df.loc['total'] = df.select_dtypes(pd.np.number).sum().round(2)
            df = df.to_dict('records') 
            print(df) #this prints the correct data for each table
            return   dash_table.DataTable(
                id= 'paypal_payout_table'+str(df[0]),
                data = df,
#extra stuff
                persistence=True,
                row_deletable=False,
                selected_columns=[],  # ids of columns that user selects
                selected_rows=[],  # indices of rows that user selects
                page_action="native",  # all data is passed to the table up-front or not ('none')
                sort_action='native',
                fill_width=False,
                style_as_list_view=True,
                style_cell={'padding': '4px', 'font-family': 'sans-serif'},
                style_header={
                    'backgroundColor': 'white',
                    'fontWeight': 'bold'
                },
                style_data={
                    'whiteSpace': 'normal',
                    'height': 'auto',
                },
                css=[{"selector": ".show-hide", "rule": "display: none"}]
            )

Hello @kelly_gfc,

It looks like your return is on the wrong line.

Are you trying to print out once table per loop? If so, you’ll need to make a list of tables and return the list. :slight_smile:

2 Likes

Got it. Thank you!!

1 Like