Python中如何让个人博客支持展示jupyter notebook?
如题...
hexo,wordpress 之类的,主要是展示代码的运行结果
Python中如何让个人博客支持展示jupyter notebook?
9 回复
最好不要吧…… 毕竟这是需要后端去运行代码的东西。实在要做也要将 jupyter 后端和博客后端分离开……
核心方案: 将 Jupyter Notebook 转换为 HTML 或 Markdown 格式,然后嵌入你的博客框架。
具体实现(以静态博客为例):
-
转换 Notebook:
# 安装 nbconvert pip install nbconvert # 转换为 HTML(带完整样式) jupyter nbconvert --to html your_notebook.ipynb # 转换为 Markdown(更轻量,便于嵌入) jupyter nbconvert --to markdown your_notebook.ipynb -
嵌入博客:
- HTML 输出: 直接将生成的
.html文件内容复制到你的博客文章模板中。 - Markdown 输出: 将生成的
.md文件内容粘贴到你的 Markdown 博客文章里(确保你的博客支持渲染 Markdown 和代码块)。
- HTML 输出: 直接将生成的
如果你的博客是动态的(如 Django/Flask):
- 使用
nbconvert的 Python API 在视图函数中动态转换:import nbformat from nbconvert import HTMLExporter def notebook_to_html(notebook_path): with open(notebook_path) as f: nb = nbformat.read(f, as_version=4) html_exporter = HTMLExporter() body, _ = html_exporter.from_notebook_node(nb) return body - 将返回的 HTML 字符串传递给模板渲染。
一句话建议: 用 nbconvert 转成 HTML/Markdown 再嵌入博客。
好像可以静态输出的。或者放 github 上。
输出 HTML
试过,效果不是很好
可以输出 md, html
博客本来就是给别人看的,肯定不可以运行啊
html,效果还行啊,也有目录插件,你找找


