Call this to embed IPython at the current point in your program. The first invocation of this will create a :class:`terminal.embed.InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. If you don't want the kernel to initialize
(*, header="", compile_flags=None, **kwargs)
| 355 | |
| 356 | |
| 357 | def embed(*, header="", compile_flags=None, **kwargs): |
| 358 | """Call this to embed IPython at the current point in your program. |
| 359 | |
| 360 | The first invocation of this will create a :class:`terminal.embed.InteractiveShellEmbed` |
| 361 | instance and then call it. Consecutive calls just call the already |
| 362 | created instance. |
| 363 | |
| 364 | If you don't want the kernel to initialize the namespace |
| 365 | from the scope of the surrounding function, |
| 366 | and/or you want to load full IPython configuration, |
| 367 | you probably want `IPython.start_ipython()` instead. |
| 368 | |
| 369 | Here is a simple example:: |
| 370 | |
| 371 | from IPython import embed |
| 372 | a = 10 |
| 373 | b = 20 |
| 374 | embed(header='First time') |
| 375 | c = 30 |
| 376 | d = 40 |
| 377 | embed() |
| 378 | |
| 379 | Parameters |
| 380 | ---------- |
| 381 | |
| 382 | header : str |
| 383 | Optional header string to print at startup. |
| 384 | compile_flags |
| 385 | Passed to the `compile_flags` parameter of :py:meth:`terminal.embed.InteractiveShellEmbed.mainloop()`, |
| 386 | which is called when the :class:`terminal.embed.InteractiveShellEmbed` instance is called. |
| 387 | **kwargs : various, optional |
| 388 | Any other kwargs will be passed to the :class:`terminal.embed.InteractiveShellEmbed` constructor. |
| 389 | Full customization can be done by passing a traitlets :class:`Config` in as the |
| 390 | `config` argument (see :ref:`configure_start_ipython` and :ref:`terminal_options`). |
| 391 | """ |
| 392 | config = kwargs.get('config') |
| 393 | if config is None: |
| 394 | config = load_default_config() |
| 395 | config.InteractiveShellEmbed = config.TerminalInteractiveShell |
| 396 | kwargs["config"] = config |
| 397 | using = kwargs.get("using", "sync") |
| 398 | colors = kwargs.pop("colors", "nocolor") |
| 399 | if using: |
| 400 | kwargs["config"].update( |
| 401 | { |
| 402 | "TerminalInteractiveShell": { |
| 403 | "loop_runner": using, |
| 404 | "colors": colors, |
| 405 | "autoawait": using != "sync", |
| 406 | } |
| 407 | } |
| 408 | ) |
| 409 | # save ps1/ps2 if defined |
| 410 | ps1 = None |
| 411 | ps2 = None |
| 412 | try: |
| 413 | ps1 = sys.ps1 |
| 414 | ps2 = sys.ps2 |
no test coverage detected
searching dependent graphs…