如何在 github 上的 Jupyter Notebook 中显示 SVG 图像

Chr*_*_77 7 svg github jupyter-notebook

我想分享一个 Jupyter 笔记本的链接,我在其中演示某些计算并在 SVG 文件中显示其输出(理想情况下,仅使用标准库)。在这种情况下,使用像 matplotlib 这样的绘图库是不切实际的,因为图像没有图形的结构(我尝试并发现为我需要显示的输出类型编写一个小型绘图库是最实用的) 。

我首选的显示 Notebook 的平台是 github,因为我想使用 Notebook 作为我的计算引擎的演示。我可以使用以下命令在本地笔记本中显示 SVG 文件:

from IPython.core.display import SVG
SVG(filename='test.svg')
Run Code Online (Sandbox Code Playgroud)

但是,如果我将 Notebook 上传到 github,则不会渲染图像,并且输出如下所示:

Out[1]:SVG图像

我当前的解决方法是在我的私人网站上显示 HTML 渲染的笔记本。不过,如果能够直接在 github 上显示带有渲染图像的 Notebook 就更好了。

Kar*_*Żak 5

您可以使用我的名为ipyplot 的小包来实现您正在寻找的东西

import ipyplot

ipyplot.plot_images(
    ['Freesample.svg'], # images should be passed in as an array
    img_width=250,
    force_b64=True # this is important to be able to render the image correctly on GitHub
)
Run Code Online (Sandbox Code Playgroud)

force_b64flag 将确保图像转换为 base64 字符串,以便它能够在 GitHub 上正确呈现。唯一的限制是,如果单个笔记本中有太多图像 - GitHub 渲染器将会崩溃......

输出:
在此输入图像描述