Streamlit
✕Streamlit Introduction
- Open-source Python library for building interactive web apps.
- Use Cases:
Data Dashboard,Model demo,Data Explorationetc. - Installed as:
pip install streamlit - Run app as:
streamlit run app_name.py - Simple API: Build apps with pure Python code.
- Interactivity: Widgets and state management for dynamic interactions.
- Media Support: Easily display images, videos, audio, etc.
- Easy Deployment: Deploy on Streamlit Cloud or other platforms.
Key Features:
Text Element
- Streamlit provides functions to display text in various formats.
import streamlit as st st.title("My Streamlit App") st.header("This is a header") st.subheader("This is a subheader") st.text("This is a text element.") st.write("This is a write element.") st.markdown("This is a **markdown** example.") st.code("def hello():\n\tprint('Hello, Streamlit!')")
Example:
Input Widgets: Text, Number, Date
- Streamlit provides various widgets for text, number, date inputs.
- Explore more widgets at Streamlit Documentation.
name = st.text_input("Enter your name") feedback = st.text_area("Enter your feedback") age = st.number_input("Enter your age", min_value=0, max_value=120) dob = st.date_input("Select your date of birth") st.write(f"Name: {name}, Feedback: {feedback}, Age: {age}, DOB: {dob}")
Examples:
Input Widget: Selection
- Selection widgets allow users to choose from predefined options.
color = st.selectbox("Choose a color", ["Red", "Green", "Blue"]) options = st.multiselect("Select options", ["Option 1", "Option 2"]) agree_to_terms = st.checkbox("Check this box to agree the terms.") gender = st.radio("Select your gender", ["Male", "Female", "Other"]) if st.button("Submit"): st.write(f"You selected {color} and options: {options}")
Examples:
File Input
- Streamlit provides a file uploader widget to allow users to upload files.
uploaded_file = st.file_uploader("Choose a csv file", type="csv") if uploaded_file is not None: st.write(f"File name: {uploaded_file.name}") st.write(f"File type: {uploaded_file.type}") st.write(f"File size: {uploaded_file.size} bytes") df = pd.read_csv(uploaded_file) st.write(df.head())
Example:
Interactive Charts
- Supports interactive charts using libraries like
Matplotlib,Seaborn,Plotly. - We can display interactive dataframe as well
import seaborn as sns fig, ax = plt.subplots() sns.barplot(x="category", y="value", data=df, ax=ax) st.dataframe(df) st.pyplot(fig)
Example:
Layout
- Used for creating multi-column layouts and organizing content.
col1, col2 = st.columns(2) with col1: st.header("Column 1") st.write("This is the first column.") with col2: st.header("Column 2") st.write("This is the second column.")
Example:
Deploying Streamlit App
- Deploying: Making app accessible to others via the web URL.
- We can create and share
ML Dashboard,Visualization,Model Demoetc. - Deployment Options:
-
Streamlit Cloud: Free hosting with easy GitHub integration. -Heroku: General-purpose cloud platform. -AWS/GCP/Azure: Scalable cloud services for production apps. - Streamlit Cloud Deployment Steps:
1. Push your Streamlit app code to a GitHub repository.
2. Go to Streamlit Cloud and sign in with GitHub.
3. Click on
New appand select your repository and branch. 4. Specify the main Python file (e.g.,app.py) and clickDeploy. 5. Our app will be live at a unique URL. 6. Note: Must addrequirements.txtwith dependencies.
