<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Python GUIs - streamlit-foundation</title><link href="https://www.pythonguis.com/" rel="alternate"/><link href="https://www.pythonguis.com/feeds/streamlit-foundation.tag.atom.xml" rel="self"/><id>https://www.pythonguis.com/</id><updated>2024-09-01T21:05:00+00:00</updated><subtitle>Create GUI applications with Python and Qt</subtitle><entry><title>Streamlit Buttons — Making things happen with Streamlit buttons</title><link href="https://www.pythonguis.com/tutorials/streamlit-buttons/" rel="alternate"/><published>2024-09-01T21:05:00+00:00</published><updated>2024-09-01T21:05:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2024-09-01:/tutorials/streamlit-buttons/</id><summary type="html">Streamlit is a popular choice for creating interactive web applications in Python. With its simple syntax and intuitive interface, developers can quickly create visually appealing dashboards.</summary><content type="html">&lt;p&gt;Streamlit is a popular choice for creating interactive web applications in Python. With its simple syntax and intuitive interface, developers can quickly create visually appealing dashboards.&lt;/p&gt;
&lt;p&gt;One of the great things about Streamlit is its ability to easily handle user interaction, and dynamically update the UI in response. One of the main way for users to trigger actions in UIs is through the use of buttons. In Streamlit, the &lt;code&gt;st.button()&lt;/code&gt; method creates a button that users can click to perform an action. Each button can be associated with a different action.&lt;/p&gt;
&lt;p&gt;In this tutorial we'll look at how you can use buttons to add interactivity to your Streamlit apps.&lt;/p&gt;
&lt;h2 id="creating-buttons-in-streamlit"&gt;Creating Buttons in Streamlit&lt;/h2&gt;
&lt;p&gt;To create a button in Streamlit, you use the &lt;code&gt;st.button()&lt;/code&gt; function, which takes an optional label as an argument. When the button is clicked, it returns &lt;code&gt;True&lt;/code&gt;, which you can use to control subsequent actions.&lt;/p&gt;
&lt;h3&gt;Basic Button Syntax&lt;/h3&gt;
&lt;p&gt;Here's a simple example of a button in Streamlit:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st

