| 1 |
"""Quick code snippets for embedding IPython into other programs. |
| 2 |
|
| 3 |
See embed_class_long.py for full details, this file has the bare minimum code for |
| 4 |
cut and paste use once you understand how to use the system.""" |
| 5 |
|
| 6 |
#--------------------------------------------------------------------------- |
| 7 |
# This code loads IPython but modifies a few things if it detects it's running |
| 8 |
# embedded in another IPython session (helps avoid confusion) |
| 9 |
|
| 10 |
try: |
| 11 |
get_ipython |
| 12 |
except NameError: |
| 13 |
banner=exit_msg='' |
| 14 |
else: |
| 15 |
banner = '*** Nested interpreter ***' |
| 16 |
exit_msg = '*** Back in main IPython ***' |
| 17 |
|
| 18 |
# First import the embed function |
| 19 |
from IPython.terminal.embed import InteractiveShellEmbed |
| 20 |
# Now create the IPython shell instance. Put ipshell() anywhere in your code |
| 21 |
# where you want it to open. |
| 22 |
ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg) |
| 23 |
|
| 24 |
#--------------------------------------------------------------------------- |
| 25 |
# This code will load an embeddable IPython shell always with no changes for |
| 26 |
# nested embededings. |
| 27 |
|
| 28 |
from IPython import embed |
| 29 |
# Now embed() will open IPython anywhere in the code. |
| 30 |
|
| 31 |
#--------------------------------------------------------------------------- |
| 32 |
# This code loads an embeddable shell only if NOT running inside |
| 33 |
# IPython. Inside IPython, the embeddable shell variable ipshell is just a |
| 34 |
# dummy function. |
| 35 |
|
| 36 |
try: |
| 37 |
get_ipython |
| 38 |
except NameError: |
| 39 |
from IPython.terminal.embed import InteractiveShellEmbed |
| 40 |
ipshell = InteractiveShellEmbed() |
| 41 |
# Now ipshell() will open IPython anywhere in the code |
| 42 |
else: |
| 43 |
# Define a dummy ipshell() so the same code doesn't crash inside an |
| 44 |
# interactive IPython |
| 45 |
def ipshell(): pass |