🐍 Streamlit for Absolute Beginners: Build Web Apps with Just Python


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
Enter fullscreen mode

Exit fullscreen mode

To check if it’s installed:

streamlit hello
Enter fullscreen mode

Exit fullscreen mode




▶️ 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.")
Enter fullscreen mode

Exit fullscreen mode

Run it with:

streamlit run app.py
Enter fullscreen mode

Exit fullscreen mode




🧱 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}!")
Enter fullscreen mode

Exit fullscreen mode

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)
Enter fullscreen mode

Exit fullscreen mode




📁 Upload and Display Files

uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
    st.write("File uploaded successfully!")
Enter fullscreen mode

Exit fullscreen mode

You can also read CSVs:

import pandas as pd

df = pd.read_csv(uploaded_file)
st.dataframe(df)
Enter fullscreen mode

Exit fullscreen mode




🧠 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()
Enter fullscreen mode

Exit fullscreen mode




🧭 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!")
Enter fullscreen mode

Exit fullscreen mode




📐 Layouts and Columns

col1, col2 = st.columns(2)
col1.write("Left column")
col2.write("Right column")
Enter fullscreen mode

Exit fullscreen mode




🧪 Displaying Code and JSON

st.code("for i in range(5): print(i)")
st.json({"name": "Alice", "age": 30})
Enter fullscreen mode

Exit fullscreen mode




📤 Exporting Results

You can let users download files:

st.download_button("Download CSV", df.to_csv(), "data.csv")
Enter fullscreen mode

Exit fullscreen mode




🌐 Deployment

Deploy your app for free using:




🧠 Bonus:

Here are some beginner-friendly tips inspired by GeeksforGeeks:

  • Use st.form() for grouped inputs and submission buttons
  • Use st.progress() and st.spinner() for long tasks
  • Use st.session_state to store user inputs across interactions
  • Use st.experimental_rerun() to refresh the app programmatically



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *