Search code examples
pythonpython-sphinxmkdocs

Embed images in Python documentation


I have a python project that I want to create the documentation...

In this documentation, it is crucial allowing embedding images. Also, I would like to not create a separate file. In others words: the *.py file would contain both the script and the documentation (that has images embed).

Of course, later on this *.py would be processed by a tool to create a markdown or HTML report.

I took a look at mkDocs and Sphinx, but I am not sure if they support these requirements.

Does anyone know how to do something similar with these tools (or another)?


Solution

  • As @mzjn suggested, you can write docstrings in your python modules. Docstrings may be valid reStructuredText. reStructuredText syntax supports inline images. The given file name of the image must be either relative to the source file or absolute to the documentation root. You must have a separate image file.

    To build docs with Sphinx, you will need a set of reStructuredText files from which you build your documentation. You can use Sphinx quick-start to generate the .rst source files.

    Once you have written your docstrings, and have created .rst source files, and configured your conf.py to use the autodoc module to pull in your module's docstrings, then you can use Sphinx and its autodoc module to build documentation in supported formats, including HTML.

    Note that images must be in a supported format for the builder's output.