Master Streamlit: Create and Reset Forms

Updated on Jan 04,2024

Master Streamlit: Create and Reset Forms

Table of Contents

  1. Introduction to Streamlit
  2. Installing Streamlit
  3. Creating a Simple Form
  4. Adding Input Fields
  5. Including a Submit Button
  6. Making the Form Reactive
  7. Adding Additional Form Elements
  8. Adding a Slider
  9. Displaying the Selected Slider Value
  10. Conclusion

Introduction to Streamlit

Streamlit is an open-source Python library that allows developers to easily Create and share custom web apps for machine learning and data science projects. With a few lines of code, You can transform your Python scripts into fully-functional web applications.

Installing Streamlit

To get started with Streamlit, you'll need to have it installed on your system. If you don't have Streamlit installed, you can easily do so by running the following command in your command prompt or terminal:

pip install streamlit

Creating a Simple Form

In this tutorial, we will be creating a simple form using Streamlit. To begin, create a new Python file and import Streamlit:

import streamlit as st

Next, let's give our form a title and a sub-heading:

st.title("Streamlit Form")
st.subheader("Enter your details below:")

Adding Input Fields

To create input fields for the form, such as name and email, we can use the st.text_input function:

name = st.text_input("Full Name")
email = st.text_input("Email")

You can add more input fields Based on your requirements.

Including a Submit Button

To submit the form, we need to include a submit button. We can use the st.form Context manager to wrap our form elements, and then add a submit button using the st.form_submit_button function:

with st.form(key="my_form"):
    submit_button = st.form_submit_button(label="Submit")

Making the Form Reactive

By default, Streamlit forms are not reactive, meaning that once the submit button is clicked, the form fields do not reset. To make our form reactive, we can use the st.form context manager's clear_on_submit argument:

with st.form(key="my_form", clear_on_submit=True):
    # Form elements

This will automatically clear the form after it has been submitted.

Adding Additional Form Elements

In addition to the input fields, we can also include other form elements such as a textarea for a message or a slider for selecting a value. For example, to add a textarea, we can use the st.text_area function:

message = st.text_area("Message")

Adding a Slider

To include a slider in our form, we can use the st.slider function. For example, let's add a slider for age:

age = st.slider("Age", min_value=10, max_value=100)

Displaying the Selected Slider Value

To display the selected value of the slider in the form, we can use the st.write function:

st.write("Selected Age:", age)

Conclusion

In this tutorial, we learned how to create a simple form using Streamlit. We explored how to add input fields, a submit button, make the form reactive, and include additional form elements such as a textarea and a slider. Streamlit provides an easy and intuitive way to create web apps for data science and machine learning projects. Give it a try and start building your own custom web apps!

Most people like