Streamlit Web App
This guide demonstrates how to create an interactive web application for data science and machine learning using Streamlit. Streamlit turns data scripts into shareable web apps in minutes, all in pure Python.
Modules Used:
streamlit: The framework for building the web app.- pandas: For data manipulation.
numpy: For generating dummy data.
Installation
pip install streamlit pandas numpy
The Code
Save this as app.py.
import streamlit as st
import pandas as pd
import numpy as np
# 1. Page Configuration
st.set_page_config(
page_title="My Streamlit App",
page_icon="📊",
layout="wide"
)
# 2. Title and Text
st.title("Simple Data Dashboard")
st.markdown("""
This is a demo app built with **Streamlit**.
It allows you to visualize data and interact with widgets easily.
""")
# 3. Sidebar
st.sidebar.header("Settings")
num_points = st.sidebar.slider("Number of data points", 10, 100, 50)
# 4. Data Generation
def get_data(points):
data = pd.DataFrame(
np.random.randn(points, 3),
columns=['A', 'B', 'C']
)
return data
df = get_data(num_points)
# 5. Layout: Columns
col1, col2 = st.columns(2)
with col1:
st.subheader("Data Table")
st.dataframe(df)
with col2:
st.subheader("Line Chart")
st.line_chart(df)
# 6. Interactive Widgets
st.divider()
st.subheader("User Input")
name = st.text_input("Enter your name", "Streamlit User")
if st.button("Say Hello"):
st.success(f"Hello, {name}! Welcome to the app.")
# 7. Expander
with st.expander("See explanation"):
st.write("""
The chart above shows random numbers generated by NumPy.
The slider in the sidebar controls how many rows are generated.
""")
Usage
To run a Streamlit app, you use the streamlit run command, not python.
streamlit run app.py
This will automatically open your default web browser to http://localhost:8501.