<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Python GUIs - buttons</title><link href="https://www.pythonguis.com/" rel="alternate"/><link href="https://www.pythonguis.com/feeds/buttons.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></feed>