Lesson 44 of 45 ยท FastAPI Basics
Static Files
Learn to serve static files like HTML, CSS, and JavaScript using FastAPI's StaticFiles mount.
Open this lesson in the editor โWhat you'll learn
- Understand what static files are and when to use them
- Learn how to mount StaticFiles to serve files from a directory
- Understand the concept of mounting applications in FastAPI
- Serve HTML, CSS, and JavaScript files from your FastAPI app
Static Files
๐ฏ What You'll Learn
In this lesson, you'll learn how to serve static files (HTML, CSS, JavaScript, images, etc.) using FastAPI's StaticFiles mount. This is essential for building complete web applications with frontend assets.
๐ Theory
What are Static Files?
Static files are files that don't change dynamically - they're served exactly as they exist on disk. Common examples include:
- HTML files
- CSS stylesheets
- JavaScript files
- Images (PNG, JPG, SVG)
- Fonts
- PDF documents
Using StaticFiles in FastAPI
FastAPI provides StaticFiles to automatically serve files from a directory:
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
# Mount static files at /static path
app.mount("/static", StaticFiles(directory="static"), name="static")
Understanding "Mounting"
Mounting means adding a complete independent application at a specific path. Key points:
- The mounted application handles all sub-paths under its mount point
- It's completely independent from your main FastAPI app
- OpenAPI docs won't include the mounted application
- Different from
APIRouterwhich integrates with your app
Mount Parameters
app.mount("/static", StaticFiles(directory="static"), name="static")
- First parameter (
"/static"): The URL path where files will be served directory: The local directory containing your static filesname: Internal name used by FastAPI for the mount
Example Directory Structure
myapp/
โโโ main.py
โโโ static/
โโโ index.html
โโโ styles.css
โโโ script.js
โโโ images/
โโโ logo.png
With the mount above, files would be accessible at:
http://localhost:8000/static/index.htmlhttp://localhost:8000/static/styles.csshttp://localhost:8000/static/images/logo.png
๐ง Key Concepts
- StaticFiles: A Starlette class that FastAPI re-exports for convenience
- Mounting: Adding an independent application at a specific path
- Directory Serving: Automatically maps URL paths to file system paths
- Production Use: Essential for serving frontend assets in full-stack applications
๐ก Best Practices
- Organize by Type: Group files in subdirectories (css/, js/, images/)
- Use CDNs: For production, consider CDN for better performance
- Security: Never expose sensitive files in static directories
- Cache Headers: StaticFiles automatically sets appropriate cache headers
- Mount Order: Mount static files after defining API routes to avoid conflicts
๐ Real-World Use Cases
- Single Page Applications: Serve your React/Vue/Angular app
- Documentation: Host API documentation or user guides
- Media Files: Serve user-uploaded images or documents
- Admin Panels: Serve admin dashboard HTML/CSS/JS
โ ๏ธ Browser Environment Note
In this interactive tutorial running in your browser (Pyodide), we can't actually access a file system to serve static files. However, understanding this concept is crucial for building real FastAPI applications in production environments.
In production, you would:
- Create a
static/directory in your project - Add your HTML, CSS, JS, and other files
- Mount StaticFiles as shown above
- Access files via
/static/filename
๐ Additional Resources
Hint
Use app.mount() with StaticFiles to serve files from a directory. The directory parameter should match your file structure.
Now write it
The editor runs a real FastAPI server and grades your code against this lesson's own endpoints.
Start Static Files