Streamlit in 3 Minutes

Streamlit is an open-source Python library that allows you to create interactive web apps for machine learning and data science.

Dayanithi
Data And Beyond
3 min readApr 14, 2023

--

Photo by Marcus Cramer on Unsplash

Streamlit is a powerful and user-friendly open-source Python library that makes it easier to build interactive web applications for machine learning and data science. With Streamlit, developers and data scientists can create engaging, informative, and visually appealing apps with just a few lines of code.

Install Streamlit and import

!pip uninstall streamlit
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt

One of the main benefits of Streamlit is its simplicity. You can write your code in a familiar environment and use the library’s high-level APIs to quickly build complex and interactive web applications. Whether you’re a seasoned software engineer or a beginner in data science, Streamlit makes it easy to get started.

Streamlit also offers a wide range of tools for creating data visualizations. Whether you want to plot a scatterplot, display a histogram, or create a map, Streamlit has you covered. You can even use it to build interactive dashboards that allow users to explore your data in real-time.

Another benefit of Streamlit is its flexibility. You can use it to build apps for a wide range of use cases, from simple data visualizations to complex machine learning models. Streamlit even supports deployment, so you can easily share your apps with others.

This code will create a web app that displays a line graph of x and y

st.title("Line Plot Example")
data = pd.DataFrame({
"x": [1, 2, 3, 4], # Load sample data
"y": [10, 20, 30, 40]})
plt.plot(data["x"], data["y"]) # Plot the data
st.pyplot() # Show the plot in the Streamlit app

When you run this code, Streamlit will create a new web page that you can interact with. You can pan and zoom the plot, and you can use the Streamlit app to explore your data in real-time.

This example demonstrating some of Streamlit’s unique features, including:

  • The slider component, slider allows you to choose a value within a specified range by dragging a handle along a track. You can use sliders to select a number, a date, or any other type of value.
num_points = st.slider("Number of points", min_value=100, max_value=1000, value=500, step=100)
  • The text input component, which updates in real-time as the user types
text_input = st.text_input("Enter some text:")
st.write("The name :", text_input)
  • The checkbox component, a Checkbox is a toggle switch that allows the user to turn an option on or off. You can use Checkboxes to allow the user to select multiple options.
show_plot = st.checkbox("Show plot", value=True)
if not show_plot:
plt.close()
  • The Select box component, a SelectBox allows you to choose one option from a dropdown list. You can use Select Box to present a list of options for the user to choose from.
plot_color = st.selectbox("Plot color", ["blue", "red", "green"])
plt.plot(data["x"], data["y"], color=plot_color)
  • The camera input helps to take a photo on the site . comes in handy when deploying and using computer vision models .
st.camera_input("Take a picture")
  • You can display a progress bar in a Streamlit app . You can use this to display the progress of a long-running task, such as a download or data processing.
st.progress(progress_variable_1_to_100)

When everything done , this is how you launch it .

streamlit run app.py
Screenshot captured by the author

Take the local URL and put it in chrome . There you go ,now you have your streamlit web app up and running .

--

--