If you're new to coding or just starting with Python, Streamlit is the easiest way to build interactive web apps β no HTML, CSS, or JavaScript needed. This guide walks you through everything you need to know, with simple explanations and examples.
π¦ What Is Streamlit?
Streamlit is a Python library that lets you create web apps using just Python scripts. Itβs widely used for:
- Data science dashboards
- Machine learning model demos
- Interactive tools and prototypes
π οΈ Installation
Open your terminal and run:
pip install streamlit
To check if itβs installed:
streamlit hello
βΆοΈ Your First App
Create a file called app.py:
import streamlit as st
st.title("Hello, Streamlit!")
st.write("This is your first web app.")
Run it with:
streamlit run app.py
π§± Streamlit Basics
Here are the most common Streamlit functions:
| Purpose | Function | Example |
|---|---|---|
| Title | st.title() |
st.title("My App") |
| Header | st.header() |
st.header("Welcome") |
| Text | st.write() |
st.write("Hello world") |
| Markdown | st.markdown() |
st.markdown("**Bold Text**") |
| Image | st.image() |
st.image("cat.jpg") |
| Video | st.video() |
st.video("demo.mp4") |
| Audio | st.audio() |
st.audio("song.mp3") |
ποΈ Widgets for Interactivity
Widgets let users interact with your app:
name = st.text_input("Enter your name")
if name:
st.write(f"Hello, {name}!")
Other widgets:
st.button("Click me")st.checkbox("I agree")st.radio("Choose one", ["A", "B", "C"])st.selectbox("Pick one", ["Apple", "Banana"])st.slider("Choose a number", 0, 100)st.date_input("Pick a date")st.file_uploader("Upload a file")
π Displaying Data
Streamlit makes it easy to show data:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 2), columns=["A", "B"])
st.dataframe(df)
st.line_chart(df)
st.bar_chart(df)
π Upload and Display Files
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
st.write("File uploaded successfully!")
You can also read CSVs:
import pandas as pd
df = pd.read_csv(uploaded_file)
st.dataframe(df)
π§ Caching for Speed
Use @st.cache_data to avoid reloading data every time:
@st.cache_data
def load_data():
return pd.read_csv("data.csv")
df = load_data()
π§ Sidebar Navigation
st.sidebar.title("Navigation")
option = st.sidebar.selectbox("Choose a page", ["Home", "About"])
if option == "Home":
st.write("Welcome to the Home page!")
π Layouts and Columns
col1, col2 = st.columns(2)
col1.write("Left column")
col2.write("Right column")
π§ͺ Displaying Code and JSON
st.code("for i in range(5): print(i)")
st.json({"name": "Alice", "age": 30})
π€ Exporting Results
You can let users download files:
st.download_button("Download CSV", df.to_csv(), "data.csv")
π Deployment
Deploy your app for free using:
- Streamlit Community Cloud
- Heroku, Render, or AWS for advanced hosting
π§ Bonus:
Here are some beginner-friendly tips inspired by GeeksforGeeks:
- Use
st.form()for grouped inputs and submission buttons - Use
st.progress()andst.spinner()for long tasks - Use
st.session_stateto store user inputs across interactions - Use
st.experimental_rerun()to refresh the app programmatically
Top comments (0)