Blogging for FastAPI
Project description
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
- RSS feed
- SEO-friendly
- Sitemap
- Docker support
Basic Usage
- Import the
add_blog_to_fastapi
function - Run the instantiated FastAPI app throught 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",
}
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
-
Update the version in
pyproject.toml
andfastapi_blog/__init__.py
-
Update changelog.md
-
Build the distribution locally:
rm -rf dist
pip install -U build
python -m build
- Upload the distribution to PyPI:
pip install -U twine
python -m twine upload dist/*
- Create a new release on GitHub and tag the release:
git commit -am "Release for vXYZ"
make tag
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi-blog-0.4.0.tar.gz
.
File metadata
- Download URL: fastapi-blog-0.4.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a70944325d74eab464bbc276c4c01dba36f571ce203e17a7f4b5fa5fadbf27b2 |
|
MD5 | 1c4ab3a48e9dbf61a448cd59666f5a1a |
|
BLAKE2b-256 | b2be683a72e076600fc33c110ff490abeab5617e0b1d33c56df264bd35fc108c |
File details
Details for the file fastapi_blog-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_blog-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55e951525f26d5ec3387ebc3f1f1ca65782ea70e08791e157b354e89f3022e7b |
|
MD5 | 069004aab279a8ec363d25783f334783 |
|
BLAKE2b-256 | 1967c5af6dfe3691c6790a1e3ec5891a81edacf20b4865fd8e3c60a315dbb307 |