Slider display multiple options

Summary

The issue i have is with the slider (double slider) functionality. I would like to have it display all the options within it’s range, instead of the option that is selected. For eg,

color = st.select_slider(
    'Select a color of the rainbow',
    options=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
st.write('My favorite color is', color)

Expected behavior:

When i run this run this code, it prints ‘My favourite color is…’. It gives a different color depending on the color where the slider mark pointer is. I would like it to give the range of colors instead. For example, if the slider pointer is at green, it should say ‘my favourite color is red, orange, yellow and green.’

This is because the actual code only prints ‘my favourite color is green’ if green is where the slider pointer is and i want to change that.

Depending of what you pass as the value parameter of st.select_slider, you get a single element selection or a range. From https://docs.streamlit.io/library/api-reference/widgets/st.select_slider:

value (a supported type or a tuple/list of supported types or None)
The value of the slider when it first renders. If a tuple/list of two values is passed here, then a range slider with those lower and upper bounds is rendered. For example, if set to (1, 10) the slider will have a selectable range between 1 and 10. Defaults to first option.

When passing the tuple, you only get back the lower and upper bounds, not all the elements in between. For that, you’ll need some additional trick.

🖱️ Example single value:

import streamlit as st
list_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

single_color = st.select_slider(
    'Select a color of the rainbow',
    options= list_colors,
    value='orange')

st.write('My favorite color is', single_color)

🖱️ Example range limits:

import streamlit as st
list_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

two_colors = st.select_slider(
    'Select a color of the rainbow',
    options= list_colors,
    value=('orange','blue'))

st.write('My favorite color is', two_colors)

🖱️ Example all elements within range:

import streamlit as st
list_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

getColor = lambda i: list_colors[i]

color_range = st.select_slider(
    'Select a color of the rainbow',
    options= range(len(list_colors)),
    value=(1,4),
    format_func=getColor)

st.write('My favorite color is', ", ".join(list_colors[color_range[0]:color_range[1]+1]))

Thank you @edsaac

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.