✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Radio Button Label update using Button

I have a callbacks function that displays a datatable based on calculations from a database and the user-inputs. The app contains multiple dcc.Input and dcc.RadioItems which are fed into this callback.

We want to create separate buttons that act as scenario creators and fill certain values into the Input and RadioItems functions.

I am able to change the value attribute of all of them but the change in value isn’t reflected in the label of the Radio Button. Code is as below:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

app=dash.Dash()
app.title=‘Testing’

app.layout=html.Div([
html.Div(dcc.Input(id=‘Page-Input’, value=‘1’, type=‘text’,debounce=True),style={‘width’: ‘90%’, ‘display’: ‘inline-block’}),
dcc.RadioItems(
id=‘Page-Radio’,
options=[{‘label’: i, ‘value’: i} for i in [‘1’,‘2’,‘3’,‘4’]],
value=‘1’,
inputStyle={“margin-right”: “15px”, “margin-left”: “10px”}
),
html.Div(html.Button(id=‘Button-1’, n_clicks_timestamp=0, children=‘1’),style={‘padding’: 10}),
html.Div(html.Button(id=‘Button-2’, n_clicks_timestamp=0, children=‘2’),style={‘padding’: 10}),
html.Div(html.Button(id=‘Button-3’, n_clicks_timestamp=0, children=‘3’),style={‘padding’: 10}),
html.Div(html.Button(id=‘Button-4’, n_clicks_timestamp=0, children=‘4’),style={‘padding’: 10}),
html.Div(id=‘Page-Output’)
])

@app.callback(Output(‘Page-Output’,‘children’),
[Input(‘Page-Input’,‘value’),
Input(‘Page-Radio’,‘value’),])
def Testing(function_input,power):
Number=int(function_input)
Number_power=int(power)
return html.P('Number is: ‘+str(Number)+’ Power is: ‘+str(Number_power)+’ Answer is: '+str(Number**Number_power))

@app.callback([Output(‘Page-Input’,‘value’),
Output(‘Page-Radio’,‘value’),
Output(‘Page-Radio’,‘label’)],
[Input(‘Button-1’,‘n_clicks_timestamp’),
Input(‘Button-2’,‘n_clicks_timestamp’),
Input(‘Button-3’,‘n_clicks_timestamp’),
Input(‘Button-4’,‘n_clicks_timestamp’),
])
def Input_Editor(first,second,third,fourth):
scenarios=[first,second,third,fourth]
if first==0 and second==0 and third==0 and fourth==0 :
raise PreventUpdate
elif first==max(scenarios):
return 1,1,1
elif second==max(scenarios):
return 2,2,2
elif third==max(scenarios):
return 3,3,3
else:
return 4,4,4

1 Like

Has this problem been solved?