Call this to embed IPython at the current point in your program. The first invocation of this will create an :class:`InteractiveShellEmbed` instance and then call it. Consecutive calls just call the already created instance. If you don't want the kernel to initialize the namespace
(**kwargs)
| 335 | |
| 336 | |
| 337 | def embed(**kwargs): |
| 338 | """Call this to embed IPython at the current point in your program. |
| 339 | |
| 340 | The first invocation of this will create an :class:`InteractiveShellEmbed` |
| 341 | instance and then call it. Consecutive calls just call the already |
| 342 | created instance. |
| 343 | |
| 344 | If you don't want the kernel to initialize the namespace |
| 345 | from the scope of the surrounding function, |
| 346 | and/or you want to load full IPython configuration, |
| 347 | you probably want `IPython.start_ipython()` instead. |
| 348 | |
| 349 | Here is a simple example:: |
| 350 | |
| 351 | from IPython import embed |
| 352 | a = 10 |
| 353 | b = 20 |
| 354 | embed(header='First time') |
| 355 | c = 30 |
| 356 | d = 40 |
| 357 | embed() |
| 358 | |
| 359 | Full customization can be done by passing a :class:`Config` in as the |
| 360 | config argument. |
| 361 | """ |
| 362 | config = kwargs.get('config') |
| 363 | header = kwargs.pop('header', u'') |
| 364 | compile_flags = kwargs.pop('compile_flags', None) |
| 365 | if config is None: |
| 366 | config = load_default_config() |
| 367 | config.InteractiveShellEmbed = config.TerminalInteractiveShell |
| 368 | kwargs['config'] = config |
| 369 | using = kwargs.get('using', 'sync') |
| 370 | if using : |
| 371 | kwargs['config'].update({'TerminalInteractiveShell':{'loop_runner':using, 'colors':'NoColor', 'autoawait': using!='sync'}}) |
| 372 | #save ps1/ps2 if defined |
| 373 | ps1 = None |
| 374 | ps2 = None |
| 375 | try: |
| 376 | ps1 = sys.ps1 |
| 377 | ps2 = sys.ps2 |
| 378 | except AttributeError: |
| 379 | pass |
| 380 | #save previous instance |
| 381 | saved_shell_instance = InteractiveShell._instance |
| 382 | if saved_shell_instance is not None: |
| 383 | cls = type(saved_shell_instance) |
| 384 | cls.clear_instance() |
| 385 | frame = sys._getframe(1) |
| 386 | shell = InteractiveShellEmbed.instance(_init_location_id='%s:%s' % ( |
| 387 | frame.f_code.co_filename, frame.f_lineno), **kwargs) |
| 388 | shell(header=header, stack_depth=2, compile_flags=compile_flags, |
| 389 | _call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno)) |
| 390 | InteractiveShellEmbed.clear_instance() |
| 391 | #restore previous instance |
| 392 | if saved_shell_instance is not None: |
| 393 | cls = type(saved_shell_instance) |
| 394 | cls.clear_instance() |
no test coverage detected