Concept #128Easyextended-ai-concepts

What is Streamlit?

#gen-ai#python

Answer

What is Streamlit?

Streamlit is an open-source Python framework that lets you turn Python scripts into interactive web applications — without needing any web development knowledge (no HTML, CSS, JavaScript required).

Why Streamlit?

Data scientists and AI engineers often build powerful Python code but lack frontend skills. Streamlit solves this:

python
# This is a complete web app — just Python!
import streamlit as st
import anthropic

st.title("AI Chat App")
user_input = st.text_input("Ask me anything:")

if user_input:
    client = anthropic.Anthropic()
    response = client.messages.create(
        model="claude-opus-4-6",
        max_tokens=512,
        messages=[{"role": "user", "content": user_input}]
    )
    st.write(response.content[0].text)

Run:

text
streamlit run app.py
→ Opens in browser immediately.

Core Components

python
import streamlit as st

# Text elements
st.title("My App")
st.header("Section")
st.write("Regular text or **markdown**")
st.code("print('hello')", language="python")

# Input widgets
text = st.text_input("Enter text")
number = st.number_input("Enter number", min_value=0, max_value=100)
option = st.selectbox("Choose", ["Option A", "Option B"])
file = st.file_uploader("Upload file")
button = st.button("Click me")

# Display data
st.dataframe(df)           # Interactive table
st.chart(df)               # Line chart
st.plotly_chart(fig)       # Plotly charts
st.image(img)              # Images
st.json({"key": "value"})  # JSON viewer

# Layout
col1, col2 = st.columns(2)
with col1:
    st.write("Left column")
with col2:
    st.write("Right column")

# State management
if "messages" not in st.session_state:
    st.session_state.messages = []

Building an AI Chat Interface

python
import streamlit as st
from anthropic import Anthropic

client = Anthropic()

st.title("Claude Chat")

# Initialize session state
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# User input
if prompt := st.chat_input("Message Claude..."):
    # Add user message
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Get Claude response with streaming
    with st.chat_message("assistant"):
        with client.messages.stream(
            model="claude-opus-4-6",
            max_tokens=1024,
            messages=st.session_state.messages
        ) as stream:
            response = st.write_stream(stream.text_stream)

    st.session_state.messages.append({"role": "assistant", "content": response})

Deployment

bash
# Local development
streamlit run app.py

# Deploy to Streamlit Cloud (free for public apps)
# 1. Push to GitHub
# 2. Go to share.streamlit.io
# 3. Connect your repo — deployed in minutes

# Docker deployment
# FROM python:3.11-slim
# RUN pip install streamlit anthropic
# EXPOSE 8501
# CMD ["streamlit", "run", "app.py", "--server.port=8501"]

When to Use Streamlit

Use CaseStreamlit?
Quick AI demo✅ Perfect
Internal data dashboard✅ Great
ML model exploration✅ Great
Customer-facing production app⚠️ Limited (consider FastAPI + React)
Complex UI with many pages⚠️ Works but limited
REST API❌ Use FastAPI instead

Streamlit vs Alternatives

ToolBest For
StreamlitData scientists, AI demos, internal tools
GradioML model demos, HuggingFace integration
Dash (Plotly)Analytics dashboards, more UI control
FastAPI + ReactProduction web apps