DEV Community

Cover image for Get Started With Streamlit
Anoop Johny
Anoop Johny

Posted on

Get Started With Streamlit

Introduction
Python frameworks such as Streamlit enable beginners and newbies to build and deploy highly interactive web applications spanning fields such as Data Science and Machine Learning. Creating Web applications has never been easier when beginners are not required to have front-end development experience.

Installation
Ensure Python is installed on your system before proceeding with the installation of "Streamlit". To do so execute the following in the command line terminal.
python -h

Now once you have confirmed Python is installed execute this command using PIP to install the streamlit python package.
Installation of Streamlit Python Framework to your local workspace using the PIP command
To verify the installation of Streamlit run :

streamlit hello
Enter fullscreen mode Exit fullscreen mode

Once this has been executed a new window should open up in your browser on the local port:

  Local URL: http://localhost:8501
Enter fullscreen mode Exit fullscreen mode

This is your default Streamlit application running on port 8501 once successful installation has been achieved.

Now let's create your first streamlit app.

Your First Streamlit App

  1. Create any new python file with a name ,eg., app.py.
  2. Lets import the Streamlit Library>> Import streamlit as st
  3. Name a title and welcome message text to your app.
st.title("My App")
st.write("Welcome to my Streamlit Application")
Enter fullscreen mode Exit fullscreen mode
  1. Now run the streamlit app by executing the command>>
streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

Upon execution the default browser should open your app on the port 8501 and you should see your message:

Streamlit My App window opens on port 8501 on execution of command 'streamlit run app.py'

Some key features
Streamlit allows users to build several interactive widgets which enable building dynamic applications.

Widgets are crucial for building dynamic web applications

Some of these widgets are :

  • Text Input
name = st.text_input("Enter your name:")
st.write(f"Hello, {name}!")
Enter fullscreen mode Exit fullscreen mode
  • Slider Input
number = st.slider("Select a number", 0, 100, 50)
st.write(f"You selected: {number}")
Enter fullscreen mode Exit fullscreen mode
  • Button
if st.button("Click Me"):
    st.write("Button clicked!")
Enter fullscreen mode Exit fullscreen mode

Data Use cases

Streamlit allows to make depictions of tabular data in chart formats>>

  • DataFrame Display:
import pandas as pd

df = pd.DataFrame({
    "Column 1": [10, 20, 30, 40],
    "Column 2": [50, 60, 70, 80]
})
st.dataframe(df)
Enter fullscreen mode Exit fullscreen mode

This is a basic data frame and it would show up as >>

A basic dataframe in streamlit

  • Charts:
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
st.pyplot(fig)

Enter fullscreen mode Exit fullscreen mode

A basic sine wave with X and Y co-ordinates is created in the app >>

Sine wave chart in streamlit

A sidebar can be added for better UI organization :

  • Sidebar
st.sidebar.header("Sidebar Header")
option = st.sidebar.selectbox("Choose an option:", ["Option 1", "Option 2"])
st.sidebar.write(f"You selected: {option}")
Enter fullscreen mode Exit fullscreen mode

Side bars are great for managing several different tabs for a large application.

Deployment of Streamlit Apps

Once your app is completed, you can deploy it using:

Streamlit Community Cloud (free hosting for Streamlit apps)

Heroku, AWS, or Google Cloud

For deployment into the Streamlit Cloud, push your code to GitHub and link it to Streamlit Cloud.

read more about streamlit docs here.

Conclusion
Streamlit is a potent tool for easy development of interactive web apps for data science and machine learning. The elegantly simple syntax and rich feature set make it an ideal choice for all developers and data scientists who wish to quickly convert their ideas into real-life applications.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs