Skip to main content

Sphinx extension to render the image by script or command

Project description

A sphinx extension to render the image/figure generated by the command body.

author:

“Yongping Guo”<guoyoooping@163.com>

1. Installing and setup

pip install sphinxcontrib-cmd2img

And just add sphinxcontrib.cmd2img to the list of extensions in the conf.py file. For example:

extensions = ['sphinxcontrib.cmd2img']

2. Introduction and examples

In rst we we use image and figure directive to render image/figure in the target html document, which give us much convenience. In fact we could rending more things than that.

Sometime some command would convert or generate a image, we would like to render it efficiently and directly, for example:

ditaa is a small command-line utility that can convert diagrams drawn using ascii art (‘drawings’ that contain characters that resemble lines like | / - ), into proper bitmap graphics. We could use the following directive to render the image generated by ditaa:

.. cmd2img:: ditaa

      +--------+   +-------+    +-------+
      |        | --+ ditaa +--> |       |
      |  Text  |   +-------+    |diagram|
      |Document|   |!magic!|    |       |
      |     {d}|   |       |    |       |
      +---+----+   +-------+    +-------+
          :                         ^
          |       Lots of work      |
          +-------------------------+

Or use the following directive to render it as a figure, for a figure, we can add a caption, to render it as .svg file, we add –svg in the command line:

.. cmd2fig:: ditaa --svg
   :caption: figure 1. An example to use ditaa to render a figure

      +--------+   +-------+    +-------+
      |        | --+ ditaa +--> |       |
      |  Text  |   +-------+    |diagram|
      |Document|   |!magic!|    |       |
      |     {d}|   |       |    |       |
      +---+----+   +-------+    +-------+
          :                         ^
          |       Lots of work      |
          +-------------------------+

Another example is gnuplot:

.. cmd2img:: gnuplot
    :image: gnuplot_test.png

    set output 'gnuplot_test.png'
    set terminal pngcairo
    set y2tics -100, 10
    set ytics nomirror
    plot sin(1/x) axis x1y1, 100*cos(x) axis x1y2

Another example is python:

.. cmd2img:: python3
   :image: test.png
   :caption: 例子

   import numpy as np
   import matplotlib.pyplot as plt

   theta = np.linspace(0, 2 * np.pi, 200)
   r = 20
   x = r*np.cos(theta)
   y = r*np.sin(theta)

   plt.figure(figsize=(6, 6), dpi=300)
   plt.text(3.14, 1, r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center', fontsize=20)
   plt.plot(x, y)
   plt.xlabel(r'$\varepsilon$',fontsize=20)
   plt.ylabel(r'$\alpha$',fontsize=20)
   plt.title("A simple plot")
   plt.savefig("test.png", dpi=120)

Another example is convert, it will be rendered as a gif in the target:

.. cmd2img:: convert rose: -duplicate 29 -virtual-pixel Gray -distort SRT '%[fx:360.0*t/n]' -set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif

Another example:

.. cmd2img:: dot -T png

    digraph example {
        a [label="sphinx", href="http://www.sphinx-doc.org/en/master/usage/extensions/index.html", target="_top"];
        b [label="other"];
        a -> b;
    }

3 Options

sphinxcontrib-cmd2img provide some options for easy use.

3.1 command options

For command options, you should add it right after the command, for example:

.. cmd2fig:: ditaa --no-antialias
   :caption: figure 2. illustration for command option.

    +--------+   +-------+    +-------+
    |        | --+ ditaa +--> |       |
    |  Text  |   +-------+    |diagram|
    |Document|   |!magic!|    |       |
    |     {d}|   |       |    |       |
    +---+----+   +-------+    +-------+
        :                         ^
        |       Lots of work      |
        +-------------------------+

3.2 sphinxcontrib-cmd2img options

  • image:

    For those command whose the output name is embeded in the body, Users should copy the name here.

  • show_source:

    for text generated iamge, if the source code is shown.

  • watermark:

    Add water mark in the image

  • gravity:

    watermark gravity, see detail imagematick command convert -draw

  • location:

    watermark location, see detail imagematick command convert -draw

  • fill:

    watermark contention, see detail imagematick command convert -draw

  • pointsize:

    watermark pointsize, see detail imagematick command convert -draw

  • font:

    watermark font, see detail imagematick command convert -draw

For example:

.. cmd2fig:: gnuplot
    :caption: 在plot 命令里指定范围
    :image: gnuplot_test.png
    :width: 600

    set output 'gnuplot_test.png'
    set terminal pngcairo
    plot [-5:5] (sin(1/x) - cos(x))*erfc(x)

5. License

GPLv3

6. Changelog

0.1 Initial upload. 0.2 Correct minor typo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinxcontrib-cmd2img-1.0.tar.gz (8.8 kB view details)

Uploaded Source

File details

Details for the file sphinxcontrib-cmd2img-1.0.tar.gz.

File metadata

  • Download URL: sphinxcontrib-cmd2img-1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0a3

File hashes

Hashes for sphinxcontrib-cmd2img-1.0.tar.gz
Algorithm Hash digest
SHA256 a7ee9656a852f38eda416c86c19b4f067a9064b37dfdbcc42bafc178837a8638
MD5 12fff801f0aaa8d6c2a28f0b11d1d560
BLAKE2b-256 a6b68a33faa0105a1678e9d000c28149e4850b8a27bb7be3b5d9845855070451

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page