Specific callbacks for slider values?

hi everyone,
i’m trying to make it so that when i reach a specific value on my slider, a specific command is executed. this is the code i’ve gotten close to success with, but it still yields no results. does anyone have any advice on what i can do to make this work? thank you

app.layout = html.Div([
    dcc.Slider(0, 10,
        id='gain',
        step=1,
        value=0,
        marks={
            1: {'label': '1 nA', 'style': {'color':'#7EAE86'}},
            2: {'label': '10 nA', 'style': {'color':'#7EAE86'}},
            3: {'label': '100 nA', 'style': {'color':'#7EAE86'}},
            4: {'label': '1 µA', 'style': {'color':'#7E95AE'}},
            5: {'label': '10 µA', 'style': {'color':'#7E95AE'}},
            6: {'label': '100 µA', 'style': {'color':'#7E95AE'}},
            7: {'label': '1 mA', 'style': {'color':'#9B7EAE'}},
            8: {'label': '10 mA', 'style': {'color':'#9B7EAE'}},
            9: {'label': '50 mA', 'style': {'color':'#9B7EAE'}},
        }
        ),
        html.Div(id='slider')
])
@app.callback(
    Output('slider', 'children'),
    Input('gain', 'value'))
def update_output(value):
    if value == 1:
        g0 = inst.write('GAIN 0')
        return [g0, 'set to 0']
    elif value == 2:
        g1 = inst.write('GAIN 1')
        return g1
    elif value == 3:
        g2 = inst.write('GAIN 2')
        return g2
    elif value == 4:
        g3 = inst.write('GAIN 3')
        return g3
    elif value == 5:
        g4 = inst.write('GAIN 4')
        return g4
    elif value == 6:
        g5 = inst.write('GAIN 5')
        return g5
    elif value == 7:
        g6 = inst.write('GAIN 6')
        return g6
    elif value == 8:
        g7 = inst.write('GAIN 7')
        return g7
    elif value == 9:
        g8 = inst.write('GAIN 8')
        return g8

Hi,

Your code looks correct, apart from inst that is not defined in the snippet and it could be the problem. Could you share how inst is defined?

inst is how we communicate with the instrument normally, and works fine otherwise with other components. this specific slider is the issue…I think the problem lies in the if statements. are there any examples out there of specific slider values triggering a specific callback? thank you so much!

Ok, but what is the type of inst.write("GAIN 1")? Is it a string, a float or something else?

It could be that the type of g’s is not accepted as a children prop. In this case, you should see an error message in debug mode.

Apart from that, the if statements are correct. You could make it DRY by writing something like:

@app.callback(Output("slider", "children"), Input("gain", "value"))
def update_output(value):
    g = inst.write(f"GAIN {value - 1}")

    if value == 1:
        return [g, "set to 0"]
    else:
        return g

Hope this helps!