Skip to main content

FastAPI Blog

A simple, easy-to-use blog application built with FastAPI.

Features

  • Write blog posts in Markdown
  • Syntax highlighting for code blocks
  • Responsive design
  • Dark mode
  • Overloadable templates
  • Live, working configuration examples
  • SEO-friendly
  • Sitemap
  • Docker support

Basic Usage

  1. Import the add_blog_to_fastapi function
  2. Run the instantiated FastAPI app through the add_blog_to_fastapi function

This all you need to do:

from fastapi_blog import add_blog_to_fastapi
from fastapi import FastAPI


app = FastAPI()
app = add_blog_to_fastapi(app)


@app.get("/")
async def index() -> dict:
    return {
        "message": "Check out the blog at the URL",
        "url": "http://localhost:8000/blog",
    }
  1. Add the first blog entry

Assuming your FastAPI app is defined in a main.py module at the root of your project, create a file at posts/first-blog-post.md:

---
date: "2024-03-21T22:20:50.52Z"
published: true
tags:
  - fastapi
  - fastapi-blog
title: First blog post
description: This is the first blog post entry.
---

Exciting times in the world of fastapi-blog are ahead!

## This is a markdown header

And this is a markdown paragraph with a [link](https://github.com/pydanny/fastapi-blog).
  1. Add the first page

Assuming your FastAPI app is defined in a main.py module at the root of your project, create a file at pages/about.md:

---
title: "About Me"
description: "A little bit of background about me"
author: "Daniel Roy Greenfeld"
---

[TOC]

## Intro about me

I'm probably best known as "[pydanny](https://www.google.com/search?q=pydanny)", one of the authors of Two Scoops of Django.

I love to hang out with my [wife](https://audrey.feldroy.com/), play with my [daughter](/tags/uma), do [Brazilian Jiu-Jitsu](https://academyofbrazilianjiujitsu.com/), write [books](/books), and read books.

- [Mastodon](https://fosstodon.org/@danielfeldroy)
- [LinkedIn](https://www.linkedin.com/in/danielfeldroy/)
- [Twitter](https://twitter.com/pydanny)

## About this site

This site is written in:

- Python
- FastAPI
- fastapi-blog
- Sakura minimal CSS framework
- Markdown
- Vanilla HTML

Advanced Usage

fastapi_blog is configurable through the add_blog_to_fastapi function.

Replacing the default templates

This example is Django-like in that your local templates will overload the default ones.

import fastapi_blog
import jinja2
from fastapi import FastAPI


django_style_jinja2_loader = jinja2.ChoiceLoader(
    [
        jinja2.FileSystemLoader("templates"),
        jinja2.PackageLoader("fastapi_blog", "templates"),
    ]
)

app = FastAPI()
app = fastapi_blog.add_blog_to_fastapi(
    app, prefix=prefix, jinja2_loader=django_style_jinja2_loader
)


@app.get("/")
async def index() -> dict:
    return {
        "message": "Check out the blog at the URL",
        "url": f"http://localhost:8000/blog",
    }

Changing the location of the blog url

Perhaps you want to have the blog at the root?

import fastapi_blog
from fastapi import FastAPI


app = FastAPI()
app = fastapi_blog.add_blog_to_fastapi(
    app, prefix="change"
)


@app.get("/api")
async def index() -> dict:
    return {
        "message": "Check out the blog at the URL",
        "url": "http://localhost:8000/change",
    }

Blog at root URL

This is for when your blog/CMS needs to be at the root of the project

import fastapi_blog
from fastapi import FastAPI


app = FastAPI()


@app.get("/api")
async def index() -> dict:
    return {
        "message": "Check out the blog at the URL",
        "url": "http://localhost:8000",
    }

# Because the prefix is None, the call to add_blog_to_fastapi
# needs to happen after the other view functions are defined.
app = fastapi_blog.add_blog_to_fastapi(app, prefix=None)

Add favorite articles to the homepage

import fastapi_blog
from fastapi import FastAPI


favorite_post_ids = {
    "code-code-code",
    "thirty-minute-rule",
    "2023-11-three-years-at-kraken-tech",
}

app = FastAPI()
app = fastapi_blog.add_blog_to_fastapi(app, favorite_post_ids=favorite_post_ids)


@app.get("/")
async def index() -> dict:
    return {
        "message": "Check out the blog at the URL",
        "url": "http://localhost:8000/blog",
    }

Add page not in the blog list of posts

In the pages directory of your blog, add markdown files with frontmatter. You can then find it by going to the URL with that name. For example, adding this pages/about.md to the default config would make this appear at http://localhost:8000/blog/about.

---
title: "About Daniel Roy Greenfeld"
description: "A little bit of background about Daniel Roy Greenfeld"
author: "Daniel Roy Greenfeld"
---

I'm probably best known as "[pydanny](https://www.google.com/search?q=pydanny)", one of the authors of [Two Scoops of Django](/books/tech).

Installation and Running Example Sites

Option 1: Local Virtualenv

You can install this into a virtualenv using the pyproject.toml file:

pip install fastapi-blog
make run

Option 2: Docker (Local Dockerfile)

Or into a Docker container using the local Dockerfile:

docker build -t fastapi-blog .
docker run -d -p 8000:8000 fastapi-blog

Option 3: Docker (Prebuilt)

Or using a prebuilt Docker image from GitHub Container Registry:

docker run -d -p 8000:8000 ghcr.io/aroygreenfeld/fastapi-blog:latest

This is if you just want to run the application without building it yourself.

Releasing a new version

  1. Update the version in pyproject.toml and fastapi_blog/__init__.py

  2. Update changelog.md

  3. Build the distribution locally:

rm -rf dist
pip install -U build
python -m build
  1. Upload the distribution to PyPI:
pip install -U twine
python -m twine upload dist/*
  1. Create a new release on GitHub and tag the release:
git commit -am "Release for vXYZ"
make tag

Contributors

pydanny
Daniel Roy Greenfeld
audreyfeldroy
Audrey Roy Greenfeld

Release files for fastapi-blog 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-blog 0.6.0
File Size Uploaded
fastapi-blog-0.6.0.tar.gz 18.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-blog 0.6.0
File Interpreter ABI Platform
fastapi_blog-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 35.9 kB

Release files / fastapi-blog-0.6.0.tar.gz

Download URL fastapi-blog-0.6.0.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f017c94cbd48f492586b3e18ff7e901dd5cc2a07700f32586f8c5a3430eb10bc
BLAKE2b-256 checksum
How to use checksums
5ed3ee10bce25b0691bfc0e733a73a94d1dbe7dcfc3d60874e9ad50ced7e4712
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release files / fastapi_blog-0.6.0-py3-none-any.whl

Download URL fastapi_blog-0.6.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2dc6b69aa3fd6116d29c1a4b01778424ff809dc7fe69aa437546bc4a906cb6f3
BLAKE2b-256 checksum
How to use checksums
3252323465efed0de94f86ede29ade6d9c195ce222de6f84b302247b152f3ffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page