Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the variable value in a sphinx document

Imagining that I have a module names module.py and inside it, I have a private variable named _VAR, I want to print the value of it in my sphinx documentation.

module.py

_VAR = 20

And in sphinx, I want something like this:

index.rst

The value of ``module._VAR`` is :py:print:`module._VAR`

And the output would be:

The value of module._VAR is 20
like image 918
LuRsT Avatar asked Sep 30 '22 15:09

LuRsT


1 Answers

It is completely possible to do what you want via the autodoc functionality. You just need to make sure that the value is explicitly documented in the code like so:

_VAR = 20 #: Notice the colon, this tells sphinx to use this comment as docstring for this value

You can then get this value into your documentation by either doing automodule with include-private or by explicitly doing an autodata on just that variable name.

Note that you can use the same tactic (#:) to explicitly document the attributes of classes, and they will show up in the documentation for an autoclass (or automodule since that implicitly will doc the class), including any literal values they are initialized to.

like image 184
aruisdante Avatar answered Oct 04 '22 05:10

aruisdante