Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initialization of range slider with session state #3586

Closed
scott-trinkle opened this issue Jul 21, 2021 · 2 comments · Fixed by #3905
Closed

Allow initialization of range slider with session state #3586

scott-trinkle opened this issue Jul 21, 2021 · 2 comments · Fixed by #3905
Assignees
Labels
area:widgets feature:state priority:P1 status:confirmed Bug has been confirmed by the Streamlit team type:bug Something isn't working

Comments

@scott-trinkle
Copy link

Summary

I want to be able to create a slider and a checkbox where if the checkbox is unclicked, the slider functions as normal with some default value val1, but if the checkbox is clicked, the slider is locked in to a default value val2 and resets to this value with every rerun, independent of user action.

This functionality is possible with a standard slider using session state and key to initialize the slider value depending on the value of the checkbox, however, streamlit throws an error if you attempt to use session state to initialize a tuple of initial values for the slider (i.e. to create a range_slider).

Steps to reproduce

Code snippet:
Functionality for typical single-value slider (this works as expected):

import streamlit as st

val1 = 30
val2 = 70
low = 0
high = 100

use_val2 = st.checkbox('Val2?')

if use_val2:
    st.session_state.my_slider = val2
elif 'my_slider' not in st.session_state:
    st.session_state.my_slider = val1

slider = st.slider('My slider', min_value=low, max_value=high, key='my_slider')

This raises the error:

import streamlit as st

val1_0, val1_f = 30, 50
val2_0, val2_f = 70, 90
low = 0
high = 100

use_val2 = st.checkbox('Val2?')

if use_val2:
    st.session_state.my_slider = (val2_0, val2_f)
elif 'my_slider' not in st.session_state:
    st.session_state.my_slider = (val1_0, val1_f)

slider = st.slider('My range slider', min_value=low,
                   max_value=high, key='my_slider')

Expected behavior:

I would like the default slider range to be 30-50 and adjustable if use_val2 is checked, and 70-90 and fixed if use_val2 is not checked.

Actual behavior:
Raises TypeError: (30, 50) has type tuple, but expected one of: int, long, float

Is this a regression?

No

Debug info

  • Streamlit version: 0.84.0
  • Python version: 3.8.11
  • Using: pip
  • OS version: Windows 10
  • Browser version: Chrome
@scott-trinkle scott-trinkle added type:bug Something isn't working status:needs-triage Has not been triaged by the Streamlit team labels Jul 21, 2021
@scott-trinkle
Copy link
Author

I found a workaround for now by initializing values in the call to slider and manually setting them when use_val2 is checked.

import streamlit as st

val1_0, val1_f = 30, 50
val2_0, val2_f = 70, 90
low = 0
high = 100

use_val2 = st.checkbox('Val2?')
if use_val2:
    st.session_state.my_slider = (val2_0, val2_f)
slider = st.slider('My range slider', min_value=low,
                   max_value=high, value=(val1_0, val1_f), key='my_slider')

This throws a warning described at the bottom of this page...is there an easy way to disable this warning?

@vdonato vdonato removed the status:needs-triage Has not been triaged by the Streamlit team label Jul 22, 2021
@vdonato vdonato self-assigned this Jul 22, 2021
@vdonato
Copy link
Collaborator

vdonato commented Jul 22, 2021

Thanks for reporting this @scott-trinkle! It seems like st.slider's type validation is a bit too restrictive now that it's possible to set a widget's value via st.session_state. I'll try to get a fix out for this if I have some time to do so ​in the next few weeks.

This throws a warning described at the bottom of this page...is there an easy way to disable this warning?

There's unfortunately no easy way to disable the warning you're getting at the moment, but I agree that it's something we may want to add a config option for. Thankfully, I think the warning should only appear a single time until streamlit is completely restarted, so hopefully the warning will only be mildly annoying until then.

@kmcgrady kmcgrady added status:confirmed Bug has been confirmed by the Streamlit team priority:P1 labels Aug 4, 2021
@vdonato vdonato assigned AnOctopus and unassigned vdonato Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:widgets feature:state priority:P1 status:confirmed Bug has been confirmed by the Streamlit team type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants