Hello, i am new in plotly and dash.
I wanted to implement a start and stop mechanism by one button.
As first step, I wanted that, o page load the button will show Start and after clicking it will show Stop
import dash
from dash.dependencies import Input, Output
import dash_html_components as html
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Button(children='Start', id='btn1', n_clicks=0)
])
@app.callback(Output('btn1', 'children'),
Input('btn1', 'children'))
def displayClick(cld):
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'btn1' in changed_id:
if cld=='Start':
return 'Stop'
else:
return 'Start'
if __name__ == '__main__':
app.run_server(debug=True)
It is not working even the initial āStartā not showing.
Iām not saying that you canāt use children, but if you want to fire the callback every time the user clicks the button, then you need to put n_clicks as input to see if itās changing.
Dash has a restriction that you canāt use the same component as Input an also as an Output in the same callback, (but now exists some other ways to do that).
But you can do the same using the property as State instead of as Input, it allows you to use it as a variable in your callback.
For your case, this code must work: