I am trying to disable button when Click untill to finish a process then enable again like the below process:
- user press button
- button gets disabled
- script starts to run
- script ends to run
- button is enabled
- 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…