Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

literalinclude : how to include only a variable using pyobject?

I'm using Sphinx to document parts of my Python code.

I would like to include only a variable from my class in my documentation.

For example, I tried this but without success :

.. literalinclude:: MyClass.py
    :pyobject: MyClass.aVariable
    :language: python

I see only one solution but if my code change in the next release the line numbers of the variable could change :

.. literalinclude:: MyClass.py
    :language: python
    :lines: 2-3

Is there another solution to filter only this variable ?

like image 520
Sirius Avatar asked Jun 20 '26 12:06

Sirius


1 Answers

As workaround, I use the start-after and end-before options but I have to add two comments in my code before and after the variables.

For example :

The class :

class MyClass():
    # Start variables
    aVariable = {"a":123 , "b":456}
    # End variables 
        def __init__(self):
            pass

The RST file :

.. literalinclude:: MyClass.py
    :start-after: # Start variables 
    :end-before: # End variables 
    :language: python

It's not perfect, but it works.

like image 178
Sirius Avatar answered Jun 23 '26 03:06

Sirius