|
@@
-1022,200
+1022,14
b' The following sample file illustrating how to use the embedding'
|
|
1022
|
functionality is provided in the examples directory as example-embed.py.
|
|
1022
|
functionality is provided in the examples directory as example-embed.py.
|
|
1023
|
It should be fairly self-explanatory:
|
|
1023
|
It should be fairly self-explanatory:
|
|
1024
|
|
|
1024
|
|
|
1025
|
.. sourcecode:: python
|
|
1025
|
.. literalinclude:: ../../examples/core/example-embed.py
|
|
1026
|
|
|
1026
|
:language: python
|
|
1027
|
#!/usr/bin/env python
|
|
|
|
|
1028
|
|
|
|
|
|
1029
|
"""An example of how to embed an IPython shell into a running program.
|
|
|
|
|
1030
|
|
|
|
|
|
1031
|
Please see the documentation in the IPython.Shell module for more details.
|
|
|
|
|
1032
|
|
|
|
|
|
1033
|
The accompanying file example-embed-short.py has quick code fragments for
|
|
|
|
|
1034
|
embedding which you can cut and paste in your code once you understand how
|
|
|
|
|
1035
|
things work.
|
|
|
|
|
1036
|
|
|
|
|
|
1037
|
The code in this file is deliberately extra-verbose, meant for learning."""
|
|
|
|
|
1038
|
|
|
|
|
|
1039
|
# The basics to get you going:
|
|
|
|
|
1040
|
|
|
|
|
|
1041
|
# IPython sets the __IPYTHON__ variable so you can know if you have nested
|
|
|
|
|
1042
|
# copies running.
|
|
|
|
|
1043
|
|
|
|
|
|
1044
|
# Try running this code both at the command line and from inside IPython (with
|
|
|
|
|
1045
|
# %run example-embed.py)
|
|
|
|
|
1046
|
from IPython.config.loader import Config
|
|
|
|
|
1047
|
try:
|
|
|
|
|
1048
|
get_ipython
|
|
|
|
|
1049
|
except NameError:
|
|
|
|
|
1050
|
nested = 0
|
|
|
|
|
1051
|
cfg = Config()
|
|
|
|
|
1052
|
shell_config = cfg.InteractiveShellEmbed
|
|
|
|
|
1053
|
shell_config.prompt_in1 = 'In <\\#>: '
|
|
|
|
|
1054
|
shell_config.prompt_in2 = ' .\\D.: '
|
|
|
|
|
1055
|
shell_config.prompt_out = 'Out<\\#>: '
|
|
|
|
|
1056
|
else:
|
|
|
|
|
1057
|
print "Running nested copies of IPython."
|
|
|
|
|
1058
|
print "The prompts for the nested copy have been modified"
|
|
|
|
|
1059
|
cfg = Config()
|
|
|
|
|
1060
|
nested = 1
|
|
|
|
|
1061
|
|
|
|
|
|
1062
|
# First import the embeddable shell class
|
|
|
|
|
1063
|
from IPython.frontend.terminal.embed import InteractiveShellEmbed
|
|
|
|
|
1064
|
|
|
|
|
|
1065
|
# Now create an instance of the embeddable shell. The first argument is a
|
|
|
|
|
1066
|
# string with options exactly as you would type them if you were starting
|
|
|
|
|
1067
|
# IPython at the system command line. Any parameters you want to define for
|
|
|
|
|
1068
|
# configuration can thus be specified here.
|
|
|
|
|
1069
|
ipshell = InteractiveShellEmbed(config=cfg,
|
|
|
|
|
1070
|
banner1 = 'Dropping into IPython',
|
|
|
|
|
1071
|
exit_msg = 'Leaving Interpreter, back to program.')
|
|
|
|
|
1072
|
|
|
|
|
|
1073
|
# Make a second instance, you can have as many as you want.
|
|
|
|
|
1074
|
cfg2 = cfg.copy()
|
|
|
|
|
1075
|
shell_config = cfg2.InteractiveShellEmbed
|
|
|
|
|
1076
|
shell_config.prompt_in1 = 'In2<\\#>: '
|
|
|
|
|
1077
|
if not nested:
|
|
|
|
|
1078
|
shell_config.prompt_in1 = 'In2<\\#>: '
|
|
|
|
|
1079
|
shell_config.prompt_in2 = ' .\\D.: '
|
|
|
|
|
1080
|
shell_config.prompt_out = 'Out<\\#>: '
|
|
|
|
|
1081
|
ipshell2 = InteractiveShellEmbed(config=cfg,
|
|
|
|
|
1082
|
banner1 = 'Second IPython instance.')
|
|
|
|
|
1083
|
|
|
|
|
|
1084
|
print '\nHello. This is printed from the main controller program.\n'
|
|
|
|
|
1085
|
|
|
|
|
|
1086
|
# You can then call ipshell() anywhere you need it (with an optional
|
|
|
|
|
1087
|
# message):
|
|
|
|
|
1088
|
ipshell('***Called from top level. '
|
|
|
|
|
1089
|
'Hit Ctrl-D to exit interpreter and continue program.\n'
|
|
|
|
|
1090
|
'Note that if you use %kill_embedded, you can fully deactivate\n'
|
|
|
|
|
1091
|
'This embedded instance so it will never turn on again')
|
|
|
|
|
1092
|
|
|
|
|
|
1093
|
print '\nBack in caller program, moving along...\n'
|
|
|
|
|
1094
|
|
|
|
|
|
1095
|
#---------------------------------------------------------------------------
|
|
|
|
|
1096
|
# More details:
|
|
|
|
|
1097
|
|
|
|
|
|
1098
|
# InteractiveShellEmbed instances don't print the standard system banner and
|
|
|
|
|
1099
|
# messages. The IPython banner (which actually may contain initialization
|
|
|
|
|
1100
|
# messages) is available as get_ipython().banner in case you want it.
|
|
|
|
|
1101
|
|
|
|
|
|
1102
|
# InteractiveShellEmbed instances print the following information everytime they
|
|
|
|
|
1103
|
# start:
|
|
|
|
|
1104
|
|
|
|
|
|
1105
|
# - A global startup banner.
|
|
|
|
|
1106
|
|
|
|
|
|
1107
|
# - A call-specific header string, which you can use to indicate where in the
|
|
|
|
|
1108
|
# execution flow the shell is starting.
|
|
|
|
|
1109
|
|
|
|
|
|
1110
|
# They also print an exit message every time they exit.
|
|
|
|
|
1111
|
|
|
|
|
|
1112
|
# Both the startup banner and the exit message default to None, and can be set
|
|
|
|
|
1113
|
# either at the instance constructor or at any other time with the
|
|
|
|
|
1114
|
# by setting the banner and exit_msg attributes.
|
|
|
|
|
1115
|
|
|
|
|
|
1116
|
# The shell instance can be also put in 'dummy' mode globally or on a per-call
|
|
|
|
|
1117
|
# basis. This gives you fine control for debugging without having to change
|
|
|
|
|
1118
|
# code all over the place.
|
|
|
|
|
1119
|
|
|
|
|
|
1120
|
# The code below illustrates all this.
|
|
|
|
|
1121
|
|
|
|
|
|
1122
|
|
|
|
|
|
1123
|
# This is how the global banner and exit_msg can be reset at any point
|
|
|
|
|
1124
|
ipshell.banner = 'Entering interpreter - New Banner'
|
|
|
|
|
1125
|
ipshell.exit_msg = 'Leaving interpreter - New exit_msg'
|
|
|
|
|
1126
|
|
|
|
|
|
1127
|
def foo(m):
|
|
|
|
|
1128
|
s = 'spam'
|
|
|
|
|
1129
|
ipshell('***In foo(). Try %whos, or print s or m:')
|
|
|
|
|
1130
|
print 'foo says m = ',m
|
|
|
|
|
1131
|
|
|
|
|
|
1132
|
def bar(n):
|
|
|
|
|
1133
|
s = 'eggs'
|
|
|
|
|
1134
|
ipshell('***In bar(). Try %whos, or print s or n:')
|
|
|
|
|
1135
|
print 'bar says n = ',n
|
|
|
|
|
1136
|
|
|
|
|
|
1137
|
# Some calls to the above functions which will trigger IPython:
|
|
|
|
|
1138
|
print 'Main program calling foo("eggs")\n'
|
|
|
|
|
1139
|
foo('eggs')
|
|
|
|
|
1140
|
|
|
|
|
|
1141
|
# The shell can be put in 'dummy' mode where calls to it silently return. This
|
|
|
|
|
1142
|
# allows you, for example, to globally turn off debugging for a program with a
|
|
|
|
|
1143
|
# single call.
|
|
|
|
|
1144
|
ipshell.dummy_mode = True
|
|
|
|
|
1145
|
print '\nTrying to call IPython which is now "dummy":'
|
|
|
|
|
1146
|
ipshell()
|
|
|
|
|
1147
|
print 'Nothing happened...'
|
|
|
|
|
1148
|
# The global 'dummy' mode can still be overridden for a single call
|
|
|
|
|
1149
|
print '\nOverriding dummy mode manually:'
|
|
|
|
|
1150
|
ipshell(dummy=False)
|
|
|
|
|
1151
|
|
|
|
|
|
1152
|
# Reactivate the IPython shell
|
|
|
|
|
1153
|
ipshell.dummy_mode = False
|
|
|
|
|
1154
|
|
|
|
|
|
1155
|
print 'You can even have multiple embedded instances:'
|
|
|
|
|
1156
|
ipshell2()
|
|
|
|
|
1157
|
|
|
|
|
|
1158
|
print '\nMain program calling bar("spam")\n'
|
|
|
|
|
1159
|
bar('spam')
|
|
|
|
|
1160
|
|
|
|
|
|
1161
|
print 'Main program finished. Bye!'
|
|
|
|
|
1162
|
|
|
|
|
|
1163
|
#********************** End of file <example-embed.py> ***********************
|
|
|
|
|
1164
|
|
|
1027
|
|
|
1165
|
Once you understand how the system functions, you can use the following
|
|
1028
|
Once you understand how the system functions, you can use the following
|
|
1166
|
code fragments in your programs which are ready for cut and paste:
|
|
1029
|
code fragments in your programs which are ready for cut and paste:
|
|
1167
|
|
|
1030
|
|
|
1168
|
.. sourcecode:: python
|
|
1031
|
.. literalinclude:: ../../examples/core/example-embed-short.py
|
|
1169
|
|
|
1032
|
:language: python
|
|
1170
|
"""Quick code snippets for embedding IPython into other programs.
|
|
|
|
|
1171
|
|
|
|
|
|
1172
|
See example-embed.py for full details, this file has the bare minimum code for
|
|
|
|
|
1173
|
cut and paste use once you understand how to use the system."""
|
|
|
|
|
1174
|
|
|
|
|
|
1175
|
#---------------------------------------------------------------------------
|
|
|
|
|
1176
|
# This code loads IPython but modifies a few things if it detects it's running
|
|
|
|
|
1177
|
# embedded in another IPython session (helps avoid confusion)
|
|
|
|
|
1178
|
|
|
|
|
|
1179
|
try:
|
|
|
|
|
1180
|
get_ipython
|
|
|
|
|
1181
|
except NameError:
|
|
|
|
|
1182
|
argv = ['']
|
|
|
|
|
1183
|
banner = exit_msg = ''
|
|
|
|
|
1184
|
else:
|
|
|
|
|
1185
|
# Command-line options for IPython (a list like sys.argv)
|
|
|
|
|
1186
|
argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:']
|
|
|
|
|
1187
|
banner = '*** Nested interpreter ***'
|
|
|
|
|
1188
|
exit_msg = '*** Back in main IPython ***'
|
|
|
|
|
1189
|
|
|
|
|
|
1190
|
# First import the embeddable shell class
|
|
|
|
|
1191
|
from IPython.Shell import IPShellEmbed
|
|
|
|
|
1192
|
# Now create the IPython shell instance. Put ipshell() anywhere in your code
|
|
|
|
|
1193
|
# where you want it to open.
|
|
|
|
|
1194
|
ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg)
|
|
|
|
|
1195
|
|
|
|
|
|
1196
|
#---------------------------------------------------------------------------
|
|
|
|
|
1197
|
# This code will load an embeddable IPython shell always with no changes for
|
|
|
|
|
1198
|
# nested embededings.
|
|
|
|
|
1199
|
|
|
|
|
|
1200
|
from IPython import embed
|
|
|
|
|
1201
|
# Now embed() will open IPython anywhere in the code.
|
|
|
|
|
1202
|
|
|
|
|
|
1203
|
#---------------------------------------------------------------------------
|
|
|
|
|
1204
|
# This code loads an embeddable shell only if NOT running inside
|
|
|
|
|
1205
|
# IPython. Inside IPython, the embeddable shell variable ipshell is just a
|
|
|
|
|
1206
|
# dummy function.
|
|
|
|
|
1207
|
|
|
|
|
|
1208
|
try:
|
|
|
|
|
1209
|
get_ipython
|
|
|
|
|
1210
|
except NameError:
|
|
|
|
|
1211
|
from IPython.frontend.terminal.embed import embed
|
|
|
|
|
1212
|
# Now embed() will open IPython anywhere in the code
|
|
|
|
|
1213
|
else:
|
|
|
|
|
1214
|
# Define a dummy embed() so the same code doesn't crash inside an
|
|
|
|
|
1215
|
# interactive IPython
|
|
|
|
|
1216
|
def embed(): pass
|
|
|
|
|
1217
|
|
|
|
|
|
1218
|
#******************* End of file <example-embed-short.py> ********************
|
|
|
|
|
1219
|
|
|
1033
|
|
|
1220
|
Using the Python debugger (pdb)
|
|
1034
|
Using the Python debugger (pdb)
|
|
1221
|
===============================
|
|
1035
|
===============================
|
|
@@
-1492,49
+1306,11
b' This allows you to show a piece of code, run it and then execute'
|
|
1492
|
interactively commands based on the variables just created. Once you
|
|
1306
|
interactively commands based on the variables just created. Once you
|
|
1493
|
want to continue, you simply execute the next block of the demo. The
|
|
1307
|
want to continue, you simply execute the next block of the demo. The
|
|
1494
|
following listing shows the markup necessary for dividing a script into
|
|
1308
|
following listing shows the markup necessary for dividing a script into
|
|
1495
|
sections for execution as a demo::
|
|
1309
|
sections for execution as a demo:
|
|
1496
|
|
|
|
|
|
1497
|
|
|
|
|
|
1498
|
"""A simple interactive demo to illustrate the use of IPython's Demo class.
|
|
|
|
|
1499
|
|
|
|
|
|
1500
|
Any python script can be run as a demo, but that does little more than showing
|
|
|
|
|
1501
|
it on-screen, syntax-highlighted in one shot. If you add a little simple
|
|
|
|
|
1502
|
markup, you can stop at specified intervals and return to the ipython prompt,
|
|
|
|
|
1503
|
resuming execution later.
|
|
|
|
|
1504
|
"""
|
|
|
|
|
1505
|
|
|
|
|
|
1506
|
print 'Hello, welcome to an interactive IPython demo.'
|
|
|
|
|
1507
|
print 'Executing this block should require confirmation before proceeding,'
|
|
|
|
|
1508
|
print 'unless auto_all has been set to true in the demo object'
|
|
|
|
|
1509
|
|
|
|
|
|
1510
|
# The mark below defines a block boundary, which is a point where IPython will
|
|
|
|
|
1511
|
# stop execution and return to the interactive prompt.
|
|
|
|
|
1512
|
# Note that in actual interactive execution,
|
|
|
|
|
1513
|
# <demo> --- stop ---
|
|
|
|
|
1514
|
|
|
|
|
|
1515
|
x = 1
|
|
|
|
|
1516
|
y = 2
|
|
|
|
|
1517
|
|
|
|
|
|
1518
|
# <demo> --- stop ---
|
|
|
|
|
1519
|
|
|
|
|
|
1520
|
# the mark below makes this block as silent
|
|
|
|
|
1521
|
# <demo> silent
|
|
|
|
|
1522
|
|
|
|
|
|
1523
|
print 'This is a silent block, which gets executed but not printed.'
|
|
|
|
|
1524
|
|
|
|
|
|
1525
|
# <demo> --- stop ---
|
|
|
|
|
1526
|
# <demo> auto
|
|
|
|
|
1527
|
print 'This is an automatic block.'
|
|
|
|
|
1528
|
print 'It is executed without asking for confirmation, but printed.'
|
|
|
|
|
1529
|
z = x+y
|
|
|
|
|
1530
|
|
|
|
|
|
1531
|
print 'z=',x
|
|
|
|
|
1532
|
|
|
1310
|
|
|
1533
|
# <demo> --- stop ---
|
|
1311
|
.. literalinclude:: ../../examples/lib/example-demo.py
|
|
1534
|
# This is just another normal block.
|
|
1312
|
:language: python
|
|
1535
|
print 'z is now:', z
|
|
|
|
|
1536
|
|
|
1313
|
|
|
1537
|
print 'bye!'
|
|
|
|
|
1538
|
|
|
1314
|
|
|
1539
|
In order to run a file as a demo, you must first make a Demo object out
|
|
1315
|
In order to run a file as a demo, you must first make a Demo object out
|
|
1540
|
of it. If the file is named myscript.py, the following code will make a
|
|
1316
|
of it. If the file is named myscript.py, the following code will make a
|