yongrenjie / sphinx-exec-directive

Sphinx extension to run Python code blocks and display the output.
https://yongrenjie.github.io/sphinx-exec-directive/
MIT License
3 stars 3 forks source link

2 feature requests: "intertext" and REPL output support #8

Closed tikuma-lsuhsc closed 2 years ago

tikuma-lsuhsc commented 2 years ago

Just found this extension, and I really like the :context: option.

Trying this extension for a bit, I have a couple features that would enhance its output IMHO.

  1. :intertext: option (similar to \intertext LaTeX math command) to insert a (paragraph) text between 2 blocks.

    For example

    .. exec::
    :intertext: outputs
    
    x = 5
    print(f'{x=}')

    to expand to rst code:

    ::
    x = 5
    print(f'{x=}')
    
    outputs
    
    ::
    x=5
  2. :repl: option Run the code as if running it in the interpreter

    This option enables formatting the code lines as a doctest_block. If a line is free of any assignment operator, print the output of __repl__() of the line output.

    Using the same example from above,

    .. exec::
    :repl: true
    
    x = 5
    f'{x=}' # no print()

produces


  >>> x = 5
  >>> f'{x=}' # no print()

  ::

    'x=5'

What do you think?

yongrenjie commented 2 years ago

Hello! Thanks for this :) this extension is hardly a complete work, so there's tons of room for improvement. I like both of your suggestions! I'm not sure I have the time to implement it right now but if you feel like doing a PR, I'd be more than happy to look, and if not I could look to get around to it some time soon.

tikuma-lsuhsc commented 2 years ago

Glad you like these ideas! I don't know much about sphinx extensions atm but I'll look into it and see if I can cook something up.