项目作者: j-martin

项目描述 :
Rough Sphinx extension for diagrams
高级语言: Python
项目地址: git://github.com/j-martin/sphinx-diagrams.git
创建时间: 2020-08-20T17:46:56Z
项目社区:https://github.com/j-martin/sphinx-diagrams

开源协议:BSD 2-Clause "Simplified" License

下载


sphinx-diagrams

This is a rough (incomplete, but working) Sphinx extension for
Diagrams. Please refer to the open
issue for Sphinx support
for
the latest up-to-date version when officially supported.

Usage

Install

  1. $ pip3 install sphinx-diagrams

Adding the extension

conf.py

  1. extensions = [
  2. "sphinx_diagrams",
  3. ]

Adding the diagram (inline)

The simplest way is to use SphinxDiagram and inline the code in your document.
Consider using an external diagram/python script (see below) as it has much
shorter iteration loop than running sphinx and most likely better supported by
your editor or IDE.

source/index.rst

  1. Diagram - Deployment
  2. ====================
  3. .. diagrams::
  4. from diagrams import Cluster
  5. from diagrams.k8s.compute import Deployment
  6. from sphinx_diagrams import SphinxDiagram
  7. with SphinxDiagram(title="GKE"):
  8. with Cluster("GCP Project"):
  9. KubernetesEngine("Primary Cluster")

Adding a diagram (external python code)

Write the code

source/diagrams_infrastructure.py

You can still use SphinxDiagram in your own code. This class handles arguments
like :filename: and visibility (showing the diagram via xdg-open/open) for
you.

  1. from diagrams import Cluster
  2. from diagrams.k8s.compute import Deployment
  3. from sphinx_diagrams import SphinxDiagram
  4. with SphinxDiagram(title="GKE"):
  5. with Cluster("GCP Project"):
  6. KubernetesEngine("Primary Cluster")

Alternatively, you can use Diagram (from diagrams) directly. Note that the
extension will pass two arguments to your diagram script. The first one is the
filename as sys.argv[1] it expects (it needs to match the one outputted by
diagrams) and the value false as sys.argv[2]. The later can be used to
tell your script not to show (open) the generate diagram.

  1. import sys
  2. from diagrams import Diagram, Cluster
  3. from diagrams.gcp.compute import KubernetesEngine
  4. with Diagram("GKE", filename=sys.argv[1], show=sys.argv[2].lower() == 'true'):
  5. with Cluster("GCP Project"):
  6. KubernetesEngine("Primary Cluster")

Referencing the diagram

source/index.rst

  1. Diagram - Deployment
  2. ====================
  3. .. diagrams:: diagrams_infrastructure.py
  4. :filename: diagram-deployment.png

If using SphinxDiagram (above) or if the filename of the diagram script is the
same as the output file (e.g.: diagrams_infrastructure.py => diagrams_infrastructure.png) then the :filename: is not necessary.

  1. Diagram - Deployment
  2. ====================
  3. .. diagrams:: diagrams_infrastructure.py

Credit

This code is based on
sphinx.graphviz.

SPDX-License-Identifier: BSD-2-Clause