how to build a sample stream lit app
How to build a sample stream lit app?
What is Streamlit?
Streamlit is an open-source Python library that allows you to quickly build and deploy interactive web applications for machine learning and data science. It simplifies UI development, requiring only Python code, without needing frontend expertise in HTML, CSS, or JavaScript.
Key Features of Streamlit
- Simple and Pythonic (
st.write()
,st.button()
, etc.) - Automatically refreshes when code changes
- Supports interactive widgets (
st.slider()
,st.selectbox()
, etc.) - Ideal for data science and ML model deployment
- Easily deployable with Streamlit Cloud, AWS, GCP, or Heroku
Building a Sample Streamlit App
1. Install Streamlit
First, install Streamlit via pip:
pip install streamlit
2. Create a Simple Streamlit App
Create a Python script, e.g., app.py
, and add the following code:
import streamlit as st
import pandas as pd
import numpy as np
# Set app title
st.title("Simple Streamlit App")
# Add a header
st.header("Interactive Widgets")
# Add a slider
age = st.slider("Select your age", 0, 100, 25)
# Add a text input
name = st.text_input("Enter your name", "John Doe")
# Add a button
if st.button("Submit"):
st.write(f"Hello, {name}! You are {age} years old.")
# Display a dataframe
st.header("Random DataFrame")
df = pd.DataFrame(np.random.randn(10, 3), columns=["A", "B", "C"])
st.dataframe(df)
# Add a simple chart
st.header("Line Chart")
st.line_chart(df)
3. Run the App
Navigate to the directory where app.py
is located and run:
streamlit run app.py
This will open the app in your web browser.
Enhancing Your Streamlit App
Here are some additional components you can add:
- File Upload:
st.file_uploader()
- Charts:
st.bar_chart()
,st.pyplot()
,st.plotly_chart()
- Maps:
st.map()
- Columns & Layouts:
st.columns()
,st.sidebar()
- AI Integration: Use OpenAI/GPT APIs with
st.chat_input()
Comments
Post a Comment