Issue with button and slider combination

Hello,
There seem to be a problem or bug when I use a button and slider as a combination.

import streamlit as st

# button/slider mostly works if rpm = 1
rpm = 1
# button/slider doesn't work at all if rpm = 0
#rpm = 0

if st.button('Stop motor'):
    rpm = 0
rpm = st.slider('RPM', 0, 3000, rpm)

st.write(f'Motor speed is {rpm} rpm')

If I assign 0 to rpm (line4) the “Stop motor” button doesn’t work at all, but if I assign any other value to rpm, like 1 for example, the button seems to work.
I need to set the slider to 0 with the button

Thanks,
Rhys

Hey @rhyshorner,

First, welcome to the Streamlit Community!!! :tada: :tada: :tada: :partying_face: :partying_face: :smiling_face_with_three_hearts:

I’m not sure I understand your issue here, when I use the code snippet you provided it seems to work exactly as you asked.

I can set the rpm to some value (say 300) and when I press the button and write it out to the screen it will reset rpm to 0 and then the slider will be set to 0.

before button press:
Screen Shot 2021-05-20 at 11.42.12 AM

after:
Screen Shot 2021-05-20 at 11.42.27 AM

can you do a screen record or screenshots of this code where it’s not working for you?

Happy Streamlit-ing!
Marisa

Hello,
Thanks for such a quick response, perhaps I have some setup issue or a version thing maybe. I am still getting the same thing.
I’m running “Streamlit, version 0.82.0” and running a cmd terminal through vscode and displaying in a google chrome browser, py --version returns “Python 3.9.5”.
Maybe the way my code is set out isn’t right for the way streamlit works perhaps.

If I set the rpm variable initially to 0 this is the behaviour I get;
Code;

import streamlit as st

rpm = 0
st.write(f"initial rpm value: {rpm}")

if st.button('Stop motor'):
    rpm = 0
rpm = st.slider('RPM', 0, 3000, rpm)

st.write(f'Motor speed is {rpm} rpm')

after moving slider;

after clicking “Stop Motor” button;

But if I set the rpm variable to anything above 0 this is the behavior I get;
code;

import streamlit as st

rpm = 1
st.write(f"initial rpm value: {rpm}")

if st.button('Stop motor'):
    rpm = 0
rpm = st.slider('RPM', 0, 3000, rpm)

st.write(f'Motor speed is {rpm} rpm')

after moving slider;

after clicking button;

Thanks,
Rhys

@rhyshorner

:open_mouth: oooooo I see now! if rpm is set to 0, the slider doesn’t use this to set the slider value if you have moved the slider and tried to reset it with the button. But if you set rpm to any other value, when the slider renders it will re-set itself to have the value 0 if you press the button.

OK let me check on a few things here. (p.s. I am able to recreate this here I am also using streamlit 0.82)

Happy Streamlit-ing!
Marisa