1

I am writing a py project and use sphinx for the documentation. This is working fine so far.

I know it is possible to include code with something like this:



class Foo():
    """Documatation for Foo

    .. code-block:: python

         # Use Foo
         f = Foo()
    """

...

I have some larger code sections, which should be included in the documentation. In the upper example, I have the trouble, when e.g. the name of Foo() is changing, I have to change it twice.

Is there a way include something like this in the code?

""" ..docu-code-from-hear """

a = 1
b = 2
c = a+b

""" ..docu-code-to-hear """
mzjn
  • 48,958
  • 13
  • 128
  • 248
Matthias
  • 11
  • 1
  • Can you explain in more detail what you want? There is nothing built-in that resembles `docu-code-from-hear` and `docu-code-to-hear`. What would be the result from that? The example has two docstrings, but an object (module, function, class, method) can have only one docstring. – mzjn Jan 09 '22 at 14:59
  • Ok the 2 docstrings here are missleading. In my real case want 20 lines of code (out of 50 from the whole function) to be included in the documentation. I know that I could just "copy" these lines into a docstring (or an rst file) with the ..code-block:: statement, but with future changes in the code I would always have to correct the documentation manually... I would like to avoid this manual action. – Matthias Jan 09 '22 at 15:24
  • Not sure if this helps, but have you tried `literalinclude`? https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude? – mzjn Jan 09 '22 at 15:29
  • `literalinclude` works for the moment. Haven't seen the option to select some lines before. Thanks – Matthias Jan 09 '22 at 17:39

0 Answers0