3 Input Widgets Explained
Key Concepts
- Text Input: Allows users to enter text.
- Number Input: Allows users to enter numeric values.
- Checkbox: Allows users to select or deselect options.
- Selectbox: Allows users to choose from a list of options.
Text Input
The st.text_input widget is used to collect text input from users. This is useful for gathering names, addresses, or any other textual data.
import streamlit as st
user_name = st.text_input("Enter your name")
st.write(f"Hello, {user_name}!")
Number Input
The st.number_input widget allows users to input numeric values. This widget is ideal for collecting quantities, ages, or any numerical data.
import streamlit as st
age = st.number_input("Enter your age", min_value=0, max_value=120)
st.write(f"You are {age} years old.")
Checkbox
The st.checkbox widget provides a simple way for users to select or deselect options. This is useful for creating binary choices.
import streamlit as st
agree = st.checkbox("Do you agree?")
if agree:
st.write("You agreed!")
else:
st.write("You did not agree.")
Selectbox
The st.selectbox widget allows users to choose from a list of options. This is useful for creating dropdown menus or single-choice questions.
import streamlit as st
option = st.selectbox("Choose an option", ["Option 1", "Option 2", "Option 3"])
st.write(f"You selected: {option}")
Examples
Here are some examples to illustrate the concepts:
import streamlit as st
st.title("Input Widgets in Streamlit")
st.markdown("## Text Input")
user_name = st.text_input("Enter your name")
st.write(f"Hello, {user_name}!")
st.markdown("## Number Input")
age = st.number_input("Enter your age", min_value=0, max_value=120)
st.write(f"You are {age} years old.")
st.markdown("## Checkbox")
agree = st.checkbox("Do you agree?")
if agree:
st.write("You agreed!")
else:
st.write("You did not agree.")
st.markdown("## Selectbox")
option = st.selectbox("Choose an option", ["Option 1", "Option 2", "Option 3"])
st.write(f"You selected: {option}")
© 2024 Ahmed Baheeg Khorshid. All rights reserved.