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

Allow REPL-style input/output #6

Open yongrenjie opened 3 years ago

yongrenjie commented 3 years ago

When exec'ing the doctests we would have to strip away the first n characters. Probably this will be set with yet another option.

yongrenjie commented 1 year ago

from https://stackoverflow.com/a/25558325 (slightly updated for py3), the following allows a chunk of text to be directly executed as if in a REPL:

import sys
import code

infile = open('cmd.py')
def readcmd(prompt):
    line = infile.readline()
    if not line:
        sys.exit(0)

    print(prompt, end="")
    print(line.rstrip())
    return line.rstrip()

code.interact(readfunc=readcmd)