在 Astro 博客中引用 Python 和 Jupyter Notebook
展示博客如何直接引用 uv 管理的 Python 源文件和 Jupyter Notebook 已保存内容。
这篇文章是 Python 内容引用链的最小示例。Python 项目由 uv 管理,博客构建只读取源文件和 Notebook 已保存的输出,不会执行其中的程序。
引用 .py 文件
下面的代码直接来自仓库中的 app/demo.py。修改源文件并重新生成内容后,文章中的代码会同步更新。
"""Small uv-managed demo used by the Python content article."""
def build_greeting(name: str) -> str:
"""Return a greeting that can be reused by scripts and notebooks."""
return f"你好,{name}!欢迎查看博客里的 Python 示例。"
def main() -> None:
print(build_greeting("想飞的鱼"))
if __name__ == "__main__":
main()
本地可以直接运行:
uv run python -m app.demo
引用 .ipynb 文件
下面的内容来自 notebooks/demo.ipynb,包含 Notebook 的 Markdown 单元格、代码单元格和已经保存的标准输出。
从 app 引用 Python 程序
这个 Notebook 只展示已经保存的代码和输出,博客构建不会重新执行它。
In [1]:
from app.demo import build_greeting
print(build_greeting("Notebook"))你好,Notebook!欢迎查看博客里的 Python 示例。
更新博客中的 Python 内容
修改 .py 或 .ipynb 后执行:
npm run python:render
提交前可以检查生成内容是否同步:
npm run python:check
这种方式保持了静态博客构建的稳定性:Cloudflare Pages 不负责执行 Notebook,也不需要在部署阶段重新计算输出。
讨论与反馈