if st.button('Click Me'):
    st.write("Button clicked!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Simple Streamlit app with a single button" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-buttons/streamlit-button.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button.png?tr=w-600 600w" loading="lazy" width="1372" height="786"/&gt;
&lt;em&gt;Simple Streamlit app with a single button&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;st.button('Click Me')&lt;/code&gt; creates a button labeled &lt;em&gt;Click Me&lt;/em&gt;. When the button is clicked, it returns &lt;code&gt;True&lt;/code&gt; and the &lt;code&gt;if&lt;/code&gt; evaluates to &lt;em&gt;true&lt;/em&gt; running the nested code underneath -- displaying the message "Button clicked!"&lt;/p&gt;
&lt;p&gt;This basic structure is the foundation of working with buttons in Streamlit. Through this simple mechanism you can build quite complex interactivity.&lt;/p&gt;
&lt;h2 id="multiple-buttons-for-different-actions"&gt;Multiple Buttons for Different Actions&lt;/h2&gt;
&lt;p&gt;Building on the basic button structure, you can create multiple buttons within your Streamlit app, each associated with different actions. For instance, let's create buttons that display different messages based on which is clicked.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st

if st.button('Show Greeting'):
    st.write("Hello, welcome to the app!")

if st.button('Show Goodbye'):
    st.write("Goodbye! See you soon.")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Simple Streamlit app with two buttons" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-buttons/streamlit-two-buttons.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-two-buttons.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-two-buttons.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-two-buttons.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-two-buttons.png?tr=w-600 600w" loading="lazy" width="1325" height="1008"/&gt;
&lt;em&gt;Simple Streamlit app with two buttons&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Each button is wrapped in a conditional statement. When a button is pressed, the corresponding action is executed. Depending on the button pressed, different messages are displayed, providing immediate feedback to the user.&lt;/p&gt;
&lt;p&gt;This structure is versatile and can be expanded to include more buttons and actions.&lt;/p&gt;
&lt;h2 id="displaying-dynamic-content-based-on-button-clicks"&gt;Displaying Dynamic Content Based on Button Clicks&lt;/h2&gt;
&lt;p&gt;Buttons can be used to display all types of content dynamically, including text, images, and charts.  For example, below is a similar example but displaying images.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st

img_url_1 = "https://placehold.co/150/FF0000"
img_url_2 = "https://placehold.co/150/8ACE00"

if st.button('Show Red Image'):
    st.image(img_url_1, caption="This is a red image")

if st.button('Show Green Image'):
    st.image(img_url_2, caption="This is a green image")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Simple Streamlit app with two buttons showing images" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-buttons/streamlit-button-images.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button-images.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button-images.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button-images.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-button-images.png?tr=w-600 600w" loading="lazy" width="955" height="819"/&gt;
&lt;em&gt;Simple Streamlit app with two buttons showing images&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When the &lt;em&gt;Show Red Image&lt;/em&gt; button is pressed, a red image is displayed. The same goes for the &lt;em&gt;Show Green Image&lt;/em&gt; button. This setup allows users to switch between different images based on their preferences.&lt;/p&gt;
&lt;p&gt;Note that the state isn't persisted between each interaction. When you click on the "Show Red Image" the green image will disappear, and vice versa. This isn't a &lt;em&gt;toggle&lt;/em&gt; but a natural consequence of how Streamlit works: the code of the script is executed on each interaction, so only one button can be in a "clicked" state at any time.&lt;/p&gt;
&lt;p class="admonition admonition-tip"&gt;&lt;span class="admonition-kind"&gt;&lt;i class="fas fa-lightbulb"&gt;&lt;/i&gt;&lt;/span&gt;  To persist state between runs of the script, you can use Streamlit's state management features. We'll cover this in a future tutorial.&lt;/p&gt;
&lt;h2 id="dynamic-forms-based-on-button-press"&gt;Dynamic Forms Based on Button Press&lt;/h2&gt;
&lt;p&gt;Dynamic forms allow users to provide input in a structured way, which can vary based on user actions. This is particularly useful for collecting information without overwhelming users with multiple fields.&lt;/p&gt;
&lt;p&gt;Here's a quick example where users can input their name and age based on button presses:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st

# Title
st.title("Dynamic Forms Based on Button Press")

# Name Input Field
if st.button('Enter Name'):
    name = st.text_input('What is your name?')
    if name:
        st.write(f"Hello, {name}\!")

# Age Input Field

if st.button('Enter Age'):
    age = st.number_input('What is your age?', min_value=1, max_value=120)
    if age:
        st.write(f"Your age is {age}.")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The button &lt;code&gt;Enter Name&lt;/code&gt; triggers a text input field when clicked, allowing users to enter their names. The button &lt;code&gt;Enter Age&lt;/code&gt; displays a number input field for users to enter their age. The app provides immediate feedback based on user input.&lt;/p&gt;
&lt;h3&gt;Handling Form Submission&lt;/h3&gt;
&lt;p&gt;For more complex collections of inputs that you want to work together, consider using &lt;code&gt;st.form()&lt;/code&gt; to group inputs, allowing users to submit all inputs at once:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st

# Title
st.title("Dynamic Forms Based on Button Press")

with st.form("my_form"):
    name = st.text_input('What is your name?')
    age = st.number_input('What is your age?', min_value=1, max_value=120)
    submitted = st.form_submit_button("Submit")

    if submitted:
        st.write(f"Hello, {name}\! Your age is {age}.")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit form with submit button" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-buttons/streamlit-form-submit.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-form-submit.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-form-submit.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-form-submit.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-buttons/streamlit-form-submit.png?tr=w-600 600w" loading="lazy" width="1100" height="477"/&gt;
&lt;em&gt;Streamlit form with submit button&lt;/em&gt;&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In this tutorial, we explored how to make things happen in Streamlit using buttons. We learned how to create multiple buttons and display dynamic content based on user interaction.&lt;/p&gt;
&lt;p&gt;Now that you have a basic understanding of buttons in Streamlit, you can add basic interaction to your Streamlit applications.&lt;/p&gt;</content><category term="streamlit"/><category term="foundation"/><category term="buttons"/><category term="streamlit-foundation"/></entry><entry><title>Streamlit Widgets — An Overview of Commonly Used Widgets in Streamlit</title><link href="https://www.pythonguis.com/tutorials/streamlit-widgets/" rel="alternate"/><published>2024-09-01T21:05:00+00:00</published><updated>2024-09-01T21:05:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2024-09-01:/tutorials/streamlit-widgets/</id><summary type="html">Streamlit is a powerful Python library designed to build interactive web apps with minimal code. One of its core features is an extensive collection of widgets that allow users to interact with the app in various ways, such as providing inputs, triggering actions, or visualizing data. Streamlit makes it easy to create these elements with simple, intuitive syntax.</summary><content type="html">&lt;p&gt;Streamlit is a powerful Python library designed to build interactive web apps with minimal code. One of its core features is an extensive collection of widgets that allow users to interact with the app in various ways, such as providing inputs, triggering actions, or visualizing data. Streamlit makes it easy to create these elements with simple, intuitive syntax.&lt;/p&gt;
&lt;p&gt;In &lt;a href="/tutorials/getting-started-with-streamlit"&gt;the previous tutorial&lt;/a&gt;, we saw how to get started with Streamlit and run it on your local host. Here, we'll cover the main widgets available in Streamlit, explaining how they work, and how to customize them using various examples.&lt;/p&gt;
&lt;h2 id="getting-started-with-widgets-streamlit"&gt;Getting started with Widgets Streamlit&lt;/h2&gt;
&lt;p&gt;Widgets in Streamlit are simple yet customizable. By combining multiple widgets together in different layouts, you can create interactive dashboards, data visualizations, and forms. Whether you want to include buttons, sliders, checkboxes, or display tables and plots, Streamlit offers a wide range of widgets that cater to different needs. Adding a widget to your Streamlit app is as easy as calling a single function, and customizing its behavior requires only minimal code.&lt;/p&gt;
&lt;p&gt;In this guide, we will explore the various widgets that Streamlit offers, from basic input elements like text boxes and radio buttons to more complex visual components like plots and tables. We'll also dive into how to customize the behavior of these widgets to suit your app's specific requirements, such as adjusting slider ranges, modifying button text, and adding captions to images. By the end of this article, you'll have a solid understanding of how to leverage Streamlit widgets to enhance the interactivity and functionality of your applications.&lt;/p&gt;
&lt;p&gt;Let's start by exploring the basic widgets Streamlit provides and how you can easily integrate them into your app. Make sure you have installed the streamlit module on your system and imported it on your Python file.&lt;/p&gt;
&lt;h2 id="buttons-in-streamlit"&gt;Buttons in Streamlit&lt;/h2&gt;
&lt;p&gt;Buttons are one of the most essential and commonly used components in any interactive application. In Streamlit, the &lt;code&gt;st.button()&lt;/code&gt; widget provides an easy and effective way to allow users to trigger actions, interact with your app, or make decisions. With just a few lines of Python code, you can integrate buttons into your Streamlit app to perform tasks like data processing, changing app state, or displaying content.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;st.button()&lt;/code&gt; widget creates a clickable button on the interface. When clicked, it returns &lt;code&gt;True&lt;/code&gt;, which can be used to trigger specific actions. If the button is not clicked, it returns &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The basic syntax for creating a button in Streamlit is given below:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
if st.button('Click Me'):
    st.write("Button clicked!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="A Streamlit button widget" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/button.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button.png?tr=w-600 600w" loading="lazy" width="645" height="189"/&gt;
&lt;em&gt;A Streamlit button widget&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this simple example, the button's label is "Click Me". When clicked, the app prints "Button clicked!" to the interface.&lt;/p&gt;
&lt;p&gt;The most common customization for a button is its label &amp;mdash; the text that appears on the button. The label is the first argument you pass to the &lt;code&gt;st.button()&lt;/code&gt; function. For example, we can change the "Click Me" to a "Submit" button.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
if st.button('Submit'):
    st.write("Form submitted successfully!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="A Streamlit button widget with a different label" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/button-label.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-label.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-label.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-label.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-label.png?tr=w-600 600w" loading="lazy" width="648" height="168"/&gt;
&lt;em&gt;A Streamlit button widget with a different label&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You can customize this label to suit the action you want to convey to the user. Whether it's Submit, Cancel, Run, or any custom text, Streamlit will display it as the button text&lt;/p&gt;
&lt;p&gt;Moreover, Streamlit makes it simple to handle multiple buttons on the same page. You can define multiple &lt;code&gt;st.button()&lt;/code&gt; widgets, each with its own label and action. Here is an example of how we can add multiple buttons.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
if st.button('Button A'):
    st.write("Button A clicked!")

if st.button('Button B'):
    st.write("Button B clicked!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Multiple Streamlit buttons" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/button-multiple.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-multiple.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-multiple.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-multiple.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/button-multiple.png?tr=w-600 600w" loading="lazy" width="650" height="227"/&gt;
&lt;em&gt;Multiple Streamlit buttons&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Similarly, you can add as many buttons as you wish.&lt;/p&gt;
&lt;h2 id="checkboxes-in-streamlit"&gt;Checkboxes in Streamlit&lt;/h2&gt;
&lt;p&gt;Checkboxes are a fundamental widget in Streamlit that allows users to toggle between two states: checked &lt;code&gt;True&lt;/code&gt; or unchecked &lt;code&gt;False&lt;/code&gt;. They are ideal for scenarios where you want users to make binary choices, such as showing or hiding content, enabling or disabling features, or making Yes/No decisions. It can also be used to select multiple options as the same time as well. The &lt;code&gt;st.checkbox()&lt;/code&gt; widget is incredibly versatile and easy to implement, making it a key component in creating interactive applications.&lt;/p&gt;
&lt;p&gt;As mentioned, the &lt;code&gt;st.checkbox()&lt;/code&gt; widget creates a simple checkbox in your Streamlit app. When a user checks the box, it returns &lt;code&gt;True&lt;/code&gt;, and when the box is unchecked, it returns &lt;code&gt;False&lt;/code&gt;. Here is a basic example of checkboxes in Streamlit.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;show_text = st.checkbox('Show text')
if show_text:
    st.write("You checked the box!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit checkbox" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/checkbox.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox.png?tr=w-600 600w" loading="lazy" width="642" height="158"/&gt;
&lt;em&gt;Streamlit checkbox&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the checkbox is labeled "Show text". When the user checks the box, the app displays "You checked the box!" on the interface.&lt;/p&gt;
&lt;p&gt;The most common customization for a checkbox is its label, which appears next to the checkbox itself. This label should clearly indicate what action will occur when the checkbox is checked.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;subscribe = st.checkbox('Subscribe to our newsletter')
if subscribe:
    st.write("Thanks for subscribing!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit checkbox with custom label" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-600 600w" loading="lazy" width="648" height="292" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-600 600w" loading="lazy" width="648" height="292"/&gt;
&lt;em&gt;Streamlit checkbox with custom label&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here, the checkbox label is customized to "Subscribe to our newsletter", and when the user checks it, a thank-you message is displayed.&lt;/p&gt;
&lt;p&gt;By default, checkboxes in Streamlit are unchecked (&lt;code&gt;False&lt;/code&gt;). However, you can change this by setting the value parameter to &lt;code&gt;True&lt;/code&gt;, so that the checkbox is pre-checked when the app loads.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;subscribe = st.checkbox('Subscribe to our newsletter', value=True)
if subscribe:
    st.write("Thanks for subscribing!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In this case, the checkbox is checked by default, and the welcome message is shown immediately when the app starts.&lt;/p&gt;
&lt;p&gt;Furthermore, Streamlit makes it easy to handle multiple checkboxes, each controlling different parts of your app. You can create several checkboxes, and based on their states, you can conditionally display content or execute logic.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;option1 = st.checkbox('Enable Feature 1')
option2 = st.checkbox('Enable Feature 2')

if option1:
    st.write("Feature 1 is enabled!")
if option2:
    st.write("Feature 2 is enabled!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Multiple Streamlit checkboxes" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-600 600w" loading="lazy" width="648" height="292" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-label.png?tr=w-600 600w" loading="lazy" width="648" height="292"/&gt;
&lt;em&gt;Multiple Streamlit checkboxes&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Sometimes, you may need to generate checkboxes dynamically, especially if the number of checkboxes depends on user input or the result of some computation.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;options = ['Apple', 'Banana', 'Cherry']
selected = []

for fruit in options:
    if st.checkbox(fruit):
        selected.append(fruit)

st.write(f'Selected fruits: {", ".join(selected)}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Getting Streamlit checkboxes selection state" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/checkbox-selected.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-selected.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-selected.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-selected.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/checkbox-selected.png?tr=w-600 600w" loading="lazy" width="641" height="243"/&gt;
&lt;em&gt;Getting Streamlit checkboxes selection state&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example a list of fruit names is used to dynamically create a set of checkboxes. When a user checks a box, the corresponding fruit is added to a list, and the selected fruits are displayed.&lt;/p&gt;
&lt;h2 id="radio-buttons-in-streamlit"&gt;Radio buttons in Streamlit&lt;/h2&gt;
&lt;p&gt;Radio buttons are an essential widget in Streamlit that allow users to select a single option from a predefined list of choices. They are perfect for scenarios where only one selection can be made at a time, such as selecting a category, choosing between modes, or answering questions. The &lt;code&gt;st.radio()&lt;/code&gt; widget provides an intuitive and simple way for users to interact with your Streamlit application.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;st.radio()&lt;/code&gt; widget creates a list of radio buttons where the user can select only one option at a time. It returns the selected option, which can be used to drive various actions in the app. Let us have a look at a very simple example of a radio button.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;choice = st.radio('Choose an option:', ['Option 1', 'Option 2', 'Option 3'])
st.write(f'You selected: {choice}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit radio buttons" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/radio.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-600 600w" loading="lazy" width="648" height="242" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-600 600w" loading="lazy" width="648" height="242"/&gt;
&lt;em&gt;Streamlit radio buttons&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A label "Choose an option:" is provided.&lt;/li&gt;
&lt;li&gt;The user can select one of three options: "Option 1", "Option 2", or "Option 3".&lt;/li&gt;
&lt;li&gt;The app displays the selected option below the radio buttons.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By default, the first option in the list of radio buttons is selected. However, you can customize this by setting the index parameter, which specifies which option should be selected when the app loads.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;travel_mode = st.radio('Preferred mode of travel:', ['Car', 'Bike', 'Plane'], index=2)
st.write(f'You selected: {travel_mode}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit radio buttons with default selection" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/radio.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-600 600w" loading="lazy" width="648" height="242" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/radio.png?tr=w-600 600w" loading="lazy" width="648" height="242"/&gt;
&lt;em&gt;Streamlit radio buttons with default selection&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;index=2&lt;/code&gt; means the third option ("Plane") is selected by default when the app is loaded.&lt;/p&gt;
&lt;h2 id="select-box-in-streamlit"&gt;Select box in Streamlit&lt;/h2&gt;
&lt;p&gt;A select box in Streamlit is a widget that lets users choose a single option from a dropdown list. This widget is perfect for situations where you have a predefined list of options but want to save space on your interface by not displaying all the options upfront. The &lt;code&gt;st.selectbox()&lt;/code&gt; widget is highly customizable and can be used for anything from simple selections to dynamically populated lists. It's ideal for situations where you want to offer multiple choices, but only display the currently selected item.
Let us create a simple dropdown menu with three options.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;option = st.selectbox('Choose an option:', ['Option 1', 'Option 2', 'Option 3'])
st.write(f'You selected: {option}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit select box" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/select.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/select.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/select.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/select.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/select.png?tr=w-600 600w" loading="lazy" width="676" height="322"/&gt;
&lt;em&gt;Streamlit select box&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A label "Choose an option:" is provided.&lt;/li&gt;
&lt;li&gt;The user can select one of the options from the dropdown list: "Option 1", "Option 2", or "Option 3".&lt;/li&gt;
&lt;li&gt;The app displays the selected option.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By default, the first item in the list of options is selected in a select box. However, you can specify a different default selection by setting the index parameter. The index corresponds to the zero-based position of the option in the list.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;city = st.selectbox('Select your city:', ['New York', 'London', 'Paris', 'Tokyo'], index=2)
st.write(f'You selected: {city}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;When you run this code, you will notice that, Paris will be selected as city because it is at index 2.&lt;/p&gt;
&lt;h2 id="slider-in-streamlit"&gt;Slider in Streamlit&lt;/h2&gt;
&lt;p&gt;Sliders are a popular widget in Streamlit that allows users to select values by dragging a handle across a range. They are perfect for collecting numerical inputs or setting parameters like dates, time, and ranges. Streamlit's &lt;code&gt;st.slider()&lt;/code&gt; widget provides a flexible and easy-to-use interface for adding sliders to your app, enabling users to interactively choose values with precision. It can handle integers, floats, dates, and times, making it highly versatile for various use cases.&lt;/p&gt;
&lt;p&gt;Let us create a simple slider where a user can select any number from 0 to 100.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;value = st.slider('Select a value:', 0, 100)
st.write(f'You selected: {value}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit slider widget" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/slider.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider.png?tr=w-600 600w" loading="lazy" width="678" height="220"/&gt;
&lt;em&gt;Streamlit slider widget&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;By default, the slider handle is set to the minimum value of the range, but you can specify a default value by providing a value argument. This is useful when you want the slider to start at a specific position.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;temperature = st.slider('Set the temperature:', -50, 50, value=20)
st.write(f'Temperature set to: {temperature}&amp;deg;C')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit slider widget with default value" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/slider-default.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-default.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-default.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-default.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-default.png?tr=w-600 600w" loading="lazy" width="675" height="216"/&gt;
&lt;em&gt;Streamlit slider widget with default value&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;By default, the 20 will be selected as set temperature.&lt;/p&gt;
&lt;p&gt;Sliders in Streamlit can handle both integers and floating-point numbers. To use floating-point values, you simply specify a range with float values. You can also control the step size between values using the step parameter.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;price = st.slider('Select a price:', 0.0, 1000.0, step=0.5)
st.write(f'Price selected: ${price}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit float slider widget" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/slider-float.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-float.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-float.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-float.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-float.png?tr=w-600 600w" loading="lazy" width="678" height="224"/&gt;
&lt;em&gt;Streamlit float slider widget&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;slider&lt;/code&gt; lets the user select a price between 0.0 and 1000.0.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;step=0.5&lt;/code&gt; ensures that the slider increments or decrements by 0.5 units.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Streamlit's slider widget also allows users to select a range of values, which is particularly useful when you need two values (e.g., a start and end date, or a minimum and maximum range). To do this, provide a tuple as the value argument.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;salary_range = st.slider('Select a salary range:', 20000, 100000, (30000, 80000))
st.write(f'Selected salary range: ${salary_range[0]} - ${salary_range[1]}')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit range slider widget" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/slider-range.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-range.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-range.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-range.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-range.png?tr=w-600 600w" loading="lazy" width="675" height="218"/&gt;
&lt;em&gt;Streamlit range slider widget&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here, the slider has a range from 20000 to 100000. The user can select a minimum and maximum value for the salary range (initially set between 30000 and 80000).&lt;/p&gt;
&lt;p&gt;Apart from that, Streamlit sliders can also handle date and time values, which is especially useful when users need to select a specific day or time range. You can create sliders with datetime.date and datetime.time objects to allow users to make date-based selections.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
import datetime

date = st.slider('Select a date:', datetime.date(2020, 1, 1), datetime.date(2024, 12, 31), value=datetime.date(2023, 1, 1))
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit date slider" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/slider-date.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-date.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-date.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-date.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-date.png?tr=w-600 600w" loading="lazy" width="677" height="221"/&gt;
&lt;em&gt;Streamlit date slider&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the user can select a date between January 1, 2020, and December 31, 2024. The default value is set to January 1, 2023.&lt;/p&gt;
&lt;p&gt;You can use sliders with any other widgets or functions to make it more dynamic and interactive. For example, we can combine them with conditional logic to adjust the behavior or content of an app based on the selected value. This is particularly useful for creating dynamic and interactive experiences.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;rating = st.slider('Rate our service:', 1, 5)

if rating &amp;lt;= 2:
    st.write('We are sorry to hear that. Please let us know how we can improve.')
else:
    st.write('Thank you for your feedback!')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit slider interactivity" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/slider-interactive.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-interactive.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-interactive.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-interactive.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/slider-interactive.png?tr=w-600 600w" loading="lazy" width="672" height="214"/&gt;
&lt;em&gt;Streamlit slider interactivity&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The slider allows users to rate a service between 1 and 5. Based on the rating, different messages are displayed.&lt;/p&gt;
&lt;h2 id="different-input-options-in-streamlit"&gt;Different input options in Streamlit&lt;/h2&gt;
&lt;p&gt;In Streamlit, input options are essential for creating interactive applications, allowing users to interact with data and visualizations dynamically. Streamlit offers a variety of input widgets that cater to different types of data and user interactions. Below are the main input options that we will be discussing in this section:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Text Input: The &lt;code&gt;st.text_input()&lt;/code&gt; widget allows users to enter single-line text data. It's useful for collecting short information like names, email addresses, or any single-line text input.&lt;/li&gt;
&lt;li&gt;Text Area: For multi-line input, &lt;code&gt;st.text_area()&lt;/code&gt; is the preferred choice. It provides users with a larger space for entering longer content, like descriptions, notes, or code snippets.&lt;/li&gt;
&lt;li&gt;Number Input: Streamlit provides the &lt;code&gt;st.number_input()&lt;/code&gt; widget for numerical inputs. Users can specify integer or float values within a defined range and step size, which makes it suitable for settings like entering age, prices, or percentages.&lt;/li&gt;
&lt;li&gt;Date and Time Input: For handling date and time, Streamlit provides the &lt;code&gt;st.date_input()&lt;/code&gt; and &lt;code&gt;st.time_input()&lt;/code&gt; widgets, allowing users to pick dates and times easily. This is useful for scheduling or filtering data by time ranges.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now, let us discuss each of these options in details by taking examples.&lt;/p&gt;
&lt;h3&gt;Date and Time Input&lt;/h3&gt;
&lt;p&gt;Among its many features, Streamlit provides robust support for handling date and time inputs, allowing developers to easily integrate date and time pickers into their applications. This is useful in a wide variety of contexts, such as scheduling applications, time series analysis, filtering data based on time ranges, or tracking events.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;st.date_input()&lt;/code&gt; widget in Streamlit provides a simple and intuitive interface for users to select dates. This widget can be used to input a single date or a range of dates. The basic syntax of &lt;code&gt;date_input()&lt;/code&gt; function is given below will possible parameter values.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;st.date_input("Date", value=None, min_value=None, max_value=None, key=None, help=None, on_change=None)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit date input" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/date.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date.png?tr=w-600 600w" loading="lazy" width="797" height="503"/&gt;
&lt;em&gt;Streamlit date input&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let us explore the parameters in turn:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;label&lt;/code&gt;: The label to display alongside the widget.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;value&lt;/code&gt;: The default date(s) to show in the widget. This can be a single date or a tuple of two dates for range selection. Defaults to today's date.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;min_value&lt;/code&gt;: The earliest date that can be selected. Defaults to no minimum.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_value&lt;/code&gt;: The latest date that can be selected. Defaults to no maximum.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;key&lt;/code&gt;: An optional key that uniquely identifies this widget.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;help&lt;/code&gt;: A tooltip that displays when the user hovers over the widget.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;on_change&lt;/code&gt;: A callback function that runs when the input changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In addition to that, you can also use &lt;code&gt;st.date_input()&lt;/code&gt; to allow users to pick a range of dates by passing a tuple of two datetime.date objects as the default value.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
import datetime

# Date range input
start_date = datetime.date(2023, 9, 1)
end_date = datetime.date(2023, 9, 30)
date_range = st.date_input("Select a date range", (start_date, end_date))
st.write(f"Start date: {date_range[0]}")
st.write(f"End date: {date_range[1]}")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit date range input" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/date-range.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-600 600w" loading="lazy" width="795" height="266" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-600 600w" loading="lazy" width="795" height="266"/&gt;
&lt;em&gt;Streamlit date range input&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this case, the user can select a range of dates. The widget will return a tuple containing the start and end dates.&lt;/p&gt;
&lt;p&gt;On the other hand, the &lt;code&gt;st.time_input()&lt;/code&gt; widget allows users to select a specific time. This widget is useful in scenarios like scheduling events or setting alarms. Here is the simple syntax off &lt;code&gt;time_input()&lt;/code&gt; function with its possible parameter values.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;st.time_input(label="Time", value=None, key=None, help=None, on_change=None)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit time input" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/date-range.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-600 600w" loading="lazy" width="795" height="266" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-range.png?tr=w-600 600w" loading="lazy" width="795" height="266"/&gt;
&lt;em&gt;Streamlit time input&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The parameters in the &lt;code&gt;time_input&lt;/code&gt; functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;label&lt;/code&gt;: The label displayed next to the widget.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;value&lt;/code&gt;: The default time shown in the widget. This can be a datetime.time object. Defaults to the current time.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;key&lt;/code&gt;: An optional key that uniquely identifies the widget.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;help&lt;/code&gt;: Tooltip displayed when the user hovers over the widget.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;on_change&lt;/code&gt;: A callback function that runs when the input changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In many applications, you'll need both date and time inputs together. Although Streamlit doesn't provide a single widget for selecting both date and time, you can combine the &lt;code&gt;st.date_input()&lt;/code&gt; and &lt;code&gt;st.time_input()&lt;/code&gt; widgets to achieve this functionality.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
import datetime

# Date input
date = st.date_input("Pick a date", datetime.date.today())
# Time input
time = st.time_input("Pick a time", datetime.time(9, 00))

# Combine date and time
selected_datetime = datetime.datetime.combine(date, time)
st.write(f"Selected date and time: {selected_datetime}")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Combining Streamlit widgets for selecting date and time" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/date-time-combined.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-time-combined.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-time-combined.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-time-combined.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/date-time-combined.png?tr=w-600 600w" loading="lazy" width="797" height="305"/&gt;
&lt;em&gt;Combining Streamlit widgets for selecting date and time&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This code lets the user select both a date and a time, then combines them into a single &lt;code&gt;datetime.datetime&lt;/code&gt; object.&lt;/p&gt;
&lt;h3&gt;Text and area input&lt;/h3&gt;
&lt;p&gt;One of the essential features of any web app is gathering user input, and Streamlit provides several widgets to capture user input easily. Among them, text input widgets play a crucial role in allowing users to input free-form text data. In this article, we will dive into Streamlit's text input widgets and explore their capabilities, practical applications, and customization options.&lt;/p&gt;
&lt;p&gt;Streamlit offers two primary widgets for capturing text input:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Single-Line Text Input: Captured using the &lt;code&gt;st.text_input()&lt;/code&gt; widget.&lt;/li&gt;
&lt;li&gt;Multi-Line Text Area: Captured using the &lt;code&gt;st.text_area()&lt;/code&gt; widget.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both widgets are used to gather text-based input from users but differ in the amount of text they are designed to handle. Let's take a closer look at each of these widgets.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;st.text_input()&lt;/code&gt; widget allows users to input a single line of text. This is ideal for cases where you want to gather short responses such as names, email addresses, usernames, search queries, or small pieces of data.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Single-line text input
name = st.text_input("Enter your name")
st.write(f"Hello, {name}!")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit text input widget" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/input.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input.png?tr=w-600 600w" loading="lazy" width="797" height="196"/&gt;
&lt;em&gt;Streamlit text input widget&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This example creates a simple input box where users can enter their names. The app then displays a message using the entered text.&lt;/p&gt;
&lt;p&gt;In some cases, we may need to limit the total number of characters entered by the user. So, we can use the &lt;code&gt;max_chars&lt;/code&gt;parameter as shown below:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Text input with character limit
username = st.text_input("Enter your username", max_chars=15)
st.write(f"Your username is: {username}")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This is similar to the previous example, but this time the user is not allowed to enter characters more than 15. In the &lt;code&gt;text_input()&lt;/code&gt; function, we can also specify the type of text we want the user to enter. For example, if we want a user to enter an email and password, we can specify those as shown below:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;email = st.text_input("Enter your email")
st.write(f"Email entered: {email}")
password = st.text_input("Enter your password", type='password')
st.write(f"Password length: {len(password)} characters")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit password input widget" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/input-password.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input-password.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input-password.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input-password.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/input-password.png?tr=w-600 600w" loading="lazy" width="797" height="330"/&gt;
&lt;em&gt;Streamlit password input widget&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;As shown above, when we enter the password, it will not be visible.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;st.text_area()&lt;/code&gt; widget is ideal when you need to capture longer inputs or multi-line text, such as feedback, code snippets, or detailed descriptions. It provides users with a resizable text area where they can enter more extensive information&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Multi-line text area
feedback = st.text_area("Your feedback", "Enter your comments here...")
st.write(f"Your feedback: {feedback}")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit text area for text input" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/textarea.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/textarea.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/textarea.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/textarea.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/textarea.png?tr=w-600 600w" loading="lazy" width="800" height="275"/&gt;
&lt;em&gt;Streamlit text area for text input&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the text area allows the user to enter multiple lines of text. The entered feedback is then displayed. In a similar way as we did before, you can limit the max number of characters by assigning a value to the &lt;code&gt;max_chars&lt;/code&gt; parameter.&lt;/p&gt;
&lt;h3&gt;Number Input in Streamlit&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;st.number_input()&lt;/code&gt; widget allows users to input numbers, offering several customization options like setting minimum and maximum values, adjusting step increments, and choosing between integers and floating-point numbers. This widget is particularly useful for inputs like prices, quantities, percentages, or any scenario where numerical precision is required.&lt;/p&gt;
&lt;p&gt;The simplest form of the &lt;code&gt;st.number_input()&lt;/code&gt; widget involves asking the user to input a number without setting any constraints like minimum or maximum values.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;age = st.number_input("Enter your age")
st.write(f"Your age is: {age}")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Streamlit numeric input" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/number.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number.png?tr=w-600 600w" loading="lazy" width="800" height="227"/&gt;
&lt;em&gt;Streamlit numeric input&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the widget accepts any number, and the user's input is displayed back on the screen.&lt;/p&gt;
&lt;p&gt;You can restrict the input to a certain range by specifying the &lt;code&gt;min_value&lt;/code&gt; and &lt;code&gt;max_value&lt;/code&gt; parameters. This is particularly useful when you need to validate the input against specific boundaries.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Number input with a range
rating = st.number_input("Rate your experience", min_value=1, max_value=5)
st.write(f"Your rating is: {rating}")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Numeric input with validation" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/number-valid.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-valid.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-valid.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-valid.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-valid.png?tr=w-600 600w" loading="lazy" width="803" height="225"/&gt;
&lt;em&gt;Numeric input with validation&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the user can only select a rating between 1 and 5, ensuring valid input.
You can set a default value that appears in the input box when the app first loads, and you can also define how much the value should increase or decrease when the user interacts with the widget.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Number input with default value and step size
quantity = st.number_input("Select quantity", min_value=0, max_value=100, value=10, step=5)
st.write(f"You have selected: {quantity} units")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Numeric input with validation and step size" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/number-step.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-step.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-step.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-step.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/number-step.png?tr=w-600 600w" loading="lazy" width="792" height="240"/&gt;
&lt;em&gt;Numeric input with validation and step size&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the widget starts with a default value of 10, and the user can adjust the value in increments of 5, from 0 to 100.&lt;/p&gt;
&lt;h2 id="file-uploader-in-streamlit"&gt;File uploader in Streamlit&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;st.file_uploader()&lt;/code&gt; widget, which allows users to upload files directly into your Streamlit app. This functionality is vital in many applications, such as data analysis tools, machine learning models, and document processing systems. This widget is a convenient tool for enabling users to upload files of various types, such as text files, CSVs, images, PDFs, and more. Once uploaded, the files can be processed or analyzed directly within the Streamlit app.&lt;/p&gt;
&lt;p&gt;The simplest use case of &lt;code&gt;st.file_uploader()&lt;/code&gt; is to upload a single file of a specific type.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import pandas as pd

# Single file uploader for CSV files
uploaded_file = st.file_uploader("Upload a CSV file", type="csv")

if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
    st.write(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Upload CSV files" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/upload-csv-file.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-csv-file.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-csv-file.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-csv-file.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-csv-file.png?tr=w-600 600w" loading="lazy" width="803" height="518"/&gt;
&lt;em&gt;Upload CSV files&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the user can upload a CSV file, and the app reads and displays the contents using the pandas library.&lt;/p&gt;
&lt;p&gt;We can also upload images and show them in our web app. For images, we need to specify the type of images as shown in the example below:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import streamlit as st
from PIL import Image

# Upload an image file
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])

if uploaded_image is not None:
    image = Image.open(uploaded_image)
    st.image(image, caption="Uploaded Image", use_column_width=True)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Upload image files" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/upload-image.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-image.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-image.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-image.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-image.png?tr=w-600 600w" loading="lazy" width="788" height="740"/&gt;
&lt;em&gt;Upload image files&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here, users can upload image files in formats like PNG, JPG, or JPEG. Once uploaded, the app uses the Pillow library to open and display the image.&lt;/p&gt;
&lt;p&gt;Furthermore, you can use the accept_multiple_files parameter to allow users to upload several files at once. This is useful when handling bulk uploads or cases where multiple files need to be processed together.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Multiple file uploader for text files
uploaded_files = st.file_uploader("Upload multiple text files", type="txt", accept_multiple_files=True)

if uploaded_files:
    for uploaded_file in uploaded_files:
        st.write(f"File name: {uploaded_file.name}")
        content = uploaded_file.read().decode("utf-8")
        st.write(content)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Uploading multiple files" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/upload-multiple-files.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-multiple-files.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-multiple-files.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-multiple-files.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-multiple-files.png?tr=w-600 600w" loading="lazy" width="802" height="468"/&gt;
&lt;em&gt;Uploading multiple files&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this example, the user can upload multiple text files. The app reads and displays the contents of each file.&lt;/p&gt;
&lt;p&gt;Streamlit also allows you to validate uploaded files based on specific conditions, such as file size, format, or content.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;uploaded_file = st.file_uploader("Upload a file")

if uploaded_file is not None:
    # Check file size (less than 2 MB)
    file_size = uploaded_file.size
    if file_size &amp;gt; 2 * 1024 * 1024:
        st.error("File size exceeds 2 MB limit!")
    else:
        st.success("File uploaded successfully.")
        st.write(f"File size: {file_size} bytes")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img alt="Uploading file too large" src="https://www.pythonguis.com/static/tutorials/streamlit/streamlit-widgets/upload-file-too-large.png" srcset="https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-file-too-large.png?tr=w-100 100w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-file-too-large.png?tr=w-200 200w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-file-too-large.png?tr=w-400 400w, https://ik.imagekit.io/mfitzp/pythonguis/static/tutorials/streamlit/streamlit-widgets/upload-file-too-large.png?tr=w-600 600w" loading="lazy" width="798" height="333"/&gt;
&lt;em&gt;Uploading file too large&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the file was not uploaded because it exceeds the max size.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Streamlit's widget system is a powerful and intuitive way to add interactivity to web applications. From simple text and number inputs to more complex file uploaders and sliders, Streamlit provides a wide range of widgets that allow users to seamlessly interact with your app. These widgets can be easily integrated into data-driven applications, enabling users to input data, upload files, and adjust parameters in real-time.&lt;/p&gt;
&lt;p&gt;Streamlit widgets are designed to be highly customizable, offering various configuration options like minimum and maximum values, step sizes, file type restrictions, and dynamic callbacks. With minimal code, developers can create sophisticated, interactive applications that enhance user experience and make complex workflows more accessible.&lt;/p&gt;</content><category term="streamlit"/><category term="foundation"/><category term="streamlit-foundation"/></entry></feed>