Dash Datatable- How to automatically fill cell based on data in another cell?

Hello, everyone.
So I tried to create a table that can be edited by the user. This is what the table looks like:

What I want to do is to fill the Enrollment Fees column based on user input. There are certain condition, for example if the Group = Children and Level = 1, the Enrollment Fees will be 150,000. Here is what my code looks like for now:

from dash import Dash, dash_table, html
import pandas as pd
from collections import OrderedDict
import numpy as np
from dash.dash_table.Format import Format
app = Dash(__name__)

datelist = pd.date_range(start="2022-09-01", periods=100,).date.tolist()


df=pd.DataFrame(OrderedDict([
                    ('Name',[ ]),
                    ('Group',[ ]),
                    ('Level',[ ]),
                    ('Enrollment Fees',[ ]),
                    ('Status',[ ])
                ]))
df['Name'] = ['Anthony', 'Bernard', 'Chester', 'Dylan', 'Elon', 'Floyd', 'Gloria', 'Hendry', 'Isaac']
df['Group'] = ['Children','Children','Children', 'Teenager','Teenager','Teenager', 'Adult', 'Adult', 'Adult']
df['Level'] = ['1','1','1', '2', '2', '2', '3', '3', '3']

conditions =[
        (df['Group'] == 'Children') & (df['Level']=='1'),
        (df['Group'] == 'Children') & (df['Level'] == '2'),
        (df['Group'] == 'Children') & (df['Level'] == '3'),
        (df['Group'] == 'Teenager') & (df['Level'] == '1'),
        (df['Group'] == 'Teenager') & (df['Level'] == '2'),
        (df['Group'] == 'Teenager') & (df['Level'] == '3'),
        (df['Group'] == 'Adult') & (df['Level'] == '1'),
        (df['Group'] == 'Adult') & (df['Level'] == '2'),
        (df['Group'] == 'Adult') & (df['Level'] == '3'),
]
choices = [150000, 200000, 250000, 225000, 275000, 350000, 300000, 400000, 500000]

df['Enrollment Fees'] = np.select(conditions, choices)


df['Status'] = ['Paid','Paid','Paid', 'Not Done', 'Not Done', 'Not Done', 'Cancelled', 'Cancelled', 'Cancelled']

app.layout = html.Div([
    dash_table.DataTable(
        id='daftar-baru',
        data=df.to_dict('records'),
        columns=[
            {'id':'Name', 'name':'Name'},
            {'id':'Group', 'name':'Group', 'presentation':'dropdown'},
            {'id': 'Level', 'name': 'Level', 'presentation': 'dropdown'},
            {'id': 'Enrollment Fees', 'name': 'Enrollment Fees','type': 'numeric', 'format': Format(group=',')},
            {'id': 'Status', 'name': 'Status', 'presentation': 'dropdown'},
        ],
        editable=True,
        dropdown={
            'Group': {
                'options': [
                    {'label': i, 'value': i}
                    for i in df['Group'].unique()
                ]
            },
            'Level': {
                'options': [
                    {'label': i, 'value': i}
                    for i in df['Level'].unique()
                ]
            },
            'Status': {
                'options': [
                    {'label': i, 'value': i}
                    for i in df['Status'].unique()
                ]
            }
        }
    ),
    html.Div(id='daftar-baru-container')
])



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

Any help will be greatly appreciated :slight_smile:

hi @Susanto
That’s a good question. Thank you for providing a minimal reproducible example (MRE). You can accomplish what you want by adding a callback at the very end. I built this callback based on a similar example on updating columns in the datatable.
Don’t forget to from dash import Dash, dash_table, html, callback, Output, Input

@callback(
    Output('daftar-baru', 'data'),
    Input('daftar-baru', 'data')
)
def update_table(rows):
    for row in rows:
        if row['Group'] =='Children' and row['Level']=='1':
            row['Enrollment Fees'] = 125000
    return rows
1 Like

@adamschroeder Thank you so much Adam! the code is completely working as expected now.