跳转到主要内容

为FastAPI编写博客

项目描述

FastAPI Blog

使用FastAPI构建的简单、易于使用的博客应用程序。

特性

  • 使用Markdown编写博客文章
  • 代码块语法高亮
  • 响应式设计
  • 深色模式
  • 可覆盖模板
  • 实时、可工作的配置示例
  • SEO友好
  • 网站地图
  • Docker支持

基本用法

  1. 导入add_blog_to_fastapi函数
  2. 通过add_blog_to_fastapi函数运行实例化的FastAPI应用

这就是您需要做的全部

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. 添加第一篇博客文章

假设您的FastAPI应用定义在项目根目录下的main.py模块中,在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. 添加第一个页面

假设您的FastAPI应用定义在项目根目录下的main.py模块中,在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

高级用法

可以通过add_blog_to_fastapi函数配置fastapi_blog。

替换默认模板

这个例子类似于Django,您的本地模板将覆盖默认模板。

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",
    }

更改博客URL的位置

您可能想在根目录下有博客?

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",
    }

根URL上的博客

这是当您的博客/CMS需要在项目根目录下时使用

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)

将喜欢的文章添加到主页

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",
    }

添加不在博客文章列表中的页面

在您的博客的 pages 目录中,添加带有前置信息的 markdown 文件。然后您可以通过带有该名称的 URL 找到它。例如,将此 pages/about.md 添加到默认配置中,它将出现在 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).

安装和运行示例站点

选项 1:本地虚拟环境

您可以使用 pyproject.toml 文件将此安装到虚拟环境中

pip install fastapi-blog
make run

选项 2:Docker(本地 Dockerfile)

或者使用本地 Dockerfile 放入 Docker 容器中

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

选项 3:Docker(预构建)

或者使用来自 GitHub Container Registry 的预构建 Docker 镜像

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

如果您只想运行应用程序而无需自行构建,请选择此选项。

发布新版本

  1. 更新 pyproject.tomlfastapi_blog/__init__.py 中的版本

  2. 更新 changelog.md

  3. 在本地构建发行版

rm -rf dist
pip install -U build
python -m build
  1. 将发行版上传到 PyPI
pip install -U twine
python -m twine upload dist/*
  1. 在 GitHub 上创建一个新的发布并标记版本
git commit -am "Release for vXYZ"
make tag

贡献者

pydanny
丹尼尔·罗伊·格林菲尔德
audreyfeldroy
奥德丽·罗伊·格林菲尔德

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

fastapi-blog-0.6.0.tar.gz (18.0 kB 查看哈希值)

上传时间

构建分发

fastapi_blog-0.6.0-py3-none-any.whl (17.9 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面