Can sphinx.ext.viewcode allow me to include the _content_ of the source code in my doc in addition to linking to it?

60 Views Asked by At

I'm trying to avoid having to do literal includes with specific line numbers to embed snippets of my code in my docs.

I'm asking almost this same question: sphinx - How to include a Python function as source code

I want to include the content of the object referenced in my rst doc and have it appear as a highlighted python code snippet.

In that post no one mentions the sphinx.ext.viewcode plugin though, which includes the source code and generates links to it.

If sphinx.ext.viewcode can include the code and links in my docs is there a way to use it to include that code as a snippet in my doc pages directly as well?

For example here is what one of the links looks like: my_rendered_docs/_modules/module/sub_module/another_sub_module.html#my_func

Can I include that content directly as a python code block through some relative pathing? Or does viewcode support a directive for this?

Edit

Per comments pyobject is what I needed.

.. literalinclude :: ../../my_proj/some_path/my_module.py
   :pyobject: a_function_inside_my_module
   :linenos:
   :language: python
0

There are 0 best solutions below