DEV Community

Shaheryar
Shaheryar

Posted on

Building Your First Streamlit Application: A Beginner's Guide

Streamlit has been gaining popularity among data scientists and developers for its simplicity and effectiveness in turning data scripts into shareable web apps. In this post, we'll walk through the creation of a basic Streamlit application that demonstrates its core functionalities.

What is Streamlit?

Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. In just a few minutes you can build and deploy powerful data apps - so let's get started.

Setting Up Your Environment

First, you need to install Streamlit. It's as simple as running this command in your Python environment:

pip install streamlit
Enter fullscreen mode Exit fullscreen mode

Creating a Simple Streamlit App

Let’s create a basic app that takes user input and displays it. Open your favorite IDE or text editor, create a new Python file (app.py), and let's start coding.

Step 1: Import Streamlit

import streamlit as st
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Title and User Input

Streamlit makes it extremely easy to add elements to your app. Let's add a title and a text input field:

st.title('My First Streamlit App')

user_input = st.text_input("Enter some text")
st.write('The user entered:', user_input)
Enter fullscreen mode Exit fullscreen mode

Step 3: Run Your App

Save your app.py and run it using the following command in your terminal:

streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

Your default web browser will open with your new Streamlit app. Try entering text in the input box, and you'll see the app respond in real-time.

Expanding Your App

Streamlit offers a wide range of options to make your app interactive. You can add sliders, buttons, charts, and even display data frames. Here's how you can add a slider and use it to filter data in a dataframe:

import pandas as pd
import numpy as np

# Create a sample dataframe
df = pd.DataFrame(np.random.randn(10, 2), columns=['A', 'B'])

# Add a slider
slider_val = st.slider('Select a range', 0, 10)

# Filter the dataframe
filtered_df = df[df['A'] > slider_val]

# Display the dataframe
st.write(filtered_df)
Enter fullscreen mode Exit fullscreen mode

Conclusion

Streamlit is a powerful tool for quickly turning data scripts into interactive web apps. This post covered the basics, but there's so much more you can do with Streamlit. Dive into the Streamlit documentation to explore further, and don't forget to share your creations with the community!

Top comments (0)