Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

How to validate the new row in a Dash datatable

Hi everyone,

I’m new to Dash and trying to validate the data in a new row of my Dash data table.
When the admin input a negative integer, the alert would pop out.

My current code is as below:


@app.callback(
    [Output('editable-input-table', 'children'),
    Output('validation-alert', 'children'),
    Output('validation-alert', 'is_open')],
    Input('editable-input-table', 'data_timestamp'),
    [State('editable-input-table', 'data')],
)
def validate_new_data(timestamp, data):
    for row in data:
        if not (type(row['index']) in {int, np.int64} and row['index'] > 0):
            # or row['index'] in set(table['index']): # don't understand the purpose of this line
            return [dash.no_update, '"index" must be a unique integer']

        elif not (type(row['centre']) == str):
            # or row['centre'] in set(table['centre']): # don't understand the purpose of this line
            return [dash.no_update, '"centre" must be a unique string']

        elif not (type(row['minBooths']) in {int, np.int64} and row['minBooths'] > 0):
            return [dash.no_update, '"minBooths" must be a non-negative integer']

        elif not (type(row['maxBooths']) in {int, np.int64} and row['maxBooths'] > row['minBooths']):
            return [dash.no_update, '"minBooths" must be an integer no less than "maxBooths"']

        elif not (type(row['maxDays']) in {int, np.int64} and row['maxDays'] >= 0 and row['maxDays'] <= 7):
            return [dash.no_update, '"maxDays" must be an integer between 0 and 7']

        elif not (type(row['vaxHours']) in {int, np.int64, float} and row['vaxHours'] >= 0 and row['vaxHours'] <= 24):
            return [dash.no_update, '"vaxHours" must be an integer or float between 0 and 24']

        elif not (type(row['workingHours']) in {int, np.int64, float} and row['workingHours'] >= 0 and row[
            'workingHours'] <= 24):
            return [dash.no_update, '"workingHours" must be an integer or float between 0 and 24']

        elif not (type(row['reqDose']) in {int, np.int64} and row['reqDose'] >= 0):
            return [dash.no_update, '"reqDose" must be a non-negative integer']

        elif not (type(row['vph']) in {int, np.int64, float, np.float64} and row['vph'] >= 0):
            return [dash.no_update, '"vph" must be a non-negative integer or float']

        elif not (type(row['catchment']) in {int, np.int64} and row['catchment'] >= 0):
            return [dash.no_update, '"catchment" must be a non-negative integer']

        elif not (type(row['enabled']) in ['true', 'false']):
            return [dash.no_update, '"enabled" must be a boolean value i.e. either "True" or "False"']

        elif not (type(row['flex']) in ['true', 'false']):
            return [dash.no_update, '"flex" must be a boolean value i.e. either "True" or "False"']

        elif not (type(row['lat']) in {int, np.int64, float, np.float64} and row['lat'] >= -90 and row['lat'] <= 90):
            return [dash.no_update, '"lat" must be an integer or float between -90 and 90']

        elif not (type(row['lon']) in {int, np.int64, float, np.float64} and row['lon'] >= -90 and row['lon'] <= 90):
            return [dash.no_update, '"lon" must be an integer or float between -180 and 180']

page = html.Div([
        dbc.Row([
            html.Div(logo, style={'padding': 40}),
            dbc.Row([
                dbc.Row(html.Div(editable_input)),
                dbc.Row(html.Div(new_row)),
                dbc.Alert(id='validation-alert',
                          color="danger",
                          dismissable=True
                          ),
                dbc.Row(html.Br()),
            ]),
        ]),

Thank you for reading!

Hi,

Welcome to the community!

Your third output should be a boolean (is_open=True) that triggers the alert, but all your returned arrays have only two elements.

1 Like

Thank you, I forgot to add is_open=True.