Selectbox and Slider

I am trying to have a drop-down menu using the selectbox and then print the first X rows of the dataframe, first starting with a default value and then adapting this value with a slider. This is an exemplary code:

selection = st.selectbox("Select Analysis:", ("", "Dataframe Analysis", "Describe"))
    if st.button("Submit"):
        if selection == "Dataframe Analysis":
            nhead = st.slider("How many rows?", 1, 100, 10)
            st.write(dataframe.head(nhead))

The problem is that at the beginning the dataframe is shown with the default number of rows, but as soon as I change the slider value, it disappears and I am reverted back to the select box and the submit button. I am sure I am missing something super simple, but I feel stuck at this problem.

Any help will be greatly appreciated.

Hello @gstoyanov,

Each time you interact with your app, by clicking a button or using the slider, the whole script reruns.
In your case, when you click the button, the app reruns and st.button() returns True for this current run only. By using your slider, you’ll make the app rerun again but this time the button will return False.

I would suggest to drop that button as it just forces the user to do one more click.

1 Like