C.W.K.
Stream
Lesson 03 of 04 · published

Streamlit, Docker, and Static Spaces

~24 min · spaces, streamlit, docker

Level 0Scout
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Streamlit when you need state

Gradio's mental model is "Python function → UI." Streamlit's is "script reruns top-to-bottom on interaction." For multi-page dashboards or apps that need persistent session state, Streamlit feels more natural — st.session_state is purpose-built.

Docker Space when you need anything else

Need FastAPI? Vue? a non-standard Python? a custom Nginx? GPU drivers? Build a Dockerfile and choose sdk: docker in the manifest. Spaces builds the container and runs it. The HF container runs as user 1000 (non-root); design your Dockerfile accordingly.

Static Spaces are free hosting for HTML

For a model showcase, a tutorial, or marketing page that doesn't need a runtime, set sdk: static and push HTML/CSS/JS. HF serves it on the same domain as your other Spaces. Free, fast, no cold-start.

Code

Streamlit dashboard around a HF inference call·python
import streamlit as st
from huggingface_hub import InferenceClient

st.title("Quick Llama Sandbox")

if "history" not in st.session_state:
    st.session_state.history = []

prompt = st.text_input("Prompt")
if st.button("Send") and prompt:
    client = InferenceClient(model="meta-llama/Llama-3.1-8B-Instruct", provider="hf-inference")
    out = client.chat_completion(messages=[{"role": "user", "content": prompt}], max_tokens=200)
    st.session_state.history.append((prompt, out.choices[0].message.content))

for u, a in st.session_state.history:
    st.markdown(f"**You:** {u}")
    st.markdown(f"**Llama:** {a}")
Docker Space — README front-matter·yaml
---
title: FastAPI Demo
sdk: docker
app_port: 7860
---
Dockerfile for a Docker Space (FastAPI)·python
# Dockerfile
FROM python:3.11-slim
RUN useradd -m -u 1000 user
USER user
WORKDIR /home/user/app

COPY --chown=user requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY --chown=user . .

# Spaces expects port 7860 by default — override via app_port in manifest.
EXPOSE 7860
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]

External links

Exercise

Build a Streamlit Space dashboard that lets you compare three different chat models side-by-side. Then build the same dashboard as a Docker Space using FastAPI + a tiny HTML page. Compare deploy time, cold-start, and how easy each is to debug when something breaks.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.