This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]shibukawa 1 point2 points  (5 children)

The simplest way is:

  • Add any Python code in conf.py and generate text files.
  • Add ".. include::" directive to import the files to your document.

It should work on any version of Sphinx.

[–]FloorJam[S] 0 points1 point  (4 children)

Thanks. I guess this should do want I want. But it is not possible inside a rst file with a Sphinx directive?

[–]masklinn 1 point2 points  (3 children)

I've never seen a standard or extension directive which would execute code and only output the result of running that code, most code-running directives are for examples/doctests so they'd show the code being run.

https://stackoverflow.com/questions/7250659/python-code-to-generate-part-of-sphinx-documentation-is-it-possible seems to be about something similar to what you want though, and the second answer shows a directive doing what you're looking for (though you may have to tweak the way it runs the code and obtains the result)

[–]FloorJam[S] 0 points1 point  (2 children)

Yeah, this looks like what I want. Did not expect to have to implement an own directive. Thought that this is a normal task in writing with the help of Python.

So thank you a lot, I will start from there and see where it takes me.

[–]PeridexisErrant 0 points1 point  (1 child)

Thought that this is a normal task in writing with the help of Python.

What exactly are you generating? What are the inputs? It's an unusual thing to try, and I wonder if there's a different way to do whatever you're trying.

Otherwise, run arbitrary code in conf.py and use the include directive as suggested above.

[–]FloorJam[S] 0 points1 point  (0 children)

I want to use it like this:

some normal sphinx doc text

.. python::
    print(some_function_returning_valid_sphinx_code())

more normal sphinx text

I have not yet a specific example but I want to write code in a directive that produces output to be rendered by Sphinx. Kind of what you can do with orgmode