import streamlit as st
= st.text_input("Enter your name")
name
f"Hello {name}! Welcome to Streamlit.") st.write(
14 An Overview of Available Input Types
There are a large number of different input types available in Streamlit.
In streamlit, if we save the output of one of the built-in input widgets to a variable, this variable will be the value the user has selected, and will be used anywhere you subsequently reference this value in the app.
Let’s start by having a text input to greet the user.
Try changing the value in the input box each time, pressing enter when you have finished typing. Notice how the sentence underneath the text box updates.
14.1 Using inputs in calculations
In addition to text, we often want to be able to get numeric inputs from users.
Number boxes and sliders are two good ways to do this.
The benefit of this is that the resulting output can be guaranteed to be a number - unlike using a text input, where the user could choose to enter a non-numeric value which may break later steps of your app.
Let’s take a look at how we can use these two kinds of inputs.
All of the input types given below can take a huge range of additional parameters to further refine their behaviour.
For example, many can take default values, have an optional tooltip that appears on hover, or have the way in which their values appear adjusted.
The maximum and minimum allowed values can be set, and the input can be disabled (e.g. in response to another input).
It’s highly recommended that you take a look at the documentation to understand the full range of options available.
14.1.1 Numeric Input
import streamlit as st
"Numeric Input")
st.subheader(
= st.number_input(
chosen_number "Pick a Number"
)
f"The number you have chosen is {chosen_number}")
st.write(
= chosen_number * 5
chosen_number_multiplied_by_5
f"Your number multiplied by 5 is {chosen_number_multiplied_by_5}")
st.write(
f"Your number plus 7 is {chosen_number + 7}") st.write(
14.1.2 Numeric Slider
"Numeric Slider")
st.subheader(
= st.slider(
chosen_number_slider "Pick a Number on the slider",
=0,
min_value=250,
max_value=50
value
)
f"The number you have chosen is {chosen_number_slider}")
st.write(
= chosen_number * 8
chosen_number_slider_multiplied_by_8
f"Your number multiplied by 8 is {chosen_number_slider_multiplied_by_8}")
st.write(
f"Your number plus 3 is {chosen_number_slider + 3}") st.write(
14.1.3 Interactive Example
14.2 Other numeric, date and time inputs
Let’s have a very quick look at some of the other inputs available to us within Streamlit.
The desired type of the output will be inferred from the default value you pass into the slider.
All of the input types given below can take a huge range of additional parameters to further refine their behaviour.
For example, many can take default values, have an optional tooltip that appears on hover, or have the way in which their values appear adjusted.
The maximum and minimum allowed values for numeric, date and time sliders can be set, and the input can be disabled (e.g. in response to another input).
It’s highly recommended that you take a look at the documentation to understand the full range of options available.
14.2.1 Range Slider
= st.slider(
lower_value, upper_value "Pick a lower and upper limit",
=0,
min_value=100,
max_value=(35, 80)
value )
14.2.2 Time Slider
= st.slider(
chosen_time "Select a time:",
11, 30)
time( )
14.2.3 Date Slider
= st.slider(
selected_date "Select a date",
=datetime(2022, 1, 1),
valueformat="DD/MM/YYYY",
)
14.2.4 Date Range Slider
= st.slider(
start_date, end_date "Select a date",
=(datetime(2022, 1, 1), datetime(2023, 6, 1)),
valueformat="DD/MM/YYYY"
)
14.2.5 Time Range Slider
= st.slider(
start_time, end_time "Select a time:",
=(time(11, 30), time(12, 45))
value )
14.2.6 Datetime Slider
= st.slider(
chosen_datetime "Select a date and time",
=datetime(2022, 1, 1, 12, 0),
valueformat="DD/MM/YYYY @ hh:mm",
)
14.2.7 Date Calendar Picker
= st.date_input(
selected_date "Choose a date on the calendar picker"
)
14.2.8 Time Calendar Picker
= st.time_input(
selected_time "Select a time"
)
14.2.9 Interactive Examples
14.3 Text Inputs
All of the input types given below can take a huge range of additional parameters to further refine their behaviour.
For example, many can take default values, have an optional tooltip that appears on hover, have a placeholder appear to further guide the user’s input, or have a maximum length set.
It’s highly recommended that you take a look at the documentation to understand the full range of options available.
14.3.1 Long text
= st.text_area(
longer_text_input "Use this input to enter a larger piece of text"
)
14.3.2 Short text
= st.text_input(
shorter_text_input "We saw this before - this is an input for a short bit of text"
)
14.3.3 Interactive Examples
14.4 Selection inputs
Selection inputs are a very useful class of inputs. They allow you to give users a distinct number of options to choose from.
All of the input types given below can take a huge range of additional parameters to further refine their behaviour.
For example, you can adjust which default value is selected, have an optional tooltip that appears on hover, or change the number of allowed options in some cases.
It’s highly recommended that you take a look at the documentation to understand the full range of options available.
14.4.1 Radio
= st.radio(
radio_colour_select "Which of these colours is your favourite?",
=["Green", "Blue", "Red", "Yellow", "Purple"]
options )
14.4.2 Selectbox
= st.selectbox(
selectbox_colour_select "Which of these colours is your favourite?",
=["Green", "Blue", "Red", "Yellow", "Purple"]
options )
14.4.3 Multiselect
= st.multiselect(
multiselect_colour_select "Which of these colours are your favourite? You can pick more than one!",
=["Green", "Blue", "Red", "Yellow", "Purple"]
options )
14.4.4 Select Slider
= st.select_slider(
slider_colour_select "Which of these colours is your favourite?",
=["Green", "Blue", "Red", "Yellow", "Purple"]
options )
14.4.5 Interactive Examples
14.5 Other useful inputs - Boolean (True/False) Flags
The final inputs we’ll take a quick look at are checkbox and toggle inputs.
These are useful as they return boolean values - True
or False
.
While the input types below don’t have quite as many additional parameters as some of the others, it’s highly recommended that you take a look at the documentation to understand the full range of options available.
14.5.1 Checkbox
= st.checkbox("Tick or untick me!") checkbox_value
14.5.2 Toggle
= st.toggle("Tick or untick me!") toggle_value
14.5.3 Interactive Examples
14.6 Additional Input Types Not Covered Here
You can always refer to the excellent Streamlit documentation to see what other input widgets are available.
New official ones are sometimes added, and there are additional ones made by the community that become available over time.
14.7 Next steps with inputs
In the rest of this section, we’ll look at some of the parameters for inputs that will allow you to refine what your users will be allowed to enter, improving the usability of your app (and reducing the number of edge cases you need to account for in your programming!).