Black Lives Matter. Please consider donating to Black Girls Code today.

TypeError: '>' not supported between instances of 'NoneType' and 'int'

I am trying to disable button when Click untill to finish a process then enable again like the below process:

  1. user press button
  2. button gets disabled
  3. script starts to run
  4. script ends to run
  5. button is enabled
  6. another element gets updated with script output

or I just to to perform dcc.loading on this case.

and here’s my code:

import dash
import dash_html_components as html
from dash.dependencies import Input, Output
from Missing_relation_All import my_DB_func


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Button('Create Missing Relation Script', id='button', disabled=False),
    # other element
    html.Div(id='other-element'),
    # trigger div
    html.Div(id='trigger', children=0, style=dict(display='none'))
])


@app.callback(
    Output('button', 'disabled'),
    [Input('button', 'n_clicks'),
     Input('trigger', 'children')])
def trigger_function(n_clicks, trigger):
    context = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
    context_value = dash.callback_context.triggered[0]['value']

    # if the button triggered the function
    if context == 'button':
        # if the function is triggered at app load, this will not disable the button
        # but if the function is triggered by clicking the button, this disables the button as expected
        if n_clicks > 0:
            return True
        else:
            return False
    # if the element function completed and triggered the function
    else:
        # if the function is triggered at app load, this will not disable the button
        # but if the function is triggered by the function finishing, this enables the button as expected
        return False


@app.callback(
    [Output('other-element', 'children'),
     Output('trigger', 'children')],
    [Input('button', 'n_clicks')])
def update_element(n_clicks):
    my_DB_func()
    return (
        'my element value',
        1  # update the trigger value
    )



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

So when I am trying to run the app I found this error as the below one:

TypeError: '>' not supported between instances of 'NoneType' and 'int'

File "C:\Users\haroo501\PycharmProjects\My_Check_Missing_Relation_Tool\newbuttontest.py", line 32, in trigger_function
if n_clicks > 0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'

Traceback (most recent call last):
  File "C:\Users\haroo501\PycharmProjects\My_Check_Missing_Relation_Tool\newbuttontest.py", line 32, in trigger_function
    if n_clicks > 0:
TypeError: '>' not supported between instances of 'NoneType' and 'int'

So what I need after click the button make it Freeze or disabled till:

Process finished with exit code -1

after finish the process or finish the script which is based on the def

I hope any one have Idea how to solve this…

n_clicks is initialized as None but your code assumes it’s always a number. I suggest you start your callback with something like:

if n_clicks is None:
    raise dash.exceptions.PreventUpdate

@ Damian coud you specify more related to my code…
I appretiate that…
Thanks for your ineterests

You have the following code:

if n_clicks > 0:
	return True
else:
	return False

But in the initial callback n_clicks is None, if you try and run this code in a regular Python Script:

n_clicks = None
if n_clicks > 0:
	return True
else:
	return False

You will get the same exception:

TypeError: '>' not supported between instances of 'NoneType' and 'int'

@Damian So what I understand I Should Change the call back as the below code:

@app.callback(
    [Output('other-element', 'children'),
     Output('trigger', 'children')],
    [Input('button', 'n_clicks')])
def update_element(n_clicks):
    if n_clicks is None:
        raise dash.exceptions.PreventUpdate
    return (
        my_DB_func()
    )

Right?

I solved this like this way when Changed this part:

        if n_clicks > 0:
            return True
        else:
            return False

to this:

        if n_clicks is None:
            raise dash.exceptions.PreventUpdate
        else:
            return True

and this part:

@app.callback(
    [Output('other-element', 'children'),
     Output('trigger', 'children')],
    [Input('button', 'n_clicks')])
def update_element(n_clicks):
    my_DB_func()
    return (
        'my element value',
        1  # update the trigger value
    )

to this:

def update_element(n_clicks):
    if n_clicks == True:
        my_DB_func()
    return (
        'my element value',
        1 # update the trigger value
    )

as the final code will be something like this:

import dash
import dash_html_components as html
from dash.dependencies import Input, Output
from Missing_relation_All import my_DB_func
from dash.exceptions import PreventUpdate

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Button('Create Missing Relation Script', id='button', disabled=False),
    # other element
    html.Div(id='other-element'),
    # trigger div
    html.Div(id='trigger', children=0, style=dict(display='none'))
])


@app.callback(
    Output('button', 'disabled'),
    [Input('button', 'n_clicks'),
     Input('trigger', 'children')])
def trigger_function(n_clicks, trigger):
    context = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
    context_value = dash.callback_context.triggered[0]['value']

    # if the button triggered the function
    if context == 'button':
        # if the function is triggered at app load, this will not disable the button
        # but if the function is triggered by clicking the button, this disables the button as expected
        if n_clicks is None:
            raise dash.exceptions.PreventUpdate
        else:
            return True
    # if the element function completed and triggered the function
    else:
        # if the function is triggered at app load, this will not disable the button
        # but if the function is triggered by the function finishing, this enables the button as expected
        return False


@app.callback(
    [Output('other-element','children'),
     Output('trigger','children')],
    [Input('button','n_clicks')])
def update_element(n_clicks):
    if n_clicks == True:
        my_DB_func()
    return (
        'my element value',
        1 # update the trigger value
    )


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