A Template for Single-page App using FastAPI + Svelte
- FastAPI
- Typed & Fast Way to build a API Server using Python
- Svelte
- Fast & Easy to Modern Front-end
- and supporting TypeScript!
Clone this, then
# on local
make build
make serve
# on docker
make build-docker
make serve-docker
And open localhost:8080
.
Check ./server.py
# Define API functions
@app.get("/api/greeting")
def greeting(name: Optional[str] = None):
"""Greeting you"""
if name:
return {"msg": f"Hello {name}!"}
return {"msg": "Hi!"}
# URLs other than APIs are
# - static files under `web/public` if exists
# - /aaa/bbb -> `./web/public/aaa/bbb`
# - Returns `web/public/index.html` else
# - SPA on Front-end side
app.mount("/", MountFiles(directory="web/public", html=True), name="static")
Use make dev
to enable hot-reloading.
cd web/
and check src/App.svelte
.
Use make dev
to enable hot-reloading.