Made with ScholarlyTurn your own files into documents like this
Shared with you
The tutorial begins a blog-style web application using Python and FastAPI. The broader project is intended to include a JSON REST API, HTML pages, database integration, validation, post CRUD operations, authentication, file uploads, background tasks, router-based organization, testing, and deployment. The application will support users, profiles, posts, authorization, editing and deleting posts, and pagination.
The project is called fastAPI_blog. The tutorial uses UV as the package manager, although pip also works.
uv init fastAPI_blog cd fastAPI_blog uv add "fastapi[standard]"
Pip alternative:
pip install "fastapi[standard]"
The standard extras include FastAPI, the Uvicorn ASGI server, and the FastAPI command-line interface. The basic application starts in main.py:
from fastapi import FastAPI app = FastAPI()
FastAPI uses decorators to define routes. A basic root route returns a Python dictionary, which FastAPI converts into JSON automatically:
@app.get("/")
def home():
return {"message": "hello world"}Regular synchronous functions work, and FastAPI also supports asynchronous functions.
Start the application in development mode with either command:
fastapi dev main.py
uv run fastapi dev main
Development mode automatically reloads the application when code changes and provides helpful debugging output. The example runs at localhost:8000. fastapi run is intended for production use.
FastAPI generates interactive documentation from the application code:
Both pages list the available routes and are generated automatically.
The tutorial first uses temporary dummy data: a list of dictionaries containing each post’s ID, author, title, content, and posting date. The data will later be replaced with a database.
@app.get("/api/post")
def get_post():
return postFastAPI converts the list of dictionaries into a JSON array. The endpoint is available at /api/post and appears in the automatic API documentation.
To return HTML instead of JSON, import HTMLResponse and set it as the route’s response class:
from fastapi.responses import HTMLResponse
@app.get("/", response_class=HTMLResponse)
def home():
return f"<h1>{post[0]['title']}</h1>"The browser renders the returned HTML, and the raw markup can also be inspected through the page source.
Multiple decorators can map different URLs to the same function. The includeinschema=False option keeps browser-facing HTML routes out of the generated API documentation while leaving them functional:
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
@app.get("/post", response_class=HTMLResponse, include_in_schema=False)
def home():
...In this example, both / and /post display the same HTML page. This keeps programmatic API routes distinct from browser-facing routes in the API schema.
The next part covers Jinja2 templates, template inheritance, styling, and more maintainable HTML pages.