我有一个研究内容,可作为交互式 Jupyter Notebook 使用。我想将此笔记本作为博客文章提供,并自动进行必要的转换。
我可以使用命令行和编程工具(如
nbconvert
)将笔记本文件转换为静态 HTML 内容,并首先导出图像。
将来自文件系统或 URL 的带有图像的静态 HTML 导入 Ghost 的最佳选择是什么,这样我就不需要手动重新编辑/放置图像,因为有大量图表作为图像( >30).
如果 Ghost 可以导入 SVG 就更好了(图表可读性更好),但在紧要关头,普通的 PNG 也可以。
或者如果有简单的手动方法,例如复制粘贴包含图像的 HTML,请告诉我。
您似乎可以简单地将 Jupyter 笔记本导出为带有内联图像的静态 HTML,在 Web 浏览器中打开此 HTML 文件,然后复制粘贴到 Ghost 编辑器中。需要对 Jupyter 输出进行一些小的调整。
Ghost 编辑器将正确上传内嵌图像。
首先确保如何为
matplotlib
和您使用的其他图形后端正确设置一些设置:
# https://stackoverflow.com/a/58393562/315168
logging.getLogger('matplotlib.font_manager').disabled = True
# Plot larger and high DPI images
matplotlib.rcParams['figure.figsize'] = (12, 12)
matplotlib.rcParams['figure.dpi'] = 400
# Disable scientific notation on axes
# https://stackoverflow.com/a/28373421/315168
matplotlib.rcParams["axes.formatter.limits"] = (-99, 99)
# English decimal separator
matplotlib.rcParams["axes.formatter.use_locale"] = True
# Matplotlib developer interface and memory management are unideal
# https://stackoverflow.com/questions/27476642/matplotlib-get-rid-of-max-open-warning-output
matplotlib.rcParams.update({'figure.max_open_warning': 0})
# Get rid of all matplotlib warnings,
# as there are some that are irrelevant
warnings.filterwarnings("ignore", module = "pandas\..*" )
warnings.filterwarnings("ignore", module = "matplotlib\..*" )
# Because we use a special font
# we want to raster theimage output,
# even though this will make image quality lower
# on various devices
matplotlib_inline.backend_inline.set_matplotlib_formats("png")
然后我们可以通过运行 Jupyter 本身或 Visual Studio Code 来渲染笔记本。使用内联图像保存生成的笔记本。
运行
nbconvert
输出静态 HTML:
jupyter nbconvert --to=html --no-input --embed-images --output-dir html-export research.ipynb
在网络浏览器中打开
html-export/research.html
。我用的是火狐浏览器。
在 Ghost 编辑器中创建新的博客文章。
将静态网页中的所有内容复制粘贴到 Ghost 编辑器。