def embed(globals=None, locals=None, configure=None, vi_mode=False, history_filename=None, title=None, startup_paths=None, patch_stdout=False, return_asyncio_coroutine=False): """ Call this to embed Python shell at the current point in your program. It's similar to `IPython.embed` and `bpython.embed`. :: from prompt_toolkit.contrib.repl import embed embed(globals(), locals()) :param vi_mode: Boolean. Use Vi instead of Emacs key bindings. :param configure: Callable that will be called with the `PythonRepl` as a first argument, to trigger configuration. :param title: Title to be displayed in the terminal titlebar. (None or string.) """ assert configure is None or callable(configure) # Default globals/locals if globals is None: globals = { '__name__': '__main__', '__package__': None, '__doc__': None, '__builtins__': six.moves.builtins, } locals = locals or globals def get_globals(): return globals def get_locals(): return locals # Create eventloop. if return_asyncio_coroutine: use_asyncio_event_loop() # Create REPL. repl = PythonRepl(get_globals=get_globals, get_locals=get_locals, vi_mode=vi_mode, history_filename=history_filename, startup_paths=startup_paths) if title: repl.terminal_title = title if configure: configure(repl) app = repl.app # Start repl. patch_context = patch_stdout_context() if patch_stdout else DummyContext() if return_asyncio_coroutine: # XXX def coroutine(): with patch_context: while True: iterator = iter(app.run_async().to_asyncio_future()) try: while True: yield next(iterator) except StopIteration as exc: text = exc.args[0] repl._process_text(text) return coroutine() else: with patch_context: repl.run()
def embed( globals=None, locals=None, configure: Optional[Callable[[PythonRepl], None]] = None, vi_mode: bool = False, history_filename: Optional[str] = None, title: Optional[str] = None, startup_paths=None, patch_stdout: bool = False, return_asyncio_coroutine: bool = False, ) -> None: """ Call this to embed Python shell at the current point in your program. It's similar to `IPython.embed` and `bpython.embed`. :: from prompt_toolkit.contrib.repl import embed embed(globals(), locals()) :param vi_mode: Boolean. Use Vi instead of Emacs key bindings. :param configure: Callable that will be called with the `PythonRepl` as a first argument, to trigger configuration. :param title: Title to be displayed in the terminal titlebar. (None or string.) :param patch_stdout: When true, patch `sys.stdout` so that background threads that are printing will print nicely above the prompt. """ # Default globals/locals if globals is None: globals = { "__name__": "__main__", "__package__": None, "__doc__": None, "__builtins__": builtins, } locals = locals or globals def get_globals(): return globals def get_locals(): return locals # Create REPL. repl = PythonRepl( get_globals=get_globals, get_locals=get_locals, vi_mode=vi_mode, history_filename=history_filename, startup_paths=startup_paths, ) if title: repl.terminal_title = title if configure: configure(repl) # Start repl. patch_context: ContextManager = (patch_stdout_context() if patch_stdout else DummyContext()) if return_asyncio_coroutine: async def coroutine(): with patch_context: await repl.run_async() return coroutine() else: with patch_context: repl.run()