- Spec files
pypi-streamlit
Describes: pkg:pypi/streamlit@1.16.x
- Description
- The fastest way to build and share data apps
- Author
- tessl
- Last updated
input-widgets.md docs/
1# Input Widgets23Interactive form controls for user input. These widgets enable user interaction by capturing clicks, text input, selections, and file uploads. All widgets return values that can be used to control application flow and data processing.45## Capabilities67### Buttons and Actions89Trigger actions and downloads through button interactions.1011```python { .api }12def button(label: str, key: str = None, help: str = None, on_click=None, args=None, kwargs=None, type: str = "secondary", disabled: bool = False, use_container_width: bool = False) -> bool:13"""14Display a button widget that returns True when clicked.1516Parameters:17- label: Text displayed on the button18- key: Unique key for the widget19- help: Tooltip text shown on hover20- on_click: Callback function executed on click21- args: Arguments passed to callback function22- kwargs: Keyword arguments passed to callback function23- type: Button type ("primary" or "secondary")24- disabled: Whether the button is disabled25- use_container_width: Whether to use full container width2627Returns:28bool: True if button was clicked in this run, False otherwise29"""3031def download_button(label: str, data, file_name: str = None, mime: str = None, key: str = None, help: str = None, on_click=None, args=None, kwargs=None, type: str = "secondary", disabled: bool = False, use_container_width: bool = False) -> bool:32"""33Display a download button that triggers file download.3435Parameters:36- label: Text displayed on the button37- data: Raw data, string, or bytes to download38- file_name: Name for the downloaded file39- mime: MIME type of the file40- key: Unique key for the widget41- help: Tooltip text shown on hover42- on_click: Callback function executed on click43- args: Arguments passed to callback function44- kwargs: Keyword arguments passed to callback function45- type: Button type ("primary" or "secondary")46- disabled: Whether the button is disabled47- use_container_width: Whether to use full container width4849Returns:50bool: True if button was clicked in this run, False otherwise51"""52```5354#### Usage Example5556```python57import streamlit as st5859# Simple button60if st.button("Click me!"):61st.write("Button was clicked!")6263# Download button64csv_data = "Name,Age\nAlice,25\nBob,30"65st.download_button(66label="Download CSV",67data=csv_data,68file_name="data.csv",69mime="text/csv"70)71```7273### Boolean Input7475Capture boolean (true/false) user selections.7677```python { .api }78def checkbox(label: str, value: bool = False, key: str = None, help: str = None, on_change=None, args=None, kwargs=None) -> bool:79"""80Display a checkbox widget.8182Parameters:83- label: Text displayed next to the checkbox84- value: Initial checked state85- key: Unique key for the widget86- help: Tooltip text shown on hover87- on_change: Callback function executed when value changes88- args: Arguments passed to callback function89- kwargs: Keyword arguments passed to callback function9091Returns:92bool: Current checked state of the checkbox93"""94```9596#### Usage Example9798```python99import streamlit as st100101# Checkbox for agreement102agree = st.checkbox("I agree to the terms and conditions")103104if agree:105st.write("Thank you for agreeing!")106```107108### Selection Widgets109110Capture single or multiple selections from predefined options.111112```python { .api }113def radio(label: str, options, index: int = 0, key: str = None, help: str = None, on_change=None, args=None, kwargs=None, horizontal: bool = False):114"""115Display radio button selection widget.116117Parameters:118- label: Text displayed above the radio buttons119- options: List of options to choose from120- index: Index of the initially selected option121- key: Unique key for the widget122- help: Tooltip text shown on hover123- on_change: Callback function executed when selection changes124- args: Arguments passed to callback function125- kwargs: Keyword arguments passed to callback function126- horizontal: Whether to display options horizontally127128Returns:129The selected option value130"""131132def selectbox(label: str, options, index: int = 0, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):133"""134Display a dropdown selectbox widget.135136Parameters:137- label: Text displayed above the selectbox138- options: List of options to choose from139- index: Index of the initially selected option140- key: Unique key for the widget141- help: Tooltip text shown on hover142- on_change: Callback function executed when selection changes143- args: Arguments passed to callback function144- kwargs: Keyword arguments passed to callback function145146Returns:147The selected option value148"""149150def multiselect(label: str, options, default=None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):151"""152Display a multiselect widget for choosing multiple options.153154Parameters:155- label: Text displayed above the multiselect156- options: List of options to choose from157- default: List of initially selected options158- key: Unique key for the widget159- help: Tooltip text shown on hover160- on_change: Callback function executed when selection changes161- args: Arguments passed to callback function162- kwargs: Keyword arguments passed to callback function163164Returns:165List of selected option values166"""167```168169#### Usage Example170171```python172import streamlit as st173174# Radio buttons175choice = st.radio(176"Choose your favorite color:",177["Red", "Green", "Blue"]178)179180# Selectbox181city = st.selectbox(182"Select your city:",183["New York", "London", "Tokyo", "Sydney"]184)185186# Multiselect187interests = st.multiselect(188"Select your interests:",189["Sports", "Music", "Movies", "Reading", "Travel"],190default=["Music"]191)192193st.write(f"You chose {choice}, live in {city}, and are interested in {interests}")194```195196### Slider Inputs197198Capture numeric values through intuitive slider interfaces.199200```python { .api }201def slider(label: str, min_value=None, max_value=None, value=None, step=None, format: str = None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):202"""203Display a slider widget for numeric input.204205Parameters:206- label: Text displayed above the slider207- min_value: Minimum value allowed208- max_value: Maximum value allowed209- value: Initial value (or tuple for range slider)210- step: Step size for value increments211- format: Printf-style format string for display212- key: Unique key for the widget213- help: Tooltip text shown on hover214- on_change: Callback function executed when value changes215- args: Arguments passed to callback function216- kwargs: Keyword arguments passed to callback function217218Returns:219The selected numeric value or tuple for range slider220"""221222def select_slider(label: str, options, value=None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):223"""224Display a slider widget for selecting from predefined options.225226Parameters:227- label: Text displayed above the slider228- options: List of options to choose from229- value: Initial selected value230- key: Unique key for the widget231- help: Tooltip text shown on hover232- on_change: Callback function executed when value changes233- args: Arguments passed to callback function234- kwargs: Keyword arguments passed to callback function235236Returns:237The selected option value238"""239```240241#### Usage Example242243```python244import streamlit as st245246# Numeric slider247age = st.slider("Select your age:", 0, 100, 25)248249# Range slider250price_range = st.slider(251"Select price range:",2520.0, 100.0, (20.0, 80.0)253)254255# Select slider with custom options256size = st.select_slider(257"Select size:",258options=["XS", "S", "M", "L", "XL"],259value="M"260)261262st.write(f"Age: {age}, Price range: ${price_range[0]}-${price_range[1]}, Size: {size}")263```264265### Text Input266267Capture text and numeric input from users.268269```python { .api }270def text_input(label: str, value: str = "", max_chars: int = None, key: str = None, type: str = "default", help: str = None, autocomplete: str = None, on_change=None, args=None, kwargs=None, placeholder: str = None, disabled: bool = False) -> str:271"""272Display a single-line text input widget.273274Parameters:275- label: Text displayed above the input276- value: Initial text value277- max_chars: Maximum number of characters allowed278- key: Unique key for the widget279- type: Input type ("default" or "password")280- help: Tooltip text shown on hover281- autocomplete: Autocomplete attribute value282- on_change: Callback function executed when value changes283- args: Arguments passed to callback function284- kwargs: Keyword arguments passed to callback function285- placeholder: Placeholder text shown when empty286- disabled: Whether the input is disabled287288Returns:289str: Current text value290"""291292def text_area(label: str, value: str = "", height: int = None, max_chars: int = None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None, placeholder: str = None) -> str:293"""294Display a multi-line text input widget.295296Parameters:297- label: Text displayed above the text area298- value: Initial text value299- height: Height of the text area in pixels300- max_chars: Maximum number of characters allowed301- key: Unique key for the widget302- help: Tooltip text shown on hover303- on_change: Callback function executed when value changes304- args: Arguments passed to callback function305- kwargs: Keyword arguments passed to callback function306- placeholder: Placeholder text shown when empty307308Returns:309str: Current text value310"""311312def number_input(label: str, min_value=None, max_value=None, value=None, step=None, format: str = None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None, placeholder: str = None):313"""314Display a numeric input widget.315316Parameters:317- label: Text displayed above the input318- min_value: Minimum value allowed319- max_value: Maximum value allowed320- value: Initial numeric value321- step: Step size for value increments322- format: Printf-style format string for display323- key: Unique key for the widget324- help: Tooltip text shown on hover325- on_change: Callback function executed when value changes326- args: Arguments passed to callback function327- kwargs: Keyword arguments passed to callback function328- placeholder: Placeholder text shown when empty329330Returns:331int or float: Current numeric value332"""333```334335#### Usage Example336337```python338import streamlit as st339340# Text inputs341name = st.text_input("Enter your name:", placeholder="Your full name")342password = st.text_input("Enter password:", type="password")343344# Text area345feedback = st.text_area(346"Leave feedback:",347height=100,348placeholder="Tell us what you think..."349)350351# Number input352quantity = st.number_input(353"Quantity:",354min_value=1,355max_value=100,356value=1,357step=1358)359360st.write(f"Hello {name}! You ordered {quantity} items.")361```362363### Date and Time Input364365Capture date and time values from users.366367```python { .api }368def date_input(label: str, value=None, min_value=None, max_value=None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):369"""370Display a date input widget.371372Parameters:373- label: Text displayed above the date picker374- value: Initial date value (datetime.date object or None)375- min_value: Minimum date allowed376- max_value: Maximum date allowed377- key: Unique key for the widget378- help: Tooltip text shown on hover379- on_change: Callback function executed when value changes380- args: Arguments passed to callback function381- kwargs: Keyword arguments passed to callback function382383Returns:384datetime.date: Selected date385"""386387def time_input(label: str, value=None, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):388"""389Display a time input widget.390391Parameters:392- label: Text displayed above the time picker393- value: Initial time value (datetime.time object or None)394- key: Unique key for the widget395- help: Tooltip text shown on hover396- on_change: Callback function executed when value changes397- args: Arguments passed to callback function398- kwargs: Keyword arguments passed to callback function399400Returns:401datetime.time: Selected time402"""403```404405#### Usage Example406407```python408import streamlit as st409from datetime import datetime, date, time410411# Date input412selected_date = st.date_input(413"Select a date:",414value=date.today()415)416417# Time input418selected_time = st.time_input(419"Select a time:",420value=time(9, 0)421)422423st.write(f"You selected {selected_date} at {selected_time}")424```425426### File and Media Input427428Handle file uploads and camera input for multimedia content.429430```python { .api }431def file_uploader(label: str, type=None, accept_multiple_files: bool = False, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):432"""433Display a file uploader widget.434435Parameters:436- label: Text displayed above the file uploader437- type: List of allowed file extensions (e.g., ["csv", "txt"])438- accept_multiple_files: Whether to allow multiple file selection439- key: Unique key for the widget440- help: Tooltip text shown on hover441- on_change: Callback function executed when files change442- args: Arguments passed to callback function443- kwargs: Keyword arguments passed to callback function444445Returns:446UploadedFile or List[UploadedFile] or None: Uploaded file(s)447"""448449def camera_input(label: str, key: str = None, help: str = None, on_change=None, args=None, kwargs=None):450"""451Display a camera input widget for taking photos.452453Parameters:454- label: Text displayed above the camera input455- key: Unique key for the widget456- help: Tooltip text shown on hover457- on_change: Callback function executed when photo is taken458- args: Arguments passed to callback function459- kwargs: Keyword arguments passed to callback function460461Returns:462UploadedFile or None: Captured photo file463"""464```465466#### Usage Example467468```python469import streamlit as st470import pandas as pd471472# File uploader473uploaded_file = st.file_uploader("Choose a CSV file", type="csv")474475if uploaded_file is not None:476df = pd.read_csv(uploaded_file)477st.write("File contents:")478st.dataframe(df)479480# Multiple file upload481uploaded_files = st.file_uploader(482"Choose multiple images",483type=["png", "jpg", "jpeg"],484accept_multiple_files=True485)486487for uploaded_file in uploaded_files:488st.image(uploaded_file)489490# Camera input491camera_photo = st.camera_input("Take a photo")492493if camera_photo is not None:494st.image(camera_photo)495```496497### Color Input498499Capture color selections from users.500501```python { .api }502def color_picker(label: str, value: str = "#000000", key: str = None, help: str = None, on_change=None, args=None, kwargs=None) -> str:503"""504Display a color picker widget.505506Parameters:507- label: Text displayed above the color picker508- value: Initial color value in hex format509- key: Unique key for the widget510- help: Tooltip text shown on hover511- on_change: Callback function executed when color changes512- args: Arguments passed to callback function513- kwargs: Keyword arguments passed to callback function514515Returns:516str: Selected color in hex format (e.g., "#FF0000")517"""518```519520#### Usage Example521522```python523import streamlit as st524525# Color picker526color = st.color_picker("Pick a color:", "#FF0000")527528st.write(f"Selected color: {color}")529530# Use the color in styling531st.markdown(532f'<div style="background-color: {color}; padding: 10px; border-radius: 5px;">'533f'This div has the selected background color!'534f'</div>',535unsafe_allow_html=True536)537```538539## Widget State and Keys540541All widgets support a `key` parameter for unique identification and state management:542543```python544# Widget with key for session state access545if st.button("Increment", key="increment_btn"):546if "counter" not in st.session_state:547st.session_state.counter = 0548st.session_state.counter += 1549550st.write(f"Counter: {st.session_state.get('counter', 0)}")551```