在 sphinx-gallery 中捕获 Graphviz 图形
Capturing Graphviz figures in sphinx-gallery
我正在生成带有 sphinx-gallery
, and would like to embed graphviz
输出的示例,类似于捕获 matplotlib
数字的方式。
下面是我当前进度的屏幕截图。请注意,捕获的 Out
显示了数字的字符串或字节表示形式。
我希望 Out
成为 image/svg。
我调查了以下内容:
graphviz
可以 export SVG for Jupyter notebooks through a private _repr_svg_()
method,但 sphinx-gallery
将其解释为字符串。
sphinx-gallery
有 experimental support for "Custom Image Scrapers",但我想在我的示例中避免胶水代码,其中我“ 写入图像文件,然后将它们读回生成的 html ."
graphviz
文档是如何做到这一点的? By generating .png figures ahead of time and embedding them with .. image::
directives.
更新 (2020-11-26):添加一个 _repr_html_
包装 graphviz 的 _repr_svg_
方法似乎是最短路线。我实现了一个粗略的版本:
这是我已经尝试过的代码:
# File: examples/plot_graphviz_svg.py
"""
==================================================
Capturing the output of Graphviz in Sphinx-Gallery
==================================================
This is a quick demo trying to capture the SVG output
from Graphviz and embed it in a Sphinx Gallery.
"""
import graphviz
dig = graphviz.Digraph('G', filename='hello.gv')
dig.edge('hello', 'world')
dig._repr_svg_()
# %%
# The first output should be above, the next should appear below:
dig2 = graphviz.Digraph('G2', filename="hello2.gv")
dig2.edge('world', 'hello')
dig.pipe()
最小 sphinx
conf.py
:
project = 'sphinx-graphviz-svg'
copyright = '2020, Alexander L. Hayes'
author = 'Alexander L. Hayes'
release = '0.0.1'
extensions = [
'sphinx_gallery.gen_gallery',
]
templates_path = ['_templates']
exclude_patterns = []
html_theme = 'alabaster'
html_static_path = ['_static']
还有一个最小的 index.rst
链接到示例库:
Welcome to sphinx-graphviz-svg's documentation!
===============================================
.. toctree::
:hidden:
:maxdepth: 1
:caption: Example Gallery
auto_examples/index
我在 graphviz
repository here: #121 中有一个开放的拉取请求。
如果您不耐烦,这里有一种使用相当小的包装纸的可能性:
class PlotGraphviz:
def __init__(self, dot_string):
self.dot_string = dot_string
def _repr_html_(self):
return graphviz.Source(self.dot_string)._repr_svg_()
如果将来合并拉取请求,这会更清晰。
现在:
# File: examples/plot_graphviz_svg.py
"""
==================================================
Capturing the output of Graphviz in Sphinx-Gallery
==================================================
This is a quick demo trying to capture the SVG output
from Graphviz and embed it in a Sphinx Gallery.
"""
import graphviz
class PlotGraphviz:
def __init__(self, dot_string):
self.dot_string = dot_string
def _repr_html_(self):
return graphviz.Source(self.dot_string)._repr_svg_()
# %%
# First example:
dig = graphviz.Digraph()
dig.edge("hello", "world")
PlotGraphviz(str(dig))
# %%
# More info ...
渲染到:
我正在生成带有 sphinx-gallery
, and would like to embed graphviz
输出的示例,类似于捕获 matplotlib
数字的方式。
下面是我当前进度的屏幕截图。请注意,捕获的 Out
显示了数字的字符串或字节表示形式。
我希望 Out
成为 image/svg。
我调查了以下内容:
graphviz
可以 export SVG for Jupyter notebooks through a private_repr_svg_()
method,但sphinx-gallery
将其解释为字符串。sphinx-gallery
有 experimental support for "Custom Image Scrapers",但我想在我的示例中避免胶水代码,其中我“ 写入图像文件,然后将它们读回生成的 html ."graphviz
文档是如何做到这一点的? By generating .png figures ahead of time and embedding them with.. image::
directives.
更新 (2020-11-26):添加一个 _repr_html_
包装 graphviz 的 _repr_svg_
方法似乎是最短路线。我实现了一个粗略的版本:
这是我已经尝试过的代码:
# File: examples/plot_graphviz_svg.py
"""
==================================================
Capturing the output of Graphviz in Sphinx-Gallery
==================================================
This is a quick demo trying to capture the SVG output
from Graphviz and embed it in a Sphinx Gallery.
"""
import graphviz
dig = graphviz.Digraph('G', filename='hello.gv')
dig.edge('hello', 'world')
dig._repr_svg_()
# %%
# The first output should be above, the next should appear below:
dig2 = graphviz.Digraph('G2', filename="hello2.gv")
dig2.edge('world', 'hello')
dig.pipe()
最小 sphinx
conf.py
:
project = 'sphinx-graphviz-svg'
copyright = '2020, Alexander L. Hayes'
author = 'Alexander L. Hayes'
release = '0.0.1'
extensions = [
'sphinx_gallery.gen_gallery',
]
templates_path = ['_templates']
exclude_patterns = []
html_theme = 'alabaster'
html_static_path = ['_static']
还有一个最小的 index.rst
链接到示例库:
Welcome to sphinx-graphviz-svg's documentation!
===============================================
.. toctree::
:hidden:
:maxdepth: 1
:caption: Example Gallery
auto_examples/index
我在 graphviz
repository here: #121 中有一个开放的拉取请求。
如果您不耐烦,这里有一种使用相当小的包装纸的可能性:
class PlotGraphviz:
def __init__(self, dot_string):
self.dot_string = dot_string
def _repr_html_(self):
return graphviz.Source(self.dot_string)._repr_svg_()
如果将来合并拉取请求,这会更清晰。
现在:
# File: examples/plot_graphviz_svg.py
"""
==================================================
Capturing the output of Graphviz in Sphinx-Gallery
==================================================
This is a quick demo trying to capture the SVG output
from Graphviz and embed it in a Sphinx Gallery.
"""
import graphviz
class PlotGraphviz:
def __init__(self, dot_string):
self.dot_string = dot_string
def _repr_html_(self):
return graphviz.Source(self.dot_string)._repr_svg_()
# %%
# First example:
dig = graphviz.Digraph()
dig.edge("hello", "world")
PlotGraphviz(str(dig))
# %%
# More info ...
渲染到: