##// END OF EJS Templates
Machinery to replace the current cell instead of adding a new one
Thomas Kluyver -
Show More
@@ -1,3336 +1,3336 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Main IPython class."""
2 """Main IPython class."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 from __future__ import absolute_import, print_function
13 from __future__ import absolute_import, print_function
14
14
15 import __future__
15 import __future__
16 import abc
16 import abc
17 import ast
17 import ast
18 import atexit
18 import atexit
19 import functools
19 import functools
20 import os
20 import os
21 import re
21 import re
22 import runpy
22 import runpy
23 import sys
23 import sys
24 import tempfile
24 import tempfile
25 import traceback
25 import traceback
26 import types
26 import types
27 import subprocess
27 import subprocess
28 from io import open as io_open
28 from io import open as io_open
29
29
30 from IPython.config.configurable import SingletonConfigurable
30 from IPython.config.configurable import SingletonConfigurable
31 from IPython.core import debugger, oinspect
31 from IPython.core import debugger, oinspect
32 from IPython.core import magic
32 from IPython.core import magic
33 from IPython.core import page
33 from IPython.core import page
34 from IPython.core import prefilter
34 from IPython.core import prefilter
35 from IPython.core import shadowns
35 from IPython.core import shadowns
36 from IPython.core import ultratb
36 from IPython.core import ultratb
37 from IPython.core.alias import AliasManager, AliasError
37 from IPython.core.alias import AliasManager, AliasError
38 from IPython.core.autocall import ExitAutocall
38 from IPython.core.autocall import ExitAutocall
39 from IPython.core.builtin_trap import BuiltinTrap
39 from IPython.core.builtin_trap import BuiltinTrap
40 from IPython.core.events import EventManager, available_events
40 from IPython.core.events import EventManager, available_events
41 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
41 from IPython.core.compilerop import CachingCompiler, check_linecache_ipython
42 from IPython.core.display_trap import DisplayTrap
42 from IPython.core.display_trap import DisplayTrap
43 from IPython.core.displayhook import DisplayHook
43 from IPython.core.displayhook import DisplayHook
44 from IPython.core.displaypub import DisplayPublisher
44 from IPython.core.displaypub import DisplayPublisher
45 from IPython.core.error import InputRejected, UsageError
45 from IPython.core.error import InputRejected, UsageError
46 from IPython.core.extensions import ExtensionManager
46 from IPython.core.extensions import ExtensionManager
47 from IPython.core.formatters import DisplayFormatter
47 from IPython.core.formatters import DisplayFormatter
48 from IPython.core.history import HistoryManager
48 from IPython.core.history import HistoryManager
49 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
49 from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2
50 from IPython.core.logger import Logger
50 from IPython.core.logger import Logger
51 from IPython.core.macro import Macro
51 from IPython.core.macro import Macro
52 from IPython.core.payload import PayloadManager
52 from IPython.core.payload import PayloadManager
53 from IPython.core.prefilter import PrefilterManager
53 from IPython.core.prefilter import PrefilterManager
54 from IPython.core.profiledir import ProfileDir
54 from IPython.core.profiledir import ProfileDir
55 from IPython.core.prompts import PromptManager
55 from IPython.core.prompts import PromptManager
56 from IPython.core.usage import default_banner
56 from IPython.core.usage import default_banner
57 from IPython.lib.latextools import LaTeXTool
57 from IPython.lib.latextools import LaTeXTool
58 from IPython.testing.skipdoctest import skip_doctest
58 from IPython.testing.skipdoctest import skip_doctest
59 from IPython.utils import PyColorize
59 from IPython.utils import PyColorize
60 from IPython.utils import io
60 from IPython.utils import io
61 from IPython.utils import py3compat
61 from IPython.utils import py3compat
62 from IPython.utils import openpy
62 from IPython.utils import openpy
63 from IPython.utils.decorators import undoc
63 from IPython.utils.decorators import undoc
64 from IPython.utils.io import ask_yes_no
64 from IPython.utils.io import ask_yes_no
65 from IPython.utils.ipstruct import Struct
65 from IPython.utils.ipstruct import Struct
66 from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename, ensure_dir_exists
66 from IPython.utils.path import get_home_dir, get_ipython_dir, get_py_filename, unquote_filename, ensure_dir_exists
67 from IPython.utils.pickleshare import PickleShareDB
67 from IPython.utils.pickleshare import PickleShareDB
68 from IPython.utils.process import system, getoutput
68 from IPython.utils.process import system, getoutput
69 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
69 from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types,
70 with_metaclass, iteritems)
70 with_metaclass, iteritems)
71 from IPython.utils.strdispatch import StrDispatch
71 from IPython.utils.strdispatch import StrDispatch
72 from IPython.utils.syspathcontext import prepended_to_syspath
72 from IPython.utils.syspathcontext import prepended_to_syspath
73 from IPython.utils.text import (format_screen, LSString, SList,
73 from IPython.utils.text import (format_screen, LSString, SList,
74 DollarFormatter)
74 DollarFormatter)
75 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
75 from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,
76 List, Unicode, Instance, Type)
76 List, Unicode, Instance, Type)
77 from IPython.utils.warn import warn, error
77 from IPython.utils.warn import warn, error
78 import IPython.core.hooks
78 import IPython.core.hooks
79
79
80 #-----------------------------------------------------------------------------
80 #-----------------------------------------------------------------------------
81 # Globals
81 # Globals
82 #-----------------------------------------------------------------------------
82 #-----------------------------------------------------------------------------
83
83
84 # compiled regexps for autoindent management
84 # compiled regexps for autoindent management
85 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
85 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86
86
87 #-----------------------------------------------------------------------------
87 #-----------------------------------------------------------------------------
88 # Utilities
88 # Utilities
89 #-----------------------------------------------------------------------------
89 #-----------------------------------------------------------------------------
90
90
91 @undoc
91 @undoc
92 def softspace(file, newvalue):
92 def softspace(file, newvalue):
93 """Copied from code.py, to remove the dependency"""
93 """Copied from code.py, to remove the dependency"""
94
94
95 oldvalue = 0
95 oldvalue = 0
96 try:
96 try:
97 oldvalue = file.softspace
97 oldvalue = file.softspace
98 except AttributeError:
98 except AttributeError:
99 pass
99 pass
100 try:
100 try:
101 file.softspace = newvalue
101 file.softspace = newvalue
102 except (AttributeError, TypeError):
102 except (AttributeError, TypeError):
103 # "attribute-less object" or "read-only attributes"
103 # "attribute-less object" or "read-only attributes"
104 pass
104 pass
105 return oldvalue
105 return oldvalue
106
106
107 @undoc
107 @undoc
108 def no_op(*a, **kw): pass
108 def no_op(*a, **kw): pass
109
109
110 @undoc
110 @undoc
111 class NoOpContext(object):
111 class NoOpContext(object):
112 def __enter__(self): pass
112 def __enter__(self): pass
113 def __exit__(self, type, value, traceback): pass
113 def __exit__(self, type, value, traceback): pass
114 no_op_context = NoOpContext()
114 no_op_context = NoOpContext()
115
115
116 class SpaceInInput(Exception): pass
116 class SpaceInInput(Exception): pass
117
117
118 @undoc
118 @undoc
119 class Bunch: pass
119 class Bunch: pass
120
120
121
121
122 def get_default_colors():
122 def get_default_colors():
123 if sys.platform=='darwin':
123 if sys.platform=='darwin':
124 return "LightBG"
124 return "LightBG"
125 elif os.name=='nt':
125 elif os.name=='nt':
126 return 'Linux'
126 return 'Linux'
127 else:
127 else:
128 return 'Linux'
128 return 'Linux'
129
129
130
130
131 class SeparateUnicode(Unicode):
131 class SeparateUnicode(Unicode):
132 r"""A Unicode subclass to validate separate_in, separate_out, etc.
132 r"""A Unicode subclass to validate separate_in, separate_out, etc.
133
133
134 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
134 This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``.
135 """
135 """
136
136
137 def validate(self, obj, value):
137 def validate(self, obj, value):
138 if value == '0': value = ''
138 if value == '0': value = ''
139 value = value.replace('\\n','\n')
139 value = value.replace('\\n','\n')
140 return super(SeparateUnicode, self).validate(obj, value)
140 return super(SeparateUnicode, self).validate(obj, value)
141
141
142
142
143 class ReadlineNoRecord(object):
143 class ReadlineNoRecord(object):
144 """Context manager to execute some code, then reload readline history
144 """Context manager to execute some code, then reload readline history
145 so that interactive input to the code doesn't appear when pressing up."""
145 so that interactive input to the code doesn't appear when pressing up."""
146 def __init__(self, shell):
146 def __init__(self, shell):
147 self.shell = shell
147 self.shell = shell
148 self._nested_level = 0
148 self._nested_level = 0
149
149
150 def __enter__(self):
150 def __enter__(self):
151 if self._nested_level == 0:
151 if self._nested_level == 0:
152 try:
152 try:
153 self.orig_length = self.current_length()
153 self.orig_length = self.current_length()
154 self.readline_tail = self.get_readline_tail()
154 self.readline_tail = self.get_readline_tail()
155 except (AttributeError, IndexError): # Can fail with pyreadline
155 except (AttributeError, IndexError): # Can fail with pyreadline
156 self.orig_length, self.readline_tail = 999999, []
156 self.orig_length, self.readline_tail = 999999, []
157 self._nested_level += 1
157 self._nested_level += 1
158
158
159 def __exit__(self, type, value, traceback):
159 def __exit__(self, type, value, traceback):
160 self._nested_level -= 1
160 self._nested_level -= 1
161 if self._nested_level == 0:
161 if self._nested_level == 0:
162 # Try clipping the end if it's got longer
162 # Try clipping the end if it's got longer
163 try:
163 try:
164 e = self.current_length() - self.orig_length
164 e = self.current_length() - self.orig_length
165 if e > 0:
165 if e > 0:
166 for _ in range(e):
166 for _ in range(e):
167 self.shell.readline.remove_history_item(self.orig_length)
167 self.shell.readline.remove_history_item(self.orig_length)
168
168
169 # If it still doesn't match, just reload readline history.
169 # If it still doesn't match, just reload readline history.
170 if self.current_length() != self.orig_length \
170 if self.current_length() != self.orig_length \
171 or self.get_readline_tail() != self.readline_tail:
171 or self.get_readline_tail() != self.readline_tail:
172 self.shell.refill_readline_hist()
172 self.shell.refill_readline_hist()
173 except (AttributeError, IndexError):
173 except (AttributeError, IndexError):
174 pass
174 pass
175 # Returning False will cause exceptions to propagate
175 # Returning False will cause exceptions to propagate
176 return False
176 return False
177
177
178 def current_length(self):
178 def current_length(self):
179 return self.shell.readline.get_current_history_length()
179 return self.shell.readline.get_current_history_length()
180
180
181 def get_readline_tail(self, n=10):
181 def get_readline_tail(self, n=10):
182 """Get the last n items in readline history."""
182 """Get the last n items in readline history."""
183 end = self.shell.readline.get_current_history_length() + 1
183 end = self.shell.readline.get_current_history_length() + 1
184 start = max(end-n, 1)
184 start = max(end-n, 1)
185 ghi = self.shell.readline.get_history_item
185 ghi = self.shell.readline.get_history_item
186 return [ghi(x) for x in range(start, end)]
186 return [ghi(x) for x in range(start, end)]
187
187
188
188
189 @undoc
189 @undoc
190 class DummyMod(object):
190 class DummyMod(object):
191 """A dummy module used for IPython's interactive module when
191 """A dummy module used for IPython's interactive module when
192 a namespace must be assigned to the module's __dict__."""
192 a namespace must be assigned to the module's __dict__."""
193 pass
193 pass
194
194
195 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
196 # Main IPython class
196 # Main IPython class
197 #-----------------------------------------------------------------------------
197 #-----------------------------------------------------------------------------
198
198
199 class InteractiveShell(SingletonConfigurable):
199 class InteractiveShell(SingletonConfigurable):
200 """An enhanced, interactive shell for Python."""
200 """An enhanced, interactive shell for Python."""
201
201
202 _instance = None
202 _instance = None
203
203
204 ast_transformers = List([], config=True, help=
204 ast_transformers = List([], config=True, help=
205 """
205 """
206 A list of ast.NodeTransformer subclass instances, which will be applied
206 A list of ast.NodeTransformer subclass instances, which will be applied
207 to user input before code is run.
207 to user input before code is run.
208 """
208 """
209 )
209 )
210
210
211 autocall = Enum((0,1,2), default_value=0, config=True, help=
211 autocall = Enum((0,1,2), default_value=0, config=True, help=
212 """
212 """
213 Make IPython automatically call any callable object even if you didn't
213 Make IPython automatically call any callable object even if you didn't
214 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
214 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
215 automatically. The value can be '0' to disable the feature, '1' for
215 automatically. The value can be '0' to disable the feature, '1' for
216 'smart' autocall, where it is not applied if there are no more
216 'smart' autocall, where it is not applied if there are no more
217 arguments on the line, and '2' for 'full' autocall, where all callable
217 arguments on the line, and '2' for 'full' autocall, where all callable
218 objects are automatically called (even if no arguments are present).
218 objects are automatically called (even if no arguments are present).
219 """
219 """
220 )
220 )
221 # TODO: remove all autoindent logic and put into frontends.
221 # TODO: remove all autoindent logic and put into frontends.
222 # We can't do this yet because even runlines uses the autoindent.
222 # We can't do this yet because even runlines uses the autoindent.
223 autoindent = CBool(True, config=True, help=
223 autoindent = CBool(True, config=True, help=
224 """
224 """
225 Autoindent IPython code entered interactively.
225 Autoindent IPython code entered interactively.
226 """
226 """
227 )
227 )
228 automagic = CBool(True, config=True, help=
228 automagic = CBool(True, config=True, help=
229 """
229 """
230 Enable magic commands to be called without the leading %.
230 Enable magic commands to be called without the leading %.
231 """
231 """
232 )
232 )
233
233
234 banner = Unicode('')
234 banner = Unicode('')
235
235
236 banner1 = Unicode(default_banner, config=True,
236 banner1 = Unicode(default_banner, config=True,
237 help="""The part of the banner to be printed before the profile"""
237 help="""The part of the banner to be printed before the profile"""
238 )
238 )
239 banner2 = Unicode('', config=True,
239 banner2 = Unicode('', config=True,
240 help="""The part of the banner to be printed after the profile"""
240 help="""The part of the banner to be printed after the profile"""
241 )
241 )
242
242
243 cache_size = Integer(1000, config=True, help=
243 cache_size = Integer(1000, config=True, help=
244 """
244 """
245 Set the size of the output cache. The default is 1000, you can
245 Set the size of the output cache. The default is 1000, you can
246 change it permanently in your config file. Setting it to 0 completely
246 change it permanently in your config file. Setting it to 0 completely
247 disables the caching system, and the minimum value accepted is 20 (if
247 disables the caching system, and the minimum value accepted is 20 (if
248 you provide a value less than 20, it is reset to 0 and a warning is
248 you provide a value less than 20, it is reset to 0 and a warning is
249 issued). This limit is defined because otherwise you'll spend more
249 issued). This limit is defined because otherwise you'll spend more
250 time re-flushing a too small cache than working
250 time re-flushing a too small cache than working
251 """
251 """
252 )
252 )
253 color_info = CBool(True, config=True, help=
253 color_info = CBool(True, config=True, help=
254 """
254 """
255 Use colors for displaying information about objects. Because this
255 Use colors for displaying information about objects. Because this
256 information is passed through a pager (like 'less'), and some pagers
256 information is passed through a pager (like 'less'), and some pagers
257 get confused with color codes, this capability can be turned off.
257 get confused with color codes, this capability can be turned off.
258 """
258 """
259 )
259 )
260 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
260 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
261 default_value=get_default_colors(), config=True,
261 default_value=get_default_colors(), config=True,
262 help="Set the color scheme (NoColor, Linux, or LightBG)."
262 help="Set the color scheme (NoColor, Linux, or LightBG)."
263 )
263 )
264 colors_force = CBool(False, help=
264 colors_force = CBool(False, help=
265 """
265 """
266 Force use of ANSI color codes, regardless of OS and readline
266 Force use of ANSI color codes, regardless of OS and readline
267 availability.
267 availability.
268 """
268 """
269 # FIXME: This is essentially a hack to allow ZMQShell to show colors
269 # FIXME: This is essentially a hack to allow ZMQShell to show colors
270 # without readline on Win32. When the ZMQ formatting system is
270 # without readline on Win32. When the ZMQ formatting system is
271 # refactored, this should be removed.
271 # refactored, this should be removed.
272 )
272 )
273 debug = CBool(False, config=True)
273 debug = CBool(False, config=True)
274 deep_reload = CBool(False, config=True, help=
274 deep_reload = CBool(False, config=True, help=
275 """
275 """
276 Enable deep (recursive) reloading by default. IPython can use the
276 Enable deep (recursive) reloading by default. IPython can use the
277 deep_reload module which reloads changes in modules recursively (it
277 deep_reload module which reloads changes in modules recursively (it
278 replaces the reload() function, so you don't need to change anything to
278 replaces the reload() function, so you don't need to change anything to
279 use it). deep_reload() forces a full reload of modules whose code may
279 use it). deep_reload() forces a full reload of modules whose code may
280 have changed, which the default reload() function does not. When
280 have changed, which the default reload() function does not. When
281 deep_reload is off, IPython will use the normal reload(), but
281 deep_reload is off, IPython will use the normal reload(), but
282 deep_reload will still be available as dreload().
282 deep_reload will still be available as dreload().
283 """
283 """
284 )
284 )
285 disable_failing_post_execute = CBool(False, config=True,
285 disable_failing_post_execute = CBool(False, config=True,
286 help="Don't call post-execute functions that have failed in the past."
286 help="Don't call post-execute functions that have failed in the past."
287 )
287 )
288 display_formatter = Instance(DisplayFormatter)
288 display_formatter = Instance(DisplayFormatter)
289 displayhook_class = Type(DisplayHook)
289 displayhook_class = Type(DisplayHook)
290 display_pub_class = Type(DisplayPublisher)
290 display_pub_class = Type(DisplayPublisher)
291 data_pub_class = None
291 data_pub_class = None
292
292
293 exit_now = CBool(False)
293 exit_now = CBool(False)
294 exiter = Instance(ExitAutocall)
294 exiter = Instance(ExitAutocall)
295 def _exiter_default(self):
295 def _exiter_default(self):
296 return ExitAutocall(self)
296 return ExitAutocall(self)
297 # Monotonically increasing execution counter
297 # Monotonically increasing execution counter
298 execution_count = Integer(1)
298 execution_count = Integer(1)
299 filename = Unicode("<ipython console>")
299 filename = Unicode("<ipython console>")
300 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
300 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
301
301
302 # Input splitter, to transform input line by line and detect when a block
302 # Input splitter, to transform input line by line and detect when a block
303 # is ready to be executed.
303 # is ready to be executed.
304 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
304 input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
305 (), {'line_input_checker': True})
305 (), {'line_input_checker': True})
306
306
307 # This InputSplitter instance is used to transform completed cells before
307 # This InputSplitter instance is used to transform completed cells before
308 # running them. It allows cell magics to contain blank lines.
308 # running them. It allows cell magics to contain blank lines.
309 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
309 input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter',
310 (), {'line_input_checker': False})
310 (), {'line_input_checker': False})
311
311
312 logstart = CBool(False, config=True, help=
312 logstart = CBool(False, config=True, help=
313 """
313 """
314 Start logging to the default log file.
314 Start logging to the default log file.
315 """
315 """
316 )
316 )
317 logfile = Unicode('', config=True, help=
317 logfile = Unicode('', config=True, help=
318 """
318 """
319 The name of the logfile to use.
319 The name of the logfile to use.
320 """
320 """
321 )
321 )
322 logappend = Unicode('', config=True, help=
322 logappend = Unicode('', config=True, help=
323 """
323 """
324 Start logging to the given file in append mode.
324 Start logging to the given file in append mode.
325 """
325 """
326 )
326 )
327 object_info_string_level = Enum((0,1,2), default_value=0,
327 object_info_string_level = Enum((0,1,2), default_value=0,
328 config=True)
328 config=True)
329 pdb = CBool(False, config=True, help=
329 pdb = CBool(False, config=True, help=
330 """
330 """
331 Automatically call the pdb debugger after every exception.
331 Automatically call the pdb debugger after every exception.
332 """
332 """
333 )
333 )
334 multiline_history = CBool(sys.platform != 'win32', config=True,
334 multiline_history = CBool(sys.platform != 'win32', config=True,
335 help="Save multi-line entries as one entry in readline history"
335 help="Save multi-line entries as one entry in readline history"
336 )
336 )
337
337
338 # deprecated prompt traits:
338 # deprecated prompt traits:
339
339
340 prompt_in1 = Unicode('In [\\#]: ', config=True,
340 prompt_in1 = Unicode('In [\\#]: ', config=True,
341 help="Deprecated, use PromptManager.in_template")
341 help="Deprecated, use PromptManager.in_template")
342 prompt_in2 = Unicode(' .\\D.: ', config=True,
342 prompt_in2 = Unicode(' .\\D.: ', config=True,
343 help="Deprecated, use PromptManager.in2_template")
343 help="Deprecated, use PromptManager.in2_template")
344 prompt_out = Unicode('Out[\\#]: ', config=True,
344 prompt_out = Unicode('Out[\\#]: ', config=True,
345 help="Deprecated, use PromptManager.out_template")
345 help="Deprecated, use PromptManager.out_template")
346 prompts_pad_left = CBool(True, config=True,
346 prompts_pad_left = CBool(True, config=True,
347 help="Deprecated, use PromptManager.justify")
347 help="Deprecated, use PromptManager.justify")
348
348
349 def _prompt_trait_changed(self, name, old, new):
349 def _prompt_trait_changed(self, name, old, new):
350 table = {
350 table = {
351 'prompt_in1' : 'in_template',
351 'prompt_in1' : 'in_template',
352 'prompt_in2' : 'in2_template',
352 'prompt_in2' : 'in2_template',
353 'prompt_out' : 'out_template',
353 'prompt_out' : 'out_template',
354 'prompts_pad_left' : 'justify',
354 'prompts_pad_left' : 'justify',
355 }
355 }
356 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format(
356 warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format(
357 name=name, newname=table[name])
357 name=name, newname=table[name])
358 )
358 )
359 # protect against weird cases where self.config may not exist:
359 # protect against weird cases where self.config may not exist:
360 if self.config is not None:
360 if self.config is not None:
361 # propagate to corresponding PromptManager trait
361 # propagate to corresponding PromptManager trait
362 setattr(self.config.PromptManager, table[name], new)
362 setattr(self.config.PromptManager, table[name], new)
363
363
364 _prompt_in1_changed = _prompt_trait_changed
364 _prompt_in1_changed = _prompt_trait_changed
365 _prompt_in2_changed = _prompt_trait_changed
365 _prompt_in2_changed = _prompt_trait_changed
366 _prompt_out_changed = _prompt_trait_changed
366 _prompt_out_changed = _prompt_trait_changed
367 _prompt_pad_left_changed = _prompt_trait_changed
367 _prompt_pad_left_changed = _prompt_trait_changed
368
368
369 show_rewritten_input = CBool(True, config=True,
369 show_rewritten_input = CBool(True, config=True,
370 help="Show rewritten input, e.g. for autocall."
370 help="Show rewritten input, e.g. for autocall."
371 )
371 )
372
372
373 quiet = CBool(False, config=True)
373 quiet = CBool(False, config=True)
374
374
375 history_length = Integer(10000, config=True)
375 history_length = Integer(10000, config=True)
376
376
377 # The readline stuff will eventually be moved to the terminal subclass
377 # The readline stuff will eventually be moved to the terminal subclass
378 # but for now, we can't do that as readline is welded in everywhere.
378 # but for now, we can't do that as readline is welded in everywhere.
379 readline_use = CBool(True, config=True)
379 readline_use = CBool(True, config=True)
380 readline_remove_delims = Unicode('-/~', config=True)
380 readline_remove_delims = Unicode('-/~', config=True)
381 readline_delims = Unicode() # set by init_readline()
381 readline_delims = Unicode() # set by init_readline()
382 # don't use \M- bindings by default, because they
382 # don't use \M- bindings by default, because they
383 # conflict with 8-bit encodings. See gh-58,gh-88
383 # conflict with 8-bit encodings. See gh-58,gh-88
384 readline_parse_and_bind = List([
384 readline_parse_and_bind = List([
385 'tab: complete',
385 'tab: complete',
386 '"\C-l": clear-screen',
386 '"\C-l": clear-screen',
387 'set show-all-if-ambiguous on',
387 'set show-all-if-ambiguous on',
388 '"\C-o": tab-insert',
388 '"\C-o": tab-insert',
389 '"\C-r": reverse-search-history',
389 '"\C-r": reverse-search-history',
390 '"\C-s": forward-search-history',
390 '"\C-s": forward-search-history',
391 '"\C-p": history-search-backward',
391 '"\C-p": history-search-backward',
392 '"\C-n": history-search-forward',
392 '"\C-n": history-search-forward',
393 '"\e[A": history-search-backward',
393 '"\e[A": history-search-backward',
394 '"\e[B": history-search-forward',
394 '"\e[B": history-search-forward',
395 '"\C-k": kill-line',
395 '"\C-k": kill-line',
396 '"\C-u": unix-line-discard',
396 '"\C-u": unix-line-discard',
397 ], config=True)
397 ], config=True)
398
398
399 _custom_readline_config = False
399 _custom_readline_config = False
400
400
401 def _readline_parse_and_bind_changed(self, name, old, new):
401 def _readline_parse_and_bind_changed(self, name, old, new):
402 # notice that readline config is customized
402 # notice that readline config is customized
403 # indicates that it should have higher priority than inputrc
403 # indicates that it should have higher priority than inputrc
404 self._custom_readline_config = True
404 self._custom_readline_config = True
405
405
406 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
406 ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'],
407 default_value='last_expr', config=True,
407 default_value='last_expr', config=True,
408 help="""
408 help="""
409 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
409 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
410 run interactively (displaying output from expressions).""")
410 run interactively (displaying output from expressions).""")
411
411
412 # TODO: this part of prompt management should be moved to the frontends.
412 # TODO: this part of prompt management should be moved to the frontends.
413 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
413 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
414 separate_in = SeparateUnicode('\n', config=True)
414 separate_in = SeparateUnicode('\n', config=True)
415 separate_out = SeparateUnicode('', config=True)
415 separate_out = SeparateUnicode('', config=True)
416 separate_out2 = SeparateUnicode('', config=True)
416 separate_out2 = SeparateUnicode('', config=True)
417 wildcards_case_sensitive = CBool(True, config=True)
417 wildcards_case_sensitive = CBool(True, config=True)
418 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
418 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
419 default_value='Context', config=True)
419 default_value='Context', config=True)
420
420
421 # Subcomponents of InteractiveShell
421 # Subcomponents of InteractiveShell
422 alias_manager = Instance('IPython.core.alias.AliasManager')
422 alias_manager = Instance('IPython.core.alias.AliasManager')
423 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
423 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
424 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
424 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
425 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
425 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
426 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
426 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
427 payload_manager = Instance('IPython.core.payload.PayloadManager')
427 payload_manager = Instance('IPython.core.payload.PayloadManager')
428 history_manager = Instance('IPython.core.history.HistoryAccessorBase')
428 history_manager = Instance('IPython.core.history.HistoryAccessorBase')
429 magics_manager = Instance('IPython.core.magic.MagicsManager')
429 magics_manager = Instance('IPython.core.magic.MagicsManager')
430
430
431 profile_dir = Instance('IPython.core.application.ProfileDir')
431 profile_dir = Instance('IPython.core.application.ProfileDir')
432 @property
432 @property
433 def profile(self):
433 def profile(self):
434 if self.profile_dir is not None:
434 if self.profile_dir is not None:
435 name = os.path.basename(self.profile_dir.location)
435 name = os.path.basename(self.profile_dir.location)
436 return name.replace('profile_','')
436 return name.replace('profile_','')
437
437
438
438
439 # Private interface
439 # Private interface
440 _post_execute = Instance(dict)
440 _post_execute = Instance(dict)
441
441
442 # Tracks any GUI loop loaded for pylab
442 # Tracks any GUI loop loaded for pylab
443 pylab_gui_select = None
443 pylab_gui_select = None
444
444
445 def __init__(self, ipython_dir=None, profile_dir=None,
445 def __init__(self, ipython_dir=None, profile_dir=None,
446 user_module=None, user_ns=None,
446 user_module=None, user_ns=None,
447 custom_exceptions=((), None), **kwargs):
447 custom_exceptions=((), None), **kwargs):
448
448
449 # This is where traits with a config_key argument are updated
449 # This is where traits with a config_key argument are updated
450 # from the values on config.
450 # from the values on config.
451 super(InteractiveShell, self).__init__(**kwargs)
451 super(InteractiveShell, self).__init__(**kwargs)
452 self.configurables = [self]
452 self.configurables = [self]
453
453
454 # These are relatively independent and stateless
454 # These are relatively independent and stateless
455 self.init_ipython_dir(ipython_dir)
455 self.init_ipython_dir(ipython_dir)
456 self.init_profile_dir(profile_dir)
456 self.init_profile_dir(profile_dir)
457 self.init_instance_attrs()
457 self.init_instance_attrs()
458 self.init_environment()
458 self.init_environment()
459
459
460 # Check if we're in a virtualenv, and set up sys.path.
460 # Check if we're in a virtualenv, and set up sys.path.
461 self.init_virtualenv()
461 self.init_virtualenv()
462
462
463 # Create namespaces (user_ns, user_global_ns, etc.)
463 # Create namespaces (user_ns, user_global_ns, etc.)
464 self.init_create_namespaces(user_module, user_ns)
464 self.init_create_namespaces(user_module, user_ns)
465 # This has to be done after init_create_namespaces because it uses
465 # This has to be done after init_create_namespaces because it uses
466 # something in self.user_ns, but before init_sys_modules, which
466 # something in self.user_ns, but before init_sys_modules, which
467 # is the first thing to modify sys.
467 # is the first thing to modify sys.
468 # TODO: When we override sys.stdout and sys.stderr before this class
468 # TODO: When we override sys.stdout and sys.stderr before this class
469 # is created, we are saving the overridden ones here. Not sure if this
469 # is created, we are saving the overridden ones here. Not sure if this
470 # is what we want to do.
470 # is what we want to do.
471 self.save_sys_module_state()
471 self.save_sys_module_state()
472 self.init_sys_modules()
472 self.init_sys_modules()
473
473
474 # While we're trying to have each part of the code directly access what
474 # While we're trying to have each part of the code directly access what
475 # it needs without keeping redundant references to objects, we have too
475 # it needs without keeping redundant references to objects, we have too
476 # much legacy code that expects ip.db to exist.
476 # much legacy code that expects ip.db to exist.
477 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
477 self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db'))
478
478
479 self.init_history()
479 self.init_history()
480 self.init_encoding()
480 self.init_encoding()
481 self.init_prefilter()
481 self.init_prefilter()
482
482
483 self.init_syntax_highlighting()
483 self.init_syntax_highlighting()
484 self.init_hooks()
484 self.init_hooks()
485 self.init_events()
485 self.init_events()
486 self.init_pushd_popd_magic()
486 self.init_pushd_popd_magic()
487 # self.init_traceback_handlers use to be here, but we moved it below
487 # self.init_traceback_handlers use to be here, but we moved it below
488 # because it and init_io have to come after init_readline.
488 # because it and init_io have to come after init_readline.
489 self.init_user_ns()
489 self.init_user_ns()
490 self.init_logger()
490 self.init_logger()
491 self.init_builtins()
491 self.init_builtins()
492
492
493 # The following was in post_config_initialization
493 # The following was in post_config_initialization
494 self.init_inspector()
494 self.init_inspector()
495 # init_readline() must come before init_io(), because init_io uses
495 # init_readline() must come before init_io(), because init_io uses
496 # readline related things.
496 # readline related things.
497 self.init_readline()
497 self.init_readline()
498 # We save this here in case user code replaces raw_input, but it needs
498 # We save this here in case user code replaces raw_input, but it needs
499 # to be after init_readline(), because PyPy's readline works by replacing
499 # to be after init_readline(), because PyPy's readline works by replacing
500 # raw_input.
500 # raw_input.
501 if py3compat.PY3:
501 if py3compat.PY3:
502 self.raw_input_original = input
502 self.raw_input_original = input
503 else:
503 else:
504 self.raw_input_original = raw_input
504 self.raw_input_original = raw_input
505 # init_completer must come after init_readline, because it needs to
505 # init_completer must come after init_readline, because it needs to
506 # know whether readline is present or not system-wide to configure the
506 # know whether readline is present or not system-wide to configure the
507 # completers, since the completion machinery can now operate
507 # completers, since the completion machinery can now operate
508 # independently of readline (e.g. over the network)
508 # independently of readline (e.g. over the network)
509 self.init_completer()
509 self.init_completer()
510 # TODO: init_io() needs to happen before init_traceback handlers
510 # TODO: init_io() needs to happen before init_traceback handlers
511 # because the traceback handlers hardcode the stdout/stderr streams.
511 # because the traceback handlers hardcode the stdout/stderr streams.
512 # This logic in in debugger.Pdb and should eventually be changed.
512 # This logic in in debugger.Pdb and should eventually be changed.
513 self.init_io()
513 self.init_io()
514 self.init_traceback_handlers(custom_exceptions)
514 self.init_traceback_handlers(custom_exceptions)
515 self.init_prompts()
515 self.init_prompts()
516 self.init_display_formatter()
516 self.init_display_formatter()
517 self.init_display_pub()
517 self.init_display_pub()
518 self.init_data_pub()
518 self.init_data_pub()
519 self.init_displayhook()
519 self.init_displayhook()
520 self.init_latextool()
520 self.init_latextool()
521 self.init_magics()
521 self.init_magics()
522 self.init_alias()
522 self.init_alias()
523 self.init_logstart()
523 self.init_logstart()
524 self.init_pdb()
524 self.init_pdb()
525 self.init_extension_manager()
525 self.init_extension_manager()
526 self.init_payload()
526 self.init_payload()
527 self.hooks.late_startup_hook()
527 self.hooks.late_startup_hook()
528 self.events.trigger('shell_initialized', self)
528 self.events.trigger('shell_initialized', self)
529 atexit.register(self.atexit_operations)
529 atexit.register(self.atexit_operations)
530
530
531 def get_ipython(self):
531 def get_ipython(self):
532 """Return the currently running IPython instance."""
532 """Return the currently running IPython instance."""
533 return self
533 return self
534
534
535 #-------------------------------------------------------------------------
535 #-------------------------------------------------------------------------
536 # Trait changed handlers
536 # Trait changed handlers
537 #-------------------------------------------------------------------------
537 #-------------------------------------------------------------------------
538
538
539 def _ipython_dir_changed(self, name, new):
539 def _ipython_dir_changed(self, name, new):
540 ensure_dir_exists(new)
540 ensure_dir_exists(new)
541
541
542 def set_autoindent(self,value=None):
542 def set_autoindent(self,value=None):
543 """Set the autoindent flag, checking for readline support.
543 """Set the autoindent flag, checking for readline support.
544
544
545 If called with no arguments, it acts as a toggle."""
545 If called with no arguments, it acts as a toggle."""
546
546
547 if value != 0 and not self.has_readline:
547 if value != 0 and not self.has_readline:
548 if os.name == 'posix':
548 if os.name == 'posix':
549 warn("The auto-indent feature requires the readline library")
549 warn("The auto-indent feature requires the readline library")
550 self.autoindent = 0
550 self.autoindent = 0
551 return
551 return
552 if value is None:
552 if value is None:
553 self.autoindent = not self.autoindent
553 self.autoindent = not self.autoindent
554 else:
554 else:
555 self.autoindent = value
555 self.autoindent = value
556
556
557 #-------------------------------------------------------------------------
557 #-------------------------------------------------------------------------
558 # init_* methods called by __init__
558 # init_* methods called by __init__
559 #-------------------------------------------------------------------------
559 #-------------------------------------------------------------------------
560
560
561 def init_ipython_dir(self, ipython_dir):
561 def init_ipython_dir(self, ipython_dir):
562 if ipython_dir is not None:
562 if ipython_dir is not None:
563 self.ipython_dir = ipython_dir
563 self.ipython_dir = ipython_dir
564 return
564 return
565
565
566 self.ipython_dir = get_ipython_dir()
566 self.ipython_dir = get_ipython_dir()
567
567
568 def init_profile_dir(self, profile_dir):
568 def init_profile_dir(self, profile_dir):
569 if profile_dir is not None:
569 if profile_dir is not None:
570 self.profile_dir = profile_dir
570 self.profile_dir = profile_dir
571 return
571 return
572 self.profile_dir =\
572 self.profile_dir =\
573 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
573 ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default')
574
574
575 def init_instance_attrs(self):
575 def init_instance_attrs(self):
576 self.more = False
576 self.more = False
577
577
578 # command compiler
578 # command compiler
579 self.compile = CachingCompiler()
579 self.compile = CachingCompiler()
580
580
581 # Make an empty namespace, which extension writers can rely on both
581 # Make an empty namespace, which extension writers can rely on both
582 # existing and NEVER being used by ipython itself. This gives them a
582 # existing and NEVER being used by ipython itself. This gives them a
583 # convenient location for storing additional information and state
583 # convenient location for storing additional information and state
584 # their extensions may require, without fear of collisions with other
584 # their extensions may require, without fear of collisions with other
585 # ipython names that may develop later.
585 # ipython names that may develop later.
586 self.meta = Struct()
586 self.meta = Struct()
587
587
588 # Temporary files used for various purposes. Deleted at exit.
588 # Temporary files used for various purposes. Deleted at exit.
589 self.tempfiles = []
589 self.tempfiles = []
590 self.tempdirs = []
590 self.tempdirs = []
591
591
592 # Keep track of readline usage (later set by init_readline)
592 # Keep track of readline usage (later set by init_readline)
593 self.has_readline = False
593 self.has_readline = False
594
594
595 # keep track of where we started running (mainly for crash post-mortem)
595 # keep track of where we started running (mainly for crash post-mortem)
596 # This is not being used anywhere currently.
596 # This is not being used anywhere currently.
597 self.starting_dir = py3compat.getcwd()
597 self.starting_dir = py3compat.getcwd()
598
598
599 # Indentation management
599 # Indentation management
600 self.indent_current_nsp = 0
600 self.indent_current_nsp = 0
601
601
602 # Dict to track post-execution functions that have been registered
602 # Dict to track post-execution functions that have been registered
603 self._post_execute = {}
603 self._post_execute = {}
604
604
605 def init_environment(self):
605 def init_environment(self):
606 """Any changes we need to make to the user's environment."""
606 """Any changes we need to make to the user's environment."""
607 pass
607 pass
608
608
609 def init_encoding(self):
609 def init_encoding(self):
610 # Get system encoding at startup time. Certain terminals (like Emacs
610 # Get system encoding at startup time. Certain terminals (like Emacs
611 # under Win32 have it set to None, and we need to have a known valid
611 # under Win32 have it set to None, and we need to have a known valid
612 # encoding to use in the raw_input() method
612 # encoding to use in the raw_input() method
613 try:
613 try:
614 self.stdin_encoding = sys.stdin.encoding or 'ascii'
614 self.stdin_encoding = sys.stdin.encoding or 'ascii'
615 except AttributeError:
615 except AttributeError:
616 self.stdin_encoding = 'ascii'
616 self.stdin_encoding = 'ascii'
617
617
618 def init_syntax_highlighting(self):
618 def init_syntax_highlighting(self):
619 # Python source parser/formatter for syntax highlighting
619 # Python source parser/formatter for syntax highlighting
620 pyformat = PyColorize.Parser().format
620 pyformat = PyColorize.Parser().format
621 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
621 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
622
622
623 def init_pushd_popd_magic(self):
623 def init_pushd_popd_magic(self):
624 # for pushd/popd management
624 # for pushd/popd management
625 self.home_dir = get_home_dir()
625 self.home_dir = get_home_dir()
626
626
627 self.dir_stack = []
627 self.dir_stack = []
628
628
629 def init_logger(self):
629 def init_logger(self):
630 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
630 self.logger = Logger(self.home_dir, logfname='ipython_log.py',
631 logmode='rotate')
631 logmode='rotate')
632
632
633 def init_logstart(self):
633 def init_logstart(self):
634 """Initialize logging in case it was requested at the command line.
634 """Initialize logging in case it was requested at the command line.
635 """
635 """
636 if self.logappend:
636 if self.logappend:
637 self.magic('logstart %s append' % self.logappend)
637 self.magic('logstart %s append' % self.logappend)
638 elif self.logfile:
638 elif self.logfile:
639 self.magic('logstart %s' % self.logfile)
639 self.magic('logstart %s' % self.logfile)
640 elif self.logstart:
640 elif self.logstart:
641 self.magic('logstart')
641 self.magic('logstart')
642
642
643 def init_builtins(self):
643 def init_builtins(self):
644 # A single, static flag that we set to True. Its presence indicates
644 # A single, static flag that we set to True. Its presence indicates
645 # that an IPython shell has been created, and we make no attempts at
645 # that an IPython shell has been created, and we make no attempts at
646 # removing on exit or representing the existence of more than one
646 # removing on exit or representing the existence of more than one
647 # IPython at a time.
647 # IPython at a time.
648 builtin_mod.__dict__['__IPYTHON__'] = True
648 builtin_mod.__dict__['__IPYTHON__'] = True
649
649
650 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
650 # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to
651 # manage on enter/exit, but with all our shells it's virtually
651 # manage on enter/exit, but with all our shells it's virtually
652 # impossible to get all the cases right. We're leaving the name in for
652 # impossible to get all the cases right. We're leaving the name in for
653 # those who adapted their codes to check for this flag, but will
653 # those who adapted their codes to check for this flag, but will
654 # eventually remove it after a few more releases.
654 # eventually remove it after a few more releases.
655 builtin_mod.__dict__['__IPYTHON__active'] = \
655 builtin_mod.__dict__['__IPYTHON__active'] = \
656 'Deprecated, check for __IPYTHON__'
656 'Deprecated, check for __IPYTHON__'
657
657
658 self.builtin_trap = BuiltinTrap(shell=self)
658 self.builtin_trap = BuiltinTrap(shell=self)
659
659
660 def init_inspector(self):
660 def init_inspector(self):
661 # Object inspector
661 # Object inspector
662 self.inspector = oinspect.Inspector(oinspect.InspectColors,
662 self.inspector = oinspect.Inspector(oinspect.InspectColors,
663 PyColorize.ANSICodeColors,
663 PyColorize.ANSICodeColors,
664 'NoColor',
664 'NoColor',
665 self.object_info_string_level)
665 self.object_info_string_level)
666
666
667 def init_io(self):
667 def init_io(self):
668 # This will just use sys.stdout and sys.stderr. If you want to
668 # This will just use sys.stdout and sys.stderr. If you want to
669 # override sys.stdout and sys.stderr themselves, you need to do that
669 # override sys.stdout and sys.stderr themselves, you need to do that
670 # *before* instantiating this class, because io holds onto
670 # *before* instantiating this class, because io holds onto
671 # references to the underlying streams.
671 # references to the underlying streams.
672 if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline:
672 if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline:
673 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
673 io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
674 else:
674 else:
675 io.stdout = io.IOStream(sys.stdout)
675 io.stdout = io.IOStream(sys.stdout)
676 io.stderr = io.IOStream(sys.stderr)
676 io.stderr = io.IOStream(sys.stderr)
677
677
678 def init_prompts(self):
678 def init_prompts(self):
679 self.prompt_manager = PromptManager(shell=self, parent=self)
679 self.prompt_manager = PromptManager(shell=self, parent=self)
680 self.configurables.append(self.prompt_manager)
680 self.configurables.append(self.prompt_manager)
681 # Set system prompts, so that scripts can decide if they are running
681 # Set system prompts, so that scripts can decide if they are running
682 # interactively.
682 # interactively.
683 sys.ps1 = 'In : '
683 sys.ps1 = 'In : '
684 sys.ps2 = '...: '
684 sys.ps2 = '...: '
685 sys.ps3 = 'Out: '
685 sys.ps3 = 'Out: '
686
686
687 def init_display_formatter(self):
687 def init_display_formatter(self):
688 self.display_formatter = DisplayFormatter(parent=self)
688 self.display_formatter = DisplayFormatter(parent=self)
689 self.configurables.append(self.display_formatter)
689 self.configurables.append(self.display_formatter)
690
690
691 def init_display_pub(self):
691 def init_display_pub(self):
692 self.display_pub = self.display_pub_class(parent=self)
692 self.display_pub = self.display_pub_class(parent=self)
693 self.configurables.append(self.display_pub)
693 self.configurables.append(self.display_pub)
694
694
695 def init_data_pub(self):
695 def init_data_pub(self):
696 if not self.data_pub_class:
696 if not self.data_pub_class:
697 self.data_pub = None
697 self.data_pub = None
698 return
698 return
699 self.data_pub = self.data_pub_class(parent=self)
699 self.data_pub = self.data_pub_class(parent=self)
700 self.configurables.append(self.data_pub)
700 self.configurables.append(self.data_pub)
701
701
702 def init_displayhook(self):
702 def init_displayhook(self):
703 # Initialize displayhook, set in/out prompts and printing system
703 # Initialize displayhook, set in/out prompts and printing system
704 self.displayhook = self.displayhook_class(
704 self.displayhook = self.displayhook_class(
705 parent=self,
705 parent=self,
706 shell=self,
706 shell=self,
707 cache_size=self.cache_size,
707 cache_size=self.cache_size,
708 )
708 )
709 self.configurables.append(self.displayhook)
709 self.configurables.append(self.displayhook)
710 # This is a context manager that installs/revmoes the displayhook at
710 # This is a context manager that installs/revmoes the displayhook at
711 # the appropriate time.
711 # the appropriate time.
712 self.display_trap = DisplayTrap(hook=self.displayhook)
712 self.display_trap = DisplayTrap(hook=self.displayhook)
713
713
714 def init_latextool(self):
714 def init_latextool(self):
715 """Configure LaTeXTool."""
715 """Configure LaTeXTool."""
716 cfg = LaTeXTool.instance(parent=self)
716 cfg = LaTeXTool.instance(parent=self)
717 if cfg not in self.configurables:
717 if cfg not in self.configurables:
718 self.configurables.append(cfg)
718 self.configurables.append(cfg)
719
719
720 def init_virtualenv(self):
720 def init_virtualenv(self):
721 """Add a virtualenv to sys.path so the user can import modules from it.
721 """Add a virtualenv to sys.path so the user can import modules from it.
722 This isn't perfect: it doesn't use the Python interpreter with which the
722 This isn't perfect: it doesn't use the Python interpreter with which the
723 virtualenv was built, and it ignores the --no-site-packages option. A
723 virtualenv was built, and it ignores the --no-site-packages option. A
724 warning will appear suggesting the user installs IPython in the
724 warning will appear suggesting the user installs IPython in the
725 virtualenv, but for many cases, it probably works well enough.
725 virtualenv, but for many cases, it probably works well enough.
726
726
727 Adapted from code snippets online.
727 Adapted from code snippets online.
728
728
729 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
729 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
730 """
730 """
731 if 'VIRTUAL_ENV' not in os.environ:
731 if 'VIRTUAL_ENV' not in os.environ:
732 # Not in a virtualenv
732 # Not in a virtualenv
733 return
733 return
734
734
735 # venv detection:
735 # venv detection:
736 # stdlib venv may symlink sys.executable, so we can't use realpath.
736 # stdlib venv may symlink sys.executable, so we can't use realpath.
737 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
737 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
738 # So we just check every item in the symlink tree (generally <= 3)
738 # So we just check every item in the symlink tree (generally <= 3)
739 p = os.path.normcase(sys.executable)
739 p = os.path.normcase(sys.executable)
740 paths = [p]
740 paths = [p]
741 while os.path.islink(p):
741 while os.path.islink(p):
742 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
742 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
743 paths.append(p)
743 paths.append(p)
744 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
744 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
745 if any(p.startswith(p_venv) for p in paths):
745 if any(p.startswith(p_venv) for p in paths):
746 # Running properly in the virtualenv, don't need to do anything
746 # Running properly in the virtualenv, don't need to do anything
747 return
747 return
748
748
749 warn("Attempting to work in a virtualenv. If you encounter problems, please "
749 warn("Attempting to work in a virtualenv. If you encounter problems, please "
750 "install IPython inside the virtualenv.")
750 "install IPython inside the virtualenv.")
751 if sys.platform == "win32":
751 if sys.platform == "win32":
752 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
752 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
753 else:
753 else:
754 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
754 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
755 'python%d.%d' % sys.version_info[:2], 'site-packages')
755 'python%d.%d' % sys.version_info[:2], 'site-packages')
756
756
757 import site
757 import site
758 sys.path.insert(0, virtual_env)
758 sys.path.insert(0, virtual_env)
759 site.addsitedir(virtual_env)
759 site.addsitedir(virtual_env)
760
760
761 #-------------------------------------------------------------------------
761 #-------------------------------------------------------------------------
762 # Things related to injections into the sys module
762 # Things related to injections into the sys module
763 #-------------------------------------------------------------------------
763 #-------------------------------------------------------------------------
764
764
765 def save_sys_module_state(self):
765 def save_sys_module_state(self):
766 """Save the state of hooks in the sys module.
766 """Save the state of hooks in the sys module.
767
767
768 This has to be called after self.user_module is created.
768 This has to be called after self.user_module is created.
769 """
769 """
770 self._orig_sys_module_state = {}
770 self._orig_sys_module_state = {}
771 self._orig_sys_module_state['stdin'] = sys.stdin
771 self._orig_sys_module_state['stdin'] = sys.stdin
772 self._orig_sys_module_state['stdout'] = sys.stdout
772 self._orig_sys_module_state['stdout'] = sys.stdout
773 self._orig_sys_module_state['stderr'] = sys.stderr
773 self._orig_sys_module_state['stderr'] = sys.stderr
774 self._orig_sys_module_state['excepthook'] = sys.excepthook
774 self._orig_sys_module_state['excepthook'] = sys.excepthook
775 self._orig_sys_modules_main_name = self.user_module.__name__
775 self._orig_sys_modules_main_name = self.user_module.__name__
776 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
776 self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__)
777
777
778 def restore_sys_module_state(self):
778 def restore_sys_module_state(self):
779 """Restore the state of the sys module."""
779 """Restore the state of the sys module."""
780 try:
780 try:
781 for k, v in iteritems(self._orig_sys_module_state):
781 for k, v in iteritems(self._orig_sys_module_state):
782 setattr(sys, k, v)
782 setattr(sys, k, v)
783 except AttributeError:
783 except AttributeError:
784 pass
784 pass
785 # Reset what what done in self.init_sys_modules
785 # Reset what what done in self.init_sys_modules
786 if self._orig_sys_modules_main_mod is not None:
786 if self._orig_sys_modules_main_mod is not None:
787 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
787 sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod
788
788
789 #-------------------------------------------------------------------------
789 #-------------------------------------------------------------------------
790 # Things related to the banner
790 # Things related to the banner
791 #-------------------------------------------------------------------------
791 #-------------------------------------------------------------------------
792
792
793 @property
793 @property
794 def banner(self):
794 def banner(self):
795 banner = self.banner1
795 banner = self.banner1
796 if self.profile and self.profile != 'default':
796 if self.profile and self.profile != 'default':
797 banner += '\nIPython profile: %s\n' % self.profile
797 banner += '\nIPython profile: %s\n' % self.profile
798 if self.banner2:
798 if self.banner2:
799 banner += '\n' + self.banner2
799 banner += '\n' + self.banner2
800 return banner
800 return banner
801
801
802 def show_banner(self, banner=None):
802 def show_banner(self, banner=None):
803 if banner is None:
803 if banner is None:
804 banner = self.banner
804 banner = self.banner
805 self.write(banner)
805 self.write(banner)
806
806
807 #-------------------------------------------------------------------------
807 #-------------------------------------------------------------------------
808 # Things related to hooks
808 # Things related to hooks
809 #-------------------------------------------------------------------------
809 #-------------------------------------------------------------------------
810
810
811 def init_hooks(self):
811 def init_hooks(self):
812 # hooks holds pointers used for user-side customizations
812 # hooks holds pointers used for user-side customizations
813 self.hooks = Struct()
813 self.hooks = Struct()
814
814
815 self.strdispatchers = {}
815 self.strdispatchers = {}
816
816
817 # Set all default hooks, defined in the IPython.hooks module.
817 # Set all default hooks, defined in the IPython.hooks module.
818 hooks = IPython.core.hooks
818 hooks = IPython.core.hooks
819 for hook_name in hooks.__all__:
819 for hook_name in hooks.__all__:
820 # default hooks have priority 100, i.e. low; user hooks should have
820 # default hooks have priority 100, i.e. low; user hooks should have
821 # 0-100 priority
821 # 0-100 priority
822 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
822 self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False)
823
823
824 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
824 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
825 _warn_deprecated=True):
825 _warn_deprecated=True):
826 """set_hook(name,hook) -> sets an internal IPython hook.
826 """set_hook(name,hook) -> sets an internal IPython hook.
827
827
828 IPython exposes some of its internal API as user-modifiable hooks. By
828 IPython exposes some of its internal API as user-modifiable hooks. By
829 adding your function to one of these hooks, you can modify IPython's
829 adding your function to one of these hooks, you can modify IPython's
830 behavior to call at runtime your own routines."""
830 behavior to call at runtime your own routines."""
831
831
832 # At some point in the future, this should validate the hook before it
832 # At some point in the future, this should validate the hook before it
833 # accepts it. Probably at least check that the hook takes the number
833 # accepts it. Probably at least check that the hook takes the number
834 # of args it's supposed to.
834 # of args it's supposed to.
835
835
836 f = types.MethodType(hook,self)
836 f = types.MethodType(hook,self)
837
837
838 # check if the hook is for strdispatcher first
838 # check if the hook is for strdispatcher first
839 if str_key is not None:
839 if str_key is not None:
840 sdp = self.strdispatchers.get(name, StrDispatch())
840 sdp = self.strdispatchers.get(name, StrDispatch())
841 sdp.add_s(str_key, f, priority )
841 sdp.add_s(str_key, f, priority )
842 self.strdispatchers[name] = sdp
842 self.strdispatchers[name] = sdp
843 return
843 return
844 if re_key is not None:
844 if re_key is not None:
845 sdp = self.strdispatchers.get(name, StrDispatch())
845 sdp = self.strdispatchers.get(name, StrDispatch())
846 sdp.add_re(re.compile(re_key), f, priority )
846 sdp.add_re(re.compile(re_key), f, priority )
847 self.strdispatchers[name] = sdp
847 self.strdispatchers[name] = sdp
848 return
848 return
849
849
850 dp = getattr(self.hooks, name, None)
850 dp = getattr(self.hooks, name, None)
851 if name not in IPython.core.hooks.__all__:
851 if name not in IPython.core.hooks.__all__:
852 print("Warning! Hook '%s' is not one of %s" % \
852 print("Warning! Hook '%s' is not one of %s" % \
853 (name, IPython.core.hooks.__all__ ))
853 (name, IPython.core.hooks.__all__ ))
854
854
855 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
855 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
856 alternative = IPython.core.hooks.deprecated[name]
856 alternative = IPython.core.hooks.deprecated[name]
857 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
857 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative))
858
858
859 if not dp:
859 if not dp:
860 dp = IPython.core.hooks.CommandChainDispatcher()
860 dp = IPython.core.hooks.CommandChainDispatcher()
861
861
862 try:
862 try:
863 dp.add(f,priority)
863 dp.add(f,priority)
864 except AttributeError:
864 except AttributeError:
865 # it was not commandchain, plain old func - replace
865 # it was not commandchain, plain old func - replace
866 dp = f
866 dp = f
867
867
868 setattr(self.hooks,name, dp)
868 setattr(self.hooks,name, dp)
869
869
870 #-------------------------------------------------------------------------
870 #-------------------------------------------------------------------------
871 # Things related to events
871 # Things related to events
872 #-------------------------------------------------------------------------
872 #-------------------------------------------------------------------------
873
873
874 def init_events(self):
874 def init_events(self):
875 self.events = EventManager(self, available_events)
875 self.events = EventManager(self, available_events)
876
876
877 self.events.register("pre_execute", self._clear_warning_registry)
877 self.events.register("pre_execute", self._clear_warning_registry)
878
878
879 def register_post_execute(self, func):
879 def register_post_execute(self, func):
880 """DEPRECATED: Use ip.events.register('post_run_cell', func)
880 """DEPRECATED: Use ip.events.register('post_run_cell', func)
881
881
882 Register a function for calling after code execution.
882 Register a function for calling after code execution.
883 """
883 """
884 warn("ip.register_post_execute is deprecated, use "
884 warn("ip.register_post_execute is deprecated, use "
885 "ip.events.register('post_run_cell', func) instead.")
885 "ip.events.register('post_run_cell', func) instead.")
886 self.events.register('post_run_cell', func)
886 self.events.register('post_run_cell', func)
887
887
888 def _clear_warning_registry(self):
888 def _clear_warning_registry(self):
889 # clear the warning registry, so that different code blocks with
889 # clear the warning registry, so that different code blocks with
890 # overlapping line number ranges don't cause spurious suppression of
890 # overlapping line number ranges don't cause spurious suppression of
891 # warnings (see gh-6611 for details)
891 # warnings (see gh-6611 for details)
892 if "__warningregistry__" in self.user_global_ns:
892 if "__warningregistry__" in self.user_global_ns:
893 del self.user_global_ns["__warningregistry__"]
893 del self.user_global_ns["__warningregistry__"]
894
894
895 #-------------------------------------------------------------------------
895 #-------------------------------------------------------------------------
896 # Things related to the "main" module
896 # Things related to the "main" module
897 #-------------------------------------------------------------------------
897 #-------------------------------------------------------------------------
898
898
899 def new_main_mod(self, filename, modname):
899 def new_main_mod(self, filename, modname):
900 """Return a new 'main' module object for user code execution.
900 """Return a new 'main' module object for user code execution.
901
901
902 ``filename`` should be the path of the script which will be run in the
902 ``filename`` should be the path of the script which will be run in the
903 module. Requests with the same filename will get the same module, with
903 module. Requests with the same filename will get the same module, with
904 its namespace cleared.
904 its namespace cleared.
905
905
906 ``modname`` should be the module name - normally either '__main__' or
906 ``modname`` should be the module name - normally either '__main__' or
907 the basename of the file without the extension.
907 the basename of the file without the extension.
908
908
909 When scripts are executed via %run, we must keep a reference to their
909 When scripts are executed via %run, we must keep a reference to their
910 __main__ module around so that Python doesn't
910 __main__ module around so that Python doesn't
911 clear it, rendering references to module globals useless.
911 clear it, rendering references to module globals useless.
912
912
913 This method keeps said reference in a private dict, keyed by the
913 This method keeps said reference in a private dict, keyed by the
914 absolute path of the script. This way, for multiple executions of the
914 absolute path of the script. This way, for multiple executions of the
915 same script we only keep one copy of the namespace (the last one),
915 same script we only keep one copy of the namespace (the last one),
916 thus preventing memory leaks from old references while allowing the
916 thus preventing memory leaks from old references while allowing the
917 objects from the last execution to be accessible.
917 objects from the last execution to be accessible.
918 """
918 """
919 filename = os.path.abspath(filename)
919 filename = os.path.abspath(filename)
920 try:
920 try:
921 main_mod = self._main_mod_cache[filename]
921 main_mod = self._main_mod_cache[filename]
922 except KeyError:
922 except KeyError:
923 main_mod = self._main_mod_cache[filename] = types.ModuleType(
923 main_mod = self._main_mod_cache[filename] = types.ModuleType(
924 py3compat.cast_bytes_py2(modname),
924 py3compat.cast_bytes_py2(modname),
925 doc="Module created for script run in IPython")
925 doc="Module created for script run in IPython")
926 else:
926 else:
927 main_mod.__dict__.clear()
927 main_mod.__dict__.clear()
928 main_mod.__name__ = modname
928 main_mod.__name__ = modname
929
929
930 main_mod.__file__ = filename
930 main_mod.__file__ = filename
931 # It seems pydoc (and perhaps others) needs any module instance to
931 # It seems pydoc (and perhaps others) needs any module instance to
932 # implement a __nonzero__ method
932 # implement a __nonzero__ method
933 main_mod.__nonzero__ = lambda : True
933 main_mod.__nonzero__ = lambda : True
934
934
935 return main_mod
935 return main_mod
936
936
937 def clear_main_mod_cache(self):
937 def clear_main_mod_cache(self):
938 """Clear the cache of main modules.
938 """Clear the cache of main modules.
939
939
940 Mainly for use by utilities like %reset.
940 Mainly for use by utilities like %reset.
941
941
942 Examples
942 Examples
943 --------
943 --------
944
944
945 In [15]: import IPython
945 In [15]: import IPython
946
946
947 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
947 In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython')
948
948
949 In [17]: len(_ip._main_mod_cache) > 0
949 In [17]: len(_ip._main_mod_cache) > 0
950 Out[17]: True
950 Out[17]: True
951
951
952 In [18]: _ip.clear_main_mod_cache()
952 In [18]: _ip.clear_main_mod_cache()
953
953
954 In [19]: len(_ip._main_mod_cache) == 0
954 In [19]: len(_ip._main_mod_cache) == 0
955 Out[19]: True
955 Out[19]: True
956 """
956 """
957 self._main_mod_cache.clear()
957 self._main_mod_cache.clear()
958
958
959 #-------------------------------------------------------------------------
959 #-------------------------------------------------------------------------
960 # Things related to debugging
960 # Things related to debugging
961 #-------------------------------------------------------------------------
961 #-------------------------------------------------------------------------
962
962
963 def init_pdb(self):
963 def init_pdb(self):
964 # Set calling of pdb on exceptions
964 # Set calling of pdb on exceptions
965 # self.call_pdb is a property
965 # self.call_pdb is a property
966 self.call_pdb = self.pdb
966 self.call_pdb = self.pdb
967
967
968 def _get_call_pdb(self):
968 def _get_call_pdb(self):
969 return self._call_pdb
969 return self._call_pdb
970
970
971 def _set_call_pdb(self,val):
971 def _set_call_pdb(self,val):
972
972
973 if val not in (0,1,False,True):
973 if val not in (0,1,False,True):
974 raise ValueError('new call_pdb value must be boolean')
974 raise ValueError('new call_pdb value must be boolean')
975
975
976 # store value in instance
976 # store value in instance
977 self._call_pdb = val
977 self._call_pdb = val
978
978
979 # notify the actual exception handlers
979 # notify the actual exception handlers
980 self.InteractiveTB.call_pdb = val
980 self.InteractiveTB.call_pdb = val
981
981
982 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
982 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
983 'Control auto-activation of pdb at exceptions')
983 'Control auto-activation of pdb at exceptions')
984
984
985 def debugger(self,force=False):
985 def debugger(self,force=False):
986 """Call the pydb/pdb debugger.
986 """Call the pydb/pdb debugger.
987
987
988 Keywords:
988 Keywords:
989
989
990 - force(False): by default, this routine checks the instance call_pdb
990 - force(False): by default, this routine checks the instance call_pdb
991 flag and does not actually invoke the debugger if the flag is false.
991 flag and does not actually invoke the debugger if the flag is false.
992 The 'force' option forces the debugger to activate even if the flag
992 The 'force' option forces the debugger to activate even if the flag
993 is false.
993 is false.
994 """
994 """
995
995
996 if not (force or self.call_pdb):
996 if not (force or self.call_pdb):
997 return
997 return
998
998
999 if not hasattr(sys,'last_traceback'):
999 if not hasattr(sys,'last_traceback'):
1000 error('No traceback has been produced, nothing to debug.')
1000 error('No traceback has been produced, nothing to debug.')
1001 return
1001 return
1002
1002
1003 # use pydb if available
1003 # use pydb if available
1004 if debugger.has_pydb:
1004 if debugger.has_pydb:
1005 from pydb import pm
1005 from pydb import pm
1006 else:
1006 else:
1007 # fallback to our internal debugger
1007 # fallback to our internal debugger
1008 pm = lambda : self.InteractiveTB.debugger(force=True)
1008 pm = lambda : self.InteractiveTB.debugger(force=True)
1009
1009
1010 with self.readline_no_record:
1010 with self.readline_no_record:
1011 pm()
1011 pm()
1012
1012
1013 #-------------------------------------------------------------------------
1013 #-------------------------------------------------------------------------
1014 # Things related to IPython's various namespaces
1014 # Things related to IPython's various namespaces
1015 #-------------------------------------------------------------------------
1015 #-------------------------------------------------------------------------
1016 default_user_namespaces = True
1016 default_user_namespaces = True
1017
1017
1018 def init_create_namespaces(self, user_module=None, user_ns=None):
1018 def init_create_namespaces(self, user_module=None, user_ns=None):
1019 # Create the namespace where the user will operate. user_ns is
1019 # Create the namespace where the user will operate. user_ns is
1020 # normally the only one used, and it is passed to the exec calls as
1020 # normally the only one used, and it is passed to the exec calls as
1021 # the locals argument. But we do carry a user_global_ns namespace
1021 # the locals argument. But we do carry a user_global_ns namespace
1022 # given as the exec 'globals' argument, This is useful in embedding
1022 # given as the exec 'globals' argument, This is useful in embedding
1023 # situations where the ipython shell opens in a context where the
1023 # situations where the ipython shell opens in a context where the
1024 # distinction between locals and globals is meaningful. For
1024 # distinction between locals and globals is meaningful. For
1025 # non-embedded contexts, it is just the same object as the user_ns dict.
1025 # non-embedded contexts, it is just the same object as the user_ns dict.
1026
1026
1027 # FIXME. For some strange reason, __builtins__ is showing up at user
1027 # FIXME. For some strange reason, __builtins__ is showing up at user
1028 # level as a dict instead of a module. This is a manual fix, but I
1028 # level as a dict instead of a module. This is a manual fix, but I
1029 # should really track down where the problem is coming from. Alex
1029 # should really track down where the problem is coming from. Alex
1030 # Schmolck reported this problem first.
1030 # Schmolck reported this problem first.
1031
1031
1032 # A useful post by Alex Martelli on this topic:
1032 # A useful post by Alex Martelli on this topic:
1033 # Re: inconsistent value from __builtins__
1033 # Re: inconsistent value from __builtins__
1034 # Von: Alex Martelli <aleaxit@yahoo.com>
1034 # Von: Alex Martelli <aleaxit@yahoo.com>
1035 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1035 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
1036 # Gruppen: comp.lang.python
1036 # Gruppen: comp.lang.python
1037
1037
1038 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1038 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
1039 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1039 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
1040 # > <type 'dict'>
1040 # > <type 'dict'>
1041 # > >>> print type(__builtins__)
1041 # > >>> print type(__builtins__)
1042 # > <type 'module'>
1042 # > <type 'module'>
1043 # > Is this difference in return value intentional?
1043 # > Is this difference in return value intentional?
1044
1044
1045 # Well, it's documented that '__builtins__' can be either a dictionary
1045 # Well, it's documented that '__builtins__' can be either a dictionary
1046 # or a module, and it's been that way for a long time. Whether it's
1046 # or a module, and it's been that way for a long time. Whether it's
1047 # intentional (or sensible), I don't know. In any case, the idea is
1047 # intentional (or sensible), I don't know. In any case, the idea is
1048 # that if you need to access the built-in namespace directly, you
1048 # that if you need to access the built-in namespace directly, you
1049 # should start with "import __builtin__" (note, no 's') which will
1049 # should start with "import __builtin__" (note, no 's') which will
1050 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1050 # definitely give you a module. Yeah, it's somewhat confusing:-(.
1051
1051
1052 # These routines return a properly built module and dict as needed by
1052 # These routines return a properly built module and dict as needed by
1053 # the rest of the code, and can also be used by extension writers to
1053 # the rest of the code, and can also be used by extension writers to
1054 # generate properly initialized namespaces.
1054 # generate properly initialized namespaces.
1055 if (user_ns is not None) or (user_module is not None):
1055 if (user_ns is not None) or (user_module is not None):
1056 self.default_user_namespaces = False
1056 self.default_user_namespaces = False
1057 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1057 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
1058
1058
1059 # A record of hidden variables we have added to the user namespace, so
1059 # A record of hidden variables we have added to the user namespace, so
1060 # we can list later only variables defined in actual interactive use.
1060 # we can list later only variables defined in actual interactive use.
1061 self.user_ns_hidden = {}
1061 self.user_ns_hidden = {}
1062
1062
1063 # Now that FakeModule produces a real module, we've run into a nasty
1063 # Now that FakeModule produces a real module, we've run into a nasty
1064 # problem: after script execution (via %run), the module where the user
1064 # problem: after script execution (via %run), the module where the user
1065 # code ran is deleted. Now that this object is a true module (needed
1065 # code ran is deleted. Now that this object is a true module (needed
1066 # so docetst and other tools work correctly), the Python module
1066 # so docetst and other tools work correctly), the Python module
1067 # teardown mechanism runs over it, and sets to None every variable
1067 # teardown mechanism runs over it, and sets to None every variable
1068 # present in that module. Top-level references to objects from the
1068 # present in that module. Top-level references to objects from the
1069 # script survive, because the user_ns is updated with them. However,
1069 # script survive, because the user_ns is updated with them. However,
1070 # calling functions defined in the script that use other things from
1070 # calling functions defined in the script that use other things from
1071 # the script will fail, because the function's closure had references
1071 # the script will fail, because the function's closure had references
1072 # to the original objects, which are now all None. So we must protect
1072 # to the original objects, which are now all None. So we must protect
1073 # these modules from deletion by keeping a cache.
1073 # these modules from deletion by keeping a cache.
1074 #
1074 #
1075 # To avoid keeping stale modules around (we only need the one from the
1075 # To avoid keeping stale modules around (we only need the one from the
1076 # last run), we use a dict keyed with the full path to the script, so
1076 # last run), we use a dict keyed with the full path to the script, so
1077 # only the last version of the module is held in the cache. Note,
1077 # only the last version of the module is held in the cache. Note,
1078 # however, that we must cache the module *namespace contents* (their
1078 # however, that we must cache the module *namespace contents* (their
1079 # __dict__). Because if we try to cache the actual modules, old ones
1079 # __dict__). Because if we try to cache the actual modules, old ones
1080 # (uncached) could be destroyed while still holding references (such as
1080 # (uncached) could be destroyed while still holding references (such as
1081 # those held by GUI objects that tend to be long-lived)>
1081 # those held by GUI objects that tend to be long-lived)>
1082 #
1082 #
1083 # The %reset command will flush this cache. See the cache_main_mod()
1083 # The %reset command will flush this cache. See the cache_main_mod()
1084 # and clear_main_mod_cache() methods for details on use.
1084 # and clear_main_mod_cache() methods for details on use.
1085
1085
1086 # This is the cache used for 'main' namespaces
1086 # This is the cache used for 'main' namespaces
1087 self._main_mod_cache = {}
1087 self._main_mod_cache = {}
1088
1088
1089 # A table holding all the namespaces IPython deals with, so that
1089 # A table holding all the namespaces IPython deals with, so that
1090 # introspection facilities can search easily.
1090 # introspection facilities can search easily.
1091 self.ns_table = {'user_global':self.user_module.__dict__,
1091 self.ns_table = {'user_global':self.user_module.__dict__,
1092 'user_local':self.user_ns,
1092 'user_local':self.user_ns,
1093 'builtin':builtin_mod.__dict__
1093 'builtin':builtin_mod.__dict__
1094 }
1094 }
1095
1095
1096 @property
1096 @property
1097 def user_global_ns(self):
1097 def user_global_ns(self):
1098 return self.user_module.__dict__
1098 return self.user_module.__dict__
1099
1099
1100 def prepare_user_module(self, user_module=None, user_ns=None):
1100 def prepare_user_module(self, user_module=None, user_ns=None):
1101 """Prepare the module and namespace in which user code will be run.
1101 """Prepare the module and namespace in which user code will be run.
1102
1102
1103 When IPython is started normally, both parameters are None: a new module
1103 When IPython is started normally, both parameters are None: a new module
1104 is created automatically, and its __dict__ used as the namespace.
1104 is created automatically, and its __dict__ used as the namespace.
1105
1105
1106 If only user_module is provided, its __dict__ is used as the namespace.
1106 If only user_module is provided, its __dict__ is used as the namespace.
1107 If only user_ns is provided, a dummy module is created, and user_ns
1107 If only user_ns is provided, a dummy module is created, and user_ns
1108 becomes the global namespace. If both are provided (as they may be
1108 becomes the global namespace. If both are provided (as they may be
1109 when embedding), user_ns is the local namespace, and user_module
1109 when embedding), user_ns is the local namespace, and user_module
1110 provides the global namespace.
1110 provides the global namespace.
1111
1111
1112 Parameters
1112 Parameters
1113 ----------
1113 ----------
1114 user_module : module, optional
1114 user_module : module, optional
1115 The current user module in which IPython is being run. If None,
1115 The current user module in which IPython is being run. If None,
1116 a clean module will be created.
1116 a clean module will be created.
1117 user_ns : dict, optional
1117 user_ns : dict, optional
1118 A namespace in which to run interactive commands.
1118 A namespace in which to run interactive commands.
1119
1119
1120 Returns
1120 Returns
1121 -------
1121 -------
1122 A tuple of user_module and user_ns, each properly initialised.
1122 A tuple of user_module and user_ns, each properly initialised.
1123 """
1123 """
1124 if user_module is None and user_ns is not None:
1124 if user_module is None and user_ns is not None:
1125 user_ns.setdefault("__name__", "__main__")
1125 user_ns.setdefault("__name__", "__main__")
1126 user_module = DummyMod()
1126 user_module = DummyMod()
1127 user_module.__dict__ = user_ns
1127 user_module.__dict__ = user_ns
1128
1128
1129 if user_module is None:
1129 if user_module is None:
1130 user_module = types.ModuleType("__main__",
1130 user_module = types.ModuleType("__main__",
1131 doc="Automatically created module for IPython interactive environment")
1131 doc="Automatically created module for IPython interactive environment")
1132
1132
1133 # We must ensure that __builtin__ (without the final 's') is always
1133 # We must ensure that __builtin__ (without the final 's') is always
1134 # available and pointing to the __builtin__ *module*. For more details:
1134 # available and pointing to the __builtin__ *module*. For more details:
1135 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1135 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1136 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1136 user_module.__dict__.setdefault('__builtin__', builtin_mod)
1137 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1137 user_module.__dict__.setdefault('__builtins__', builtin_mod)
1138
1138
1139 if user_ns is None:
1139 if user_ns is None:
1140 user_ns = user_module.__dict__
1140 user_ns = user_module.__dict__
1141
1141
1142 return user_module, user_ns
1142 return user_module, user_ns
1143
1143
1144 def init_sys_modules(self):
1144 def init_sys_modules(self):
1145 # We need to insert into sys.modules something that looks like a
1145 # We need to insert into sys.modules something that looks like a
1146 # module but which accesses the IPython namespace, for shelve and
1146 # module but which accesses the IPython namespace, for shelve and
1147 # pickle to work interactively. Normally they rely on getting
1147 # pickle to work interactively. Normally they rely on getting
1148 # everything out of __main__, but for embedding purposes each IPython
1148 # everything out of __main__, but for embedding purposes each IPython
1149 # instance has its own private namespace, so we can't go shoving
1149 # instance has its own private namespace, so we can't go shoving
1150 # everything into __main__.
1150 # everything into __main__.
1151
1151
1152 # note, however, that we should only do this for non-embedded
1152 # note, however, that we should only do this for non-embedded
1153 # ipythons, which really mimic the __main__.__dict__ with their own
1153 # ipythons, which really mimic the __main__.__dict__ with their own
1154 # namespace. Embedded instances, on the other hand, should not do
1154 # namespace. Embedded instances, on the other hand, should not do
1155 # this because they need to manage the user local/global namespaces
1155 # this because they need to manage the user local/global namespaces
1156 # only, but they live within a 'normal' __main__ (meaning, they
1156 # only, but they live within a 'normal' __main__ (meaning, they
1157 # shouldn't overtake the execution environment of the script they're
1157 # shouldn't overtake the execution environment of the script they're
1158 # embedded in).
1158 # embedded in).
1159
1159
1160 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1160 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
1161 main_name = self.user_module.__name__
1161 main_name = self.user_module.__name__
1162 sys.modules[main_name] = self.user_module
1162 sys.modules[main_name] = self.user_module
1163
1163
1164 def init_user_ns(self):
1164 def init_user_ns(self):
1165 """Initialize all user-visible namespaces to their minimum defaults.
1165 """Initialize all user-visible namespaces to their minimum defaults.
1166
1166
1167 Certain history lists are also initialized here, as they effectively
1167 Certain history lists are also initialized here, as they effectively
1168 act as user namespaces.
1168 act as user namespaces.
1169
1169
1170 Notes
1170 Notes
1171 -----
1171 -----
1172 All data structures here are only filled in, they are NOT reset by this
1172 All data structures here are only filled in, they are NOT reset by this
1173 method. If they were not empty before, data will simply be added to
1173 method. If they were not empty before, data will simply be added to
1174 therm.
1174 therm.
1175 """
1175 """
1176 # This function works in two parts: first we put a few things in
1176 # This function works in two parts: first we put a few things in
1177 # user_ns, and we sync that contents into user_ns_hidden so that these
1177 # user_ns, and we sync that contents into user_ns_hidden so that these
1178 # initial variables aren't shown by %who. After the sync, we add the
1178 # initial variables aren't shown by %who. After the sync, we add the
1179 # rest of what we *do* want the user to see with %who even on a new
1179 # rest of what we *do* want the user to see with %who even on a new
1180 # session (probably nothing, so theye really only see their own stuff)
1180 # session (probably nothing, so theye really only see their own stuff)
1181
1181
1182 # The user dict must *always* have a __builtin__ reference to the
1182 # The user dict must *always* have a __builtin__ reference to the
1183 # Python standard __builtin__ namespace, which must be imported.
1183 # Python standard __builtin__ namespace, which must be imported.
1184 # This is so that certain operations in prompt evaluation can be
1184 # This is so that certain operations in prompt evaluation can be
1185 # reliably executed with builtins. Note that we can NOT use
1185 # reliably executed with builtins. Note that we can NOT use
1186 # __builtins__ (note the 's'), because that can either be a dict or a
1186 # __builtins__ (note the 's'), because that can either be a dict or a
1187 # module, and can even mutate at runtime, depending on the context
1187 # module, and can even mutate at runtime, depending on the context
1188 # (Python makes no guarantees on it). In contrast, __builtin__ is
1188 # (Python makes no guarantees on it). In contrast, __builtin__ is
1189 # always a module object, though it must be explicitly imported.
1189 # always a module object, though it must be explicitly imported.
1190
1190
1191 # For more details:
1191 # For more details:
1192 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1192 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1193 ns = dict()
1193 ns = dict()
1194
1194
1195 # make global variables for user access to the histories
1195 # make global variables for user access to the histories
1196 ns['_ih'] = self.history_manager.input_hist_parsed
1196 ns['_ih'] = self.history_manager.input_hist_parsed
1197 ns['_oh'] = self.history_manager.output_hist
1197 ns['_oh'] = self.history_manager.output_hist
1198 ns['_dh'] = self.history_manager.dir_hist
1198 ns['_dh'] = self.history_manager.dir_hist
1199
1199
1200 ns['_sh'] = shadowns
1200 ns['_sh'] = shadowns
1201
1201
1202 # user aliases to input and output histories. These shouldn't show up
1202 # user aliases to input and output histories. These shouldn't show up
1203 # in %who, as they can have very large reprs.
1203 # in %who, as they can have very large reprs.
1204 ns['In'] = self.history_manager.input_hist_parsed
1204 ns['In'] = self.history_manager.input_hist_parsed
1205 ns['Out'] = self.history_manager.output_hist
1205 ns['Out'] = self.history_manager.output_hist
1206
1206
1207 # Store myself as the public api!!!
1207 # Store myself as the public api!!!
1208 ns['get_ipython'] = self.get_ipython
1208 ns['get_ipython'] = self.get_ipython
1209
1209
1210 ns['exit'] = self.exiter
1210 ns['exit'] = self.exiter
1211 ns['quit'] = self.exiter
1211 ns['quit'] = self.exiter
1212
1212
1213 # Sync what we've added so far to user_ns_hidden so these aren't seen
1213 # Sync what we've added so far to user_ns_hidden so these aren't seen
1214 # by %who
1214 # by %who
1215 self.user_ns_hidden.update(ns)
1215 self.user_ns_hidden.update(ns)
1216
1216
1217 # Anything put into ns now would show up in %who. Think twice before
1217 # Anything put into ns now would show up in %who. Think twice before
1218 # putting anything here, as we really want %who to show the user their
1218 # putting anything here, as we really want %who to show the user their
1219 # stuff, not our variables.
1219 # stuff, not our variables.
1220
1220
1221 # Finally, update the real user's namespace
1221 # Finally, update the real user's namespace
1222 self.user_ns.update(ns)
1222 self.user_ns.update(ns)
1223
1223
1224 @property
1224 @property
1225 def all_ns_refs(self):
1225 def all_ns_refs(self):
1226 """Get a list of references to all the namespace dictionaries in which
1226 """Get a list of references to all the namespace dictionaries in which
1227 IPython might store a user-created object.
1227 IPython might store a user-created object.
1228
1228
1229 Note that this does not include the displayhook, which also caches
1229 Note that this does not include the displayhook, which also caches
1230 objects from the output."""
1230 objects from the output."""
1231 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1231 return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \
1232 [m.__dict__ for m in self._main_mod_cache.values()]
1232 [m.__dict__ for m in self._main_mod_cache.values()]
1233
1233
1234 def reset(self, new_session=True):
1234 def reset(self, new_session=True):
1235 """Clear all internal namespaces, and attempt to release references to
1235 """Clear all internal namespaces, and attempt to release references to
1236 user objects.
1236 user objects.
1237
1237
1238 If new_session is True, a new history session will be opened.
1238 If new_session is True, a new history session will be opened.
1239 """
1239 """
1240 # Clear histories
1240 # Clear histories
1241 self.history_manager.reset(new_session)
1241 self.history_manager.reset(new_session)
1242 # Reset counter used to index all histories
1242 # Reset counter used to index all histories
1243 if new_session:
1243 if new_session:
1244 self.execution_count = 1
1244 self.execution_count = 1
1245
1245
1246 # Flush cached output items
1246 # Flush cached output items
1247 if self.displayhook.do_full_cache:
1247 if self.displayhook.do_full_cache:
1248 self.displayhook.flush()
1248 self.displayhook.flush()
1249
1249
1250 # The main execution namespaces must be cleared very carefully,
1250 # The main execution namespaces must be cleared very carefully,
1251 # skipping the deletion of the builtin-related keys, because doing so
1251 # skipping the deletion of the builtin-related keys, because doing so
1252 # would cause errors in many object's __del__ methods.
1252 # would cause errors in many object's __del__ methods.
1253 if self.user_ns is not self.user_global_ns:
1253 if self.user_ns is not self.user_global_ns:
1254 self.user_ns.clear()
1254 self.user_ns.clear()
1255 ns = self.user_global_ns
1255 ns = self.user_global_ns
1256 drop_keys = set(ns.keys())
1256 drop_keys = set(ns.keys())
1257 drop_keys.discard('__builtin__')
1257 drop_keys.discard('__builtin__')
1258 drop_keys.discard('__builtins__')
1258 drop_keys.discard('__builtins__')
1259 drop_keys.discard('__name__')
1259 drop_keys.discard('__name__')
1260 for k in drop_keys:
1260 for k in drop_keys:
1261 del ns[k]
1261 del ns[k]
1262
1262
1263 self.user_ns_hidden.clear()
1263 self.user_ns_hidden.clear()
1264
1264
1265 # Restore the user namespaces to minimal usability
1265 # Restore the user namespaces to minimal usability
1266 self.init_user_ns()
1266 self.init_user_ns()
1267
1267
1268 # Restore the default and user aliases
1268 # Restore the default and user aliases
1269 self.alias_manager.clear_aliases()
1269 self.alias_manager.clear_aliases()
1270 self.alias_manager.init_aliases()
1270 self.alias_manager.init_aliases()
1271
1271
1272 # Flush the private list of module references kept for script
1272 # Flush the private list of module references kept for script
1273 # execution protection
1273 # execution protection
1274 self.clear_main_mod_cache()
1274 self.clear_main_mod_cache()
1275
1275
1276 def del_var(self, varname, by_name=False):
1276 def del_var(self, varname, by_name=False):
1277 """Delete a variable from the various namespaces, so that, as
1277 """Delete a variable from the various namespaces, so that, as
1278 far as possible, we're not keeping any hidden references to it.
1278 far as possible, we're not keeping any hidden references to it.
1279
1279
1280 Parameters
1280 Parameters
1281 ----------
1281 ----------
1282 varname : str
1282 varname : str
1283 The name of the variable to delete.
1283 The name of the variable to delete.
1284 by_name : bool
1284 by_name : bool
1285 If True, delete variables with the given name in each
1285 If True, delete variables with the given name in each
1286 namespace. If False (default), find the variable in the user
1286 namespace. If False (default), find the variable in the user
1287 namespace, and delete references to it.
1287 namespace, and delete references to it.
1288 """
1288 """
1289 if varname in ('__builtin__', '__builtins__'):
1289 if varname in ('__builtin__', '__builtins__'):
1290 raise ValueError("Refusing to delete %s" % varname)
1290 raise ValueError("Refusing to delete %s" % varname)
1291
1291
1292 ns_refs = self.all_ns_refs
1292 ns_refs = self.all_ns_refs
1293
1293
1294 if by_name: # Delete by name
1294 if by_name: # Delete by name
1295 for ns in ns_refs:
1295 for ns in ns_refs:
1296 try:
1296 try:
1297 del ns[varname]
1297 del ns[varname]
1298 except KeyError:
1298 except KeyError:
1299 pass
1299 pass
1300 else: # Delete by object
1300 else: # Delete by object
1301 try:
1301 try:
1302 obj = self.user_ns[varname]
1302 obj = self.user_ns[varname]
1303 except KeyError:
1303 except KeyError:
1304 raise NameError("name '%s' is not defined" % varname)
1304 raise NameError("name '%s' is not defined" % varname)
1305 # Also check in output history
1305 # Also check in output history
1306 ns_refs.append(self.history_manager.output_hist)
1306 ns_refs.append(self.history_manager.output_hist)
1307 for ns in ns_refs:
1307 for ns in ns_refs:
1308 to_delete = [n for n, o in iteritems(ns) if o is obj]
1308 to_delete = [n for n, o in iteritems(ns) if o is obj]
1309 for name in to_delete:
1309 for name in to_delete:
1310 del ns[name]
1310 del ns[name]
1311
1311
1312 # displayhook keeps extra references, but not in a dictionary
1312 # displayhook keeps extra references, but not in a dictionary
1313 for name in ('_', '__', '___'):
1313 for name in ('_', '__', '___'):
1314 if getattr(self.displayhook, name) is obj:
1314 if getattr(self.displayhook, name) is obj:
1315 setattr(self.displayhook, name, None)
1315 setattr(self.displayhook, name, None)
1316
1316
1317 def reset_selective(self, regex=None):
1317 def reset_selective(self, regex=None):
1318 """Clear selective variables from internal namespaces based on a
1318 """Clear selective variables from internal namespaces based on a
1319 specified regular expression.
1319 specified regular expression.
1320
1320
1321 Parameters
1321 Parameters
1322 ----------
1322 ----------
1323 regex : string or compiled pattern, optional
1323 regex : string or compiled pattern, optional
1324 A regular expression pattern that will be used in searching
1324 A regular expression pattern that will be used in searching
1325 variable names in the users namespaces.
1325 variable names in the users namespaces.
1326 """
1326 """
1327 if regex is not None:
1327 if regex is not None:
1328 try:
1328 try:
1329 m = re.compile(regex)
1329 m = re.compile(regex)
1330 except TypeError:
1330 except TypeError:
1331 raise TypeError('regex must be a string or compiled pattern')
1331 raise TypeError('regex must be a string or compiled pattern')
1332 # Search for keys in each namespace that match the given regex
1332 # Search for keys in each namespace that match the given regex
1333 # If a match is found, delete the key/value pair.
1333 # If a match is found, delete the key/value pair.
1334 for ns in self.all_ns_refs:
1334 for ns in self.all_ns_refs:
1335 for var in ns:
1335 for var in ns:
1336 if m.search(var):
1336 if m.search(var):
1337 del ns[var]
1337 del ns[var]
1338
1338
1339 def push(self, variables, interactive=True):
1339 def push(self, variables, interactive=True):
1340 """Inject a group of variables into the IPython user namespace.
1340 """Inject a group of variables into the IPython user namespace.
1341
1341
1342 Parameters
1342 Parameters
1343 ----------
1343 ----------
1344 variables : dict, str or list/tuple of str
1344 variables : dict, str or list/tuple of str
1345 The variables to inject into the user's namespace. If a dict, a
1345 The variables to inject into the user's namespace. If a dict, a
1346 simple update is done. If a str, the string is assumed to have
1346 simple update is done. If a str, the string is assumed to have
1347 variable names separated by spaces. A list/tuple of str can also
1347 variable names separated by spaces. A list/tuple of str can also
1348 be used to give the variable names. If just the variable names are
1348 be used to give the variable names. If just the variable names are
1349 give (list/tuple/str) then the variable values looked up in the
1349 give (list/tuple/str) then the variable values looked up in the
1350 callers frame.
1350 callers frame.
1351 interactive : bool
1351 interactive : bool
1352 If True (default), the variables will be listed with the ``who``
1352 If True (default), the variables will be listed with the ``who``
1353 magic.
1353 magic.
1354 """
1354 """
1355 vdict = None
1355 vdict = None
1356
1356
1357 # We need a dict of name/value pairs to do namespace updates.
1357 # We need a dict of name/value pairs to do namespace updates.
1358 if isinstance(variables, dict):
1358 if isinstance(variables, dict):
1359 vdict = variables
1359 vdict = variables
1360 elif isinstance(variables, string_types+(list, tuple)):
1360 elif isinstance(variables, string_types+(list, tuple)):
1361 if isinstance(variables, string_types):
1361 if isinstance(variables, string_types):
1362 vlist = variables.split()
1362 vlist = variables.split()
1363 else:
1363 else:
1364 vlist = variables
1364 vlist = variables
1365 vdict = {}
1365 vdict = {}
1366 cf = sys._getframe(1)
1366 cf = sys._getframe(1)
1367 for name in vlist:
1367 for name in vlist:
1368 try:
1368 try:
1369 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1369 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1370 except:
1370 except:
1371 print('Could not get variable %s from %s' %
1371 print('Could not get variable %s from %s' %
1372 (name,cf.f_code.co_name))
1372 (name,cf.f_code.co_name))
1373 else:
1373 else:
1374 raise ValueError('variables must be a dict/str/list/tuple')
1374 raise ValueError('variables must be a dict/str/list/tuple')
1375
1375
1376 # Propagate variables to user namespace
1376 # Propagate variables to user namespace
1377 self.user_ns.update(vdict)
1377 self.user_ns.update(vdict)
1378
1378
1379 # And configure interactive visibility
1379 # And configure interactive visibility
1380 user_ns_hidden = self.user_ns_hidden
1380 user_ns_hidden = self.user_ns_hidden
1381 if interactive:
1381 if interactive:
1382 for name in vdict:
1382 for name in vdict:
1383 user_ns_hidden.pop(name, None)
1383 user_ns_hidden.pop(name, None)
1384 else:
1384 else:
1385 user_ns_hidden.update(vdict)
1385 user_ns_hidden.update(vdict)
1386
1386
1387 def drop_by_id(self, variables):
1387 def drop_by_id(self, variables):
1388 """Remove a dict of variables from the user namespace, if they are the
1388 """Remove a dict of variables from the user namespace, if they are the
1389 same as the values in the dictionary.
1389 same as the values in the dictionary.
1390
1390
1391 This is intended for use by extensions: variables that they've added can
1391 This is intended for use by extensions: variables that they've added can
1392 be taken back out if they are unloaded, without removing any that the
1392 be taken back out if they are unloaded, without removing any that the
1393 user has overwritten.
1393 user has overwritten.
1394
1394
1395 Parameters
1395 Parameters
1396 ----------
1396 ----------
1397 variables : dict
1397 variables : dict
1398 A dictionary mapping object names (as strings) to the objects.
1398 A dictionary mapping object names (as strings) to the objects.
1399 """
1399 """
1400 for name, obj in iteritems(variables):
1400 for name, obj in iteritems(variables):
1401 if name in self.user_ns and self.user_ns[name] is obj:
1401 if name in self.user_ns and self.user_ns[name] is obj:
1402 del self.user_ns[name]
1402 del self.user_ns[name]
1403 self.user_ns_hidden.pop(name, None)
1403 self.user_ns_hidden.pop(name, None)
1404
1404
1405 #-------------------------------------------------------------------------
1405 #-------------------------------------------------------------------------
1406 # Things related to object introspection
1406 # Things related to object introspection
1407 #-------------------------------------------------------------------------
1407 #-------------------------------------------------------------------------
1408
1408
1409 def _ofind(self, oname, namespaces=None):
1409 def _ofind(self, oname, namespaces=None):
1410 """Find an object in the available namespaces.
1410 """Find an object in the available namespaces.
1411
1411
1412 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1412 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1413
1413
1414 Has special code to detect magic functions.
1414 Has special code to detect magic functions.
1415 """
1415 """
1416 oname = oname.strip()
1416 oname = oname.strip()
1417 #print '1- oname: <%r>' % oname # dbg
1417 #print '1- oname: <%r>' % oname # dbg
1418 if not oname.startswith(ESC_MAGIC) and \
1418 if not oname.startswith(ESC_MAGIC) and \
1419 not oname.startswith(ESC_MAGIC2) and \
1419 not oname.startswith(ESC_MAGIC2) and \
1420 not py3compat.isidentifier(oname, dotted=True):
1420 not py3compat.isidentifier(oname, dotted=True):
1421 return dict(found=False)
1421 return dict(found=False)
1422
1422
1423 alias_ns = None
1423 alias_ns = None
1424 if namespaces is None:
1424 if namespaces is None:
1425 # Namespaces to search in:
1425 # Namespaces to search in:
1426 # Put them in a list. The order is important so that we
1426 # Put them in a list. The order is important so that we
1427 # find things in the same order that Python finds them.
1427 # find things in the same order that Python finds them.
1428 namespaces = [ ('Interactive', self.user_ns),
1428 namespaces = [ ('Interactive', self.user_ns),
1429 ('Interactive (global)', self.user_global_ns),
1429 ('Interactive (global)', self.user_global_ns),
1430 ('Python builtin', builtin_mod.__dict__),
1430 ('Python builtin', builtin_mod.__dict__),
1431 ]
1431 ]
1432
1432
1433 # initialize results to 'null'
1433 # initialize results to 'null'
1434 found = False; obj = None; ospace = None; ds = None;
1434 found = False; obj = None; ospace = None; ds = None;
1435 ismagic = False; isalias = False; parent = None
1435 ismagic = False; isalias = False; parent = None
1436
1436
1437 # We need to special-case 'print', which as of python2.6 registers as a
1437 # We need to special-case 'print', which as of python2.6 registers as a
1438 # function but should only be treated as one if print_function was
1438 # function but should only be treated as one if print_function was
1439 # loaded with a future import. In this case, just bail.
1439 # loaded with a future import. In this case, just bail.
1440 if (oname == 'print' and not py3compat.PY3 and not \
1440 if (oname == 'print' and not py3compat.PY3 and not \
1441 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1441 (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)):
1442 return {'found':found, 'obj':obj, 'namespace':ospace,
1442 return {'found':found, 'obj':obj, 'namespace':ospace,
1443 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1443 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1444
1444
1445 # Look for the given name by splitting it in parts. If the head is
1445 # Look for the given name by splitting it in parts. If the head is
1446 # found, then we look for all the remaining parts as members, and only
1446 # found, then we look for all the remaining parts as members, and only
1447 # declare success if we can find them all.
1447 # declare success if we can find them all.
1448 oname_parts = oname.split('.')
1448 oname_parts = oname.split('.')
1449 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1449 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1450 for nsname,ns in namespaces:
1450 for nsname,ns in namespaces:
1451 try:
1451 try:
1452 obj = ns[oname_head]
1452 obj = ns[oname_head]
1453 except KeyError:
1453 except KeyError:
1454 continue
1454 continue
1455 else:
1455 else:
1456 #print 'oname_rest:', oname_rest # dbg
1456 #print 'oname_rest:', oname_rest # dbg
1457 for idx, part in enumerate(oname_rest):
1457 for idx, part in enumerate(oname_rest):
1458 try:
1458 try:
1459 parent = obj
1459 parent = obj
1460 # The last part is looked up in a special way to avoid
1460 # The last part is looked up in a special way to avoid
1461 # descriptor invocation as it may raise or have side
1461 # descriptor invocation as it may raise or have side
1462 # effects.
1462 # effects.
1463 if idx == len(oname_rest) - 1:
1463 if idx == len(oname_rest) - 1:
1464 obj = self._getattr_property(obj, part)
1464 obj = self._getattr_property(obj, part)
1465 else:
1465 else:
1466 obj = getattr(obj, part)
1466 obj = getattr(obj, part)
1467 except:
1467 except:
1468 # Blanket except b/c some badly implemented objects
1468 # Blanket except b/c some badly implemented objects
1469 # allow __getattr__ to raise exceptions other than
1469 # allow __getattr__ to raise exceptions other than
1470 # AttributeError, which then crashes IPython.
1470 # AttributeError, which then crashes IPython.
1471 break
1471 break
1472 else:
1472 else:
1473 # If we finish the for loop (no break), we got all members
1473 # If we finish the for loop (no break), we got all members
1474 found = True
1474 found = True
1475 ospace = nsname
1475 ospace = nsname
1476 break # namespace loop
1476 break # namespace loop
1477
1477
1478 # Try to see if it's magic
1478 # Try to see if it's magic
1479 if not found:
1479 if not found:
1480 obj = None
1480 obj = None
1481 if oname.startswith(ESC_MAGIC2):
1481 if oname.startswith(ESC_MAGIC2):
1482 oname = oname.lstrip(ESC_MAGIC2)
1482 oname = oname.lstrip(ESC_MAGIC2)
1483 obj = self.find_cell_magic(oname)
1483 obj = self.find_cell_magic(oname)
1484 elif oname.startswith(ESC_MAGIC):
1484 elif oname.startswith(ESC_MAGIC):
1485 oname = oname.lstrip(ESC_MAGIC)
1485 oname = oname.lstrip(ESC_MAGIC)
1486 obj = self.find_line_magic(oname)
1486 obj = self.find_line_magic(oname)
1487 else:
1487 else:
1488 # search without prefix, so run? will find %run?
1488 # search without prefix, so run? will find %run?
1489 obj = self.find_line_magic(oname)
1489 obj = self.find_line_magic(oname)
1490 if obj is None:
1490 if obj is None:
1491 obj = self.find_cell_magic(oname)
1491 obj = self.find_cell_magic(oname)
1492 if obj is not None:
1492 if obj is not None:
1493 found = True
1493 found = True
1494 ospace = 'IPython internal'
1494 ospace = 'IPython internal'
1495 ismagic = True
1495 ismagic = True
1496
1496
1497 # Last try: special-case some literals like '', [], {}, etc:
1497 # Last try: special-case some literals like '', [], {}, etc:
1498 if not found and oname_head in ["''",'""','[]','{}','()']:
1498 if not found and oname_head in ["''",'""','[]','{}','()']:
1499 obj = eval(oname_head)
1499 obj = eval(oname_head)
1500 found = True
1500 found = True
1501 ospace = 'Interactive'
1501 ospace = 'Interactive'
1502
1502
1503 return {'found':found, 'obj':obj, 'namespace':ospace,
1503 return {'found':found, 'obj':obj, 'namespace':ospace,
1504 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1504 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
1505
1505
1506 @staticmethod
1506 @staticmethod
1507 def _getattr_property(obj, attrname):
1507 def _getattr_property(obj, attrname):
1508 """Property-aware getattr to use in object finding.
1508 """Property-aware getattr to use in object finding.
1509
1509
1510 If attrname represents a property, return it unevaluated (in case it has
1510 If attrname represents a property, return it unevaluated (in case it has
1511 side effects or raises an error.
1511 side effects or raises an error.
1512
1512
1513 """
1513 """
1514 if not isinstance(obj, type):
1514 if not isinstance(obj, type):
1515 try:
1515 try:
1516 # `getattr(type(obj), attrname)` is not guaranteed to return
1516 # `getattr(type(obj), attrname)` is not guaranteed to return
1517 # `obj`, but does so for property:
1517 # `obj`, but does so for property:
1518 #
1518 #
1519 # property.__get__(self, None, cls) -> self
1519 # property.__get__(self, None, cls) -> self
1520 #
1520 #
1521 # The universal alternative is to traverse the mro manually
1521 # The universal alternative is to traverse the mro manually
1522 # searching for attrname in class dicts.
1522 # searching for attrname in class dicts.
1523 attr = getattr(type(obj), attrname)
1523 attr = getattr(type(obj), attrname)
1524 except AttributeError:
1524 except AttributeError:
1525 pass
1525 pass
1526 else:
1526 else:
1527 # This relies on the fact that data descriptors (with both
1527 # This relies on the fact that data descriptors (with both
1528 # __get__ & __set__ magic methods) take precedence over
1528 # __get__ & __set__ magic methods) take precedence over
1529 # instance-level attributes:
1529 # instance-level attributes:
1530 #
1530 #
1531 # class A(object):
1531 # class A(object):
1532 # @property
1532 # @property
1533 # def foobar(self): return 123
1533 # def foobar(self): return 123
1534 # a = A()
1534 # a = A()
1535 # a.__dict__['foobar'] = 345
1535 # a.__dict__['foobar'] = 345
1536 # a.foobar # == 123
1536 # a.foobar # == 123
1537 #
1537 #
1538 # So, a property may be returned right away.
1538 # So, a property may be returned right away.
1539 if isinstance(attr, property):
1539 if isinstance(attr, property):
1540 return attr
1540 return attr
1541
1541
1542 # Nothing helped, fall back.
1542 # Nothing helped, fall back.
1543 return getattr(obj, attrname)
1543 return getattr(obj, attrname)
1544
1544
1545 def _object_find(self, oname, namespaces=None):
1545 def _object_find(self, oname, namespaces=None):
1546 """Find an object and return a struct with info about it."""
1546 """Find an object and return a struct with info about it."""
1547 return Struct(self._ofind(oname, namespaces))
1547 return Struct(self._ofind(oname, namespaces))
1548
1548
1549 def _inspect(self, meth, oname, namespaces=None, **kw):
1549 def _inspect(self, meth, oname, namespaces=None, **kw):
1550 """Generic interface to the inspector system.
1550 """Generic interface to the inspector system.
1551
1551
1552 This function is meant to be called by pdef, pdoc & friends."""
1552 This function is meant to be called by pdef, pdoc & friends."""
1553 info = self._object_find(oname, namespaces)
1553 info = self._object_find(oname, namespaces)
1554 if info.found:
1554 if info.found:
1555 pmethod = getattr(self.inspector, meth)
1555 pmethod = getattr(self.inspector, meth)
1556 formatter = format_screen if info.ismagic else None
1556 formatter = format_screen if info.ismagic else None
1557 if meth == 'pdoc':
1557 if meth == 'pdoc':
1558 pmethod(info.obj, oname, formatter)
1558 pmethod(info.obj, oname, formatter)
1559 elif meth == 'pinfo':
1559 elif meth == 'pinfo':
1560 pmethod(info.obj, oname, formatter, info, **kw)
1560 pmethod(info.obj, oname, formatter, info, **kw)
1561 else:
1561 else:
1562 pmethod(info.obj, oname)
1562 pmethod(info.obj, oname)
1563 else:
1563 else:
1564 print('Object `%s` not found.' % oname)
1564 print('Object `%s` not found.' % oname)
1565 return 'not found' # so callers can take other action
1565 return 'not found' # so callers can take other action
1566
1566
1567 def object_inspect(self, oname, detail_level=0):
1567 def object_inspect(self, oname, detail_level=0):
1568 """Get object info about oname"""
1568 """Get object info about oname"""
1569 with self.builtin_trap:
1569 with self.builtin_trap:
1570 info = self._object_find(oname)
1570 info = self._object_find(oname)
1571 if info.found:
1571 if info.found:
1572 return self.inspector.info(info.obj, oname, info=info,
1572 return self.inspector.info(info.obj, oname, info=info,
1573 detail_level=detail_level
1573 detail_level=detail_level
1574 )
1574 )
1575 else:
1575 else:
1576 return oinspect.object_info(name=oname, found=False)
1576 return oinspect.object_info(name=oname, found=False)
1577
1577
1578 def object_inspect_text(self, oname, detail_level=0):
1578 def object_inspect_text(self, oname, detail_level=0):
1579 """Get object info as formatted text"""
1579 """Get object info as formatted text"""
1580 with self.builtin_trap:
1580 with self.builtin_trap:
1581 info = self._object_find(oname)
1581 info = self._object_find(oname)
1582 if info.found:
1582 if info.found:
1583 return self.inspector._format_info(info.obj, oname, info=info,
1583 return self.inspector._format_info(info.obj, oname, info=info,
1584 detail_level=detail_level
1584 detail_level=detail_level
1585 )
1585 )
1586 else:
1586 else:
1587 raise KeyError(oname)
1587 raise KeyError(oname)
1588
1588
1589 #-------------------------------------------------------------------------
1589 #-------------------------------------------------------------------------
1590 # Things related to history management
1590 # Things related to history management
1591 #-------------------------------------------------------------------------
1591 #-------------------------------------------------------------------------
1592
1592
1593 def init_history(self):
1593 def init_history(self):
1594 """Sets up the command history, and starts regular autosaves."""
1594 """Sets up the command history, and starts regular autosaves."""
1595 self.history_manager = HistoryManager(shell=self, parent=self)
1595 self.history_manager = HistoryManager(shell=self, parent=self)
1596 self.configurables.append(self.history_manager)
1596 self.configurables.append(self.history_manager)
1597
1597
1598 #-------------------------------------------------------------------------
1598 #-------------------------------------------------------------------------
1599 # Things related to exception handling and tracebacks (not debugging)
1599 # Things related to exception handling and tracebacks (not debugging)
1600 #-------------------------------------------------------------------------
1600 #-------------------------------------------------------------------------
1601
1601
1602 def init_traceback_handlers(self, custom_exceptions):
1602 def init_traceback_handlers(self, custom_exceptions):
1603 # Syntax error handler.
1603 # Syntax error handler.
1604 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1604 self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
1605
1605
1606 # The interactive one is initialized with an offset, meaning we always
1606 # The interactive one is initialized with an offset, meaning we always
1607 # want to remove the topmost item in the traceback, which is our own
1607 # want to remove the topmost item in the traceback, which is our own
1608 # internal code. Valid modes: ['Plain','Context','Verbose']
1608 # internal code. Valid modes: ['Plain','Context','Verbose']
1609 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1609 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1610 color_scheme='NoColor',
1610 color_scheme='NoColor',
1611 tb_offset = 1,
1611 tb_offset = 1,
1612 check_cache=check_linecache_ipython)
1612 check_cache=check_linecache_ipython)
1613
1613
1614 # The instance will store a pointer to the system-wide exception hook,
1614 # The instance will store a pointer to the system-wide exception hook,
1615 # so that runtime code (such as magics) can access it. This is because
1615 # so that runtime code (such as magics) can access it. This is because
1616 # during the read-eval loop, it may get temporarily overwritten.
1616 # during the read-eval loop, it may get temporarily overwritten.
1617 self.sys_excepthook = sys.excepthook
1617 self.sys_excepthook = sys.excepthook
1618
1618
1619 # and add any custom exception handlers the user may have specified
1619 # and add any custom exception handlers the user may have specified
1620 self.set_custom_exc(*custom_exceptions)
1620 self.set_custom_exc(*custom_exceptions)
1621
1621
1622 # Set the exception mode
1622 # Set the exception mode
1623 self.InteractiveTB.set_mode(mode=self.xmode)
1623 self.InteractiveTB.set_mode(mode=self.xmode)
1624
1624
1625 def set_custom_exc(self, exc_tuple, handler):
1625 def set_custom_exc(self, exc_tuple, handler):
1626 """set_custom_exc(exc_tuple,handler)
1626 """set_custom_exc(exc_tuple,handler)
1627
1627
1628 Set a custom exception handler, which will be called if any of the
1628 Set a custom exception handler, which will be called if any of the
1629 exceptions in exc_tuple occur in the mainloop (specifically, in the
1629 exceptions in exc_tuple occur in the mainloop (specifically, in the
1630 run_code() method).
1630 run_code() method).
1631
1631
1632 Parameters
1632 Parameters
1633 ----------
1633 ----------
1634
1634
1635 exc_tuple : tuple of exception classes
1635 exc_tuple : tuple of exception classes
1636 A *tuple* of exception classes, for which to call the defined
1636 A *tuple* of exception classes, for which to call the defined
1637 handler. It is very important that you use a tuple, and NOT A
1637 handler. It is very important that you use a tuple, and NOT A
1638 LIST here, because of the way Python's except statement works. If
1638 LIST here, because of the way Python's except statement works. If
1639 you only want to trap a single exception, use a singleton tuple::
1639 you only want to trap a single exception, use a singleton tuple::
1640
1640
1641 exc_tuple == (MyCustomException,)
1641 exc_tuple == (MyCustomException,)
1642
1642
1643 handler : callable
1643 handler : callable
1644 handler must have the following signature::
1644 handler must have the following signature::
1645
1645
1646 def my_handler(self, etype, value, tb, tb_offset=None):
1646 def my_handler(self, etype, value, tb, tb_offset=None):
1647 ...
1647 ...
1648 return structured_traceback
1648 return structured_traceback
1649
1649
1650 Your handler must return a structured traceback (a list of strings),
1650 Your handler must return a structured traceback (a list of strings),
1651 or None.
1651 or None.
1652
1652
1653 This will be made into an instance method (via types.MethodType)
1653 This will be made into an instance method (via types.MethodType)
1654 of IPython itself, and it will be called if any of the exceptions
1654 of IPython itself, and it will be called if any of the exceptions
1655 listed in the exc_tuple are caught. If the handler is None, an
1655 listed in the exc_tuple are caught. If the handler is None, an
1656 internal basic one is used, which just prints basic info.
1656 internal basic one is used, which just prints basic info.
1657
1657
1658 To protect IPython from crashes, if your handler ever raises an
1658 To protect IPython from crashes, if your handler ever raises an
1659 exception or returns an invalid result, it will be immediately
1659 exception or returns an invalid result, it will be immediately
1660 disabled.
1660 disabled.
1661
1661
1662 WARNING: by putting in your own exception handler into IPython's main
1662 WARNING: by putting in your own exception handler into IPython's main
1663 execution loop, you run a very good chance of nasty crashes. This
1663 execution loop, you run a very good chance of nasty crashes. This
1664 facility should only be used if you really know what you are doing."""
1664 facility should only be used if you really know what you are doing."""
1665
1665
1666 assert type(exc_tuple)==type(()) , \
1666 assert type(exc_tuple)==type(()) , \
1667 "The custom exceptions must be given AS A TUPLE."
1667 "The custom exceptions must be given AS A TUPLE."
1668
1668
1669 def dummy_handler(self,etype,value,tb,tb_offset=None):
1669 def dummy_handler(self,etype,value,tb,tb_offset=None):
1670 print('*** Simple custom exception handler ***')
1670 print('*** Simple custom exception handler ***')
1671 print('Exception type :',etype)
1671 print('Exception type :',etype)
1672 print('Exception value:',value)
1672 print('Exception value:',value)
1673 print('Traceback :',tb)
1673 print('Traceback :',tb)
1674 #print 'Source code :','\n'.join(self.buffer)
1674 #print 'Source code :','\n'.join(self.buffer)
1675
1675
1676 def validate_stb(stb):
1676 def validate_stb(stb):
1677 """validate structured traceback return type
1677 """validate structured traceback return type
1678
1678
1679 return type of CustomTB *should* be a list of strings, but allow
1679 return type of CustomTB *should* be a list of strings, but allow
1680 single strings or None, which are harmless.
1680 single strings or None, which are harmless.
1681
1681
1682 This function will *always* return a list of strings,
1682 This function will *always* return a list of strings,
1683 and will raise a TypeError if stb is inappropriate.
1683 and will raise a TypeError if stb is inappropriate.
1684 """
1684 """
1685 msg = "CustomTB must return list of strings, not %r" % stb
1685 msg = "CustomTB must return list of strings, not %r" % stb
1686 if stb is None:
1686 if stb is None:
1687 return []
1687 return []
1688 elif isinstance(stb, string_types):
1688 elif isinstance(stb, string_types):
1689 return [stb]
1689 return [stb]
1690 elif not isinstance(stb, list):
1690 elif not isinstance(stb, list):
1691 raise TypeError(msg)
1691 raise TypeError(msg)
1692 # it's a list
1692 # it's a list
1693 for line in stb:
1693 for line in stb:
1694 # check every element
1694 # check every element
1695 if not isinstance(line, string_types):
1695 if not isinstance(line, string_types):
1696 raise TypeError(msg)
1696 raise TypeError(msg)
1697 return stb
1697 return stb
1698
1698
1699 if handler is None:
1699 if handler is None:
1700 wrapped = dummy_handler
1700 wrapped = dummy_handler
1701 else:
1701 else:
1702 def wrapped(self,etype,value,tb,tb_offset=None):
1702 def wrapped(self,etype,value,tb,tb_offset=None):
1703 """wrap CustomTB handler, to protect IPython from user code
1703 """wrap CustomTB handler, to protect IPython from user code
1704
1704
1705 This makes it harder (but not impossible) for custom exception
1705 This makes it harder (but not impossible) for custom exception
1706 handlers to crash IPython.
1706 handlers to crash IPython.
1707 """
1707 """
1708 try:
1708 try:
1709 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1709 stb = handler(self,etype,value,tb,tb_offset=tb_offset)
1710 return validate_stb(stb)
1710 return validate_stb(stb)
1711 except:
1711 except:
1712 # clear custom handler immediately
1712 # clear custom handler immediately
1713 self.set_custom_exc((), None)
1713 self.set_custom_exc((), None)
1714 print("Custom TB Handler failed, unregistering", file=io.stderr)
1714 print("Custom TB Handler failed, unregistering", file=io.stderr)
1715 # show the exception in handler first
1715 # show the exception in handler first
1716 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1716 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1717 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1717 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1718 print("The original exception:", file=io.stdout)
1718 print("The original exception:", file=io.stdout)
1719 stb = self.InteractiveTB.structured_traceback(
1719 stb = self.InteractiveTB.structured_traceback(
1720 (etype,value,tb), tb_offset=tb_offset
1720 (etype,value,tb), tb_offset=tb_offset
1721 )
1721 )
1722 return stb
1722 return stb
1723
1723
1724 self.CustomTB = types.MethodType(wrapped,self)
1724 self.CustomTB = types.MethodType(wrapped,self)
1725 self.custom_exceptions = exc_tuple
1725 self.custom_exceptions = exc_tuple
1726
1726
1727 def excepthook(self, etype, value, tb):
1727 def excepthook(self, etype, value, tb):
1728 """One more defense for GUI apps that call sys.excepthook.
1728 """One more defense for GUI apps that call sys.excepthook.
1729
1729
1730 GUI frameworks like wxPython trap exceptions and call
1730 GUI frameworks like wxPython trap exceptions and call
1731 sys.excepthook themselves. I guess this is a feature that
1731 sys.excepthook themselves. I guess this is a feature that
1732 enables them to keep running after exceptions that would
1732 enables them to keep running after exceptions that would
1733 otherwise kill their mainloop. This is a bother for IPython
1733 otherwise kill their mainloop. This is a bother for IPython
1734 which excepts to catch all of the program exceptions with a try:
1734 which excepts to catch all of the program exceptions with a try:
1735 except: statement.
1735 except: statement.
1736
1736
1737 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1737 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1738 any app directly invokes sys.excepthook, it will look to the user like
1738 any app directly invokes sys.excepthook, it will look to the user like
1739 IPython crashed. In order to work around this, we can disable the
1739 IPython crashed. In order to work around this, we can disable the
1740 CrashHandler and replace it with this excepthook instead, which prints a
1740 CrashHandler and replace it with this excepthook instead, which prints a
1741 regular traceback using our InteractiveTB. In this fashion, apps which
1741 regular traceback using our InteractiveTB. In this fashion, apps which
1742 call sys.excepthook will generate a regular-looking exception from
1742 call sys.excepthook will generate a regular-looking exception from
1743 IPython, and the CrashHandler will only be triggered by real IPython
1743 IPython, and the CrashHandler will only be triggered by real IPython
1744 crashes.
1744 crashes.
1745
1745
1746 This hook should be used sparingly, only in places which are not likely
1746 This hook should be used sparingly, only in places which are not likely
1747 to be true IPython errors.
1747 to be true IPython errors.
1748 """
1748 """
1749 self.showtraceback((etype, value, tb), tb_offset=0)
1749 self.showtraceback((etype, value, tb), tb_offset=0)
1750
1750
1751 def _get_exc_info(self, exc_tuple=None):
1751 def _get_exc_info(self, exc_tuple=None):
1752 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1752 """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc.
1753
1753
1754 Ensures sys.last_type,value,traceback hold the exc_info we found,
1754 Ensures sys.last_type,value,traceback hold the exc_info we found,
1755 from whichever source.
1755 from whichever source.
1756
1756
1757 raises ValueError if none of these contain any information
1757 raises ValueError if none of these contain any information
1758 """
1758 """
1759 if exc_tuple is None:
1759 if exc_tuple is None:
1760 etype, value, tb = sys.exc_info()
1760 etype, value, tb = sys.exc_info()
1761 else:
1761 else:
1762 etype, value, tb = exc_tuple
1762 etype, value, tb = exc_tuple
1763
1763
1764 if etype is None:
1764 if etype is None:
1765 if hasattr(sys, 'last_type'):
1765 if hasattr(sys, 'last_type'):
1766 etype, value, tb = sys.last_type, sys.last_value, \
1766 etype, value, tb = sys.last_type, sys.last_value, \
1767 sys.last_traceback
1767 sys.last_traceback
1768
1768
1769 if etype is None:
1769 if etype is None:
1770 raise ValueError("No exception to find")
1770 raise ValueError("No exception to find")
1771
1771
1772 # Now store the exception info in sys.last_type etc.
1772 # Now store the exception info in sys.last_type etc.
1773 # WARNING: these variables are somewhat deprecated and not
1773 # WARNING: these variables are somewhat deprecated and not
1774 # necessarily safe to use in a threaded environment, but tools
1774 # necessarily safe to use in a threaded environment, but tools
1775 # like pdb depend on their existence, so let's set them. If we
1775 # like pdb depend on their existence, so let's set them. If we
1776 # find problems in the field, we'll need to revisit their use.
1776 # find problems in the field, we'll need to revisit their use.
1777 sys.last_type = etype
1777 sys.last_type = etype
1778 sys.last_value = value
1778 sys.last_value = value
1779 sys.last_traceback = tb
1779 sys.last_traceback = tb
1780
1780
1781 return etype, value, tb
1781 return etype, value, tb
1782
1782
1783 def show_usage_error(self, exc):
1783 def show_usage_error(self, exc):
1784 """Show a short message for UsageErrors
1784 """Show a short message for UsageErrors
1785
1785
1786 These are special exceptions that shouldn't show a traceback.
1786 These are special exceptions that shouldn't show a traceback.
1787 """
1787 """
1788 self.write_err("UsageError: %s" % exc)
1788 self.write_err("UsageError: %s" % exc)
1789
1789
1790 def get_exception_only(self, exc_tuple=None):
1790 def get_exception_only(self, exc_tuple=None):
1791 """
1791 """
1792 Return as a string (ending with a newline) the exception that
1792 Return as a string (ending with a newline) the exception that
1793 just occurred, without any traceback.
1793 just occurred, without any traceback.
1794 """
1794 """
1795 etype, value, tb = self._get_exc_info(exc_tuple)
1795 etype, value, tb = self._get_exc_info(exc_tuple)
1796 msg = traceback.format_exception_only(etype, value)
1796 msg = traceback.format_exception_only(etype, value)
1797 return ''.join(msg)
1797 return ''.join(msg)
1798
1798
1799 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1799 def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None,
1800 exception_only=False):
1800 exception_only=False):
1801 """Display the exception that just occurred.
1801 """Display the exception that just occurred.
1802
1802
1803 If nothing is known about the exception, this is the method which
1803 If nothing is known about the exception, this is the method which
1804 should be used throughout the code for presenting user tracebacks,
1804 should be used throughout the code for presenting user tracebacks,
1805 rather than directly invoking the InteractiveTB object.
1805 rather than directly invoking the InteractiveTB object.
1806
1806
1807 A specific showsyntaxerror() also exists, but this method can take
1807 A specific showsyntaxerror() also exists, but this method can take
1808 care of calling it if needed, so unless you are explicitly catching a
1808 care of calling it if needed, so unless you are explicitly catching a
1809 SyntaxError exception, don't try to analyze the stack manually and
1809 SyntaxError exception, don't try to analyze the stack manually and
1810 simply call this method."""
1810 simply call this method."""
1811
1811
1812 try:
1812 try:
1813 try:
1813 try:
1814 etype, value, tb = self._get_exc_info(exc_tuple)
1814 etype, value, tb = self._get_exc_info(exc_tuple)
1815 except ValueError:
1815 except ValueError:
1816 self.write_err('No traceback available to show.\n')
1816 self.write_err('No traceback available to show.\n')
1817 return
1817 return
1818
1818
1819 if issubclass(etype, SyntaxError):
1819 if issubclass(etype, SyntaxError):
1820 # Though this won't be called by syntax errors in the input
1820 # Though this won't be called by syntax errors in the input
1821 # line, there may be SyntaxError cases with imported code.
1821 # line, there may be SyntaxError cases with imported code.
1822 self.showsyntaxerror(filename)
1822 self.showsyntaxerror(filename)
1823 elif etype is UsageError:
1823 elif etype is UsageError:
1824 self.show_usage_error(value)
1824 self.show_usage_error(value)
1825 else:
1825 else:
1826 if exception_only:
1826 if exception_only:
1827 stb = ['An exception has occurred, use %tb to see '
1827 stb = ['An exception has occurred, use %tb to see '
1828 'the full traceback.\n']
1828 'the full traceback.\n']
1829 stb.extend(self.InteractiveTB.get_exception_only(etype,
1829 stb.extend(self.InteractiveTB.get_exception_only(etype,
1830 value))
1830 value))
1831 else:
1831 else:
1832 try:
1832 try:
1833 # Exception classes can customise their traceback - we
1833 # Exception classes can customise their traceback - we
1834 # use this in IPython.parallel for exceptions occurring
1834 # use this in IPython.parallel for exceptions occurring
1835 # in the engines. This should return a list of strings.
1835 # in the engines. This should return a list of strings.
1836 stb = value._render_traceback_()
1836 stb = value._render_traceback_()
1837 except Exception:
1837 except Exception:
1838 stb = self.InteractiveTB.structured_traceback(etype,
1838 stb = self.InteractiveTB.structured_traceback(etype,
1839 value, tb, tb_offset=tb_offset)
1839 value, tb, tb_offset=tb_offset)
1840
1840
1841 self._showtraceback(etype, value, stb)
1841 self._showtraceback(etype, value, stb)
1842 if self.call_pdb:
1842 if self.call_pdb:
1843 # drop into debugger
1843 # drop into debugger
1844 self.debugger(force=True)
1844 self.debugger(force=True)
1845 return
1845 return
1846
1846
1847 # Actually show the traceback
1847 # Actually show the traceback
1848 self._showtraceback(etype, value, stb)
1848 self._showtraceback(etype, value, stb)
1849
1849
1850 except KeyboardInterrupt:
1850 except KeyboardInterrupt:
1851 self.write_err('\n' + self.get_exception_only())
1851 self.write_err('\n' + self.get_exception_only())
1852
1852
1853 def _showtraceback(self, etype, evalue, stb):
1853 def _showtraceback(self, etype, evalue, stb):
1854 """Actually show a traceback.
1854 """Actually show a traceback.
1855
1855
1856 Subclasses may override this method to put the traceback on a different
1856 Subclasses may override this method to put the traceback on a different
1857 place, like a side channel.
1857 place, like a side channel.
1858 """
1858 """
1859 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1859 print(self.InteractiveTB.stb2text(stb), file=io.stdout)
1860
1860
1861 def showsyntaxerror(self, filename=None):
1861 def showsyntaxerror(self, filename=None):
1862 """Display the syntax error that just occurred.
1862 """Display the syntax error that just occurred.
1863
1863
1864 This doesn't display a stack trace because there isn't one.
1864 This doesn't display a stack trace because there isn't one.
1865
1865
1866 If a filename is given, it is stuffed in the exception instead
1866 If a filename is given, it is stuffed in the exception instead
1867 of what was there before (because Python's parser always uses
1867 of what was there before (because Python's parser always uses
1868 "<string>" when reading from a string).
1868 "<string>" when reading from a string).
1869 """
1869 """
1870 etype, value, last_traceback = self._get_exc_info()
1870 etype, value, last_traceback = self._get_exc_info()
1871
1871
1872 if filename and issubclass(etype, SyntaxError):
1872 if filename and issubclass(etype, SyntaxError):
1873 try:
1873 try:
1874 value.filename = filename
1874 value.filename = filename
1875 except:
1875 except:
1876 # Not the format we expect; leave it alone
1876 # Not the format we expect; leave it alone
1877 pass
1877 pass
1878
1878
1879 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1879 stb = self.SyntaxTB.structured_traceback(etype, value, [])
1880 self._showtraceback(etype, value, stb)
1880 self._showtraceback(etype, value, stb)
1881
1881
1882 # This is overridden in TerminalInteractiveShell to show a message about
1882 # This is overridden in TerminalInteractiveShell to show a message about
1883 # the %paste magic.
1883 # the %paste magic.
1884 def showindentationerror(self):
1884 def showindentationerror(self):
1885 """Called by run_cell when there's an IndentationError in code entered
1885 """Called by run_cell when there's an IndentationError in code entered
1886 at the prompt.
1886 at the prompt.
1887
1887
1888 This is overridden in TerminalInteractiveShell to show a message about
1888 This is overridden in TerminalInteractiveShell to show a message about
1889 the %paste magic."""
1889 the %paste magic."""
1890 self.showsyntaxerror()
1890 self.showsyntaxerror()
1891
1891
1892 #-------------------------------------------------------------------------
1892 #-------------------------------------------------------------------------
1893 # Things related to readline
1893 # Things related to readline
1894 #-------------------------------------------------------------------------
1894 #-------------------------------------------------------------------------
1895
1895
1896 def init_readline(self):
1896 def init_readline(self):
1897 """Command history completion/saving/reloading."""
1897 """Command history completion/saving/reloading."""
1898
1898
1899 if self.readline_use:
1899 if self.readline_use:
1900 import IPython.utils.rlineimpl as readline
1900 import IPython.utils.rlineimpl as readline
1901
1901
1902 self.rl_next_input = None
1902 self.rl_next_input = None
1903 self.rl_do_indent = False
1903 self.rl_do_indent = False
1904
1904
1905 if not self.readline_use or not readline.have_readline:
1905 if not self.readline_use or not readline.have_readline:
1906 self.has_readline = False
1906 self.has_readline = False
1907 self.readline = None
1907 self.readline = None
1908 # Set a number of methods that depend on readline to be no-op
1908 # Set a number of methods that depend on readline to be no-op
1909 self.readline_no_record = no_op_context
1909 self.readline_no_record = no_op_context
1910 self.set_readline_completer = no_op
1910 self.set_readline_completer = no_op
1911 self.set_custom_completer = no_op
1911 self.set_custom_completer = no_op
1912 if self.readline_use:
1912 if self.readline_use:
1913 warn('Readline services not available or not loaded.')
1913 warn('Readline services not available or not loaded.')
1914 else:
1914 else:
1915 self.has_readline = True
1915 self.has_readline = True
1916 self.readline = readline
1916 self.readline = readline
1917 sys.modules['readline'] = readline
1917 sys.modules['readline'] = readline
1918
1918
1919 # Platform-specific configuration
1919 # Platform-specific configuration
1920 if os.name == 'nt':
1920 if os.name == 'nt':
1921 # FIXME - check with Frederick to see if we can harmonize
1921 # FIXME - check with Frederick to see if we can harmonize
1922 # naming conventions with pyreadline to avoid this
1922 # naming conventions with pyreadline to avoid this
1923 # platform-dependent check
1923 # platform-dependent check
1924 self.readline_startup_hook = readline.set_pre_input_hook
1924 self.readline_startup_hook = readline.set_pre_input_hook
1925 else:
1925 else:
1926 self.readline_startup_hook = readline.set_startup_hook
1926 self.readline_startup_hook = readline.set_startup_hook
1927
1927
1928 # Readline config order:
1928 # Readline config order:
1929 # - IPython config (default value)
1929 # - IPython config (default value)
1930 # - custom inputrc
1930 # - custom inputrc
1931 # - IPython config (user customized)
1931 # - IPython config (user customized)
1932
1932
1933 # load IPython config before inputrc if default
1933 # load IPython config before inputrc if default
1934 # skip if libedit because parse_and_bind syntax is different
1934 # skip if libedit because parse_and_bind syntax is different
1935 if not self._custom_readline_config and not readline.uses_libedit:
1935 if not self._custom_readline_config and not readline.uses_libedit:
1936 for rlcommand in self.readline_parse_and_bind:
1936 for rlcommand in self.readline_parse_and_bind:
1937 readline.parse_and_bind(rlcommand)
1937 readline.parse_and_bind(rlcommand)
1938
1938
1939 # Load user's initrc file (readline config)
1939 # Load user's initrc file (readline config)
1940 # Or if libedit is used, load editrc.
1940 # Or if libedit is used, load editrc.
1941 inputrc_name = os.environ.get('INPUTRC')
1941 inputrc_name = os.environ.get('INPUTRC')
1942 if inputrc_name is None:
1942 if inputrc_name is None:
1943 inputrc_name = '.inputrc'
1943 inputrc_name = '.inputrc'
1944 if readline.uses_libedit:
1944 if readline.uses_libedit:
1945 inputrc_name = '.editrc'
1945 inputrc_name = '.editrc'
1946 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1946 inputrc_name = os.path.join(self.home_dir, inputrc_name)
1947 if os.path.isfile(inputrc_name):
1947 if os.path.isfile(inputrc_name):
1948 try:
1948 try:
1949 readline.read_init_file(inputrc_name)
1949 readline.read_init_file(inputrc_name)
1950 except:
1950 except:
1951 warn('Problems reading readline initialization file <%s>'
1951 warn('Problems reading readline initialization file <%s>'
1952 % inputrc_name)
1952 % inputrc_name)
1953
1953
1954 # load IPython config after inputrc if user has customized
1954 # load IPython config after inputrc if user has customized
1955 if self._custom_readline_config:
1955 if self._custom_readline_config:
1956 for rlcommand in self.readline_parse_and_bind:
1956 for rlcommand in self.readline_parse_and_bind:
1957 readline.parse_and_bind(rlcommand)
1957 readline.parse_and_bind(rlcommand)
1958
1958
1959 # Remove some chars from the delimiters list. If we encounter
1959 # Remove some chars from the delimiters list. If we encounter
1960 # unicode chars, discard them.
1960 # unicode chars, discard them.
1961 delims = readline.get_completer_delims()
1961 delims = readline.get_completer_delims()
1962 if not py3compat.PY3:
1962 if not py3compat.PY3:
1963 delims = delims.encode("ascii", "ignore")
1963 delims = delims.encode("ascii", "ignore")
1964 for d in self.readline_remove_delims:
1964 for d in self.readline_remove_delims:
1965 delims = delims.replace(d, "")
1965 delims = delims.replace(d, "")
1966 delims = delims.replace(ESC_MAGIC, '')
1966 delims = delims.replace(ESC_MAGIC, '')
1967 readline.set_completer_delims(delims)
1967 readline.set_completer_delims(delims)
1968 # Store these so we can restore them if something like rpy2 modifies
1968 # Store these so we can restore them if something like rpy2 modifies
1969 # them.
1969 # them.
1970 self.readline_delims = delims
1970 self.readline_delims = delims
1971 # otherwise we end up with a monster history after a while:
1971 # otherwise we end up with a monster history after a while:
1972 readline.set_history_length(self.history_length)
1972 readline.set_history_length(self.history_length)
1973
1973
1974 self.refill_readline_hist()
1974 self.refill_readline_hist()
1975 self.readline_no_record = ReadlineNoRecord(self)
1975 self.readline_no_record = ReadlineNoRecord(self)
1976
1976
1977 # Configure auto-indent for all platforms
1977 # Configure auto-indent for all platforms
1978 self.set_autoindent(self.autoindent)
1978 self.set_autoindent(self.autoindent)
1979
1979
1980 def refill_readline_hist(self):
1980 def refill_readline_hist(self):
1981 # Load the last 1000 lines from history
1981 # Load the last 1000 lines from history
1982 self.readline.clear_history()
1982 self.readline.clear_history()
1983 stdin_encoding = sys.stdin.encoding or "utf-8"
1983 stdin_encoding = sys.stdin.encoding or "utf-8"
1984 last_cell = u""
1984 last_cell = u""
1985 for _, _, cell in self.history_manager.get_tail(1000,
1985 for _, _, cell in self.history_manager.get_tail(1000,
1986 include_latest=True):
1986 include_latest=True):
1987 # Ignore blank lines and consecutive duplicates
1987 # Ignore blank lines and consecutive duplicates
1988 cell = cell.rstrip()
1988 cell = cell.rstrip()
1989 if cell and (cell != last_cell):
1989 if cell and (cell != last_cell):
1990 try:
1990 try:
1991 if self.multiline_history:
1991 if self.multiline_history:
1992 self.readline.add_history(py3compat.unicode_to_str(cell,
1992 self.readline.add_history(py3compat.unicode_to_str(cell,
1993 stdin_encoding))
1993 stdin_encoding))
1994 else:
1994 else:
1995 for line in cell.splitlines():
1995 for line in cell.splitlines():
1996 self.readline.add_history(py3compat.unicode_to_str(line,
1996 self.readline.add_history(py3compat.unicode_to_str(line,
1997 stdin_encoding))
1997 stdin_encoding))
1998 last_cell = cell
1998 last_cell = cell
1999
1999
2000 except TypeError:
2000 except TypeError:
2001 # The history DB can get corrupted so it returns strings
2001 # The history DB can get corrupted so it returns strings
2002 # containing null bytes, which readline objects to.
2002 # containing null bytes, which readline objects to.
2003 continue
2003 continue
2004
2004
2005 @skip_doctest
2005 @skip_doctest
2006 def set_next_input(self, s):
2006 def set_next_input(self, s, replace=False):
2007 """ Sets the 'default' input string for the next command line.
2007 """ Sets the 'default' input string for the next command line.
2008
2008
2009 Requires readline.
2009 Requires readline.
2010
2010
2011 Example::
2011 Example::
2012
2012
2013 In [1]: _ip.set_next_input("Hello Word")
2013 In [1]: _ip.set_next_input("Hello Word")
2014 In [2]: Hello Word_ # cursor is here
2014 In [2]: Hello Word_ # cursor is here
2015 """
2015 """
2016 self.rl_next_input = py3compat.cast_bytes_py2(s)
2016 self.rl_next_input = py3compat.cast_bytes_py2(s)
2017
2017
2018 # Maybe move this to the terminal subclass?
2018 # Maybe move this to the terminal subclass?
2019 def pre_readline(self):
2019 def pre_readline(self):
2020 """readline hook to be used at the start of each line.
2020 """readline hook to be used at the start of each line.
2021
2021
2022 Currently it handles auto-indent only."""
2022 Currently it handles auto-indent only."""
2023
2023
2024 if self.rl_do_indent:
2024 if self.rl_do_indent:
2025 self.readline.insert_text(self._indent_current_str())
2025 self.readline.insert_text(self._indent_current_str())
2026 if self.rl_next_input is not None:
2026 if self.rl_next_input is not None:
2027 self.readline.insert_text(self.rl_next_input)
2027 self.readline.insert_text(self.rl_next_input)
2028 self.rl_next_input = None
2028 self.rl_next_input = None
2029
2029
2030 def _indent_current_str(self):
2030 def _indent_current_str(self):
2031 """return the current level of indentation as a string"""
2031 """return the current level of indentation as a string"""
2032 return self.input_splitter.indent_spaces * ' '
2032 return self.input_splitter.indent_spaces * ' '
2033
2033
2034 #-------------------------------------------------------------------------
2034 #-------------------------------------------------------------------------
2035 # Things related to text completion
2035 # Things related to text completion
2036 #-------------------------------------------------------------------------
2036 #-------------------------------------------------------------------------
2037
2037
2038 def init_completer(self):
2038 def init_completer(self):
2039 """Initialize the completion machinery.
2039 """Initialize the completion machinery.
2040
2040
2041 This creates completion machinery that can be used by client code,
2041 This creates completion machinery that can be used by client code,
2042 either interactively in-process (typically triggered by the readline
2042 either interactively in-process (typically triggered by the readline
2043 library), programatically (such as in test suites) or out-of-prcess
2043 library), programatically (such as in test suites) or out-of-prcess
2044 (typically over the network by remote frontends).
2044 (typically over the network by remote frontends).
2045 """
2045 """
2046 from IPython.core.completer import IPCompleter
2046 from IPython.core.completer import IPCompleter
2047 from IPython.core.completerlib import (module_completer,
2047 from IPython.core.completerlib import (module_completer,
2048 magic_run_completer, cd_completer, reset_completer)
2048 magic_run_completer, cd_completer, reset_completer)
2049
2049
2050 self.Completer = IPCompleter(shell=self,
2050 self.Completer = IPCompleter(shell=self,
2051 namespace=self.user_ns,
2051 namespace=self.user_ns,
2052 global_namespace=self.user_global_ns,
2052 global_namespace=self.user_global_ns,
2053 use_readline=self.has_readline,
2053 use_readline=self.has_readline,
2054 parent=self,
2054 parent=self,
2055 )
2055 )
2056 self.configurables.append(self.Completer)
2056 self.configurables.append(self.Completer)
2057
2057
2058 # Add custom completers to the basic ones built into IPCompleter
2058 # Add custom completers to the basic ones built into IPCompleter
2059 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
2059 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
2060 self.strdispatchers['complete_command'] = sdisp
2060 self.strdispatchers['complete_command'] = sdisp
2061 self.Completer.custom_completers = sdisp
2061 self.Completer.custom_completers = sdisp
2062
2062
2063 self.set_hook('complete_command', module_completer, str_key = 'import')
2063 self.set_hook('complete_command', module_completer, str_key = 'import')
2064 self.set_hook('complete_command', module_completer, str_key = 'from')
2064 self.set_hook('complete_command', module_completer, str_key = 'from')
2065 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
2065 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
2066 self.set_hook('complete_command', cd_completer, str_key = '%cd')
2066 self.set_hook('complete_command', cd_completer, str_key = '%cd')
2067 self.set_hook('complete_command', reset_completer, str_key = '%reset')
2067 self.set_hook('complete_command', reset_completer, str_key = '%reset')
2068
2068
2069 # Only configure readline if we truly are using readline. IPython can
2069 # Only configure readline if we truly are using readline. IPython can
2070 # do tab-completion over the network, in GUIs, etc, where readline
2070 # do tab-completion over the network, in GUIs, etc, where readline
2071 # itself may be absent
2071 # itself may be absent
2072 if self.has_readline:
2072 if self.has_readline:
2073 self.set_readline_completer()
2073 self.set_readline_completer()
2074
2074
2075 def complete(self, text, line=None, cursor_pos=None):
2075 def complete(self, text, line=None, cursor_pos=None):
2076 """Return the completed text and a list of completions.
2076 """Return the completed text and a list of completions.
2077
2077
2078 Parameters
2078 Parameters
2079 ----------
2079 ----------
2080
2080
2081 text : string
2081 text : string
2082 A string of text to be completed on. It can be given as empty and
2082 A string of text to be completed on. It can be given as empty and
2083 instead a line/position pair are given. In this case, the
2083 instead a line/position pair are given. In this case, the
2084 completer itself will split the line like readline does.
2084 completer itself will split the line like readline does.
2085
2085
2086 line : string, optional
2086 line : string, optional
2087 The complete line that text is part of.
2087 The complete line that text is part of.
2088
2088
2089 cursor_pos : int, optional
2089 cursor_pos : int, optional
2090 The position of the cursor on the input line.
2090 The position of the cursor on the input line.
2091
2091
2092 Returns
2092 Returns
2093 -------
2093 -------
2094 text : string
2094 text : string
2095 The actual text that was completed.
2095 The actual text that was completed.
2096
2096
2097 matches : list
2097 matches : list
2098 A sorted list with all possible completions.
2098 A sorted list with all possible completions.
2099
2099
2100 The optional arguments allow the completion to take more context into
2100 The optional arguments allow the completion to take more context into
2101 account, and are part of the low-level completion API.
2101 account, and are part of the low-level completion API.
2102
2102
2103 This is a wrapper around the completion mechanism, similar to what
2103 This is a wrapper around the completion mechanism, similar to what
2104 readline does at the command line when the TAB key is hit. By
2104 readline does at the command line when the TAB key is hit. By
2105 exposing it as a method, it can be used by other non-readline
2105 exposing it as a method, it can be used by other non-readline
2106 environments (such as GUIs) for text completion.
2106 environments (such as GUIs) for text completion.
2107
2107
2108 Simple usage example:
2108 Simple usage example:
2109
2109
2110 In [1]: x = 'hello'
2110 In [1]: x = 'hello'
2111
2111
2112 In [2]: _ip.complete('x.l')
2112 In [2]: _ip.complete('x.l')
2113 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2113 Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
2114 """
2114 """
2115
2115
2116 # Inject names into __builtin__ so we can complete on the added names.
2116 # Inject names into __builtin__ so we can complete on the added names.
2117 with self.builtin_trap:
2117 with self.builtin_trap:
2118 return self.Completer.complete(text, line, cursor_pos)
2118 return self.Completer.complete(text, line, cursor_pos)
2119
2119
2120 def set_custom_completer(self, completer, pos=0):
2120 def set_custom_completer(self, completer, pos=0):
2121 """Adds a new custom completer function.
2121 """Adds a new custom completer function.
2122
2122
2123 The position argument (defaults to 0) is the index in the completers
2123 The position argument (defaults to 0) is the index in the completers
2124 list where you want the completer to be inserted."""
2124 list where you want the completer to be inserted."""
2125
2125
2126 newcomp = types.MethodType(completer,self.Completer)
2126 newcomp = types.MethodType(completer,self.Completer)
2127 self.Completer.matchers.insert(pos,newcomp)
2127 self.Completer.matchers.insert(pos,newcomp)
2128
2128
2129 def set_readline_completer(self):
2129 def set_readline_completer(self):
2130 """Reset readline's completer to be our own."""
2130 """Reset readline's completer to be our own."""
2131 self.readline.set_completer(self.Completer.rlcomplete)
2131 self.readline.set_completer(self.Completer.rlcomplete)
2132
2132
2133 def set_completer_frame(self, frame=None):
2133 def set_completer_frame(self, frame=None):
2134 """Set the frame of the completer."""
2134 """Set the frame of the completer."""
2135 if frame:
2135 if frame:
2136 self.Completer.namespace = frame.f_locals
2136 self.Completer.namespace = frame.f_locals
2137 self.Completer.global_namespace = frame.f_globals
2137 self.Completer.global_namespace = frame.f_globals
2138 else:
2138 else:
2139 self.Completer.namespace = self.user_ns
2139 self.Completer.namespace = self.user_ns
2140 self.Completer.global_namespace = self.user_global_ns
2140 self.Completer.global_namespace = self.user_global_ns
2141
2141
2142 #-------------------------------------------------------------------------
2142 #-------------------------------------------------------------------------
2143 # Things related to magics
2143 # Things related to magics
2144 #-------------------------------------------------------------------------
2144 #-------------------------------------------------------------------------
2145
2145
2146 def init_magics(self):
2146 def init_magics(self):
2147 from IPython.core import magics as m
2147 from IPython.core import magics as m
2148 self.magics_manager = magic.MagicsManager(shell=self,
2148 self.magics_manager = magic.MagicsManager(shell=self,
2149 parent=self,
2149 parent=self,
2150 user_magics=m.UserMagics(self))
2150 user_magics=m.UserMagics(self))
2151 self.configurables.append(self.magics_manager)
2151 self.configurables.append(self.magics_manager)
2152
2152
2153 # Expose as public API from the magics manager
2153 # Expose as public API from the magics manager
2154 self.register_magics = self.magics_manager.register
2154 self.register_magics = self.magics_manager.register
2155 self.define_magic = self.magics_manager.define_magic
2155 self.define_magic = self.magics_manager.define_magic
2156
2156
2157 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2157 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2158 m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics,
2158 m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics,
2159 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2159 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2160 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2160 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2161 )
2161 )
2162
2162
2163 # Register Magic Aliases
2163 # Register Magic Aliases
2164 mman = self.magics_manager
2164 mman = self.magics_manager
2165 # FIXME: magic aliases should be defined by the Magics classes
2165 # FIXME: magic aliases should be defined by the Magics classes
2166 # or in MagicsManager, not here
2166 # or in MagicsManager, not here
2167 mman.register_alias('ed', 'edit')
2167 mman.register_alias('ed', 'edit')
2168 mman.register_alias('hist', 'history')
2168 mman.register_alias('hist', 'history')
2169 mman.register_alias('rep', 'recall')
2169 mman.register_alias('rep', 'recall')
2170 mman.register_alias('SVG', 'svg', 'cell')
2170 mman.register_alias('SVG', 'svg', 'cell')
2171 mman.register_alias('HTML', 'html', 'cell')
2171 mman.register_alias('HTML', 'html', 'cell')
2172 mman.register_alias('file', 'writefile', 'cell')
2172 mman.register_alias('file', 'writefile', 'cell')
2173
2173
2174 # FIXME: Move the color initialization to the DisplayHook, which
2174 # FIXME: Move the color initialization to the DisplayHook, which
2175 # should be split into a prompt manager and displayhook. We probably
2175 # should be split into a prompt manager and displayhook. We probably
2176 # even need a centralize colors management object.
2176 # even need a centralize colors management object.
2177 self.magic('colors %s' % self.colors)
2177 self.magic('colors %s' % self.colors)
2178
2178
2179 # Defined here so that it's included in the documentation
2179 # Defined here so that it's included in the documentation
2180 @functools.wraps(magic.MagicsManager.register_function)
2180 @functools.wraps(magic.MagicsManager.register_function)
2181 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2181 def register_magic_function(self, func, magic_kind='line', magic_name=None):
2182 self.magics_manager.register_function(func,
2182 self.magics_manager.register_function(func,
2183 magic_kind=magic_kind, magic_name=magic_name)
2183 magic_kind=magic_kind, magic_name=magic_name)
2184
2184
2185 def run_line_magic(self, magic_name, line):
2185 def run_line_magic(self, magic_name, line):
2186 """Execute the given line magic.
2186 """Execute the given line magic.
2187
2187
2188 Parameters
2188 Parameters
2189 ----------
2189 ----------
2190 magic_name : str
2190 magic_name : str
2191 Name of the desired magic function, without '%' prefix.
2191 Name of the desired magic function, without '%' prefix.
2192
2192
2193 line : str
2193 line : str
2194 The rest of the input line as a single string.
2194 The rest of the input line as a single string.
2195 """
2195 """
2196 fn = self.find_line_magic(magic_name)
2196 fn = self.find_line_magic(magic_name)
2197 if fn is None:
2197 if fn is None:
2198 cm = self.find_cell_magic(magic_name)
2198 cm = self.find_cell_magic(magic_name)
2199 etpl = "Line magic function `%%%s` not found%s."
2199 etpl = "Line magic function `%%%s` not found%s."
2200 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2200 extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, '
2201 'did you mean that instead?)' % magic_name )
2201 'did you mean that instead?)' % magic_name )
2202 error(etpl % (magic_name, extra))
2202 error(etpl % (magic_name, extra))
2203 else:
2203 else:
2204 # Note: this is the distance in the stack to the user's frame.
2204 # Note: this is the distance in the stack to the user's frame.
2205 # This will need to be updated if the internal calling logic gets
2205 # This will need to be updated if the internal calling logic gets
2206 # refactored, or else we'll be expanding the wrong variables.
2206 # refactored, or else we'll be expanding the wrong variables.
2207 stack_depth = 2
2207 stack_depth = 2
2208 magic_arg_s = self.var_expand(line, stack_depth)
2208 magic_arg_s = self.var_expand(line, stack_depth)
2209 # Put magic args in a list so we can call with f(*a) syntax
2209 # Put magic args in a list so we can call with f(*a) syntax
2210 args = [magic_arg_s]
2210 args = [magic_arg_s]
2211 kwargs = {}
2211 kwargs = {}
2212 # Grab local namespace if we need it:
2212 # Grab local namespace if we need it:
2213 if getattr(fn, "needs_local_scope", False):
2213 if getattr(fn, "needs_local_scope", False):
2214 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2214 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2215 with self.builtin_trap:
2215 with self.builtin_trap:
2216 result = fn(*args,**kwargs)
2216 result = fn(*args,**kwargs)
2217 return result
2217 return result
2218
2218
2219 def run_cell_magic(self, magic_name, line, cell):
2219 def run_cell_magic(self, magic_name, line, cell):
2220 """Execute the given cell magic.
2220 """Execute the given cell magic.
2221
2221
2222 Parameters
2222 Parameters
2223 ----------
2223 ----------
2224 magic_name : str
2224 magic_name : str
2225 Name of the desired magic function, without '%' prefix.
2225 Name of the desired magic function, without '%' prefix.
2226
2226
2227 line : str
2227 line : str
2228 The rest of the first input line as a single string.
2228 The rest of the first input line as a single string.
2229
2229
2230 cell : str
2230 cell : str
2231 The body of the cell as a (possibly multiline) string.
2231 The body of the cell as a (possibly multiline) string.
2232 """
2232 """
2233 fn = self.find_cell_magic(magic_name)
2233 fn = self.find_cell_magic(magic_name)
2234 if fn is None:
2234 if fn is None:
2235 lm = self.find_line_magic(magic_name)
2235 lm = self.find_line_magic(magic_name)
2236 etpl = "Cell magic `%%{0}` not found{1}."
2236 etpl = "Cell magic `%%{0}` not found{1}."
2237 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2237 extra = '' if lm is None else (' (But line magic `%{0}` exists, '
2238 'did you mean that instead?)'.format(magic_name))
2238 'did you mean that instead?)'.format(magic_name))
2239 error(etpl.format(magic_name, extra))
2239 error(etpl.format(magic_name, extra))
2240 elif cell == '':
2240 elif cell == '':
2241 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2241 message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name)
2242 if self.find_line_magic(magic_name) is not None:
2242 if self.find_line_magic(magic_name) is not None:
2243 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2243 message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name)
2244 raise UsageError(message)
2244 raise UsageError(message)
2245 else:
2245 else:
2246 # Note: this is the distance in the stack to the user's frame.
2246 # Note: this is the distance in the stack to the user's frame.
2247 # This will need to be updated if the internal calling logic gets
2247 # This will need to be updated if the internal calling logic gets
2248 # refactored, or else we'll be expanding the wrong variables.
2248 # refactored, or else we'll be expanding the wrong variables.
2249 stack_depth = 2
2249 stack_depth = 2
2250 magic_arg_s = self.var_expand(line, stack_depth)
2250 magic_arg_s = self.var_expand(line, stack_depth)
2251 with self.builtin_trap:
2251 with self.builtin_trap:
2252 result = fn(magic_arg_s, cell)
2252 result = fn(magic_arg_s, cell)
2253 return result
2253 return result
2254
2254
2255 def find_line_magic(self, magic_name):
2255 def find_line_magic(self, magic_name):
2256 """Find and return a line magic by name.
2256 """Find and return a line magic by name.
2257
2257
2258 Returns None if the magic isn't found."""
2258 Returns None if the magic isn't found."""
2259 return self.magics_manager.magics['line'].get(magic_name)
2259 return self.magics_manager.magics['line'].get(magic_name)
2260
2260
2261 def find_cell_magic(self, magic_name):
2261 def find_cell_magic(self, magic_name):
2262 """Find and return a cell magic by name.
2262 """Find and return a cell magic by name.
2263
2263
2264 Returns None if the magic isn't found."""
2264 Returns None if the magic isn't found."""
2265 return self.magics_manager.magics['cell'].get(magic_name)
2265 return self.magics_manager.magics['cell'].get(magic_name)
2266
2266
2267 def find_magic(self, magic_name, magic_kind='line'):
2267 def find_magic(self, magic_name, magic_kind='line'):
2268 """Find and return a magic of the given type by name.
2268 """Find and return a magic of the given type by name.
2269
2269
2270 Returns None if the magic isn't found."""
2270 Returns None if the magic isn't found."""
2271 return self.magics_manager.magics[magic_kind].get(magic_name)
2271 return self.magics_manager.magics[magic_kind].get(magic_name)
2272
2272
2273 def magic(self, arg_s):
2273 def magic(self, arg_s):
2274 """DEPRECATED. Use run_line_magic() instead.
2274 """DEPRECATED. Use run_line_magic() instead.
2275
2275
2276 Call a magic function by name.
2276 Call a magic function by name.
2277
2277
2278 Input: a string containing the name of the magic function to call and
2278 Input: a string containing the name of the magic function to call and
2279 any additional arguments to be passed to the magic.
2279 any additional arguments to be passed to the magic.
2280
2280
2281 magic('name -opt foo bar') is equivalent to typing at the ipython
2281 magic('name -opt foo bar') is equivalent to typing at the ipython
2282 prompt:
2282 prompt:
2283
2283
2284 In[1]: %name -opt foo bar
2284 In[1]: %name -opt foo bar
2285
2285
2286 To call a magic without arguments, simply use magic('name').
2286 To call a magic without arguments, simply use magic('name').
2287
2287
2288 This provides a proper Python function to call IPython's magics in any
2288 This provides a proper Python function to call IPython's magics in any
2289 valid Python code you can type at the interpreter, including loops and
2289 valid Python code you can type at the interpreter, including loops and
2290 compound statements.
2290 compound statements.
2291 """
2291 """
2292 # TODO: should we issue a loud deprecation warning here?
2292 # TODO: should we issue a loud deprecation warning here?
2293 magic_name, _, magic_arg_s = arg_s.partition(' ')
2293 magic_name, _, magic_arg_s = arg_s.partition(' ')
2294 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2294 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2295 return self.run_line_magic(magic_name, magic_arg_s)
2295 return self.run_line_magic(magic_name, magic_arg_s)
2296
2296
2297 #-------------------------------------------------------------------------
2297 #-------------------------------------------------------------------------
2298 # Things related to macros
2298 # Things related to macros
2299 #-------------------------------------------------------------------------
2299 #-------------------------------------------------------------------------
2300
2300
2301 def define_macro(self, name, themacro):
2301 def define_macro(self, name, themacro):
2302 """Define a new macro
2302 """Define a new macro
2303
2303
2304 Parameters
2304 Parameters
2305 ----------
2305 ----------
2306 name : str
2306 name : str
2307 The name of the macro.
2307 The name of the macro.
2308 themacro : str or Macro
2308 themacro : str or Macro
2309 The action to do upon invoking the macro. If a string, a new
2309 The action to do upon invoking the macro. If a string, a new
2310 Macro object is created by passing the string to it.
2310 Macro object is created by passing the string to it.
2311 """
2311 """
2312
2312
2313 from IPython.core import macro
2313 from IPython.core import macro
2314
2314
2315 if isinstance(themacro, string_types):
2315 if isinstance(themacro, string_types):
2316 themacro = macro.Macro(themacro)
2316 themacro = macro.Macro(themacro)
2317 if not isinstance(themacro, macro.Macro):
2317 if not isinstance(themacro, macro.Macro):
2318 raise ValueError('A macro must be a string or a Macro instance.')
2318 raise ValueError('A macro must be a string or a Macro instance.')
2319 self.user_ns[name] = themacro
2319 self.user_ns[name] = themacro
2320
2320
2321 #-------------------------------------------------------------------------
2321 #-------------------------------------------------------------------------
2322 # Things related to the running of system commands
2322 # Things related to the running of system commands
2323 #-------------------------------------------------------------------------
2323 #-------------------------------------------------------------------------
2324
2324
2325 def system_piped(self, cmd):
2325 def system_piped(self, cmd):
2326 """Call the given cmd in a subprocess, piping stdout/err
2326 """Call the given cmd in a subprocess, piping stdout/err
2327
2327
2328 Parameters
2328 Parameters
2329 ----------
2329 ----------
2330 cmd : str
2330 cmd : str
2331 Command to execute (can not end in '&', as background processes are
2331 Command to execute (can not end in '&', as background processes are
2332 not supported. Should not be a command that expects input
2332 not supported. Should not be a command that expects input
2333 other than simple text.
2333 other than simple text.
2334 """
2334 """
2335 if cmd.rstrip().endswith('&'):
2335 if cmd.rstrip().endswith('&'):
2336 # this is *far* from a rigorous test
2336 # this is *far* from a rigorous test
2337 # We do not support backgrounding processes because we either use
2337 # We do not support backgrounding processes because we either use
2338 # pexpect or pipes to read from. Users can always just call
2338 # pexpect or pipes to read from. Users can always just call
2339 # os.system() or use ip.system=ip.system_raw
2339 # os.system() or use ip.system=ip.system_raw
2340 # if they really want a background process.
2340 # if they really want a background process.
2341 raise OSError("Background processes not supported.")
2341 raise OSError("Background processes not supported.")
2342
2342
2343 # we explicitly do NOT return the subprocess status code, because
2343 # we explicitly do NOT return the subprocess status code, because
2344 # a non-None value would trigger :func:`sys.displayhook` calls.
2344 # a non-None value would trigger :func:`sys.displayhook` calls.
2345 # Instead, we store the exit_code in user_ns.
2345 # Instead, we store the exit_code in user_ns.
2346 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2346 self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))
2347
2347
2348 def system_raw(self, cmd):
2348 def system_raw(self, cmd):
2349 """Call the given cmd in a subprocess using os.system on Windows or
2349 """Call the given cmd in a subprocess using os.system on Windows or
2350 subprocess.call using the system shell on other platforms.
2350 subprocess.call using the system shell on other platforms.
2351
2351
2352 Parameters
2352 Parameters
2353 ----------
2353 ----------
2354 cmd : str
2354 cmd : str
2355 Command to execute.
2355 Command to execute.
2356 """
2356 """
2357 cmd = self.var_expand(cmd, depth=1)
2357 cmd = self.var_expand(cmd, depth=1)
2358 # protect os.system from UNC paths on Windows, which it can't handle:
2358 # protect os.system from UNC paths on Windows, which it can't handle:
2359 if sys.platform == 'win32':
2359 if sys.platform == 'win32':
2360 from IPython.utils._process_win32 import AvoidUNCPath
2360 from IPython.utils._process_win32 import AvoidUNCPath
2361 with AvoidUNCPath() as path:
2361 with AvoidUNCPath() as path:
2362 if path is not None:
2362 if path is not None:
2363 cmd = '"pushd %s &&"%s' % (path, cmd)
2363 cmd = '"pushd %s &&"%s' % (path, cmd)
2364 cmd = py3compat.unicode_to_str(cmd)
2364 cmd = py3compat.unicode_to_str(cmd)
2365 try:
2365 try:
2366 ec = os.system(cmd)
2366 ec = os.system(cmd)
2367 except KeyboardInterrupt:
2367 except KeyboardInterrupt:
2368 self.write_err('\n' + self.get_exception_only())
2368 self.write_err('\n' + self.get_exception_only())
2369 ec = -2
2369 ec = -2
2370 else:
2370 else:
2371 cmd = py3compat.unicode_to_str(cmd)
2371 cmd = py3compat.unicode_to_str(cmd)
2372 # For posix the result of the subprocess.call() below is an exit
2372 # For posix the result of the subprocess.call() below is an exit
2373 # code, which by convention is zero for success, positive for
2373 # code, which by convention is zero for success, positive for
2374 # program failure. Exit codes above 128 are reserved for signals,
2374 # program failure. Exit codes above 128 are reserved for signals,
2375 # and the formula for converting a signal to an exit code is usually
2375 # and the formula for converting a signal to an exit code is usually
2376 # signal_number+128. To more easily differentiate between exit
2376 # signal_number+128. To more easily differentiate between exit
2377 # codes and signals, ipython uses negative numbers. For instance
2377 # codes and signals, ipython uses negative numbers. For instance
2378 # since control-c is signal 2 but exit code 130, ipython's
2378 # since control-c is signal 2 but exit code 130, ipython's
2379 # _exit_code variable will read -2. Note that some shells like
2379 # _exit_code variable will read -2. Note that some shells like
2380 # csh and fish don't follow sh/bash conventions for exit codes.
2380 # csh and fish don't follow sh/bash conventions for exit codes.
2381 executable = os.environ.get('SHELL', None)
2381 executable = os.environ.get('SHELL', None)
2382 try:
2382 try:
2383 # Use env shell instead of default /bin/sh
2383 # Use env shell instead of default /bin/sh
2384 ec = subprocess.call(cmd, shell=True, executable=executable)
2384 ec = subprocess.call(cmd, shell=True, executable=executable)
2385 except KeyboardInterrupt:
2385 except KeyboardInterrupt:
2386 # intercept control-C; a long traceback is not useful here
2386 # intercept control-C; a long traceback is not useful here
2387 self.write_err('\n' + self.get_exception_only())
2387 self.write_err('\n' + self.get_exception_only())
2388 ec = 130
2388 ec = 130
2389 if ec > 128:
2389 if ec > 128:
2390 ec = -(ec - 128)
2390 ec = -(ec - 128)
2391
2391
2392 # We explicitly do NOT return the subprocess status code, because
2392 # We explicitly do NOT return the subprocess status code, because
2393 # a non-None value would trigger :func:`sys.displayhook` calls.
2393 # a non-None value would trigger :func:`sys.displayhook` calls.
2394 # Instead, we store the exit_code in user_ns. Note the semantics
2394 # Instead, we store the exit_code in user_ns. Note the semantics
2395 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2395 # of _exit_code: for control-c, _exit_code == -signal.SIGNIT,
2396 # but raising SystemExit(_exit_code) will give status 254!
2396 # but raising SystemExit(_exit_code) will give status 254!
2397 self.user_ns['_exit_code'] = ec
2397 self.user_ns['_exit_code'] = ec
2398
2398
2399 # use piped system by default, because it is better behaved
2399 # use piped system by default, because it is better behaved
2400 system = system_piped
2400 system = system_piped
2401
2401
2402 def getoutput(self, cmd, split=True, depth=0):
2402 def getoutput(self, cmd, split=True, depth=0):
2403 """Get output (possibly including stderr) from a subprocess.
2403 """Get output (possibly including stderr) from a subprocess.
2404
2404
2405 Parameters
2405 Parameters
2406 ----------
2406 ----------
2407 cmd : str
2407 cmd : str
2408 Command to execute (can not end in '&', as background processes are
2408 Command to execute (can not end in '&', as background processes are
2409 not supported.
2409 not supported.
2410 split : bool, optional
2410 split : bool, optional
2411 If True, split the output into an IPython SList. Otherwise, an
2411 If True, split the output into an IPython SList. Otherwise, an
2412 IPython LSString is returned. These are objects similar to normal
2412 IPython LSString is returned. These are objects similar to normal
2413 lists and strings, with a few convenience attributes for easier
2413 lists and strings, with a few convenience attributes for easier
2414 manipulation of line-based output. You can use '?' on them for
2414 manipulation of line-based output. You can use '?' on them for
2415 details.
2415 details.
2416 depth : int, optional
2416 depth : int, optional
2417 How many frames above the caller are the local variables which should
2417 How many frames above the caller are the local variables which should
2418 be expanded in the command string? The default (0) assumes that the
2418 be expanded in the command string? The default (0) assumes that the
2419 expansion variables are in the stack frame calling this function.
2419 expansion variables are in the stack frame calling this function.
2420 """
2420 """
2421 if cmd.rstrip().endswith('&'):
2421 if cmd.rstrip().endswith('&'):
2422 # this is *far* from a rigorous test
2422 # this is *far* from a rigorous test
2423 raise OSError("Background processes not supported.")
2423 raise OSError("Background processes not supported.")
2424 out = getoutput(self.var_expand(cmd, depth=depth+1))
2424 out = getoutput(self.var_expand(cmd, depth=depth+1))
2425 if split:
2425 if split:
2426 out = SList(out.splitlines())
2426 out = SList(out.splitlines())
2427 else:
2427 else:
2428 out = LSString(out)
2428 out = LSString(out)
2429 return out
2429 return out
2430
2430
2431 #-------------------------------------------------------------------------
2431 #-------------------------------------------------------------------------
2432 # Things related to aliases
2432 # Things related to aliases
2433 #-------------------------------------------------------------------------
2433 #-------------------------------------------------------------------------
2434
2434
2435 def init_alias(self):
2435 def init_alias(self):
2436 self.alias_manager = AliasManager(shell=self, parent=self)
2436 self.alias_manager = AliasManager(shell=self, parent=self)
2437 self.configurables.append(self.alias_manager)
2437 self.configurables.append(self.alias_manager)
2438
2438
2439 #-------------------------------------------------------------------------
2439 #-------------------------------------------------------------------------
2440 # Things related to extensions
2440 # Things related to extensions
2441 #-------------------------------------------------------------------------
2441 #-------------------------------------------------------------------------
2442
2442
2443 def init_extension_manager(self):
2443 def init_extension_manager(self):
2444 self.extension_manager = ExtensionManager(shell=self, parent=self)
2444 self.extension_manager = ExtensionManager(shell=self, parent=self)
2445 self.configurables.append(self.extension_manager)
2445 self.configurables.append(self.extension_manager)
2446
2446
2447 #-------------------------------------------------------------------------
2447 #-------------------------------------------------------------------------
2448 # Things related to payloads
2448 # Things related to payloads
2449 #-------------------------------------------------------------------------
2449 #-------------------------------------------------------------------------
2450
2450
2451 def init_payload(self):
2451 def init_payload(self):
2452 self.payload_manager = PayloadManager(parent=self)
2452 self.payload_manager = PayloadManager(parent=self)
2453 self.configurables.append(self.payload_manager)
2453 self.configurables.append(self.payload_manager)
2454
2454
2455 #-------------------------------------------------------------------------
2455 #-------------------------------------------------------------------------
2456 # Things related to the prefilter
2456 # Things related to the prefilter
2457 #-------------------------------------------------------------------------
2457 #-------------------------------------------------------------------------
2458
2458
2459 def init_prefilter(self):
2459 def init_prefilter(self):
2460 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2460 self.prefilter_manager = PrefilterManager(shell=self, parent=self)
2461 self.configurables.append(self.prefilter_manager)
2461 self.configurables.append(self.prefilter_manager)
2462 # Ultimately this will be refactored in the new interpreter code, but
2462 # Ultimately this will be refactored in the new interpreter code, but
2463 # for now, we should expose the main prefilter method (there's legacy
2463 # for now, we should expose the main prefilter method (there's legacy
2464 # code out there that may rely on this).
2464 # code out there that may rely on this).
2465 self.prefilter = self.prefilter_manager.prefilter_lines
2465 self.prefilter = self.prefilter_manager.prefilter_lines
2466
2466
2467 def auto_rewrite_input(self, cmd):
2467 def auto_rewrite_input(self, cmd):
2468 """Print to the screen the rewritten form of the user's command.
2468 """Print to the screen the rewritten form of the user's command.
2469
2469
2470 This shows visual feedback by rewriting input lines that cause
2470 This shows visual feedback by rewriting input lines that cause
2471 automatic calling to kick in, like::
2471 automatic calling to kick in, like::
2472
2472
2473 /f x
2473 /f x
2474
2474
2475 into::
2475 into::
2476
2476
2477 ------> f(x)
2477 ------> f(x)
2478
2478
2479 after the user's input prompt. This helps the user understand that the
2479 after the user's input prompt. This helps the user understand that the
2480 input line was transformed automatically by IPython.
2480 input line was transformed automatically by IPython.
2481 """
2481 """
2482 if not self.show_rewritten_input:
2482 if not self.show_rewritten_input:
2483 return
2483 return
2484
2484
2485 rw = self.prompt_manager.render('rewrite') + cmd
2485 rw = self.prompt_manager.render('rewrite') + cmd
2486
2486
2487 try:
2487 try:
2488 # plain ascii works better w/ pyreadline, on some machines, so
2488 # plain ascii works better w/ pyreadline, on some machines, so
2489 # we use it and only print uncolored rewrite if we have unicode
2489 # we use it and only print uncolored rewrite if we have unicode
2490 rw = str(rw)
2490 rw = str(rw)
2491 print(rw, file=io.stdout)
2491 print(rw, file=io.stdout)
2492 except UnicodeEncodeError:
2492 except UnicodeEncodeError:
2493 print("------> " + cmd)
2493 print("------> " + cmd)
2494
2494
2495 #-------------------------------------------------------------------------
2495 #-------------------------------------------------------------------------
2496 # Things related to extracting values/expressions from kernel and user_ns
2496 # Things related to extracting values/expressions from kernel and user_ns
2497 #-------------------------------------------------------------------------
2497 #-------------------------------------------------------------------------
2498
2498
2499 def _user_obj_error(self):
2499 def _user_obj_error(self):
2500 """return simple exception dict
2500 """return simple exception dict
2501
2501
2502 for use in user_expressions
2502 for use in user_expressions
2503 """
2503 """
2504
2504
2505 etype, evalue, tb = self._get_exc_info()
2505 etype, evalue, tb = self._get_exc_info()
2506 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2506 stb = self.InteractiveTB.get_exception_only(etype, evalue)
2507
2507
2508 exc_info = {
2508 exc_info = {
2509 u'status' : 'error',
2509 u'status' : 'error',
2510 u'traceback' : stb,
2510 u'traceback' : stb,
2511 u'ename' : unicode_type(etype.__name__),
2511 u'ename' : unicode_type(etype.__name__),
2512 u'evalue' : py3compat.safe_unicode(evalue),
2512 u'evalue' : py3compat.safe_unicode(evalue),
2513 }
2513 }
2514
2514
2515 return exc_info
2515 return exc_info
2516
2516
2517 def _format_user_obj(self, obj):
2517 def _format_user_obj(self, obj):
2518 """format a user object to display dict
2518 """format a user object to display dict
2519
2519
2520 for use in user_expressions
2520 for use in user_expressions
2521 """
2521 """
2522
2522
2523 data, md = self.display_formatter.format(obj)
2523 data, md = self.display_formatter.format(obj)
2524 value = {
2524 value = {
2525 'status' : 'ok',
2525 'status' : 'ok',
2526 'data' : data,
2526 'data' : data,
2527 'metadata' : md,
2527 'metadata' : md,
2528 }
2528 }
2529 return value
2529 return value
2530
2530
2531 def user_expressions(self, expressions):
2531 def user_expressions(self, expressions):
2532 """Evaluate a dict of expressions in the user's namespace.
2532 """Evaluate a dict of expressions in the user's namespace.
2533
2533
2534 Parameters
2534 Parameters
2535 ----------
2535 ----------
2536 expressions : dict
2536 expressions : dict
2537 A dict with string keys and string values. The expression values
2537 A dict with string keys and string values. The expression values
2538 should be valid Python expressions, each of which will be evaluated
2538 should be valid Python expressions, each of which will be evaluated
2539 in the user namespace.
2539 in the user namespace.
2540
2540
2541 Returns
2541 Returns
2542 -------
2542 -------
2543 A dict, keyed like the input expressions dict, with the rich mime-typed
2543 A dict, keyed like the input expressions dict, with the rich mime-typed
2544 display_data of each value.
2544 display_data of each value.
2545 """
2545 """
2546 out = {}
2546 out = {}
2547 user_ns = self.user_ns
2547 user_ns = self.user_ns
2548 global_ns = self.user_global_ns
2548 global_ns = self.user_global_ns
2549
2549
2550 for key, expr in iteritems(expressions):
2550 for key, expr in iteritems(expressions):
2551 try:
2551 try:
2552 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2552 value = self._format_user_obj(eval(expr, global_ns, user_ns))
2553 except:
2553 except:
2554 value = self._user_obj_error()
2554 value = self._user_obj_error()
2555 out[key] = value
2555 out[key] = value
2556 return out
2556 return out
2557
2557
2558 #-------------------------------------------------------------------------
2558 #-------------------------------------------------------------------------
2559 # Things related to the running of code
2559 # Things related to the running of code
2560 #-------------------------------------------------------------------------
2560 #-------------------------------------------------------------------------
2561
2561
2562 def ex(self, cmd):
2562 def ex(self, cmd):
2563 """Execute a normal python statement in user namespace."""
2563 """Execute a normal python statement in user namespace."""
2564 with self.builtin_trap:
2564 with self.builtin_trap:
2565 exec(cmd, self.user_global_ns, self.user_ns)
2565 exec(cmd, self.user_global_ns, self.user_ns)
2566
2566
2567 def ev(self, expr):
2567 def ev(self, expr):
2568 """Evaluate python expression expr in user namespace.
2568 """Evaluate python expression expr in user namespace.
2569
2569
2570 Returns the result of evaluation
2570 Returns the result of evaluation
2571 """
2571 """
2572 with self.builtin_trap:
2572 with self.builtin_trap:
2573 return eval(expr, self.user_global_ns, self.user_ns)
2573 return eval(expr, self.user_global_ns, self.user_ns)
2574
2574
2575 def safe_execfile(self, fname, *where, **kw):
2575 def safe_execfile(self, fname, *where, **kw):
2576 """A safe version of the builtin execfile().
2576 """A safe version of the builtin execfile().
2577
2577
2578 This version will never throw an exception, but instead print
2578 This version will never throw an exception, but instead print
2579 helpful error messages to the screen. This only works on pure
2579 helpful error messages to the screen. This only works on pure
2580 Python files with the .py extension.
2580 Python files with the .py extension.
2581
2581
2582 Parameters
2582 Parameters
2583 ----------
2583 ----------
2584 fname : string
2584 fname : string
2585 The name of the file to be executed.
2585 The name of the file to be executed.
2586 where : tuple
2586 where : tuple
2587 One or two namespaces, passed to execfile() as (globals,locals).
2587 One or two namespaces, passed to execfile() as (globals,locals).
2588 If only one is given, it is passed as both.
2588 If only one is given, it is passed as both.
2589 exit_ignore : bool (False)
2589 exit_ignore : bool (False)
2590 If True, then silence SystemExit for non-zero status (it is always
2590 If True, then silence SystemExit for non-zero status (it is always
2591 silenced for zero status, as it is so common).
2591 silenced for zero status, as it is so common).
2592 raise_exceptions : bool (False)
2592 raise_exceptions : bool (False)
2593 If True raise exceptions everywhere. Meant for testing.
2593 If True raise exceptions everywhere. Meant for testing.
2594 shell_futures : bool (False)
2594 shell_futures : bool (False)
2595 If True, the code will share future statements with the interactive
2595 If True, the code will share future statements with the interactive
2596 shell. It will both be affected by previous __future__ imports, and
2596 shell. It will both be affected by previous __future__ imports, and
2597 any __future__ imports in the code will affect the shell. If False,
2597 any __future__ imports in the code will affect the shell. If False,
2598 __future__ imports are not shared in either direction.
2598 __future__ imports are not shared in either direction.
2599
2599
2600 """
2600 """
2601 kw.setdefault('exit_ignore', False)
2601 kw.setdefault('exit_ignore', False)
2602 kw.setdefault('raise_exceptions', False)
2602 kw.setdefault('raise_exceptions', False)
2603 kw.setdefault('shell_futures', False)
2603 kw.setdefault('shell_futures', False)
2604
2604
2605 fname = os.path.abspath(os.path.expanduser(fname))
2605 fname = os.path.abspath(os.path.expanduser(fname))
2606
2606
2607 # Make sure we can open the file
2607 # Make sure we can open the file
2608 try:
2608 try:
2609 with open(fname) as thefile:
2609 with open(fname) as thefile:
2610 pass
2610 pass
2611 except:
2611 except:
2612 warn('Could not open file <%s> for safe execution.' % fname)
2612 warn('Could not open file <%s> for safe execution.' % fname)
2613 return
2613 return
2614
2614
2615 # Find things also in current directory. This is needed to mimic the
2615 # Find things also in current directory. This is needed to mimic the
2616 # behavior of running a script from the system command line, where
2616 # behavior of running a script from the system command line, where
2617 # Python inserts the script's directory into sys.path
2617 # Python inserts the script's directory into sys.path
2618 dname = os.path.dirname(fname)
2618 dname = os.path.dirname(fname)
2619
2619
2620 with prepended_to_syspath(dname):
2620 with prepended_to_syspath(dname):
2621 try:
2621 try:
2622 glob, loc = (where + (None, ))[:2]
2622 glob, loc = (where + (None, ))[:2]
2623 py3compat.execfile(
2623 py3compat.execfile(
2624 fname, glob, loc,
2624 fname, glob, loc,
2625 self.compile if kw['shell_futures'] else None)
2625 self.compile if kw['shell_futures'] else None)
2626 except SystemExit as status:
2626 except SystemExit as status:
2627 # If the call was made with 0 or None exit status (sys.exit(0)
2627 # If the call was made with 0 or None exit status (sys.exit(0)
2628 # or sys.exit() ), don't bother showing a traceback, as both of
2628 # or sys.exit() ), don't bother showing a traceback, as both of
2629 # these are considered normal by the OS:
2629 # these are considered normal by the OS:
2630 # > python -c'import sys;sys.exit(0)'; echo $?
2630 # > python -c'import sys;sys.exit(0)'; echo $?
2631 # 0
2631 # 0
2632 # > python -c'import sys;sys.exit()'; echo $?
2632 # > python -c'import sys;sys.exit()'; echo $?
2633 # 0
2633 # 0
2634 # For other exit status, we show the exception unless
2634 # For other exit status, we show the exception unless
2635 # explicitly silenced, but only in short form.
2635 # explicitly silenced, but only in short form.
2636 if kw['raise_exceptions']:
2636 if kw['raise_exceptions']:
2637 raise
2637 raise
2638 if status.code and not kw['exit_ignore']:
2638 if status.code and not kw['exit_ignore']:
2639 self.showtraceback(exception_only=True)
2639 self.showtraceback(exception_only=True)
2640 except:
2640 except:
2641 if kw['raise_exceptions']:
2641 if kw['raise_exceptions']:
2642 raise
2642 raise
2643 # tb offset is 2 because we wrap execfile
2643 # tb offset is 2 because we wrap execfile
2644 self.showtraceback(tb_offset=2)
2644 self.showtraceback(tb_offset=2)
2645
2645
2646 def safe_execfile_ipy(self, fname, shell_futures=False):
2646 def safe_execfile_ipy(self, fname, shell_futures=False):
2647 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2647 """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax.
2648
2648
2649 Parameters
2649 Parameters
2650 ----------
2650 ----------
2651 fname : str
2651 fname : str
2652 The name of the file to execute. The filename must have a
2652 The name of the file to execute. The filename must have a
2653 .ipy or .ipynb extension.
2653 .ipy or .ipynb extension.
2654 shell_futures : bool (False)
2654 shell_futures : bool (False)
2655 If True, the code will share future statements with the interactive
2655 If True, the code will share future statements with the interactive
2656 shell. It will both be affected by previous __future__ imports, and
2656 shell. It will both be affected by previous __future__ imports, and
2657 any __future__ imports in the code will affect the shell. If False,
2657 any __future__ imports in the code will affect the shell. If False,
2658 __future__ imports are not shared in either direction.
2658 __future__ imports are not shared in either direction.
2659 """
2659 """
2660 fname = os.path.abspath(os.path.expanduser(fname))
2660 fname = os.path.abspath(os.path.expanduser(fname))
2661
2661
2662 # Make sure we can open the file
2662 # Make sure we can open the file
2663 try:
2663 try:
2664 with open(fname) as thefile:
2664 with open(fname) as thefile:
2665 pass
2665 pass
2666 except:
2666 except:
2667 warn('Could not open file <%s> for safe execution.' % fname)
2667 warn('Could not open file <%s> for safe execution.' % fname)
2668 return
2668 return
2669
2669
2670 # Find things also in current directory. This is needed to mimic the
2670 # Find things also in current directory. This is needed to mimic the
2671 # behavior of running a script from the system command line, where
2671 # behavior of running a script from the system command line, where
2672 # Python inserts the script's directory into sys.path
2672 # Python inserts the script's directory into sys.path
2673 dname = os.path.dirname(fname)
2673 dname = os.path.dirname(fname)
2674
2674
2675 def get_cells():
2675 def get_cells():
2676 """generator for sequence of code blocks to run"""
2676 """generator for sequence of code blocks to run"""
2677 if fname.endswith('.ipynb'):
2677 if fname.endswith('.ipynb'):
2678 from IPython.nbformat import read
2678 from IPython.nbformat import read
2679 with io_open(fname) as f:
2679 with io_open(fname) as f:
2680 nb = read(f, as_version=4)
2680 nb = read(f, as_version=4)
2681 if not nb.cells:
2681 if not nb.cells:
2682 return
2682 return
2683 for cell in nb.cells:
2683 for cell in nb.cells:
2684 if cell.cell_type == 'code':
2684 if cell.cell_type == 'code':
2685 yield cell.source
2685 yield cell.source
2686 else:
2686 else:
2687 with open(fname) as f:
2687 with open(fname) as f:
2688 yield f.read()
2688 yield f.read()
2689
2689
2690 with prepended_to_syspath(dname):
2690 with prepended_to_syspath(dname):
2691 try:
2691 try:
2692 for cell in get_cells():
2692 for cell in get_cells():
2693 # self.run_cell currently captures all exceptions
2693 # self.run_cell currently captures all exceptions
2694 # raised in user code. It would be nice if there were
2694 # raised in user code. It would be nice if there were
2695 # versions of run_cell that did raise, so
2695 # versions of run_cell that did raise, so
2696 # we could catch the errors.
2696 # we could catch the errors.
2697 self.run_cell(cell, silent=True, shell_futures=shell_futures)
2697 self.run_cell(cell, silent=True, shell_futures=shell_futures)
2698 except:
2698 except:
2699 self.showtraceback()
2699 self.showtraceback()
2700 warn('Unknown failure executing file: <%s>' % fname)
2700 warn('Unknown failure executing file: <%s>' % fname)
2701
2701
2702 def safe_run_module(self, mod_name, where):
2702 def safe_run_module(self, mod_name, where):
2703 """A safe version of runpy.run_module().
2703 """A safe version of runpy.run_module().
2704
2704
2705 This version will never throw an exception, but instead print
2705 This version will never throw an exception, but instead print
2706 helpful error messages to the screen.
2706 helpful error messages to the screen.
2707
2707
2708 `SystemExit` exceptions with status code 0 or None are ignored.
2708 `SystemExit` exceptions with status code 0 or None are ignored.
2709
2709
2710 Parameters
2710 Parameters
2711 ----------
2711 ----------
2712 mod_name : string
2712 mod_name : string
2713 The name of the module to be executed.
2713 The name of the module to be executed.
2714 where : dict
2714 where : dict
2715 The globals namespace.
2715 The globals namespace.
2716 """
2716 """
2717 try:
2717 try:
2718 try:
2718 try:
2719 where.update(
2719 where.update(
2720 runpy.run_module(str(mod_name), run_name="__main__",
2720 runpy.run_module(str(mod_name), run_name="__main__",
2721 alter_sys=True)
2721 alter_sys=True)
2722 )
2722 )
2723 except SystemExit as status:
2723 except SystemExit as status:
2724 if status.code:
2724 if status.code:
2725 raise
2725 raise
2726 except:
2726 except:
2727 self.showtraceback()
2727 self.showtraceback()
2728 warn('Unknown failure executing module: <%s>' % mod_name)
2728 warn('Unknown failure executing module: <%s>' % mod_name)
2729
2729
2730 def _run_cached_cell_magic(self, magic_name, line):
2730 def _run_cached_cell_magic(self, magic_name, line):
2731 """Special method to call a cell magic with the data stored in self.
2731 """Special method to call a cell magic with the data stored in self.
2732 """
2732 """
2733 cell = self._current_cell_magic_body
2733 cell = self._current_cell_magic_body
2734 self._current_cell_magic_body = None
2734 self._current_cell_magic_body = None
2735 return self.run_cell_magic(magic_name, line, cell)
2735 return self.run_cell_magic(magic_name, line, cell)
2736
2736
2737 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2737 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2738 """Run a complete IPython cell.
2738 """Run a complete IPython cell.
2739
2739
2740 Parameters
2740 Parameters
2741 ----------
2741 ----------
2742 raw_cell : str
2742 raw_cell : str
2743 The code (including IPython code such as %magic functions) to run.
2743 The code (including IPython code such as %magic functions) to run.
2744 store_history : bool
2744 store_history : bool
2745 If True, the raw and translated cell will be stored in IPython's
2745 If True, the raw and translated cell will be stored in IPython's
2746 history. For user code calling back into IPython's machinery, this
2746 history. For user code calling back into IPython's machinery, this
2747 should be set to False.
2747 should be set to False.
2748 silent : bool
2748 silent : bool
2749 If True, avoid side-effects, such as implicit displayhooks and
2749 If True, avoid side-effects, such as implicit displayhooks and
2750 and logging. silent=True forces store_history=False.
2750 and logging. silent=True forces store_history=False.
2751 shell_futures : bool
2751 shell_futures : bool
2752 If True, the code will share future statements with the interactive
2752 If True, the code will share future statements with the interactive
2753 shell. It will both be affected by previous __future__ imports, and
2753 shell. It will both be affected by previous __future__ imports, and
2754 any __future__ imports in the code will affect the shell. If False,
2754 any __future__ imports in the code will affect the shell. If False,
2755 __future__ imports are not shared in either direction.
2755 __future__ imports are not shared in either direction.
2756 """
2756 """
2757 if (not raw_cell) or raw_cell.isspace():
2757 if (not raw_cell) or raw_cell.isspace():
2758 return
2758 return
2759
2759
2760 if silent:
2760 if silent:
2761 store_history = False
2761 store_history = False
2762
2762
2763 self.events.trigger('pre_execute')
2763 self.events.trigger('pre_execute')
2764 if not silent:
2764 if not silent:
2765 self.events.trigger('pre_run_cell')
2765 self.events.trigger('pre_run_cell')
2766
2766
2767 # If any of our input transformation (input_transformer_manager or
2767 # If any of our input transformation (input_transformer_manager or
2768 # prefilter_manager) raises an exception, we store it in this variable
2768 # prefilter_manager) raises an exception, we store it in this variable
2769 # so that we can display the error after logging the input and storing
2769 # so that we can display the error after logging the input and storing
2770 # it in the history.
2770 # it in the history.
2771 preprocessing_exc_tuple = None
2771 preprocessing_exc_tuple = None
2772 try:
2772 try:
2773 # Static input transformations
2773 # Static input transformations
2774 cell = self.input_transformer_manager.transform_cell(raw_cell)
2774 cell = self.input_transformer_manager.transform_cell(raw_cell)
2775 except SyntaxError:
2775 except SyntaxError:
2776 preprocessing_exc_tuple = sys.exc_info()
2776 preprocessing_exc_tuple = sys.exc_info()
2777 cell = raw_cell # cell has to exist so it can be stored/logged
2777 cell = raw_cell # cell has to exist so it can be stored/logged
2778 else:
2778 else:
2779 if len(cell.splitlines()) == 1:
2779 if len(cell.splitlines()) == 1:
2780 # Dynamic transformations - only applied for single line commands
2780 # Dynamic transformations - only applied for single line commands
2781 with self.builtin_trap:
2781 with self.builtin_trap:
2782 try:
2782 try:
2783 # use prefilter_lines to handle trailing newlines
2783 # use prefilter_lines to handle trailing newlines
2784 # restore trailing newline for ast.parse
2784 # restore trailing newline for ast.parse
2785 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2785 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
2786 except Exception:
2786 except Exception:
2787 # don't allow prefilter errors to crash IPython
2787 # don't allow prefilter errors to crash IPython
2788 preprocessing_exc_tuple = sys.exc_info()
2788 preprocessing_exc_tuple = sys.exc_info()
2789
2789
2790 # Store raw and processed history
2790 # Store raw and processed history
2791 if store_history:
2791 if store_history:
2792 self.history_manager.store_inputs(self.execution_count,
2792 self.history_manager.store_inputs(self.execution_count,
2793 cell, raw_cell)
2793 cell, raw_cell)
2794 if not silent:
2794 if not silent:
2795 self.logger.log(cell, raw_cell)
2795 self.logger.log(cell, raw_cell)
2796
2796
2797 # Display the exception if input processing failed.
2797 # Display the exception if input processing failed.
2798 if preprocessing_exc_tuple is not None:
2798 if preprocessing_exc_tuple is not None:
2799 self.showtraceback(preprocessing_exc_tuple)
2799 self.showtraceback(preprocessing_exc_tuple)
2800 if store_history:
2800 if store_history:
2801 self.execution_count += 1
2801 self.execution_count += 1
2802 return
2802 return
2803
2803
2804 # Our own compiler remembers the __future__ environment. If we want to
2804 # Our own compiler remembers the __future__ environment. If we want to
2805 # run code with a separate __future__ environment, use the default
2805 # run code with a separate __future__ environment, use the default
2806 # compiler
2806 # compiler
2807 compiler = self.compile if shell_futures else CachingCompiler()
2807 compiler = self.compile if shell_futures else CachingCompiler()
2808
2808
2809 with self.builtin_trap:
2809 with self.builtin_trap:
2810 cell_name = self.compile.cache(cell, self.execution_count)
2810 cell_name = self.compile.cache(cell, self.execution_count)
2811
2811
2812 with self.display_trap:
2812 with self.display_trap:
2813 # Compile to bytecode
2813 # Compile to bytecode
2814 try:
2814 try:
2815 code_ast = compiler.ast_parse(cell, filename=cell_name)
2815 code_ast = compiler.ast_parse(cell, filename=cell_name)
2816 except IndentationError:
2816 except IndentationError:
2817 self.showindentationerror()
2817 self.showindentationerror()
2818 if store_history:
2818 if store_history:
2819 self.execution_count += 1
2819 self.execution_count += 1
2820 return None
2820 return None
2821 except (OverflowError, SyntaxError, ValueError, TypeError,
2821 except (OverflowError, SyntaxError, ValueError, TypeError,
2822 MemoryError):
2822 MemoryError):
2823 self.showsyntaxerror()
2823 self.showsyntaxerror()
2824 if store_history:
2824 if store_history:
2825 self.execution_count += 1
2825 self.execution_count += 1
2826 return None
2826 return None
2827
2827
2828 # Apply AST transformations
2828 # Apply AST transformations
2829 try:
2829 try:
2830 code_ast = self.transform_ast(code_ast)
2830 code_ast = self.transform_ast(code_ast)
2831 except InputRejected:
2831 except InputRejected:
2832 self.showtraceback()
2832 self.showtraceback()
2833 if store_history:
2833 if store_history:
2834 self.execution_count += 1
2834 self.execution_count += 1
2835 return None
2835 return None
2836
2836
2837 # Execute the user code
2837 # Execute the user code
2838 interactivity = "none" if silent else self.ast_node_interactivity
2838 interactivity = "none" if silent else self.ast_node_interactivity
2839 self.run_ast_nodes(code_ast.body, cell_name,
2839 self.run_ast_nodes(code_ast.body, cell_name,
2840 interactivity=interactivity, compiler=compiler)
2840 interactivity=interactivity, compiler=compiler)
2841
2841
2842 self.events.trigger('post_execute')
2842 self.events.trigger('post_execute')
2843 if not silent:
2843 if not silent:
2844 self.events.trigger('post_run_cell')
2844 self.events.trigger('post_run_cell')
2845
2845
2846 if store_history:
2846 if store_history:
2847 # Write output to the database. Does nothing unless
2847 # Write output to the database. Does nothing unless
2848 # history output logging is enabled.
2848 # history output logging is enabled.
2849 self.history_manager.store_output(self.execution_count)
2849 self.history_manager.store_output(self.execution_count)
2850 # Each cell is a *single* input, regardless of how many lines it has
2850 # Each cell is a *single* input, regardless of how many lines it has
2851 self.execution_count += 1
2851 self.execution_count += 1
2852
2852
2853 def transform_ast(self, node):
2853 def transform_ast(self, node):
2854 """Apply the AST transformations from self.ast_transformers
2854 """Apply the AST transformations from self.ast_transformers
2855
2855
2856 Parameters
2856 Parameters
2857 ----------
2857 ----------
2858 node : ast.Node
2858 node : ast.Node
2859 The root node to be transformed. Typically called with the ast.Module
2859 The root node to be transformed. Typically called with the ast.Module
2860 produced by parsing user input.
2860 produced by parsing user input.
2861
2861
2862 Returns
2862 Returns
2863 -------
2863 -------
2864 An ast.Node corresponding to the node it was called with. Note that it
2864 An ast.Node corresponding to the node it was called with. Note that it
2865 may also modify the passed object, so don't rely on references to the
2865 may also modify the passed object, so don't rely on references to the
2866 original AST.
2866 original AST.
2867 """
2867 """
2868 for transformer in self.ast_transformers:
2868 for transformer in self.ast_transformers:
2869 try:
2869 try:
2870 node = transformer.visit(node)
2870 node = transformer.visit(node)
2871 except InputRejected:
2871 except InputRejected:
2872 # User-supplied AST transformers can reject an input by raising
2872 # User-supplied AST transformers can reject an input by raising
2873 # an InputRejected. Short-circuit in this case so that we
2873 # an InputRejected. Short-circuit in this case so that we
2874 # don't unregister the transform.
2874 # don't unregister the transform.
2875 raise
2875 raise
2876 except Exception:
2876 except Exception:
2877 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2877 warn("AST transformer %r threw an error. It will be unregistered." % transformer)
2878 self.ast_transformers.remove(transformer)
2878 self.ast_transformers.remove(transformer)
2879
2879
2880 if self.ast_transformers:
2880 if self.ast_transformers:
2881 ast.fix_missing_locations(node)
2881 ast.fix_missing_locations(node)
2882 return node
2882 return node
2883
2883
2884
2884
2885 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2885 def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr',
2886 compiler=compile):
2886 compiler=compile):
2887 """Run a sequence of AST nodes. The execution mode depends on the
2887 """Run a sequence of AST nodes. The execution mode depends on the
2888 interactivity parameter.
2888 interactivity parameter.
2889
2889
2890 Parameters
2890 Parameters
2891 ----------
2891 ----------
2892 nodelist : list
2892 nodelist : list
2893 A sequence of AST nodes to run.
2893 A sequence of AST nodes to run.
2894 cell_name : str
2894 cell_name : str
2895 Will be passed to the compiler as the filename of the cell. Typically
2895 Will be passed to the compiler as the filename of the cell. Typically
2896 the value returned by ip.compile.cache(cell).
2896 the value returned by ip.compile.cache(cell).
2897 interactivity : str
2897 interactivity : str
2898 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2898 'all', 'last', 'last_expr' or 'none', specifying which nodes should be
2899 run interactively (displaying output from expressions). 'last_expr'
2899 run interactively (displaying output from expressions). 'last_expr'
2900 will run the last node interactively only if it is an expression (i.e.
2900 will run the last node interactively only if it is an expression (i.e.
2901 expressions in loops or other blocks are not displayed. Other values
2901 expressions in loops or other blocks are not displayed. Other values
2902 for this parameter will raise a ValueError.
2902 for this parameter will raise a ValueError.
2903 compiler : callable
2903 compiler : callable
2904 A function with the same interface as the built-in compile(), to turn
2904 A function with the same interface as the built-in compile(), to turn
2905 the AST nodes into code objects. Default is the built-in compile().
2905 the AST nodes into code objects. Default is the built-in compile().
2906 """
2906 """
2907 if not nodelist:
2907 if not nodelist:
2908 return
2908 return
2909
2909
2910 if interactivity == 'last_expr':
2910 if interactivity == 'last_expr':
2911 if isinstance(nodelist[-1], ast.Expr):
2911 if isinstance(nodelist[-1], ast.Expr):
2912 interactivity = "last"
2912 interactivity = "last"
2913 else:
2913 else:
2914 interactivity = "none"
2914 interactivity = "none"
2915
2915
2916 if interactivity == 'none':
2916 if interactivity == 'none':
2917 to_run_exec, to_run_interactive = nodelist, []
2917 to_run_exec, to_run_interactive = nodelist, []
2918 elif interactivity == 'last':
2918 elif interactivity == 'last':
2919 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2919 to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
2920 elif interactivity == 'all':
2920 elif interactivity == 'all':
2921 to_run_exec, to_run_interactive = [], nodelist
2921 to_run_exec, to_run_interactive = [], nodelist
2922 else:
2922 else:
2923 raise ValueError("Interactivity was %r" % interactivity)
2923 raise ValueError("Interactivity was %r" % interactivity)
2924
2924
2925 exec_count = self.execution_count
2925 exec_count = self.execution_count
2926
2926
2927 try:
2927 try:
2928 for i, node in enumerate(to_run_exec):
2928 for i, node in enumerate(to_run_exec):
2929 mod = ast.Module([node])
2929 mod = ast.Module([node])
2930 code = compiler(mod, cell_name, "exec")
2930 code = compiler(mod, cell_name, "exec")
2931 if self.run_code(code):
2931 if self.run_code(code):
2932 return True
2932 return True
2933
2933
2934 for i, node in enumerate(to_run_interactive):
2934 for i, node in enumerate(to_run_interactive):
2935 mod = ast.Interactive([node])
2935 mod = ast.Interactive([node])
2936 code = compiler(mod, cell_name, "single")
2936 code = compiler(mod, cell_name, "single")
2937 if self.run_code(code):
2937 if self.run_code(code):
2938 return True
2938 return True
2939
2939
2940 # Flush softspace
2940 # Flush softspace
2941 if softspace(sys.stdout, 0):
2941 if softspace(sys.stdout, 0):
2942 print()
2942 print()
2943
2943
2944 except:
2944 except:
2945 # It's possible to have exceptions raised here, typically by
2945 # It's possible to have exceptions raised here, typically by
2946 # compilation of odd code (such as a naked 'return' outside a
2946 # compilation of odd code (such as a naked 'return' outside a
2947 # function) that did parse but isn't valid. Typically the exception
2947 # function) that did parse but isn't valid. Typically the exception
2948 # is a SyntaxError, but it's safest just to catch anything and show
2948 # is a SyntaxError, but it's safest just to catch anything and show
2949 # the user a traceback.
2949 # the user a traceback.
2950
2950
2951 # We do only one try/except outside the loop to minimize the impact
2951 # We do only one try/except outside the loop to minimize the impact
2952 # on runtime, and also because if any node in the node list is
2952 # on runtime, and also because if any node in the node list is
2953 # broken, we should stop execution completely.
2953 # broken, we should stop execution completely.
2954 self.showtraceback()
2954 self.showtraceback()
2955
2955
2956 return False
2956 return False
2957
2957
2958 def run_code(self, code_obj):
2958 def run_code(self, code_obj):
2959 """Execute a code object.
2959 """Execute a code object.
2960
2960
2961 When an exception occurs, self.showtraceback() is called to display a
2961 When an exception occurs, self.showtraceback() is called to display a
2962 traceback.
2962 traceback.
2963
2963
2964 Parameters
2964 Parameters
2965 ----------
2965 ----------
2966 code_obj : code object
2966 code_obj : code object
2967 A compiled code object, to be executed
2967 A compiled code object, to be executed
2968
2968
2969 Returns
2969 Returns
2970 -------
2970 -------
2971 False : successful execution.
2971 False : successful execution.
2972 True : an error occurred.
2972 True : an error occurred.
2973 """
2973 """
2974 # Set our own excepthook in case the user code tries to call it
2974 # Set our own excepthook in case the user code tries to call it
2975 # directly, so that the IPython crash handler doesn't get triggered
2975 # directly, so that the IPython crash handler doesn't get triggered
2976 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2976 old_excepthook, sys.excepthook = sys.excepthook, self.excepthook
2977
2977
2978 # we save the original sys.excepthook in the instance, in case config
2978 # we save the original sys.excepthook in the instance, in case config
2979 # code (such as magics) needs access to it.
2979 # code (such as magics) needs access to it.
2980 self.sys_excepthook = old_excepthook
2980 self.sys_excepthook = old_excepthook
2981 outflag = 1 # happens in more places, so it's easier as default
2981 outflag = 1 # happens in more places, so it's easier as default
2982 try:
2982 try:
2983 try:
2983 try:
2984 self.hooks.pre_run_code_hook()
2984 self.hooks.pre_run_code_hook()
2985 #rprint('Running code', repr(code_obj)) # dbg
2985 #rprint('Running code', repr(code_obj)) # dbg
2986 exec(code_obj, self.user_global_ns, self.user_ns)
2986 exec(code_obj, self.user_global_ns, self.user_ns)
2987 finally:
2987 finally:
2988 # Reset our crash handler in place
2988 # Reset our crash handler in place
2989 sys.excepthook = old_excepthook
2989 sys.excepthook = old_excepthook
2990 except SystemExit:
2990 except SystemExit:
2991 self.showtraceback(exception_only=True)
2991 self.showtraceback(exception_only=True)
2992 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2992 warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1)
2993 except self.custom_exceptions:
2993 except self.custom_exceptions:
2994 etype, value, tb = sys.exc_info()
2994 etype, value, tb = sys.exc_info()
2995 self.CustomTB(etype, value, tb)
2995 self.CustomTB(etype, value, tb)
2996 except:
2996 except:
2997 self.showtraceback()
2997 self.showtraceback()
2998 else:
2998 else:
2999 outflag = 0
2999 outflag = 0
3000 return outflag
3000 return outflag
3001
3001
3002 # For backwards compatibility
3002 # For backwards compatibility
3003 runcode = run_code
3003 runcode = run_code
3004
3004
3005 #-------------------------------------------------------------------------
3005 #-------------------------------------------------------------------------
3006 # Things related to GUI support and pylab
3006 # Things related to GUI support and pylab
3007 #-------------------------------------------------------------------------
3007 #-------------------------------------------------------------------------
3008
3008
3009 def enable_gui(self, gui=None):
3009 def enable_gui(self, gui=None):
3010 raise NotImplementedError('Implement enable_gui in a subclass')
3010 raise NotImplementedError('Implement enable_gui in a subclass')
3011
3011
3012 def enable_matplotlib(self, gui=None):
3012 def enable_matplotlib(self, gui=None):
3013 """Enable interactive matplotlib and inline figure support.
3013 """Enable interactive matplotlib and inline figure support.
3014
3014
3015 This takes the following steps:
3015 This takes the following steps:
3016
3016
3017 1. select the appropriate eventloop and matplotlib backend
3017 1. select the appropriate eventloop and matplotlib backend
3018 2. set up matplotlib for interactive use with that backend
3018 2. set up matplotlib for interactive use with that backend
3019 3. configure formatters for inline figure display
3019 3. configure formatters for inline figure display
3020 4. enable the selected gui eventloop
3020 4. enable the selected gui eventloop
3021
3021
3022 Parameters
3022 Parameters
3023 ----------
3023 ----------
3024 gui : optional, string
3024 gui : optional, string
3025 If given, dictates the choice of matplotlib GUI backend to use
3025 If given, dictates the choice of matplotlib GUI backend to use
3026 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3026 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3027 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3027 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3028 matplotlib (as dictated by the matplotlib build-time options plus the
3028 matplotlib (as dictated by the matplotlib build-time options plus the
3029 user's matplotlibrc configuration file). Note that not all backends
3029 user's matplotlibrc configuration file). Note that not all backends
3030 make sense in all contexts, for example a terminal ipython can't
3030 make sense in all contexts, for example a terminal ipython can't
3031 display figures inline.
3031 display figures inline.
3032 """
3032 """
3033 from IPython.core import pylabtools as pt
3033 from IPython.core import pylabtools as pt
3034 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3034 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3035
3035
3036 if gui != 'inline':
3036 if gui != 'inline':
3037 # If we have our first gui selection, store it
3037 # If we have our first gui selection, store it
3038 if self.pylab_gui_select is None:
3038 if self.pylab_gui_select is None:
3039 self.pylab_gui_select = gui
3039 self.pylab_gui_select = gui
3040 # Otherwise if they are different
3040 # Otherwise if they are different
3041 elif gui != self.pylab_gui_select:
3041 elif gui != self.pylab_gui_select:
3042 print ('Warning: Cannot change to a different GUI toolkit: %s.'
3042 print ('Warning: Cannot change to a different GUI toolkit: %s.'
3043 ' Using %s instead.' % (gui, self.pylab_gui_select))
3043 ' Using %s instead.' % (gui, self.pylab_gui_select))
3044 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3044 gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
3045
3045
3046 pt.activate_matplotlib(backend)
3046 pt.activate_matplotlib(backend)
3047 pt.configure_inline_support(self, backend)
3047 pt.configure_inline_support(self, backend)
3048
3048
3049 # Now we must activate the gui pylab wants to use, and fix %run to take
3049 # Now we must activate the gui pylab wants to use, and fix %run to take
3050 # plot updates into account
3050 # plot updates into account
3051 self.enable_gui(gui)
3051 self.enable_gui(gui)
3052 self.magics_manager.registry['ExecutionMagics'].default_runner = \
3052 self.magics_manager.registry['ExecutionMagics'].default_runner = \
3053 pt.mpl_runner(self.safe_execfile)
3053 pt.mpl_runner(self.safe_execfile)
3054
3054
3055 return gui, backend
3055 return gui, backend
3056
3056
3057 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
3057 def enable_pylab(self, gui=None, import_all=True, welcome_message=False):
3058 """Activate pylab support at runtime.
3058 """Activate pylab support at runtime.
3059
3059
3060 This turns on support for matplotlib, preloads into the interactive
3060 This turns on support for matplotlib, preloads into the interactive
3061 namespace all of numpy and pylab, and configures IPython to correctly
3061 namespace all of numpy and pylab, and configures IPython to correctly
3062 interact with the GUI event loop. The GUI backend to be used can be
3062 interact with the GUI event loop. The GUI backend to be used can be
3063 optionally selected with the optional ``gui`` argument.
3063 optionally selected with the optional ``gui`` argument.
3064
3064
3065 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
3065 This method only adds preloading the namespace to InteractiveShell.enable_matplotlib.
3066
3066
3067 Parameters
3067 Parameters
3068 ----------
3068 ----------
3069 gui : optional, string
3069 gui : optional, string
3070 If given, dictates the choice of matplotlib GUI backend to use
3070 If given, dictates the choice of matplotlib GUI backend to use
3071 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3071 (should be one of IPython's supported backends, 'qt', 'osx', 'tk',
3072 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3072 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
3073 matplotlib (as dictated by the matplotlib build-time options plus the
3073 matplotlib (as dictated by the matplotlib build-time options plus the
3074 user's matplotlibrc configuration file). Note that not all backends
3074 user's matplotlibrc configuration file). Note that not all backends
3075 make sense in all contexts, for example a terminal ipython can't
3075 make sense in all contexts, for example a terminal ipython can't
3076 display figures inline.
3076 display figures inline.
3077 import_all : optional, bool, default: True
3077 import_all : optional, bool, default: True
3078 Whether to do `from numpy import *` and `from pylab import *`
3078 Whether to do `from numpy import *` and `from pylab import *`
3079 in addition to module imports.
3079 in addition to module imports.
3080 welcome_message : deprecated
3080 welcome_message : deprecated
3081 This argument is ignored, no welcome message will be displayed.
3081 This argument is ignored, no welcome message will be displayed.
3082 """
3082 """
3083 from IPython.core.pylabtools import import_pylab
3083 from IPython.core.pylabtools import import_pylab
3084
3084
3085 gui, backend = self.enable_matplotlib(gui)
3085 gui, backend = self.enable_matplotlib(gui)
3086
3086
3087 # We want to prevent the loading of pylab to pollute the user's
3087 # We want to prevent the loading of pylab to pollute the user's
3088 # namespace as shown by the %who* magics, so we execute the activation
3088 # namespace as shown by the %who* magics, so we execute the activation
3089 # code in an empty namespace, and we update *both* user_ns and
3089 # code in an empty namespace, and we update *both* user_ns and
3090 # user_ns_hidden with this information.
3090 # user_ns_hidden with this information.
3091 ns = {}
3091 ns = {}
3092 import_pylab(ns, import_all)
3092 import_pylab(ns, import_all)
3093 # warn about clobbered names
3093 # warn about clobbered names
3094 ignored = set(["__builtins__"])
3094 ignored = set(["__builtins__"])
3095 both = set(ns).intersection(self.user_ns).difference(ignored)
3095 both = set(ns).intersection(self.user_ns).difference(ignored)
3096 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3096 clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ]
3097 self.user_ns.update(ns)
3097 self.user_ns.update(ns)
3098 self.user_ns_hidden.update(ns)
3098 self.user_ns_hidden.update(ns)
3099 return gui, backend, clobbered
3099 return gui, backend, clobbered
3100
3100
3101 #-------------------------------------------------------------------------
3101 #-------------------------------------------------------------------------
3102 # Utilities
3102 # Utilities
3103 #-------------------------------------------------------------------------
3103 #-------------------------------------------------------------------------
3104
3104
3105 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3105 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3106 """Expand python variables in a string.
3106 """Expand python variables in a string.
3107
3107
3108 The depth argument indicates how many frames above the caller should
3108 The depth argument indicates how many frames above the caller should
3109 be walked to look for the local namespace where to expand variables.
3109 be walked to look for the local namespace where to expand variables.
3110
3110
3111 The global namespace for expansion is always the user's interactive
3111 The global namespace for expansion is always the user's interactive
3112 namespace.
3112 namespace.
3113 """
3113 """
3114 ns = self.user_ns.copy()
3114 ns = self.user_ns.copy()
3115 try:
3115 try:
3116 frame = sys._getframe(depth+1)
3116 frame = sys._getframe(depth+1)
3117 except ValueError:
3117 except ValueError:
3118 # This is thrown if there aren't that many frames on the stack,
3118 # This is thrown if there aren't that many frames on the stack,
3119 # e.g. if a script called run_line_magic() directly.
3119 # e.g. if a script called run_line_magic() directly.
3120 pass
3120 pass
3121 else:
3121 else:
3122 ns.update(frame.f_locals)
3122 ns.update(frame.f_locals)
3123
3123
3124 try:
3124 try:
3125 # We have to use .vformat() here, because 'self' is a valid and common
3125 # We have to use .vformat() here, because 'self' is a valid and common
3126 # name, and expanding **ns for .format() would make it collide with
3126 # name, and expanding **ns for .format() would make it collide with
3127 # the 'self' argument of the method.
3127 # the 'self' argument of the method.
3128 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3128 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3129 except Exception:
3129 except Exception:
3130 # if formatter couldn't format, just let it go untransformed
3130 # if formatter couldn't format, just let it go untransformed
3131 pass
3131 pass
3132 return cmd
3132 return cmd
3133
3133
3134 def mktempfile(self, data=None, prefix='ipython_edit_'):
3134 def mktempfile(self, data=None, prefix='ipython_edit_'):
3135 """Make a new tempfile and return its filename.
3135 """Make a new tempfile and return its filename.
3136
3136
3137 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3137 This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp),
3138 but it registers the created filename internally so ipython cleans it up
3138 but it registers the created filename internally so ipython cleans it up
3139 at exit time.
3139 at exit time.
3140
3140
3141 Optional inputs:
3141 Optional inputs:
3142
3142
3143 - data(None): if data is given, it gets written out to the temp file
3143 - data(None): if data is given, it gets written out to the temp file
3144 immediately, and the file is closed again."""
3144 immediately, and the file is closed again."""
3145
3145
3146 dirname = tempfile.mkdtemp(prefix=prefix)
3146 dirname = tempfile.mkdtemp(prefix=prefix)
3147 self.tempdirs.append(dirname)
3147 self.tempdirs.append(dirname)
3148
3148
3149 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3149 handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname)
3150 os.close(handle) # On Windows, there can only be one open handle on a file
3150 os.close(handle) # On Windows, there can only be one open handle on a file
3151 self.tempfiles.append(filename)
3151 self.tempfiles.append(filename)
3152
3152
3153 if data:
3153 if data:
3154 tmp_file = open(filename,'w')
3154 tmp_file = open(filename,'w')
3155 tmp_file.write(data)
3155 tmp_file.write(data)
3156 tmp_file.close()
3156 tmp_file.close()
3157 return filename
3157 return filename
3158
3158
3159 # TODO: This should be removed when Term is refactored.
3159 # TODO: This should be removed when Term is refactored.
3160 def write(self,data):
3160 def write(self,data):
3161 """Write a string to the default output"""
3161 """Write a string to the default output"""
3162 io.stdout.write(data)
3162 io.stdout.write(data)
3163
3163
3164 # TODO: This should be removed when Term is refactored.
3164 # TODO: This should be removed when Term is refactored.
3165 def write_err(self,data):
3165 def write_err(self,data):
3166 """Write a string to the default error output"""
3166 """Write a string to the default error output"""
3167 io.stderr.write(data)
3167 io.stderr.write(data)
3168
3168
3169 def ask_yes_no(self, prompt, default=None):
3169 def ask_yes_no(self, prompt, default=None):
3170 if self.quiet:
3170 if self.quiet:
3171 return True
3171 return True
3172 return ask_yes_no(prompt,default)
3172 return ask_yes_no(prompt,default)
3173
3173
3174 def show_usage(self):
3174 def show_usage(self):
3175 """Show a usage message"""
3175 """Show a usage message"""
3176 page.page(IPython.core.usage.interactive_usage)
3176 page.page(IPython.core.usage.interactive_usage)
3177
3177
3178 def extract_input_lines(self, range_str, raw=False):
3178 def extract_input_lines(self, range_str, raw=False):
3179 """Return as a string a set of input history slices.
3179 """Return as a string a set of input history slices.
3180
3180
3181 Parameters
3181 Parameters
3182 ----------
3182 ----------
3183 range_str : string
3183 range_str : string
3184 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3184 The set of slices is given as a string, like "~5/6-~4/2 4:8 9",
3185 since this function is for use by magic functions which get their
3185 since this function is for use by magic functions which get their
3186 arguments as strings. The number before the / is the session
3186 arguments as strings. The number before the / is the session
3187 number: ~n goes n back from the current session.
3187 number: ~n goes n back from the current session.
3188
3188
3189 raw : bool, optional
3189 raw : bool, optional
3190 By default, the processed input is used. If this is true, the raw
3190 By default, the processed input is used. If this is true, the raw
3191 input history is used instead.
3191 input history is used instead.
3192
3192
3193 Notes
3193 Notes
3194 -----
3194 -----
3195
3195
3196 Slices can be described with two notations:
3196 Slices can be described with two notations:
3197
3197
3198 * ``N:M`` -> standard python form, means including items N...(M-1).
3198 * ``N:M`` -> standard python form, means including items N...(M-1).
3199 * ``N-M`` -> include items N..M (closed endpoint).
3199 * ``N-M`` -> include items N..M (closed endpoint).
3200 """
3200 """
3201 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3201 lines = self.history_manager.get_range_by_str(range_str, raw=raw)
3202 return "\n".join(x for _, _, x in lines)
3202 return "\n".join(x for _, _, x in lines)
3203
3203
3204 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3204 def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
3205 """Get a code string from history, file, url, or a string or macro.
3205 """Get a code string from history, file, url, or a string or macro.
3206
3206
3207 This is mainly used by magic functions.
3207 This is mainly used by magic functions.
3208
3208
3209 Parameters
3209 Parameters
3210 ----------
3210 ----------
3211
3211
3212 target : str
3212 target : str
3213
3213
3214 A string specifying code to retrieve. This will be tried respectively
3214 A string specifying code to retrieve. This will be tried respectively
3215 as: ranges of input history (see %history for syntax), url,
3215 as: ranges of input history (see %history for syntax), url,
3216 correspnding .py file, filename, or an expression evaluating to a
3216 correspnding .py file, filename, or an expression evaluating to a
3217 string or Macro in the user namespace.
3217 string or Macro in the user namespace.
3218
3218
3219 raw : bool
3219 raw : bool
3220 If true (default), retrieve raw history. Has no effect on the other
3220 If true (default), retrieve raw history. Has no effect on the other
3221 retrieval mechanisms.
3221 retrieval mechanisms.
3222
3222
3223 py_only : bool (default False)
3223 py_only : bool (default False)
3224 Only try to fetch python code, do not try alternative methods to decode file
3224 Only try to fetch python code, do not try alternative methods to decode file
3225 if unicode fails.
3225 if unicode fails.
3226
3226
3227 Returns
3227 Returns
3228 -------
3228 -------
3229 A string of code.
3229 A string of code.
3230
3230
3231 ValueError is raised if nothing is found, and TypeError if it evaluates
3231 ValueError is raised if nothing is found, and TypeError if it evaluates
3232 to an object of another type. In each case, .args[0] is a printable
3232 to an object of another type. In each case, .args[0] is a printable
3233 message.
3233 message.
3234 """
3234 """
3235 code = self.extract_input_lines(target, raw=raw) # Grab history
3235 code = self.extract_input_lines(target, raw=raw) # Grab history
3236 if code:
3236 if code:
3237 return code
3237 return code
3238 utarget = unquote_filename(target)
3238 utarget = unquote_filename(target)
3239 try:
3239 try:
3240 if utarget.startswith(('http://', 'https://')):
3240 if utarget.startswith(('http://', 'https://')):
3241 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3241 return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie)
3242 except UnicodeDecodeError:
3242 except UnicodeDecodeError:
3243 if not py_only :
3243 if not py_only :
3244 # Deferred import
3244 # Deferred import
3245 try:
3245 try:
3246 from urllib.request import urlopen # Py3
3246 from urllib.request import urlopen # Py3
3247 except ImportError:
3247 except ImportError:
3248 from urllib import urlopen
3248 from urllib import urlopen
3249 response = urlopen(target)
3249 response = urlopen(target)
3250 return response.read().decode('latin1')
3250 return response.read().decode('latin1')
3251 raise ValueError(("'%s' seem to be unreadable.") % utarget)
3251 raise ValueError(("'%s' seem to be unreadable.") % utarget)
3252
3252
3253 potential_target = [target]
3253 potential_target = [target]
3254 try :
3254 try :
3255 potential_target.insert(0,get_py_filename(target))
3255 potential_target.insert(0,get_py_filename(target))
3256 except IOError:
3256 except IOError:
3257 pass
3257 pass
3258
3258
3259 for tgt in potential_target :
3259 for tgt in potential_target :
3260 if os.path.isfile(tgt): # Read file
3260 if os.path.isfile(tgt): # Read file
3261 try :
3261 try :
3262 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3262 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3263 except UnicodeDecodeError :
3263 except UnicodeDecodeError :
3264 if not py_only :
3264 if not py_only :
3265 with io_open(tgt,'r', encoding='latin1') as f :
3265 with io_open(tgt,'r', encoding='latin1') as f :
3266 return f.read()
3266 return f.read()
3267 raise ValueError(("'%s' seem to be unreadable.") % target)
3267 raise ValueError(("'%s' seem to be unreadable.") % target)
3268 elif os.path.isdir(os.path.expanduser(tgt)):
3268 elif os.path.isdir(os.path.expanduser(tgt)):
3269 raise ValueError("'%s' is a directory, not a regular file." % target)
3269 raise ValueError("'%s' is a directory, not a regular file." % target)
3270
3270
3271 if search_ns:
3271 if search_ns:
3272 # Inspect namespace to load object source
3272 # Inspect namespace to load object source
3273 object_info = self.object_inspect(target, detail_level=1)
3273 object_info = self.object_inspect(target, detail_level=1)
3274 if object_info['found'] and object_info['source']:
3274 if object_info['found'] and object_info['source']:
3275 return object_info['source']
3275 return object_info['source']
3276
3276
3277 try: # User namespace
3277 try: # User namespace
3278 codeobj = eval(target, self.user_ns)
3278 codeobj = eval(target, self.user_ns)
3279 except Exception:
3279 except Exception:
3280 raise ValueError(("'%s' was not found in history, as a file, url, "
3280 raise ValueError(("'%s' was not found in history, as a file, url, "
3281 "nor in the user namespace.") % target)
3281 "nor in the user namespace.") % target)
3282
3282
3283 if isinstance(codeobj, string_types):
3283 if isinstance(codeobj, string_types):
3284 return codeobj
3284 return codeobj
3285 elif isinstance(codeobj, Macro):
3285 elif isinstance(codeobj, Macro):
3286 return codeobj.value
3286 return codeobj.value
3287
3287
3288 raise TypeError("%s is neither a string nor a macro." % target,
3288 raise TypeError("%s is neither a string nor a macro." % target,
3289 codeobj)
3289 codeobj)
3290
3290
3291 #-------------------------------------------------------------------------
3291 #-------------------------------------------------------------------------
3292 # Things related to IPython exiting
3292 # Things related to IPython exiting
3293 #-------------------------------------------------------------------------
3293 #-------------------------------------------------------------------------
3294 def atexit_operations(self):
3294 def atexit_operations(self):
3295 """This will be executed at the time of exit.
3295 """This will be executed at the time of exit.
3296
3296
3297 Cleanup operations and saving of persistent data that is done
3297 Cleanup operations and saving of persistent data that is done
3298 unconditionally by IPython should be performed here.
3298 unconditionally by IPython should be performed here.
3299
3299
3300 For things that may depend on startup flags or platform specifics (such
3300 For things that may depend on startup flags or platform specifics (such
3301 as having readline or not), register a separate atexit function in the
3301 as having readline or not), register a separate atexit function in the
3302 code that has the appropriate information, rather than trying to
3302 code that has the appropriate information, rather than trying to
3303 clutter
3303 clutter
3304 """
3304 """
3305 # Close the history session (this stores the end time and line count)
3305 # Close the history session (this stores the end time and line count)
3306 # this must be *before* the tempfile cleanup, in case of temporary
3306 # this must be *before* the tempfile cleanup, in case of temporary
3307 # history db
3307 # history db
3308 self.history_manager.end_session()
3308 self.history_manager.end_session()
3309
3309
3310 # Cleanup all tempfiles and folders left around
3310 # Cleanup all tempfiles and folders left around
3311 for tfile in self.tempfiles:
3311 for tfile in self.tempfiles:
3312 try:
3312 try:
3313 os.unlink(tfile)
3313 os.unlink(tfile)
3314 except OSError:
3314 except OSError:
3315 pass
3315 pass
3316
3316
3317 for tdir in self.tempdirs:
3317 for tdir in self.tempdirs:
3318 try:
3318 try:
3319 os.rmdir(tdir)
3319 os.rmdir(tdir)
3320 except OSError:
3320 except OSError:
3321 pass
3321 pass
3322
3322
3323 # Clear all user namespaces to release all references cleanly.
3323 # Clear all user namespaces to release all references cleanly.
3324 self.reset(new_session=False)
3324 self.reset(new_session=False)
3325
3325
3326 # Run user hooks
3326 # Run user hooks
3327 self.hooks.shutdown_hook()
3327 self.hooks.shutdown_hook()
3328
3328
3329 def cleanup(self):
3329 def cleanup(self):
3330 self.restore_sys_module_state()
3330 self.restore_sys_module_state()
3331
3331
3332
3332
3333 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3333 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3334 """An abstract base class for InteractiveShell."""
3334 """An abstract base class for InteractiveShell."""
3335
3335
3336 InteractiveShellABC.register(InteractiveShell)
3336 InteractiveShellABC.register(InteractiveShell)
@@ -1,573 +1,573 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3 /**
3 /**
4 *
4 *
5 *
5 *
6 * @module codecell
6 * @module codecell
7 * @namespace codecell
7 * @namespace codecell
8 * @class CodeCell
8 * @class CodeCell
9 */
9 */
10
10
11
11
12 define([
12 define([
13 'base/js/namespace',
13 'base/js/namespace',
14 'jquery',
14 'jquery',
15 'base/js/utils',
15 'base/js/utils',
16 'base/js/keyboard',
16 'base/js/keyboard',
17 'notebook/js/cell',
17 'notebook/js/cell',
18 'notebook/js/outputarea',
18 'notebook/js/outputarea',
19 'notebook/js/completer',
19 'notebook/js/completer',
20 'notebook/js/celltoolbar',
20 'notebook/js/celltoolbar',
21 'codemirror/lib/codemirror',
21 'codemirror/lib/codemirror',
22 'codemirror/mode/python/python',
22 'codemirror/mode/python/python',
23 'notebook/js/codemirror-ipython'
23 'notebook/js/codemirror-ipython'
24 ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) {
24 ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) {
25 "use strict";
25 "use strict";
26
26
27 var Cell = cell.Cell;
27 var Cell = cell.Cell;
28
28
29 /* local util for codemirror */
29 /* local util for codemirror */
30 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
30 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
31
31
32 /**
32 /**
33 *
33 *
34 * function to delete until previous non blanking space character
34 * function to delete until previous non blanking space character
35 * or first multiple of 4 tabstop.
35 * or first multiple of 4 tabstop.
36 * @private
36 * @private
37 */
37 */
38 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
38 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
39 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
39 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
40 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
40 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
41 var cur = cm.getCursor(), line = cm.getLine(cur.line);
41 var cur = cm.getCursor(), line = cm.getLine(cur.line);
42 var tabsize = cm.getOption('tabSize');
42 var tabsize = cm.getOption('tabSize');
43 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
43 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
44 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
44 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
45 var select = cm.getRange(from,cur);
45 var select = cm.getRange(from,cur);
46 if( select.match(/^\ +$/) !== null){
46 if( select.match(/^\ +$/) !== null){
47 cm.replaceRange("",from,cur);
47 cm.replaceRange("",from,cur);
48 } else {
48 } else {
49 cm.deleteH(-1,"char");
49 cm.deleteH(-1,"char");
50 }
50 }
51 };
51 };
52
52
53 var keycodes = keyboard.keycodes;
53 var keycodes = keyboard.keycodes;
54
54
55 var CodeCell = function (kernel, options) {
55 var CodeCell = function (kernel, options) {
56 /**
56 /**
57 * Constructor
57 * Constructor
58 *
58 *
59 * A Cell conceived to write code.
59 * A Cell conceived to write code.
60 *
60 *
61 * Parameters:
61 * Parameters:
62 * kernel: Kernel instance
62 * kernel: Kernel instance
63 * The kernel doesn't have to be set at creation time, in that case
63 * The kernel doesn't have to be set at creation time, in that case
64 * it will be null and set_kernel has to be called later.
64 * it will be null and set_kernel has to be called later.
65 * options: dictionary
65 * options: dictionary
66 * Dictionary of keyword arguments.
66 * Dictionary of keyword arguments.
67 * events: $(Events) instance
67 * events: $(Events) instance
68 * config: dictionary
68 * config: dictionary
69 * keyboard_manager: KeyboardManager instance
69 * keyboard_manager: KeyboardManager instance
70 * notebook: Notebook instance
70 * notebook: Notebook instance
71 * tooltip: Tooltip instance
71 * tooltip: Tooltip instance
72 */
72 */
73 this.kernel = kernel || null;
73 this.kernel = kernel || null;
74 this.notebook = options.notebook;
74 this.notebook = options.notebook;
75 this.collapsed = false;
75 this.collapsed = false;
76 this.events = options.events;
76 this.events = options.events;
77 this.tooltip = options.tooltip;
77 this.tooltip = options.tooltip;
78 this.config = options.config;
78 this.config = options.config;
79
79
80 // create all attributed in constructor function
80 // create all attributed in constructor function
81 // even if null for V8 VM optimisation
81 // even if null for V8 VM optimisation
82 this.input_prompt_number = null;
82 this.input_prompt_number = null;
83 this.celltoolbar = null;
83 this.celltoolbar = null;
84 this.output_area = null;
84 this.output_area = null;
85 // Keep a stack of the 'active' output areas (where active means the
85 // Keep a stack of the 'active' output areas (where active means the
86 // output area that recieves output). When a user activates an output
86 // output area that recieves output). When a user activates an output
87 // area, it gets pushed to the stack. Then, when the output area is
87 // area, it gets pushed to the stack. Then, when the output area is
88 // deactivated, it's popped from the stack. When the stack is empty,
88 // deactivated, it's popped from the stack. When the stack is empty,
89 // the cell's output area is used.
89 // the cell's output area is used.
90 this.active_output_areas = [];
90 this.active_output_areas = [];
91 var that = this;
91 var that = this;
92 Object.defineProperty(this, 'active_output_area', {
92 Object.defineProperty(this, 'active_output_area', {
93 get: function() {
93 get: function() {
94 if (that.active_output_areas && that.active_output_areas.length > 0) {
94 if (that.active_output_areas && that.active_output_areas.length > 0) {
95 return that.active_output_areas[that.active_output_areas.length-1];
95 return that.active_output_areas[that.active_output_areas.length-1];
96 } else {
96 } else {
97 return that.output_area;
97 return that.output_area;
98 }
98 }
99 },
99 },
100 });
100 });
101
101
102 this.last_msg_id = null;
102 this.last_msg_id = null;
103 this.completer = null;
103 this.completer = null;
104
104
105
105
106 var config = utils.mergeopt(CodeCell, this.config);
106 var config = utils.mergeopt(CodeCell, this.config);
107 Cell.apply(this,[{
107 Cell.apply(this,[{
108 config: config,
108 config: config,
109 keyboard_manager: options.keyboard_manager,
109 keyboard_manager: options.keyboard_manager,
110 events: this.events}]);
110 events: this.events}]);
111
111
112 // Attributes we want to override in this subclass.
112 // Attributes we want to override in this subclass.
113 this.cell_type = "code";
113 this.cell_type = "code";
114 this.element.focusout(
114 this.element.focusout(
115 function() { that.auto_highlight(); }
115 function() { that.auto_highlight(); }
116 );
116 );
117 };
117 };
118
118
119 CodeCell.options_default = {
119 CodeCell.options_default = {
120 cm_config : {
120 cm_config : {
121 extraKeys: {
121 extraKeys: {
122 "Tab" : "indentMore",
122 "Tab" : "indentMore",
123 "Shift-Tab" : "indentLess",
123 "Shift-Tab" : "indentLess",
124 "Backspace" : "delSpaceToPrevTabStop",
124 "Backspace" : "delSpaceToPrevTabStop",
125 "Cmd-/" : "toggleComment",
125 "Cmd-/" : "toggleComment",
126 "Ctrl-/" : "toggleComment"
126 "Ctrl-/" : "toggleComment"
127 },
127 },
128 mode: 'ipython',
128 mode: 'ipython',
129 theme: 'ipython',
129 theme: 'ipython',
130 matchBrackets: true
130 matchBrackets: true
131 }
131 }
132 };
132 };
133
133
134 CodeCell.msg_cells = {};
134 CodeCell.msg_cells = {};
135
135
136 CodeCell.prototype = Object.create(Cell.prototype);
136 CodeCell.prototype = Object.create(Cell.prototype);
137
137
138 /**
138 /**
139 * @method push_output_area
139 * @method push_output_area
140 */
140 */
141 CodeCell.prototype.push_output_area = function (output_area) {
141 CodeCell.prototype.push_output_area = function (output_area) {
142 this.active_output_areas.push(output_area);
142 this.active_output_areas.push(output_area);
143 };
143 };
144
144
145 /**
145 /**
146 * @method pop_output_area
146 * @method pop_output_area
147 */
147 */
148 CodeCell.prototype.pop_output_area = function (output_area) {
148 CodeCell.prototype.pop_output_area = function (output_area) {
149 var index = this.active_output_areas.lastIndexOf(output_area);
149 var index = this.active_output_areas.lastIndexOf(output_area);
150 if (index > -1) {
150 if (index > -1) {
151 this.active_output_areas.splice(index, 1);
151 this.active_output_areas.splice(index, 1);
152 }
152 }
153 };
153 };
154
154
155 /**
155 /**
156 * @method auto_highlight
156 * @method auto_highlight
157 */
157 */
158 CodeCell.prototype.auto_highlight = function () {
158 CodeCell.prototype.auto_highlight = function () {
159 this._auto_highlight(this.config.cell_magic_highlight);
159 this._auto_highlight(this.config.cell_magic_highlight);
160 };
160 };
161
161
162 /** @method create_element */
162 /** @method create_element */
163 CodeCell.prototype.create_element = function () {
163 CodeCell.prototype.create_element = function () {
164 Cell.prototype.create_element.apply(this, arguments);
164 Cell.prototype.create_element.apply(this, arguments);
165
165
166 var cell = $('<div></div>').addClass('cell code_cell');
166 var cell = $('<div></div>').addClass('cell code_cell');
167 cell.attr('tabindex','2');
167 cell.attr('tabindex','2');
168
168
169 var input = $('<div></div>').addClass('input');
169 var input = $('<div></div>').addClass('input');
170 var prompt = $('<div/>').addClass('prompt input_prompt');
170 var prompt = $('<div/>').addClass('prompt input_prompt');
171 var inner_cell = $('<div/>').addClass('inner_cell');
171 var inner_cell = $('<div/>').addClass('inner_cell');
172 this.celltoolbar = new celltoolbar.CellToolbar({
172 this.celltoolbar = new celltoolbar.CellToolbar({
173 cell: this,
173 cell: this,
174 notebook: this.notebook});
174 notebook: this.notebook});
175 inner_cell.append(this.celltoolbar.element);
175 inner_cell.append(this.celltoolbar.element);
176 var input_area = $('<div/>').addClass('input_area');
176 var input_area = $('<div/>').addClass('input_area');
177 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
177 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
178 this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this))
178 this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this))
179 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
179 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
180 inner_cell.append(input_area);
180 inner_cell.append(input_area);
181 input.append(prompt).append(inner_cell);
181 input.append(prompt).append(inner_cell);
182
182
183 var widget_area = $('<div/>')
183 var widget_area = $('<div/>')
184 .addClass('widget-area')
184 .addClass('widget-area')
185 .hide();
185 .hide();
186 this.widget_area = widget_area;
186 this.widget_area = widget_area;
187 var widget_prompt = $('<div/>')
187 var widget_prompt = $('<div/>')
188 .addClass('prompt')
188 .addClass('prompt')
189 .appendTo(widget_area);
189 .appendTo(widget_area);
190 var widget_subarea = $('<div/>')
190 var widget_subarea = $('<div/>')
191 .addClass('widget-subarea')
191 .addClass('widget-subarea')
192 .appendTo(widget_area);
192 .appendTo(widget_area);
193 this.widget_subarea = widget_subarea;
193 this.widget_subarea = widget_subarea;
194 var widget_clear_buton = $('<button />')
194 var widget_clear_buton = $('<button />')
195 .addClass('close')
195 .addClass('close')
196 .html('&times;')
196 .html('&times;')
197 .click(function() {
197 .click(function() {
198 widget_area.slideUp('', function(){ widget_subarea.html(''); });
198 widget_area.slideUp('', function(){ widget_subarea.html(''); });
199 })
199 })
200 .appendTo(widget_prompt);
200 .appendTo(widget_prompt);
201
201
202 var output = $('<div></div>');
202 var output = $('<div></div>');
203 cell.append(input).append(widget_area).append(output);
203 cell.append(input).append(widget_area).append(output);
204 this.element = cell;
204 this.element = cell;
205 this.output_area = new outputarea.OutputArea({
205 this.output_area = new outputarea.OutputArea({
206 selector: output,
206 selector: output,
207 prompt_area: true,
207 prompt_area: true,
208 events: this.events,
208 events: this.events,
209 keyboard_manager: this.keyboard_manager});
209 keyboard_manager: this.keyboard_manager});
210 this.completer = new completer.Completer(this, this.events);
210 this.completer = new completer.Completer(this, this.events);
211 };
211 };
212
212
213 /** @method bind_events */
213 /** @method bind_events */
214 CodeCell.prototype.bind_events = function () {
214 CodeCell.prototype.bind_events = function () {
215 Cell.prototype.bind_events.apply(this);
215 Cell.prototype.bind_events.apply(this);
216 var that = this;
216 var that = this;
217
217
218 this.element.focusout(
218 this.element.focusout(
219 function() { that.auto_highlight(); }
219 function() { that.auto_highlight(); }
220 );
220 );
221 };
221 };
222
222
223
223
224 /**
224 /**
225 * This method gets called in CodeMirror's onKeyDown/onKeyPress
225 * This method gets called in CodeMirror's onKeyDown/onKeyPress
226 * handlers and is used to provide custom key handling. Its return
226 * handlers and is used to provide custom key handling. Its return
227 * value is used to determine if CodeMirror should ignore the event:
227 * value is used to determine if CodeMirror should ignore the event:
228 * true = ignore, false = don't ignore.
228 * true = ignore, false = don't ignore.
229 * @method handle_codemirror_keyevent
229 * @method handle_codemirror_keyevent
230 */
230 */
231
231
232 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
232 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
233
233
234 var that = this;
234 var that = this;
235 // whatever key is pressed, first, cancel the tooltip request before
235 // whatever key is pressed, first, cancel the tooltip request before
236 // they are sent, and remove tooltip if any, except for tab again
236 // they are sent, and remove tooltip if any, except for tab again
237 var tooltip_closed = null;
237 var tooltip_closed = null;
238 if (event.type === 'keydown' && event.which != keycodes.tab ) {
238 if (event.type === 'keydown' && event.which != keycodes.tab ) {
239 tooltip_closed = this.tooltip.remove_and_cancel_tooltip();
239 tooltip_closed = this.tooltip.remove_and_cancel_tooltip();
240 }
240 }
241
241
242 var cur = editor.getCursor();
242 var cur = editor.getCursor();
243 if (event.keyCode === keycodes.enter){
243 if (event.keyCode === keycodes.enter){
244 this.auto_highlight();
244 this.auto_highlight();
245 }
245 }
246
246
247 if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) {
247 if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) {
248 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
248 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
249 // browser and keyboard layout !
249 // browser and keyboard layout !
250 // Pressing '(' , request tooltip, don't forget to reappend it
250 // Pressing '(' , request tooltip, don't forget to reappend it
251 // The second argument says to hide the tooltip if the docstring
251 // The second argument says to hide the tooltip if the docstring
252 // is actually empty
252 // is actually empty
253 this.tooltip.pending(that, true);
253 this.tooltip.pending(that, true);
254 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
254 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
255 // If tooltip is active, cancel it. The call to
255 // If tooltip is active, cancel it. The call to
256 // remove_and_cancel_tooltip above doesn't pass, force=true.
256 // remove_and_cancel_tooltip above doesn't pass, force=true.
257 // Because of this it won't actually close the tooltip
257 // Because of this it won't actually close the tooltip
258 // if it is in sticky mode. Thus, we have to check again if it is open
258 // if it is in sticky mode. Thus, we have to check again if it is open
259 // and close it with force=true.
259 // and close it with force=true.
260 if (!this.tooltip._hidden) {
260 if (!this.tooltip._hidden) {
261 this.tooltip.remove_and_cancel_tooltip(true);
261 this.tooltip.remove_and_cancel_tooltip(true);
262 }
262 }
263 // If we closed the tooltip, don't let CM or the global handlers
263 // If we closed the tooltip, don't let CM or the global handlers
264 // handle this event.
264 // handle this event.
265 event.codemirrorIgnore = true;
265 event.codemirrorIgnore = true;
266 event.preventDefault();
266 event.preventDefault();
267 return true;
267 return true;
268 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
268 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
269 if (editor.somethingSelected() || editor.getSelections().length !== 1){
269 if (editor.somethingSelected() || editor.getSelections().length !== 1){
270 var anchor = editor.getCursor("anchor");
270 var anchor = editor.getCursor("anchor");
271 var head = editor.getCursor("head");
271 var head = editor.getCursor("head");
272 if( anchor.line != head.line){
272 if( anchor.line != head.line){
273 return false;
273 return false;
274 }
274 }
275 }
275 }
276 this.tooltip.request(that);
276 this.tooltip.request(that);
277 event.codemirrorIgnore = true;
277 event.codemirrorIgnore = true;
278 event.preventDefault();
278 event.preventDefault();
279 return true;
279 return true;
280 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
280 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
281 // Tab completion.
281 // Tab completion.
282 this.tooltip.remove_and_cancel_tooltip();
282 this.tooltip.remove_and_cancel_tooltip();
283
283
284 // completion does not work on multicursor, it might be possible though in some cases
284 // completion does not work on multicursor, it might be possible though in some cases
285 if (editor.somethingSelected() || editor.getSelections().length > 1) {
285 if (editor.somethingSelected() || editor.getSelections().length > 1) {
286 return false;
286 return false;
287 }
287 }
288 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
288 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
289 if (pre_cursor.trim() === "") {
289 if (pre_cursor.trim() === "") {
290 // Don't autocomplete if the part of the line before the cursor
290 // Don't autocomplete if the part of the line before the cursor
291 // is empty. In this case, let CodeMirror handle indentation.
291 // is empty. In this case, let CodeMirror handle indentation.
292 return false;
292 return false;
293 } else {
293 } else {
294 event.codemirrorIgnore = true;
294 event.codemirrorIgnore = true;
295 event.preventDefault();
295 event.preventDefault();
296 this.completer.startCompletion();
296 this.completer.startCompletion();
297 return true;
297 return true;
298 }
298 }
299 }
299 }
300
300
301 // keyboard event wasn't one of those unique to code cells, let's see
301 // keyboard event wasn't one of those unique to code cells, let's see
302 // if it's one of the generic ones (i.e. check edit mode shortcuts)
302 // if it's one of the generic ones (i.e. check edit mode shortcuts)
303 return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]);
303 return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]);
304 };
304 };
305
305
306 // Kernel related calls.
306 // Kernel related calls.
307
307
308 CodeCell.prototype.set_kernel = function (kernel) {
308 CodeCell.prototype.set_kernel = function (kernel) {
309 this.kernel = kernel;
309 this.kernel = kernel;
310 };
310 };
311
311
312 /**
312 /**
313 * Execute current code cell to the kernel
313 * Execute current code cell to the kernel
314 * @method execute
314 * @method execute
315 */
315 */
316 CodeCell.prototype.execute = function () {
316 CodeCell.prototype.execute = function () {
317 if (!this.kernel || !this.kernel.is_connected()) {
317 if (!this.kernel || !this.kernel.is_connected()) {
318 console.log("Can't execute, kernel is not connected.");
318 console.log("Can't execute, kernel is not connected.");
319 return;
319 return;
320 }
320 }
321
321
322 this.active_output_area.clear_output();
322 this.active_output_area.clear_output();
323
323
324 // Clear widget area
324 // Clear widget area
325 this.widget_subarea.html('');
325 this.widget_subarea.html('');
326 this.widget_subarea.height('');
326 this.widget_subarea.height('');
327 this.widget_area.height('');
327 this.widget_area.height('');
328 this.widget_area.hide();
328 this.widget_area.hide();
329
329
330 this.set_input_prompt('*');
330 this.set_input_prompt('*');
331 this.element.addClass("running");
331 this.element.addClass("running");
332 if (this.last_msg_id) {
332 if (this.last_msg_id) {
333 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
333 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
334 }
334 }
335 var callbacks = this.get_callbacks();
335 var callbacks = this.get_callbacks();
336
336
337 var old_msg_id = this.last_msg_id;
337 var old_msg_id = this.last_msg_id;
338 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
338 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
339 if (old_msg_id) {
339 if (old_msg_id) {
340 delete CodeCell.msg_cells[old_msg_id];
340 delete CodeCell.msg_cells[old_msg_id];
341 }
341 }
342 CodeCell.msg_cells[this.last_msg_id] = this;
342 CodeCell.msg_cells[this.last_msg_id] = this;
343 this.render();
343 this.render();
344 this.events.trigger('execute.CodeCell', {cell: this});
344 this.events.trigger('execute.CodeCell', {cell: this});
345 };
345 };
346
346
347 /**
347 /**
348 * Construct the default callbacks for
348 * Construct the default callbacks for
349 * @method get_callbacks
349 * @method get_callbacks
350 */
350 */
351 CodeCell.prototype.get_callbacks = function () {
351 CodeCell.prototype.get_callbacks = function () {
352 var that = this;
352 var that = this;
353 return {
353 return {
354 shell : {
354 shell : {
355 reply : $.proxy(this._handle_execute_reply, this),
355 reply : $.proxy(this._handle_execute_reply, this),
356 payload : {
356 payload : {
357 set_next_input : $.proxy(this._handle_set_next_input, this),
357 set_next_input : $.proxy(this._handle_set_next_input, this),
358 page : $.proxy(this._open_with_pager, this)
358 page : $.proxy(this._open_with_pager, this)
359 }
359 }
360 },
360 },
361 iopub : {
361 iopub : {
362 output : function() {
362 output : function() {
363 that.active_output_area.handle_output.apply(that.active_output_area, arguments);
363 that.active_output_area.handle_output.apply(that.active_output_area, arguments);
364 },
364 },
365 clear_output : function() {
365 clear_output : function() {
366 that.active_output_area.handle_clear_output.apply(that.active_output_area, arguments);
366 that.active_output_area.handle_clear_output.apply(that.active_output_area, arguments);
367 },
367 },
368 },
368 },
369 input : $.proxy(this._handle_input_request, this)
369 input : $.proxy(this._handle_input_request, this)
370 };
370 };
371 };
371 };
372
372
373 CodeCell.prototype._open_with_pager = function (payload) {
373 CodeCell.prototype._open_with_pager = function (payload) {
374 this.events.trigger('open_with_text.Pager', payload);
374 this.events.trigger('open_with_text.Pager', payload);
375 };
375 };
376
376
377 /**
377 /**
378 * @method _handle_execute_reply
378 * @method _handle_execute_reply
379 * @private
379 * @private
380 */
380 */
381 CodeCell.prototype._handle_execute_reply = function (msg) {
381 CodeCell.prototype._handle_execute_reply = function (msg) {
382 this.set_input_prompt(msg.content.execution_count);
382 this.set_input_prompt(msg.content.execution_count);
383 this.element.removeClass("running");
383 this.element.removeClass("running");
384 this.events.trigger('set_dirty.Notebook', {value: true});
384 this.events.trigger('set_dirty.Notebook', {value: true});
385 };
385 };
386
386
387 /**
387 /**
388 * @method _handle_set_next_input
388 * @method _handle_set_next_input
389 * @private
389 * @private
390 */
390 */
391 CodeCell.prototype._handle_set_next_input = function (payload) {
391 CodeCell.prototype._handle_set_next_input = function (payload) {
392 var data = {'cell': this, 'text': payload.text};
392 var data = {'cell': this, 'text': payload.text, replace: payload.replace};
393 this.events.trigger('set_next_input.Notebook', data);
393 this.events.trigger('set_next_input.Notebook', data);
394 };
394 };
395
395
396 /**
396 /**
397 * @method _handle_input_request
397 * @method _handle_input_request
398 * @private
398 * @private
399 */
399 */
400 CodeCell.prototype._handle_input_request = function (msg) {
400 CodeCell.prototype._handle_input_request = function (msg) {
401 this.active_output_area.append_raw_input(msg);
401 this.active_output_area.append_raw_input(msg);
402 };
402 };
403
403
404
404
405 // Basic cell manipulation.
405 // Basic cell manipulation.
406
406
407 CodeCell.prototype.select = function () {
407 CodeCell.prototype.select = function () {
408 var cont = Cell.prototype.select.apply(this);
408 var cont = Cell.prototype.select.apply(this);
409 if (cont) {
409 if (cont) {
410 this.code_mirror.refresh();
410 this.code_mirror.refresh();
411 this.auto_highlight();
411 this.auto_highlight();
412 }
412 }
413 return cont;
413 return cont;
414 };
414 };
415
415
416 CodeCell.prototype.render = function () {
416 CodeCell.prototype.render = function () {
417 var cont = Cell.prototype.render.apply(this);
417 var cont = Cell.prototype.render.apply(this);
418 // Always execute, even if we are already in the rendered state
418 // Always execute, even if we are already in the rendered state
419 return cont;
419 return cont;
420 };
420 };
421
421
422 CodeCell.prototype.select_all = function () {
422 CodeCell.prototype.select_all = function () {
423 var start = {line: 0, ch: 0};
423 var start = {line: 0, ch: 0};
424 var nlines = this.code_mirror.lineCount();
424 var nlines = this.code_mirror.lineCount();
425 var last_line = this.code_mirror.getLine(nlines-1);
425 var last_line = this.code_mirror.getLine(nlines-1);
426 var end = {line: nlines-1, ch: last_line.length};
426 var end = {line: nlines-1, ch: last_line.length};
427 this.code_mirror.setSelection(start, end);
427 this.code_mirror.setSelection(start, end);
428 };
428 };
429
429
430
430
431 CodeCell.prototype.collapse_output = function () {
431 CodeCell.prototype.collapse_output = function () {
432 this.output_area.collapse();
432 this.output_area.collapse();
433 };
433 };
434
434
435
435
436 CodeCell.prototype.expand_output = function () {
436 CodeCell.prototype.expand_output = function () {
437 this.output_area.expand();
437 this.output_area.expand();
438 this.output_area.unscroll_area();
438 this.output_area.unscroll_area();
439 };
439 };
440
440
441 CodeCell.prototype.scroll_output = function () {
441 CodeCell.prototype.scroll_output = function () {
442 this.output_area.expand();
442 this.output_area.expand();
443 this.output_area.scroll_if_long();
443 this.output_area.scroll_if_long();
444 };
444 };
445
445
446 CodeCell.prototype.toggle_output = function () {
446 CodeCell.prototype.toggle_output = function () {
447 this.output_area.toggle_output();
447 this.output_area.toggle_output();
448 };
448 };
449
449
450 CodeCell.prototype.toggle_output_scroll = function () {
450 CodeCell.prototype.toggle_output_scroll = function () {
451 this.output_area.toggle_scroll();
451 this.output_area.toggle_scroll();
452 };
452 };
453
453
454
454
455 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
455 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
456 var ns;
456 var ns;
457 if (prompt_value === undefined || prompt_value === null) {
457 if (prompt_value === undefined || prompt_value === null) {
458 ns = "&nbsp;";
458 ns = "&nbsp;";
459 } else {
459 } else {
460 ns = encodeURIComponent(prompt_value);
460 ns = encodeURIComponent(prompt_value);
461 }
461 }
462 return 'In&nbsp;[' + ns + ']:';
462 return 'In&nbsp;[' + ns + ']:';
463 };
463 };
464
464
465 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
465 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
466 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
466 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
467 for(var i=1; i < lines_number; i++) {
467 for(var i=1; i < lines_number; i++) {
468 html.push(['...:']);
468 html.push(['...:']);
469 }
469 }
470 return html.join('<br/>');
470 return html.join('<br/>');
471 };
471 };
472
472
473 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
473 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
474
474
475
475
476 CodeCell.prototype.set_input_prompt = function (number) {
476 CodeCell.prototype.set_input_prompt = function (number) {
477 var nline = 1;
477 var nline = 1;
478 if (this.code_mirror !== undefined) {
478 if (this.code_mirror !== undefined) {
479 nline = this.code_mirror.lineCount();
479 nline = this.code_mirror.lineCount();
480 }
480 }
481 this.input_prompt_number = number;
481 this.input_prompt_number = number;
482 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
482 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
483 // This HTML call is okay because the user contents are escaped.
483 // This HTML call is okay because the user contents are escaped.
484 this.element.find('div.input_prompt').html(prompt_html);
484 this.element.find('div.input_prompt').html(prompt_html);
485 };
485 };
486
486
487
487
488 CodeCell.prototype.clear_input = function () {
488 CodeCell.prototype.clear_input = function () {
489 this.code_mirror.setValue('');
489 this.code_mirror.setValue('');
490 };
490 };
491
491
492
492
493 CodeCell.prototype.get_text = function () {
493 CodeCell.prototype.get_text = function () {
494 return this.code_mirror.getValue();
494 return this.code_mirror.getValue();
495 };
495 };
496
496
497
497
498 CodeCell.prototype.set_text = function (code) {
498 CodeCell.prototype.set_text = function (code) {
499 return this.code_mirror.setValue(code);
499 return this.code_mirror.setValue(code);
500 };
500 };
501
501
502
502
503 CodeCell.prototype.clear_output = function (wait) {
503 CodeCell.prototype.clear_output = function (wait) {
504 this.active_output_area.clear_output(wait);
504 this.active_output_area.clear_output(wait);
505 this.set_input_prompt();
505 this.set_input_prompt();
506 };
506 };
507
507
508
508
509 // JSON serialization
509 // JSON serialization
510
510
511 CodeCell.prototype.fromJSON = function (data) {
511 CodeCell.prototype.fromJSON = function (data) {
512 Cell.prototype.fromJSON.apply(this, arguments);
512 Cell.prototype.fromJSON.apply(this, arguments);
513 if (data.cell_type === 'code') {
513 if (data.cell_type === 'code') {
514 if (data.source !== undefined) {
514 if (data.source !== undefined) {
515 this.set_text(data.source);
515 this.set_text(data.source);
516 // make this value the starting point, so that we can only undo
516 // make this value the starting point, so that we can only undo
517 // to this state, instead of a blank cell
517 // to this state, instead of a blank cell
518 this.code_mirror.clearHistory();
518 this.code_mirror.clearHistory();
519 this.auto_highlight();
519 this.auto_highlight();
520 }
520 }
521 this.set_input_prompt(data.execution_count);
521 this.set_input_prompt(data.execution_count);
522 this.output_area.trusted = data.metadata.trusted || false;
522 this.output_area.trusted = data.metadata.trusted || false;
523 this.output_area.fromJSON(data.outputs);
523 this.output_area.fromJSON(data.outputs);
524 if (data.metadata.collapsed !== undefined) {
524 if (data.metadata.collapsed !== undefined) {
525 if (data.metadata.collapsed) {
525 if (data.metadata.collapsed) {
526 this.collapse_output();
526 this.collapse_output();
527 } else {
527 } else {
528 this.expand_output();
528 this.expand_output();
529 }
529 }
530 }
530 }
531 }
531 }
532 };
532 };
533
533
534
534
535 CodeCell.prototype.toJSON = function () {
535 CodeCell.prototype.toJSON = function () {
536 var data = Cell.prototype.toJSON.apply(this);
536 var data = Cell.prototype.toJSON.apply(this);
537 data.source = this.get_text();
537 data.source = this.get_text();
538 // is finite protect against undefined and '*' value
538 // is finite protect against undefined and '*' value
539 if (isFinite(this.input_prompt_number)) {
539 if (isFinite(this.input_prompt_number)) {
540 data.execution_count = this.input_prompt_number;
540 data.execution_count = this.input_prompt_number;
541 } else {
541 } else {
542 data.execution_count = null;
542 data.execution_count = null;
543 }
543 }
544 var outputs = this.output_area.toJSON();
544 var outputs = this.output_area.toJSON();
545 data.outputs = outputs;
545 data.outputs = outputs;
546 data.metadata.trusted = this.output_area.trusted;
546 data.metadata.trusted = this.output_area.trusted;
547 data.metadata.collapsed = this.output_area.collapsed;
547 data.metadata.collapsed = this.output_area.collapsed;
548 return data;
548 return data;
549 };
549 };
550
550
551 /**
551 /**
552 * handle cell level logic when a cell is unselected
552 * handle cell level logic when a cell is unselected
553 * @method unselect
553 * @method unselect
554 * @return is the action being taken
554 * @return is the action being taken
555 */
555 */
556 CodeCell.prototype.unselect = function () {
556 CodeCell.prototype.unselect = function () {
557 var cont = Cell.prototype.unselect.apply(this);
557 var cont = Cell.prototype.unselect.apply(this);
558 if (cont) {
558 if (cont) {
559 // When a code cell is usnelected, make sure that the corresponding
559 // When a code cell is usnelected, make sure that the corresponding
560 // tooltip and completer to that cell is closed.
560 // tooltip and completer to that cell is closed.
561 this.tooltip.remove_and_cancel_tooltip(true);
561 this.tooltip.remove_and_cancel_tooltip(true);
562 if (this.completer !== null) {
562 if (this.completer !== null) {
563 this.completer.close();
563 this.completer.close();
564 }
564 }
565 }
565 }
566 return cont;
566 return cont;
567 };
567 };
568
568
569 // Backwards compatability.
569 // Backwards compatability.
570 IPython.CodeCell = CodeCell;
570 IPython.CodeCell = CodeCell;
571
571
572 return {'CodeCell': CodeCell};
572 return {'CodeCell': CodeCell};
573 });
573 });
@@ -1,2518 +1,2522 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'base/js/dialog',
8 'base/js/dialog',
9 'notebook/js/cell',
9 'notebook/js/cell',
10 'notebook/js/textcell',
10 'notebook/js/textcell',
11 'notebook/js/codecell',
11 'notebook/js/codecell',
12 'services/sessions/session',
12 'services/sessions/session',
13 'notebook/js/celltoolbar',
13 'notebook/js/celltoolbar',
14 'components/marked/lib/marked',
14 'components/marked/lib/marked',
15 'codemirror/lib/codemirror',
15 'codemirror/lib/codemirror',
16 'codemirror/addon/runmode/runmode',
16 'codemirror/addon/runmode/runmode',
17 'notebook/js/mathjaxutils',
17 'notebook/js/mathjaxutils',
18 'base/js/keyboard',
18 'base/js/keyboard',
19 'notebook/js/tooltip',
19 'notebook/js/tooltip',
20 'notebook/js/celltoolbarpresets/default',
20 'notebook/js/celltoolbarpresets/default',
21 'notebook/js/celltoolbarpresets/rawcell',
21 'notebook/js/celltoolbarpresets/rawcell',
22 'notebook/js/celltoolbarpresets/slideshow',
22 'notebook/js/celltoolbarpresets/slideshow',
23 'notebook/js/scrollmanager'
23 'notebook/js/scrollmanager'
24 ], function (
24 ], function (
25 IPython,
25 IPython,
26 $,
26 $,
27 utils,
27 utils,
28 dialog,
28 dialog,
29 cellmod,
29 cellmod,
30 textcell,
30 textcell,
31 codecell,
31 codecell,
32 session,
32 session,
33 celltoolbar,
33 celltoolbar,
34 marked,
34 marked,
35 CodeMirror,
35 CodeMirror,
36 runMode,
36 runMode,
37 mathjaxutils,
37 mathjaxutils,
38 keyboard,
38 keyboard,
39 tooltip,
39 tooltip,
40 default_celltoolbar,
40 default_celltoolbar,
41 rawcell_celltoolbar,
41 rawcell_celltoolbar,
42 slideshow_celltoolbar,
42 slideshow_celltoolbar,
43 scrollmanager
43 scrollmanager
44 ) {
44 ) {
45 "use strict";
45 "use strict";
46
46
47 var Notebook = function (selector, options) {
47 var Notebook = function (selector, options) {
48 /**
48 /**
49 * Constructor
49 * Constructor
50 *
50 *
51 * A notebook contains and manages cells.
51 * A notebook contains and manages cells.
52 *
52 *
53 * Parameters:
53 * Parameters:
54 * selector: string
54 * selector: string
55 * options: dictionary
55 * options: dictionary
56 * Dictionary of keyword arguments.
56 * Dictionary of keyword arguments.
57 * events: $(Events) instance
57 * events: $(Events) instance
58 * keyboard_manager: KeyboardManager instance
58 * keyboard_manager: KeyboardManager instance
59 * contents: Contents instance
59 * contents: Contents instance
60 * save_widget: SaveWidget instance
60 * save_widget: SaveWidget instance
61 * config: dictionary
61 * config: dictionary
62 * base_url : string
62 * base_url : string
63 * notebook_path : string
63 * notebook_path : string
64 * notebook_name : string
64 * notebook_name : string
65 */
65 */
66 this.config = utils.mergeopt(Notebook, options.config);
66 this.config = utils.mergeopt(Notebook, options.config);
67 this.base_url = options.base_url;
67 this.base_url = options.base_url;
68 this.notebook_path = options.notebook_path;
68 this.notebook_path = options.notebook_path;
69 this.notebook_name = options.notebook_name;
69 this.notebook_name = options.notebook_name;
70 this.events = options.events;
70 this.events = options.events;
71 this.keyboard_manager = options.keyboard_manager;
71 this.keyboard_manager = options.keyboard_manager;
72 this.contents = options.contents;
72 this.contents = options.contents;
73 this.save_widget = options.save_widget;
73 this.save_widget = options.save_widget;
74 this.tooltip = new tooltip.Tooltip(this.events);
74 this.tooltip = new tooltip.Tooltip(this.events);
75 this.ws_url = options.ws_url;
75 this.ws_url = options.ws_url;
76 this._session_starting = false;
76 this._session_starting = false;
77 this.default_cell_type = this.config.default_cell_type || 'code';
77 this.default_cell_type = this.config.default_cell_type || 'code';
78
78
79 // Create default scroll manager.
79 // Create default scroll manager.
80 this.scroll_manager = new scrollmanager.ScrollManager(this);
80 this.scroll_manager = new scrollmanager.ScrollManager(this);
81
81
82 // TODO: This code smells (and the other `= this` line a couple lines down)
82 // TODO: This code smells (and the other `= this` line a couple lines down)
83 // We need a better way to deal with circular instance references.
83 // We need a better way to deal with circular instance references.
84 this.keyboard_manager.notebook = this;
84 this.keyboard_manager.notebook = this;
85 this.save_widget.notebook = this;
85 this.save_widget.notebook = this;
86
86
87 mathjaxutils.init();
87 mathjaxutils.init();
88
88
89 if (marked) {
89 if (marked) {
90 marked.setOptions({
90 marked.setOptions({
91 gfm : true,
91 gfm : true,
92 tables: true,
92 tables: true,
93 // FIXME: probably want central config for CodeMirror theme when we have js config
93 // FIXME: probably want central config for CodeMirror theme when we have js config
94 langPrefix: "cm-s-ipython language-",
94 langPrefix: "cm-s-ipython language-",
95 highlight: function(code, lang, callback) {
95 highlight: function(code, lang, callback) {
96 if (!lang) {
96 if (!lang) {
97 // no language, no highlight
97 // no language, no highlight
98 if (callback) {
98 if (callback) {
99 callback(null, code);
99 callback(null, code);
100 return;
100 return;
101 } else {
101 } else {
102 return code;
102 return code;
103 }
103 }
104 }
104 }
105 utils.requireCodeMirrorMode(lang, function () {
105 utils.requireCodeMirrorMode(lang, function () {
106 var el = document.createElement("div");
106 var el = document.createElement("div");
107 var mode = CodeMirror.getMode({}, lang);
107 var mode = CodeMirror.getMode({}, lang);
108 if (!mode) {
108 if (!mode) {
109 console.log("No CodeMirror mode: " + lang);
109 console.log("No CodeMirror mode: " + lang);
110 callback(null, code);
110 callback(null, code);
111 return;
111 return;
112 }
112 }
113 try {
113 try {
114 CodeMirror.runMode(code, mode, el);
114 CodeMirror.runMode(code, mode, el);
115 callback(null, el.innerHTML);
115 callback(null, el.innerHTML);
116 } catch (err) {
116 } catch (err) {
117 console.log("Failed to highlight " + lang + " code", err);
117 console.log("Failed to highlight " + lang + " code", err);
118 callback(err, code);
118 callback(err, code);
119 }
119 }
120 }, function (err) {
120 }, function (err) {
121 console.log("No CodeMirror mode: " + lang);
121 console.log("No CodeMirror mode: " + lang);
122 callback(err, code);
122 callback(err, code);
123 });
123 });
124 }
124 }
125 });
125 });
126 }
126 }
127
127
128 this.element = $(selector);
128 this.element = $(selector);
129 this.element.scroll();
129 this.element.scroll();
130 this.element.data("notebook", this);
130 this.element.data("notebook", this);
131 this.next_prompt_number = 1;
131 this.next_prompt_number = 1;
132 this.session = null;
132 this.session = null;
133 this.kernel = null;
133 this.kernel = null;
134 this.clipboard = null;
134 this.clipboard = null;
135 this.undelete_backup = null;
135 this.undelete_backup = null;
136 this.undelete_index = null;
136 this.undelete_index = null;
137 this.undelete_below = false;
137 this.undelete_below = false;
138 this.paste_enabled = false;
138 this.paste_enabled = false;
139 this.writable = false;
139 this.writable = false;
140 // It is important to start out in command mode to match the intial mode
140 // It is important to start out in command mode to match the intial mode
141 // of the KeyboardManager.
141 // of the KeyboardManager.
142 this.mode = 'command';
142 this.mode = 'command';
143 this.set_dirty(false);
143 this.set_dirty(false);
144 this.metadata = {};
144 this.metadata = {};
145 this._checkpoint_after_save = false;
145 this._checkpoint_after_save = false;
146 this.last_checkpoint = null;
146 this.last_checkpoint = null;
147 this.checkpoints = [];
147 this.checkpoints = [];
148 this.autosave_interval = 0;
148 this.autosave_interval = 0;
149 this.autosave_timer = null;
149 this.autosave_timer = null;
150 // autosave *at most* every two minutes
150 // autosave *at most* every two minutes
151 this.minimum_autosave_interval = 120000;
151 this.minimum_autosave_interval = 120000;
152 this.notebook_name_blacklist_re = /[\/\\:]/;
152 this.notebook_name_blacklist_re = /[\/\\:]/;
153 this.nbformat = 4; // Increment this when changing the nbformat
153 this.nbformat = 4; // Increment this when changing the nbformat
154 this.nbformat_minor = this.current_nbformat_minor = 0; // Increment this when changing the nbformat
154 this.nbformat_minor = this.current_nbformat_minor = 0; // Increment this when changing the nbformat
155 this.codemirror_mode = 'ipython';
155 this.codemirror_mode = 'ipython';
156 this.create_elements();
156 this.create_elements();
157 this.bind_events();
157 this.bind_events();
158 this.kernel_selector = null;
158 this.kernel_selector = null;
159 this.dirty = null;
159 this.dirty = null;
160 this.trusted = null;
160 this.trusted = null;
161 this._fully_loaded = false;
161 this._fully_loaded = false;
162
162
163 // Trigger cell toolbar registration.
163 // Trigger cell toolbar registration.
164 default_celltoolbar.register(this);
164 default_celltoolbar.register(this);
165 rawcell_celltoolbar.register(this);
165 rawcell_celltoolbar.register(this);
166 slideshow_celltoolbar.register(this);
166 slideshow_celltoolbar.register(this);
167
167
168 // prevent assign to miss-typed properties.
168 // prevent assign to miss-typed properties.
169 Object.seal(this);
169 Object.seal(this);
170 };
170 };
171
171
172 Notebook.options_default = {
172 Notebook.options_default = {
173 // can be any cell type, or the special values of
173 // can be any cell type, or the special values of
174 // 'above', 'below', or 'selected' to get the value from another cell.
174 // 'above', 'below', or 'selected' to get the value from another cell.
175 Notebook: {
175 Notebook: {
176 default_cell_type: 'code'
176 default_cell_type: 'code'
177 }
177 }
178 };
178 };
179
179
180
180
181 /**
181 /**
182 * Create an HTML and CSS representation of the notebook.
182 * Create an HTML and CSS representation of the notebook.
183 *
183 *
184 * @method create_elements
184 * @method create_elements
185 */
185 */
186 Notebook.prototype.create_elements = function () {
186 Notebook.prototype.create_elements = function () {
187 var that = this;
187 var that = this;
188 this.element.attr('tabindex','-1');
188 this.element.attr('tabindex','-1');
189 this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
189 this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
190 // We add this end_space div to the end of the notebook div to:
190 // We add this end_space div to the end of the notebook div to:
191 // i) provide a margin between the last cell and the end of the notebook
191 // i) provide a margin between the last cell and the end of the notebook
192 // ii) to prevent the div from scrolling up when the last cell is being
192 // ii) to prevent the div from scrolling up when the last cell is being
193 // edited, but is too low on the page, which browsers will do automatically.
193 // edited, but is too low on the page, which browsers will do automatically.
194 var end_space = $('<div/>').addClass('end_space');
194 var end_space = $('<div/>').addClass('end_space');
195 end_space.dblclick(function (e) {
195 end_space.dblclick(function (e) {
196 var ncells = that.ncells();
196 var ncells = that.ncells();
197 that.insert_cell_below('code',ncells-1);
197 that.insert_cell_below('code',ncells-1);
198 });
198 });
199 this.element.append(this.container);
199 this.element.append(this.container);
200 this.container.append(end_space);
200 this.container.append(end_space);
201 };
201 };
202
202
203 /**
203 /**
204 * Bind JavaScript events: key presses and custom IPython events.
204 * Bind JavaScript events: key presses and custom IPython events.
205 *
205 *
206 * @method bind_events
206 * @method bind_events
207 */
207 */
208 Notebook.prototype.bind_events = function () {
208 Notebook.prototype.bind_events = function () {
209 var that = this;
209 var that = this;
210
210
211 this.events.on('set_next_input.Notebook', function (event, data) {
211 this.events.on('set_next_input.Notebook', function (event, data) {
212 if (data.replace) {
213 data.cell.set_text(data.text);
214 } else {
212 var index = that.find_cell_index(data.cell);
215 var index = that.find_cell_index(data.cell);
213 var new_cell = that.insert_cell_below('code',index);
216 var new_cell = that.insert_cell_below('code',index);
214 new_cell.set_text(data.text);
217 new_cell.set_text(data.text);
218 }
215 that.dirty = true;
219 that.dirty = true;
216 });
220 });
217
221
218 this.events.on('unrecognized_cell.Cell', function () {
222 this.events.on('unrecognized_cell.Cell', function () {
219 that.warn_nbformat_minor();
223 that.warn_nbformat_minor();
220 });
224 });
221
225
222 this.events.on('unrecognized_output.OutputArea', function () {
226 this.events.on('unrecognized_output.OutputArea', function () {
223 that.warn_nbformat_minor();
227 that.warn_nbformat_minor();
224 });
228 });
225
229
226 this.events.on('set_dirty.Notebook', function (event, data) {
230 this.events.on('set_dirty.Notebook', function (event, data) {
227 that.dirty = data.value;
231 that.dirty = data.value;
228 });
232 });
229
233
230 this.events.on('trust_changed.Notebook', function (event, trusted) {
234 this.events.on('trust_changed.Notebook', function (event, trusted) {
231 that.trusted = trusted;
235 that.trusted = trusted;
232 });
236 });
233
237
234 this.events.on('select.Cell', function (event, data) {
238 this.events.on('select.Cell', function (event, data) {
235 var index = that.find_cell_index(data.cell);
239 var index = that.find_cell_index(data.cell);
236 that.select(index);
240 that.select(index);
237 });
241 });
238
242
239 this.events.on('edit_mode.Cell', function (event, data) {
243 this.events.on('edit_mode.Cell', function (event, data) {
240 that.handle_edit_mode(data.cell);
244 that.handle_edit_mode(data.cell);
241 });
245 });
242
246
243 this.events.on('command_mode.Cell', function (event, data) {
247 this.events.on('command_mode.Cell', function (event, data) {
244 that.handle_command_mode(data.cell);
248 that.handle_command_mode(data.cell);
245 });
249 });
246
250
247 this.events.on('spec_changed.Kernel', function(event, data) {
251 this.events.on('spec_changed.Kernel', function(event, data) {
248 that.metadata.kernelspec =
252 that.metadata.kernelspec =
249 {name: data.name, display_name: data.display_name};
253 {name: data.name, display_name: data.display_name};
250 });
254 });
251
255
252 this.events.on('kernel_ready.Kernel', function(event, data) {
256 this.events.on('kernel_ready.Kernel', function(event, data) {
253 var kinfo = data.kernel.info_reply;
257 var kinfo = data.kernel.info_reply;
254 var langinfo = kinfo.language_info || {};
258 var langinfo = kinfo.language_info || {};
255 that.metadata.language_info = langinfo;
259 that.metadata.language_info = langinfo;
256 // Mode 'null' should be plain, unhighlighted text.
260 // Mode 'null' should be plain, unhighlighted text.
257 var cm_mode = langinfo.codemirror_mode || langinfo.name || 'null';
261 var cm_mode = langinfo.codemirror_mode || langinfo.name || 'null';
258 that.set_codemirror_mode(cm_mode);
262 that.set_codemirror_mode(cm_mode);
259 });
263 });
260
264
261 var collapse_time = function (time) {
265 var collapse_time = function (time) {
262 var app_height = $('#ipython-main-app').height(); // content height
266 var app_height = $('#ipython-main-app').height(); // content height
263 var splitter_height = $('div#pager_splitter').outerHeight(true);
267 var splitter_height = $('div#pager_splitter').outerHeight(true);
264 var new_height = app_height - splitter_height;
268 var new_height = app_height - splitter_height;
265 that.element.animate({height : new_height + 'px'}, time);
269 that.element.animate({height : new_height + 'px'}, time);
266 };
270 };
267
271
268 this.element.bind('collapse_pager', function (event, extrap) {
272 this.element.bind('collapse_pager', function (event, extrap) {
269 var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
273 var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
270 collapse_time(time);
274 collapse_time(time);
271 });
275 });
272
276
273 var expand_time = function (time) {
277 var expand_time = function (time) {
274 var app_height = $('#ipython-main-app').height(); // content height
278 var app_height = $('#ipython-main-app').height(); // content height
275 var splitter_height = $('div#pager_splitter').outerHeight(true);
279 var splitter_height = $('div#pager_splitter').outerHeight(true);
276 var pager_height = $('div#pager').outerHeight(true);
280 var pager_height = $('div#pager').outerHeight(true);
277 var new_height = app_height - pager_height - splitter_height;
281 var new_height = app_height - pager_height - splitter_height;
278 that.element.animate({height : new_height + 'px'}, time);
282 that.element.animate({height : new_height + 'px'}, time);
279 };
283 };
280
284
281 this.element.bind('expand_pager', function (event, extrap) {
285 this.element.bind('expand_pager', function (event, extrap) {
282 var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
286 var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
283 expand_time(time);
287 expand_time(time);
284 });
288 });
285
289
286 // Firefox 22 broke $(window).on("beforeunload")
290 // Firefox 22 broke $(window).on("beforeunload")
287 // I'm not sure why or how.
291 // I'm not sure why or how.
288 window.onbeforeunload = function (e) {
292 window.onbeforeunload = function (e) {
289 // TODO: Make killing the kernel configurable.
293 // TODO: Make killing the kernel configurable.
290 var kill_kernel = false;
294 var kill_kernel = false;
291 if (kill_kernel) {
295 if (kill_kernel) {
292 that.session.delete();
296 that.session.delete();
293 }
297 }
294 // if we are autosaving, trigger an autosave on nav-away.
298 // if we are autosaving, trigger an autosave on nav-away.
295 // still warn, because if we don't the autosave may fail.
299 // still warn, because if we don't the autosave may fail.
296 if (that.dirty) {
300 if (that.dirty) {
297 if ( that.autosave_interval ) {
301 if ( that.autosave_interval ) {
298 // schedule autosave in a timeout
302 // schedule autosave in a timeout
299 // this gives you a chance to forcefully discard changes
303 // this gives you a chance to forcefully discard changes
300 // by reloading the page if you *really* want to.
304 // by reloading the page if you *really* want to.
301 // the timer doesn't start until you *dismiss* the dialog.
305 // the timer doesn't start until you *dismiss* the dialog.
302 setTimeout(function () {
306 setTimeout(function () {
303 if (that.dirty) {
307 if (that.dirty) {
304 that.save_notebook();
308 that.save_notebook();
305 }
309 }
306 }, 1000);
310 }, 1000);
307 return "Autosave in progress, latest changes may be lost.";
311 return "Autosave in progress, latest changes may be lost.";
308 } else {
312 } else {
309 return "Unsaved changes will be lost.";
313 return "Unsaved changes will be lost.";
310 }
314 }
311 }
315 }
312 // Null is the *only* return value that will make the browser not
316 // Null is the *only* return value that will make the browser not
313 // pop up the "don't leave" dialog.
317 // pop up the "don't leave" dialog.
314 return null;
318 return null;
315 };
319 };
316 };
320 };
317
321
318 Notebook.prototype.warn_nbformat_minor = function (event) {
322 Notebook.prototype.warn_nbformat_minor = function (event) {
319 /**
323 /**
320 * trigger a warning dialog about missing functionality from newer minor versions
324 * trigger a warning dialog about missing functionality from newer minor versions
321 */
325 */
322 var v = 'v' + this.nbformat + '.';
326 var v = 'v' + this.nbformat + '.';
323 var orig_vs = v + this.nbformat_minor;
327 var orig_vs = v + this.nbformat_minor;
324 var this_vs = v + this.current_nbformat_minor;
328 var this_vs = v + this.current_nbformat_minor;
325 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
329 var msg = "This notebook is version " + orig_vs + ", but we only fully support up to " +
326 this_vs + ". You can still work with this notebook, but cell and output types " +
330 this_vs + ". You can still work with this notebook, but cell and output types " +
327 "introduced in later notebook versions will not be available.";
331 "introduced in later notebook versions will not be available.";
328
332
329 dialog.modal({
333 dialog.modal({
330 notebook: this,
334 notebook: this,
331 keyboard_manager: this.keyboard_manager,
335 keyboard_manager: this.keyboard_manager,
332 title : "Newer Notebook",
336 title : "Newer Notebook",
333 body : msg,
337 body : msg,
334 buttons : {
338 buttons : {
335 OK : {
339 OK : {
336 "class" : "btn-danger"
340 "class" : "btn-danger"
337 }
341 }
338 }
342 }
339 });
343 });
340 }
344 }
341
345
342 /**
346 /**
343 * Set the dirty flag, and trigger the set_dirty.Notebook event
347 * Set the dirty flag, and trigger the set_dirty.Notebook event
344 *
348 *
345 * @method set_dirty
349 * @method set_dirty
346 */
350 */
347 Notebook.prototype.set_dirty = function (value) {
351 Notebook.prototype.set_dirty = function (value) {
348 if (value === undefined) {
352 if (value === undefined) {
349 value = true;
353 value = true;
350 }
354 }
351 if (this.dirty == value) {
355 if (this.dirty == value) {
352 return;
356 return;
353 }
357 }
354 this.events.trigger('set_dirty.Notebook', {value: value});
358 this.events.trigger('set_dirty.Notebook', {value: value});
355 };
359 };
356
360
357 /**
361 /**
358 * Scroll the top of the page to a given cell.
362 * Scroll the top of the page to a given cell.
359 *
363 *
360 * @method scroll_to_cell
364 * @method scroll_to_cell
361 * @param {Number} cell_number An index of the cell to view
365 * @param {Number} cell_number An index of the cell to view
362 * @param {Number} time Animation time in milliseconds
366 * @param {Number} time Animation time in milliseconds
363 * @return {Number} Pixel offset from the top of the container
367 * @return {Number} Pixel offset from the top of the container
364 */
368 */
365 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
369 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
366 var cells = this.get_cells();
370 var cells = this.get_cells();
367 time = time || 0;
371 time = time || 0;
368 cell_number = Math.min(cells.length-1,cell_number);
372 cell_number = Math.min(cells.length-1,cell_number);
369 cell_number = Math.max(0 ,cell_number);
373 cell_number = Math.max(0 ,cell_number);
370 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
374 var scroll_value = cells[cell_number].element.position().top-cells[0].element.position().top ;
371 this.element.animate({scrollTop:scroll_value}, time);
375 this.element.animate({scrollTop:scroll_value}, time);
372 return scroll_value;
376 return scroll_value;
373 };
377 };
374
378
375 /**
379 /**
376 * Scroll to the bottom of the page.
380 * Scroll to the bottom of the page.
377 *
381 *
378 * @method scroll_to_bottom
382 * @method scroll_to_bottom
379 */
383 */
380 Notebook.prototype.scroll_to_bottom = function () {
384 Notebook.prototype.scroll_to_bottom = function () {
381 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
385 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
382 };
386 };
383
387
384 /**
388 /**
385 * Scroll to the top of the page.
389 * Scroll to the top of the page.
386 *
390 *
387 * @method scroll_to_top
391 * @method scroll_to_top
388 */
392 */
389 Notebook.prototype.scroll_to_top = function () {
393 Notebook.prototype.scroll_to_top = function () {
390 this.element.animate({scrollTop:0}, 0);
394 this.element.animate({scrollTop:0}, 0);
391 };
395 };
392
396
393 // Edit Notebook metadata
397 // Edit Notebook metadata
394
398
395 Notebook.prototype.edit_metadata = function () {
399 Notebook.prototype.edit_metadata = function () {
396 var that = this;
400 var that = this;
397 dialog.edit_metadata({
401 dialog.edit_metadata({
398 md: this.metadata,
402 md: this.metadata,
399 callback: function (md) {
403 callback: function (md) {
400 that.metadata = md;
404 that.metadata = md;
401 },
405 },
402 name: 'Notebook',
406 name: 'Notebook',
403 notebook: this,
407 notebook: this,
404 keyboard_manager: this.keyboard_manager});
408 keyboard_manager: this.keyboard_manager});
405 };
409 };
406
410
407 // Cell indexing, retrieval, etc.
411 // Cell indexing, retrieval, etc.
408
412
409 /**
413 /**
410 * Get all cell elements in the notebook.
414 * Get all cell elements in the notebook.
411 *
415 *
412 * @method get_cell_elements
416 * @method get_cell_elements
413 * @return {jQuery} A selector of all cell elements
417 * @return {jQuery} A selector of all cell elements
414 */
418 */
415 Notebook.prototype.get_cell_elements = function () {
419 Notebook.prototype.get_cell_elements = function () {
416 return this.container.find(".cell").not('.cell .cell');
420 return this.container.find(".cell").not('.cell .cell');
417 };
421 };
418
422
419 /**
423 /**
420 * Get a particular cell element.
424 * Get a particular cell element.
421 *
425 *
422 * @method get_cell_element
426 * @method get_cell_element
423 * @param {Number} index An index of a cell to select
427 * @param {Number} index An index of a cell to select
424 * @return {jQuery} A selector of the given cell.
428 * @return {jQuery} A selector of the given cell.
425 */
429 */
426 Notebook.prototype.get_cell_element = function (index) {
430 Notebook.prototype.get_cell_element = function (index) {
427 var result = null;
431 var result = null;
428 var e = this.get_cell_elements().eq(index);
432 var e = this.get_cell_elements().eq(index);
429 if (e.length !== 0) {
433 if (e.length !== 0) {
430 result = e;
434 result = e;
431 }
435 }
432 return result;
436 return result;
433 };
437 };
434
438
435 /**
439 /**
436 * Try to get a particular cell by msg_id.
440 * Try to get a particular cell by msg_id.
437 *
441 *
438 * @method get_msg_cell
442 * @method get_msg_cell
439 * @param {String} msg_id A message UUID
443 * @param {String} msg_id A message UUID
440 * @return {Cell} Cell or null if no cell was found.
444 * @return {Cell} Cell or null if no cell was found.
441 */
445 */
442 Notebook.prototype.get_msg_cell = function (msg_id) {
446 Notebook.prototype.get_msg_cell = function (msg_id) {
443 return codecell.CodeCell.msg_cells[msg_id] || null;
447 return codecell.CodeCell.msg_cells[msg_id] || null;
444 };
448 };
445
449
446 /**
450 /**
447 * Count the cells in this notebook.
451 * Count the cells in this notebook.
448 *
452 *
449 * @method ncells
453 * @method ncells
450 * @return {Number} The number of cells in this notebook
454 * @return {Number} The number of cells in this notebook
451 */
455 */
452 Notebook.prototype.ncells = function () {
456 Notebook.prototype.ncells = function () {
453 return this.get_cell_elements().length;
457 return this.get_cell_elements().length;
454 };
458 };
455
459
456 /**
460 /**
457 * Get all Cell objects in this notebook.
461 * Get all Cell objects in this notebook.
458 *
462 *
459 * @method get_cells
463 * @method get_cells
460 * @return {Array} This notebook's Cell objects
464 * @return {Array} This notebook's Cell objects
461 */
465 */
462 // TODO: we are often calling cells as cells()[i], which we should optimize
466 // TODO: we are often calling cells as cells()[i], which we should optimize
463 // to cells(i) or a new method.
467 // to cells(i) or a new method.
464 Notebook.prototype.get_cells = function () {
468 Notebook.prototype.get_cells = function () {
465 return this.get_cell_elements().toArray().map(function (e) {
469 return this.get_cell_elements().toArray().map(function (e) {
466 return $(e).data("cell");
470 return $(e).data("cell");
467 });
471 });
468 };
472 };
469
473
470 /**
474 /**
471 * Get a Cell object from this notebook.
475 * Get a Cell object from this notebook.
472 *
476 *
473 * @method get_cell
477 * @method get_cell
474 * @param {Number} index An index of a cell to retrieve
478 * @param {Number} index An index of a cell to retrieve
475 * @return {Cell} Cell or null if no cell was found.
479 * @return {Cell} Cell or null if no cell was found.
476 */
480 */
477 Notebook.prototype.get_cell = function (index) {
481 Notebook.prototype.get_cell = function (index) {
478 var result = null;
482 var result = null;
479 var ce = this.get_cell_element(index);
483 var ce = this.get_cell_element(index);
480 if (ce !== null) {
484 if (ce !== null) {
481 result = ce.data('cell');
485 result = ce.data('cell');
482 }
486 }
483 return result;
487 return result;
484 };
488 };
485
489
486 /**
490 /**
487 * Get the cell below a given cell.
491 * Get the cell below a given cell.
488 *
492 *
489 * @method get_next_cell
493 * @method get_next_cell
490 * @param {Cell} cell The provided cell
494 * @param {Cell} cell The provided cell
491 * @return {Cell} the next cell or null if no cell was found.
495 * @return {Cell} the next cell or null if no cell was found.
492 */
496 */
493 Notebook.prototype.get_next_cell = function (cell) {
497 Notebook.prototype.get_next_cell = function (cell) {
494 var result = null;
498 var result = null;
495 var index = this.find_cell_index(cell);
499 var index = this.find_cell_index(cell);
496 if (this.is_valid_cell_index(index+1)) {
500 if (this.is_valid_cell_index(index+1)) {
497 result = this.get_cell(index+1);
501 result = this.get_cell(index+1);
498 }
502 }
499 return result;
503 return result;
500 };
504 };
501
505
502 /**
506 /**
503 * Get the cell above a given cell.
507 * Get the cell above a given cell.
504 *
508 *
505 * @method get_prev_cell
509 * @method get_prev_cell
506 * @param {Cell} cell The provided cell
510 * @param {Cell} cell The provided cell
507 * @return {Cell} The previous cell or null if no cell was found.
511 * @return {Cell} The previous cell or null if no cell was found.
508 */
512 */
509 Notebook.prototype.get_prev_cell = function (cell) {
513 Notebook.prototype.get_prev_cell = function (cell) {
510 var result = null;
514 var result = null;
511 var index = this.find_cell_index(cell);
515 var index = this.find_cell_index(cell);
512 if (index !== null && index > 0) {
516 if (index !== null && index > 0) {
513 result = this.get_cell(index-1);
517 result = this.get_cell(index-1);
514 }
518 }
515 return result;
519 return result;
516 };
520 };
517
521
518 /**
522 /**
519 * Get the numeric index of a given cell.
523 * Get the numeric index of a given cell.
520 *
524 *
521 * @method find_cell_index
525 * @method find_cell_index
522 * @param {Cell} cell The provided cell
526 * @param {Cell} cell The provided cell
523 * @return {Number} The cell's numeric index or null if no cell was found.
527 * @return {Number} The cell's numeric index or null if no cell was found.
524 */
528 */
525 Notebook.prototype.find_cell_index = function (cell) {
529 Notebook.prototype.find_cell_index = function (cell) {
526 var result = null;
530 var result = null;
527 this.get_cell_elements().filter(function (index) {
531 this.get_cell_elements().filter(function (index) {
528 if ($(this).data("cell") === cell) {
532 if ($(this).data("cell") === cell) {
529 result = index;
533 result = index;
530 }
534 }
531 });
535 });
532 return result;
536 return result;
533 };
537 };
534
538
535 /**
539 /**
536 * Get a given index , or the selected index if none is provided.
540 * Get a given index , or the selected index if none is provided.
537 *
541 *
538 * @method index_or_selected
542 * @method index_or_selected
539 * @param {Number} index A cell's index
543 * @param {Number} index A cell's index
540 * @return {Number} The given index, or selected index if none is provided.
544 * @return {Number} The given index, or selected index if none is provided.
541 */
545 */
542 Notebook.prototype.index_or_selected = function (index) {
546 Notebook.prototype.index_or_selected = function (index) {
543 var i;
547 var i;
544 if (index === undefined || index === null) {
548 if (index === undefined || index === null) {
545 i = this.get_selected_index();
549 i = this.get_selected_index();
546 if (i === null) {
550 if (i === null) {
547 i = 0;
551 i = 0;
548 }
552 }
549 } else {
553 } else {
550 i = index;
554 i = index;
551 }
555 }
552 return i;
556 return i;
553 };
557 };
554
558
555 /**
559 /**
556 * Get the currently selected cell.
560 * Get the currently selected cell.
557 * @method get_selected_cell
561 * @method get_selected_cell
558 * @return {Cell} The selected cell
562 * @return {Cell} The selected cell
559 */
563 */
560 Notebook.prototype.get_selected_cell = function () {
564 Notebook.prototype.get_selected_cell = function () {
561 var index = this.get_selected_index();
565 var index = this.get_selected_index();
562 return this.get_cell(index);
566 return this.get_cell(index);
563 };
567 };
564
568
565 /**
569 /**
566 * Check whether a cell index is valid.
570 * Check whether a cell index is valid.
567 *
571 *
568 * @method is_valid_cell_index
572 * @method is_valid_cell_index
569 * @param {Number} index A cell index
573 * @param {Number} index A cell index
570 * @return True if the index is valid, false otherwise
574 * @return True if the index is valid, false otherwise
571 */
575 */
572 Notebook.prototype.is_valid_cell_index = function (index) {
576 Notebook.prototype.is_valid_cell_index = function (index) {
573 if (index !== null && index >= 0 && index < this.ncells()) {
577 if (index !== null && index >= 0 && index < this.ncells()) {
574 return true;
578 return true;
575 } else {
579 } else {
576 return false;
580 return false;
577 }
581 }
578 };
582 };
579
583
580 /**
584 /**
581 * Get the index of the currently selected cell.
585 * Get the index of the currently selected cell.
582
586
583 * @method get_selected_index
587 * @method get_selected_index
584 * @return {Number} The selected cell's numeric index
588 * @return {Number} The selected cell's numeric index
585 */
589 */
586 Notebook.prototype.get_selected_index = function () {
590 Notebook.prototype.get_selected_index = function () {
587 var result = null;
591 var result = null;
588 this.get_cell_elements().filter(function (index) {
592 this.get_cell_elements().filter(function (index) {
589 if ($(this).data("cell").selected === true) {
593 if ($(this).data("cell").selected === true) {
590 result = index;
594 result = index;
591 }
595 }
592 });
596 });
593 return result;
597 return result;
594 };
598 };
595
599
596
600
597 // Cell selection.
601 // Cell selection.
598
602
599 /**
603 /**
600 * Programmatically select a cell.
604 * Programmatically select a cell.
601 *
605 *
602 * @method select
606 * @method select
603 * @param {Number} index A cell's index
607 * @param {Number} index A cell's index
604 * @return {Notebook} This notebook
608 * @return {Notebook} This notebook
605 */
609 */
606 Notebook.prototype.select = function (index) {
610 Notebook.prototype.select = function (index) {
607 if (this.is_valid_cell_index(index)) {
611 if (this.is_valid_cell_index(index)) {
608 var sindex = this.get_selected_index();
612 var sindex = this.get_selected_index();
609 if (sindex !== null && index !== sindex) {
613 if (sindex !== null && index !== sindex) {
610 // If we are about to select a different cell, make sure we are
614 // If we are about to select a different cell, make sure we are
611 // first in command mode.
615 // first in command mode.
612 if (this.mode !== 'command') {
616 if (this.mode !== 'command') {
613 this.command_mode();
617 this.command_mode();
614 }
618 }
615 this.get_cell(sindex).unselect();
619 this.get_cell(sindex).unselect();
616 }
620 }
617 var cell = this.get_cell(index);
621 var cell = this.get_cell(index);
618 cell.select();
622 cell.select();
619 if (cell.cell_type === 'heading') {
623 if (cell.cell_type === 'heading') {
620 this.events.trigger('selected_cell_type_changed.Notebook',
624 this.events.trigger('selected_cell_type_changed.Notebook',
621 {'cell_type':cell.cell_type,level:cell.level}
625 {'cell_type':cell.cell_type,level:cell.level}
622 );
626 );
623 } else {
627 } else {
624 this.events.trigger('selected_cell_type_changed.Notebook',
628 this.events.trigger('selected_cell_type_changed.Notebook',
625 {'cell_type':cell.cell_type}
629 {'cell_type':cell.cell_type}
626 );
630 );
627 }
631 }
628 }
632 }
629 return this;
633 return this;
630 };
634 };
631
635
632 /**
636 /**
633 * Programmatically select the next cell.
637 * Programmatically select the next cell.
634 *
638 *
635 * @method select_next
639 * @method select_next
636 * @return {Notebook} This notebook
640 * @return {Notebook} This notebook
637 */
641 */
638 Notebook.prototype.select_next = function () {
642 Notebook.prototype.select_next = function () {
639 var index = this.get_selected_index();
643 var index = this.get_selected_index();
640 this.select(index+1);
644 this.select(index+1);
641 return this;
645 return this;
642 };
646 };
643
647
644 /**
648 /**
645 * Programmatically select the previous cell.
649 * Programmatically select the previous cell.
646 *
650 *
647 * @method select_prev
651 * @method select_prev
648 * @return {Notebook} This notebook
652 * @return {Notebook} This notebook
649 */
653 */
650 Notebook.prototype.select_prev = function () {
654 Notebook.prototype.select_prev = function () {
651 var index = this.get_selected_index();
655 var index = this.get_selected_index();
652 this.select(index-1);
656 this.select(index-1);
653 return this;
657 return this;
654 };
658 };
655
659
656
660
657 // Edit/Command mode
661 // Edit/Command mode
658
662
659 /**
663 /**
660 * Gets the index of the cell that is in edit mode.
664 * Gets the index of the cell that is in edit mode.
661 *
665 *
662 * @method get_edit_index
666 * @method get_edit_index
663 *
667 *
664 * @return index {int}
668 * @return index {int}
665 **/
669 **/
666 Notebook.prototype.get_edit_index = function () {
670 Notebook.prototype.get_edit_index = function () {
667 var result = null;
671 var result = null;
668 this.get_cell_elements().filter(function (index) {
672 this.get_cell_elements().filter(function (index) {
669 if ($(this).data("cell").mode === 'edit') {
673 if ($(this).data("cell").mode === 'edit') {
670 result = index;
674 result = index;
671 }
675 }
672 });
676 });
673 return result;
677 return result;
674 };
678 };
675
679
676 /**
680 /**
677 * Handle when a a cell blurs and the notebook should enter command mode.
681 * Handle when a a cell blurs and the notebook should enter command mode.
678 *
682 *
679 * @method handle_command_mode
683 * @method handle_command_mode
680 * @param [cell] {Cell} Cell to enter command mode on.
684 * @param [cell] {Cell} Cell to enter command mode on.
681 **/
685 **/
682 Notebook.prototype.handle_command_mode = function (cell) {
686 Notebook.prototype.handle_command_mode = function (cell) {
683 if (this.mode !== 'command') {
687 if (this.mode !== 'command') {
684 cell.command_mode();
688 cell.command_mode();
685 this.mode = 'command';
689 this.mode = 'command';
686 this.events.trigger('command_mode.Notebook');
690 this.events.trigger('command_mode.Notebook');
687 this.keyboard_manager.command_mode();
691 this.keyboard_manager.command_mode();
688 }
692 }
689 };
693 };
690
694
691 /**
695 /**
692 * Make the notebook enter command mode.
696 * Make the notebook enter command mode.
693 *
697 *
694 * @method command_mode
698 * @method command_mode
695 **/
699 **/
696 Notebook.prototype.command_mode = function () {
700 Notebook.prototype.command_mode = function () {
697 var cell = this.get_cell(this.get_edit_index());
701 var cell = this.get_cell(this.get_edit_index());
698 if (cell && this.mode !== 'command') {
702 if (cell && this.mode !== 'command') {
699 // We don't call cell.command_mode, but rather call cell.focus_cell()
703 // We don't call cell.command_mode, but rather call cell.focus_cell()
700 // which will blur and CM editor and trigger the call to
704 // which will blur and CM editor and trigger the call to
701 // handle_command_mode.
705 // handle_command_mode.
702 cell.focus_cell();
706 cell.focus_cell();
703 }
707 }
704 };
708 };
705
709
706 /**
710 /**
707 * Handle when a cell fires it's edit_mode event.
711 * Handle when a cell fires it's edit_mode event.
708 *
712 *
709 * @method handle_edit_mode
713 * @method handle_edit_mode
710 * @param [cell] {Cell} Cell to enter edit mode on.
714 * @param [cell] {Cell} Cell to enter edit mode on.
711 **/
715 **/
712 Notebook.prototype.handle_edit_mode = function (cell) {
716 Notebook.prototype.handle_edit_mode = function (cell) {
713 if (cell && this.mode !== 'edit') {
717 if (cell && this.mode !== 'edit') {
714 cell.edit_mode();
718 cell.edit_mode();
715 this.mode = 'edit';
719 this.mode = 'edit';
716 this.events.trigger('edit_mode.Notebook');
720 this.events.trigger('edit_mode.Notebook');
717 this.keyboard_manager.edit_mode();
721 this.keyboard_manager.edit_mode();
718 }
722 }
719 };
723 };
720
724
721 /**
725 /**
722 * Make a cell enter edit mode.
726 * Make a cell enter edit mode.
723 *
727 *
724 * @method edit_mode
728 * @method edit_mode
725 **/
729 **/
726 Notebook.prototype.edit_mode = function () {
730 Notebook.prototype.edit_mode = function () {
727 var cell = this.get_selected_cell();
731 var cell = this.get_selected_cell();
728 if (cell && this.mode !== 'edit') {
732 if (cell && this.mode !== 'edit') {
729 cell.unrender();
733 cell.unrender();
730 cell.focus_editor();
734 cell.focus_editor();
731 }
735 }
732 };
736 };
733
737
734 /**
738 /**
735 * Focus the currently selected cell.
739 * Focus the currently selected cell.
736 *
740 *
737 * @method focus_cell
741 * @method focus_cell
738 **/
742 **/
739 Notebook.prototype.focus_cell = function () {
743 Notebook.prototype.focus_cell = function () {
740 var cell = this.get_selected_cell();
744 var cell = this.get_selected_cell();
741 if (cell === null) {return;} // No cell is selected
745 if (cell === null) {return;} // No cell is selected
742 cell.focus_cell();
746 cell.focus_cell();
743 };
747 };
744
748
745 // Cell movement
749 // Cell movement
746
750
747 /**
751 /**
748 * Move given (or selected) cell up and select it.
752 * Move given (or selected) cell up and select it.
749 *
753 *
750 * @method move_cell_up
754 * @method move_cell_up
751 * @param [index] {integer} cell index
755 * @param [index] {integer} cell index
752 * @return {Notebook} This notebook
756 * @return {Notebook} This notebook
753 **/
757 **/
754 Notebook.prototype.move_cell_up = function (index) {
758 Notebook.prototype.move_cell_up = function (index) {
755 var i = this.index_or_selected(index);
759 var i = this.index_or_selected(index);
756 if (this.is_valid_cell_index(i) && i > 0) {
760 if (this.is_valid_cell_index(i) && i > 0) {
757 var pivot = this.get_cell_element(i-1);
761 var pivot = this.get_cell_element(i-1);
758 var tomove = this.get_cell_element(i);
762 var tomove = this.get_cell_element(i);
759 if (pivot !== null && tomove !== null) {
763 if (pivot !== null && tomove !== null) {
760 tomove.detach();
764 tomove.detach();
761 pivot.before(tomove);
765 pivot.before(tomove);
762 this.select(i-1);
766 this.select(i-1);
763 var cell = this.get_selected_cell();
767 var cell = this.get_selected_cell();
764 cell.focus_cell();
768 cell.focus_cell();
765 }
769 }
766 this.set_dirty(true);
770 this.set_dirty(true);
767 }
771 }
768 return this;
772 return this;
769 };
773 };
770
774
771
775
772 /**
776 /**
773 * Move given (or selected) cell down and select it
777 * Move given (or selected) cell down and select it
774 *
778 *
775 * @method move_cell_down
779 * @method move_cell_down
776 * @param [index] {integer} cell index
780 * @param [index] {integer} cell index
777 * @return {Notebook} This notebook
781 * @return {Notebook} This notebook
778 **/
782 **/
779 Notebook.prototype.move_cell_down = function (index) {
783 Notebook.prototype.move_cell_down = function (index) {
780 var i = this.index_or_selected(index);
784 var i = this.index_or_selected(index);
781 if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
785 if (this.is_valid_cell_index(i) && this.is_valid_cell_index(i+1)) {
782 var pivot = this.get_cell_element(i+1);
786 var pivot = this.get_cell_element(i+1);
783 var tomove = this.get_cell_element(i);
787 var tomove = this.get_cell_element(i);
784 if (pivot !== null && tomove !== null) {
788 if (pivot !== null && tomove !== null) {
785 tomove.detach();
789 tomove.detach();
786 pivot.after(tomove);
790 pivot.after(tomove);
787 this.select(i+1);
791 this.select(i+1);
788 var cell = this.get_selected_cell();
792 var cell = this.get_selected_cell();
789 cell.focus_cell();
793 cell.focus_cell();
790 }
794 }
791 }
795 }
792 this.set_dirty();
796 this.set_dirty();
793 return this;
797 return this;
794 };
798 };
795
799
796
800
797 // Insertion, deletion.
801 // Insertion, deletion.
798
802
799 /**
803 /**
800 * Delete a cell from the notebook.
804 * Delete a cell from the notebook.
801 *
805 *
802 * @method delete_cell
806 * @method delete_cell
803 * @param [index] A cell's numeric index
807 * @param [index] A cell's numeric index
804 * @return {Notebook} This notebook
808 * @return {Notebook} This notebook
805 */
809 */
806 Notebook.prototype.delete_cell = function (index) {
810 Notebook.prototype.delete_cell = function (index) {
807 var i = this.index_or_selected(index);
811 var i = this.index_or_selected(index);
808 var cell = this.get_cell(i);
812 var cell = this.get_cell(i);
809 if (!cell.is_deletable()) {
813 if (!cell.is_deletable()) {
810 return this;
814 return this;
811 }
815 }
812
816
813 this.undelete_backup = cell.toJSON();
817 this.undelete_backup = cell.toJSON();
814 $('#undelete_cell').removeClass('disabled');
818 $('#undelete_cell').removeClass('disabled');
815 if (this.is_valid_cell_index(i)) {
819 if (this.is_valid_cell_index(i)) {
816 var old_ncells = this.ncells();
820 var old_ncells = this.ncells();
817 var ce = this.get_cell_element(i);
821 var ce = this.get_cell_element(i);
818 ce.remove();
822 ce.remove();
819 if (i === 0) {
823 if (i === 0) {
820 // Always make sure we have at least one cell.
824 // Always make sure we have at least one cell.
821 if (old_ncells === 1) {
825 if (old_ncells === 1) {
822 this.insert_cell_below('code');
826 this.insert_cell_below('code');
823 }
827 }
824 this.select(0);
828 this.select(0);
825 this.undelete_index = 0;
829 this.undelete_index = 0;
826 this.undelete_below = false;
830 this.undelete_below = false;
827 } else if (i === old_ncells-1 && i !== 0) {
831 } else if (i === old_ncells-1 && i !== 0) {
828 this.select(i-1);
832 this.select(i-1);
829 this.undelete_index = i - 1;
833 this.undelete_index = i - 1;
830 this.undelete_below = true;
834 this.undelete_below = true;
831 } else {
835 } else {
832 this.select(i);
836 this.select(i);
833 this.undelete_index = i;
837 this.undelete_index = i;
834 this.undelete_below = false;
838 this.undelete_below = false;
835 }
839 }
836 this.events.trigger('delete.Cell', {'cell': cell, 'index': i});
840 this.events.trigger('delete.Cell', {'cell': cell, 'index': i});
837 this.set_dirty(true);
841 this.set_dirty(true);
838 }
842 }
839 return this;
843 return this;
840 };
844 };
841
845
842 /**
846 /**
843 * Restore the most recently deleted cell.
847 * Restore the most recently deleted cell.
844 *
848 *
845 * @method undelete
849 * @method undelete
846 */
850 */
847 Notebook.prototype.undelete_cell = function() {
851 Notebook.prototype.undelete_cell = function() {
848 if (this.undelete_backup !== null && this.undelete_index !== null) {
852 if (this.undelete_backup !== null && this.undelete_index !== null) {
849 var current_index = this.get_selected_index();
853 var current_index = this.get_selected_index();
850 if (this.undelete_index < current_index) {
854 if (this.undelete_index < current_index) {
851 current_index = current_index + 1;
855 current_index = current_index + 1;
852 }
856 }
853 if (this.undelete_index >= this.ncells()) {
857 if (this.undelete_index >= this.ncells()) {
854 this.select(this.ncells() - 1);
858 this.select(this.ncells() - 1);
855 }
859 }
856 else {
860 else {
857 this.select(this.undelete_index);
861 this.select(this.undelete_index);
858 }
862 }
859 var cell_data = this.undelete_backup;
863 var cell_data = this.undelete_backup;
860 var new_cell = null;
864 var new_cell = null;
861 if (this.undelete_below) {
865 if (this.undelete_below) {
862 new_cell = this.insert_cell_below(cell_data.cell_type);
866 new_cell = this.insert_cell_below(cell_data.cell_type);
863 } else {
867 } else {
864 new_cell = this.insert_cell_above(cell_data.cell_type);
868 new_cell = this.insert_cell_above(cell_data.cell_type);
865 }
869 }
866 new_cell.fromJSON(cell_data);
870 new_cell.fromJSON(cell_data);
867 if (this.undelete_below) {
871 if (this.undelete_below) {
868 this.select(current_index+1);
872 this.select(current_index+1);
869 } else {
873 } else {
870 this.select(current_index);
874 this.select(current_index);
871 }
875 }
872 this.undelete_backup = null;
876 this.undelete_backup = null;
873 this.undelete_index = null;
877 this.undelete_index = null;
874 }
878 }
875 $('#undelete_cell').addClass('disabled');
879 $('#undelete_cell').addClass('disabled');
876 };
880 };
877
881
878 /**
882 /**
879 * Insert a cell so that after insertion the cell is at given index.
883 * Insert a cell so that after insertion the cell is at given index.
880 *
884 *
881 * If cell type is not provided, it will default to the type of the
885 * If cell type is not provided, it will default to the type of the
882 * currently active cell.
886 * currently active cell.
883 *
887 *
884 * Similar to insert_above, but index parameter is mandatory
888 * Similar to insert_above, but index parameter is mandatory
885 *
889 *
886 * Index will be brought back into the accessible range [0,n]
890 * Index will be brought back into the accessible range [0,n]
887 *
891 *
888 * @method insert_cell_at_index
892 * @method insert_cell_at_index
889 * @param [type] {string} in ['code','markdown', 'raw'], defaults to 'code'
893 * @param [type] {string} in ['code','markdown', 'raw'], defaults to 'code'
890 * @param [index] {int} a valid index where to insert cell
894 * @param [index] {int} a valid index where to insert cell
891 *
895 *
892 * @return cell {cell|null} created cell or null
896 * @return cell {cell|null} created cell or null
893 **/
897 **/
894 Notebook.prototype.insert_cell_at_index = function(type, index){
898 Notebook.prototype.insert_cell_at_index = function(type, index){
895
899
896 var ncells = this.ncells();
900 var ncells = this.ncells();
897 index = Math.min(index, ncells);
901 index = Math.min(index, ncells);
898 index = Math.max(index, 0);
902 index = Math.max(index, 0);
899 var cell = null;
903 var cell = null;
900 type = type || this.default_cell_type;
904 type = type || this.default_cell_type;
901 if (type === 'above') {
905 if (type === 'above') {
902 if (index > 0) {
906 if (index > 0) {
903 type = this.get_cell(index-1).cell_type;
907 type = this.get_cell(index-1).cell_type;
904 } else {
908 } else {
905 type = 'code';
909 type = 'code';
906 }
910 }
907 } else if (type === 'below') {
911 } else if (type === 'below') {
908 if (index < ncells) {
912 if (index < ncells) {
909 type = this.get_cell(index).cell_type;
913 type = this.get_cell(index).cell_type;
910 } else {
914 } else {
911 type = 'code';
915 type = 'code';
912 }
916 }
913 } else if (type === 'selected') {
917 } else if (type === 'selected') {
914 type = this.get_selected_cell().cell_type;
918 type = this.get_selected_cell().cell_type;
915 }
919 }
916
920
917 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
921 if (ncells === 0 || this.is_valid_cell_index(index) || index === ncells) {
918 var cell_options = {
922 var cell_options = {
919 events: this.events,
923 events: this.events,
920 config: this.config,
924 config: this.config,
921 keyboard_manager: this.keyboard_manager,
925 keyboard_manager: this.keyboard_manager,
922 notebook: this,
926 notebook: this,
923 tooltip: this.tooltip
927 tooltip: this.tooltip
924 };
928 };
925 switch(type) {
929 switch(type) {
926 case 'code':
930 case 'code':
927 cell = new codecell.CodeCell(this.kernel, cell_options);
931 cell = new codecell.CodeCell(this.kernel, cell_options);
928 cell.set_input_prompt();
932 cell.set_input_prompt();
929 break;
933 break;
930 case 'markdown':
934 case 'markdown':
931 cell = new textcell.MarkdownCell(cell_options);
935 cell = new textcell.MarkdownCell(cell_options);
932 break;
936 break;
933 case 'raw':
937 case 'raw':
934 cell = new textcell.RawCell(cell_options);
938 cell = new textcell.RawCell(cell_options);
935 break;
939 break;
936 default:
940 default:
937 console.log("Unrecognized cell type: ", type, cellmod);
941 console.log("Unrecognized cell type: ", type, cellmod);
938 cell = new cellmod.UnrecognizedCell(cell_options);
942 cell = new cellmod.UnrecognizedCell(cell_options);
939 }
943 }
940
944
941 if(this._insert_element_at_index(cell.element,index)) {
945 if(this._insert_element_at_index(cell.element,index)) {
942 cell.render();
946 cell.render();
943 this.events.trigger('create.Cell', {'cell': cell, 'index': index});
947 this.events.trigger('create.Cell', {'cell': cell, 'index': index});
944 cell.refresh();
948 cell.refresh();
945 // We used to select the cell after we refresh it, but there
949 // We used to select the cell after we refresh it, but there
946 // are now cases were this method is called where select is
950 // are now cases were this method is called where select is
947 // not appropriate. The selection logic should be handled by the
951 // not appropriate. The selection logic should be handled by the
948 // caller of the the top level insert_cell methods.
952 // caller of the the top level insert_cell methods.
949 this.set_dirty(true);
953 this.set_dirty(true);
950 }
954 }
951 }
955 }
952 return cell;
956 return cell;
953
957
954 };
958 };
955
959
956 /**
960 /**
957 * Insert an element at given cell index.
961 * Insert an element at given cell index.
958 *
962 *
959 * @method _insert_element_at_index
963 * @method _insert_element_at_index
960 * @param element {dom_element} a cell element
964 * @param element {dom_element} a cell element
961 * @param [index] {int} a valid index where to inser cell
965 * @param [index] {int} a valid index where to inser cell
962 * @private
966 * @private
963 *
967 *
964 * return true if everything whent fine.
968 * return true if everything whent fine.
965 **/
969 **/
966 Notebook.prototype._insert_element_at_index = function(element, index){
970 Notebook.prototype._insert_element_at_index = function(element, index){
967 if (element === undefined){
971 if (element === undefined){
968 return false;
972 return false;
969 }
973 }
970
974
971 var ncells = this.ncells();
975 var ncells = this.ncells();
972
976
973 if (ncells === 0) {
977 if (ncells === 0) {
974 // special case append if empty
978 // special case append if empty
975 this.element.find('div.end_space').before(element);
979 this.element.find('div.end_space').before(element);
976 } else if ( ncells === index ) {
980 } else if ( ncells === index ) {
977 // special case append it the end, but not empty
981 // special case append it the end, but not empty
978 this.get_cell_element(index-1).after(element);
982 this.get_cell_element(index-1).after(element);
979 } else if (this.is_valid_cell_index(index)) {
983 } else if (this.is_valid_cell_index(index)) {
980 // otherwise always somewhere to append to
984 // otherwise always somewhere to append to
981 this.get_cell_element(index).before(element);
985 this.get_cell_element(index).before(element);
982 } else {
986 } else {
983 return false;
987 return false;
984 }
988 }
985
989
986 if (this.undelete_index !== null && index <= this.undelete_index) {
990 if (this.undelete_index !== null && index <= this.undelete_index) {
987 this.undelete_index = this.undelete_index + 1;
991 this.undelete_index = this.undelete_index + 1;
988 this.set_dirty(true);
992 this.set_dirty(true);
989 }
993 }
990 return true;
994 return true;
991 };
995 };
992
996
993 /**
997 /**
994 * Insert a cell of given type above given index, or at top
998 * Insert a cell of given type above given index, or at top
995 * of notebook if index smaller than 0.
999 * of notebook if index smaller than 0.
996 *
1000 *
997 * default index value is the one of currently selected cell
1001 * default index value is the one of currently selected cell
998 *
1002 *
999 * @method insert_cell_above
1003 * @method insert_cell_above
1000 * @param [type] {string} cell type
1004 * @param [type] {string} cell type
1001 * @param [index] {integer}
1005 * @param [index] {integer}
1002 *
1006 *
1003 * @return handle to created cell or null
1007 * @return handle to created cell or null
1004 **/
1008 **/
1005 Notebook.prototype.insert_cell_above = function (type, index) {
1009 Notebook.prototype.insert_cell_above = function (type, index) {
1006 index = this.index_or_selected(index);
1010 index = this.index_or_selected(index);
1007 return this.insert_cell_at_index(type, index);
1011 return this.insert_cell_at_index(type, index);
1008 };
1012 };
1009
1013
1010 /**
1014 /**
1011 * Insert a cell of given type below given index, or at bottom
1015 * Insert a cell of given type below given index, or at bottom
1012 * of notebook if index greater than number of cells
1016 * of notebook if index greater than number of cells
1013 *
1017 *
1014 * default index value is the one of currently selected cell
1018 * default index value is the one of currently selected cell
1015 *
1019 *
1016 * @method insert_cell_below
1020 * @method insert_cell_below
1017 * @param [type] {string} cell type
1021 * @param [type] {string} cell type
1018 * @param [index] {integer}
1022 * @param [index] {integer}
1019 *
1023 *
1020 * @return handle to created cell or null
1024 * @return handle to created cell or null
1021 *
1025 *
1022 **/
1026 **/
1023 Notebook.prototype.insert_cell_below = function (type, index) {
1027 Notebook.prototype.insert_cell_below = function (type, index) {
1024 index = this.index_or_selected(index);
1028 index = this.index_or_selected(index);
1025 return this.insert_cell_at_index(type, index+1);
1029 return this.insert_cell_at_index(type, index+1);
1026 };
1030 };
1027
1031
1028
1032
1029 /**
1033 /**
1030 * Insert cell at end of notebook
1034 * Insert cell at end of notebook
1031 *
1035 *
1032 * @method insert_cell_at_bottom
1036 * @method insert_cell_at_bottom
1033 * @param {String} type cell type
1037 * @param {String} type cell type
1034 *
1038 *
1035 * @return the added cell; or null
1039 * @return the added cell; or null
1036 **/
1040 **/
1037 Notebook.prototype.insert_cell_at_bottom = function (type){
1041 Notebook.prototype.insert_cell_at_bottom = function (type){
1038 var len = this.ncells();
1042 var len = this.ncells();
1039 return this.insert_cell_below(type,len-1);
1043 return this.insert_cell_below(type,len-1);
1040 };
1044 };
1041
1045
1042 /**
1046 /**
1043 * Turn a cell into a code cell.
1047 * Turn a cell into a code cell.
1044 *
1048 *
1045 * @method to_code
1049 * @method to_code
1046 * @param {Number} [index] A cell's index
1050 * @param {Number} [index] A cell's index
1047 */
1051 */
1048 Notebook.prototype.to_code = function (index) {
1052 Notebook.prototype.to_code = function (index) {
1049 var i = this.index_or_selected(index);
1053 var i = this.index_or_selected(index);
1050 if (this.is_valid_cell_index(i)) {
1054 if (this.is_valid_cell_index(i)) {
1051 var source_cell = this.get_cell(i);
1055 var source_cell = this.get_cell(i);
1052 if (!(source_cell instanceof codecell.CodeCell)) {
1056 if (!(source_cell instanceof codecell.CodeCell)) {
1053 var target_cell = this.insert_cell_below('code',i);
1057 var target_cell = this.insert_cell_below('code',i);
1054 var text = source_cell.get_text();
1058 var text = source_cell.get_text();
1055 if (text === source_cell.placeholder) {
1059 if (text === source_cell.placeholder) {
1056 text = '';
1060 text = '';
1057 }
1061 }
1058 //metadata
1062 //metadata
1059 target_cell.metadata = source_cell.metadata;
1063 target_cell.metadata = source_cell.metadata;
1060
1064
1061 target_cell.set_text(text);
1065 target_cell.set_text(text);
1062 // make this value the starting point, so that we can only undo
1066 // make this value the starting point, so that we can only undo
1063 // to this state, instead of a blank cell
1067 // to this state, instead of a blank cell
1064 target_cell.code_mirror.clearHistory();
1068 target_cell.code_mirror.clearHistory();
1065 source_cell.element.remove();
1069 source_cell.element.remove();
1066 this.select(i);
1070 this.select(i);
1067 var cursor = source_cell.code_mirror.getCursor();
1071 var cursor = source_cell.code_mirror.getCursor();
1068 target_cell.code_mirror.setCursor(cursor);
1072 target_cell.code_mirror.setCursor(cursor);
1069 this.set_dirty(true);
1073 this.set_dirty(true);
1070 }
1074 }
1071 }
1075 }
1072 };
1076 };
1073
1077
1074 /**
1078 /**
1075 * Turn a cell into a Markdown cell.
1079 * Turn a cell into a Markdown cell.
1076 *
1080 *
1077 * @method to_markdown
1081 * @method to_markdown
1078 * @param {Number} [index] A cell's index
1082 * @param {Number} [index] A cell's index
1079 */
1083 */
1080 Notebook.prototype.to_markdown = function (index) {
1084 Notebook.prototype.to_markdown = function (index) {
1081 var i = this.index_or_selected(index);
1085 var i = this.index_or_selected(index);
1082 if (this.is_valid_cell_index(i)) {
1086 if (this.is_valid_cell_index(i)) {
1083 var source_cell = this.get_cell(i);
1087 var source_cell = this.get_cell(i);
1084
1088
1085 if (!(source_cell instanceof textcell.MarkdownCell)) {
1089 if (!(source_cell instanceof textcell.MarkdownCell)) {
1086 var target_cell = this.insert_cell_below('markdown',i);
1090 var target_cell = this.insert_cell_below('markdown',i);
1087 var text = source_cell.get_text();
1091 var text = source_cell.get_text();
1088
1092
1089 if (text === source_cell.placeholder) {
1093 if (text === source_cell.placeholder) {
1090 text = '';
1094 text = '';
1091 }
1095 }
1092 // metadata
1096 // metadata
1093 target_cell.metadata = source_cell.metadata;
1097 target_cell.metadata = source_cell.metadata;
1094 // We must show the editor before setting its contents
1098 // We must show the editor before setting its contents
1095 target_cell.unrender();
1099 target_cell.unrender();
1096 target_cell.set_text(text);
1100 target_cell.set_text(text);
1097 // make this value the starting point, so that we can only undo
1101 // make this value the starting point, so that we can only undo
1098 // to this state, instead of a blank cell
1102 // to this state, instead of a blank cell
1099 target_cell.code_mirror.clearHistory();
1103 target_cell.code_mirror.clearHistory();
1100 source_cell.element.remove();
1104 source_cell.element.remove();
1101 this.select(i);
1105 this.select(i);
1102 if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) {
1106 if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) {
1103 target_cell.render();
1107 target_cell.render();
1104 }
1108 }
1105 var cursor = source_cell.code_mirror.getCursor();
1109 var cursor = source_cell.code_mirror.getCursor();
1106 target_cell.code_mirror.setCursor(cursor);
1110 target_cell.code_mirror.setCursor(cursor);
1107 this.set_dirty(true);
1111 this.set_dirty(true);
1108 }
1112 }
1109 }
1113 }
1110 };
1114 };
1111
1115
1112 /**
1116 /**
1113 * Turn a cell into a raw text cell.
1117 * Turn a cell into a raw text cell.
1114 *
1118 *
1115 * @method to_raw
1119 * @method to_raw
1116 * @param {Number} [index] A cell's index
1120 * @param {Number} [index] A cell's index
1117 */
1121 */
1118 Notebook.prototype.to_raw = function (index) {
1122 Notebook.prototype.to_raw = function (index) {
1119 var i = this.index_or_selected(index);
1123 var i = this.index_or_selected(index);
1120 if (this.is_valid_cell_index(i)) {
1124 if (this.is_valid_cell_index(i)) {
1121 var target_cell = null;
1125 var target_cell = null;
1122 var source_cell = this.get_cell(i);
1126 var source_cell = this.get_cell(i);
1123
1127
1124 if (!(source_cell instanceof textcell.RawCell)) {
1128 if (!(source_cell instanceof textcell.RawCell)) {
1125 target_cell = this.insert_cell_below('raw',i);
1129 target_cell = this.insert_cell_below('raw',i);
1126 var text = source_cell.get_text();
1130 var text = source_cell.get_text();
1127 if (text === source_cell.placeholder) {
1131 if (text === source_cell.placeholder) {
1128 text = '';
1132 text = '';
1129 }
1133 }
1130 //metadata
1134 //metadata
1131 target_cell.metadata = source_cell.metadata;
1135 target_cell.metadata = source_cell.metadata;
1132 // We must show the editor before setting its contents
1136 // We must show the editor before setting its contents
1133 target_cell.unrender();
1137 target_cell.unrender();
1134 target_cell.set_text(text);
1138 target_cell.set_text(text);
1135 // make this value the starting point, so that we can only undo
1139 // make this value the starting point, so that we can only undo
1136 // to this state, instead of a blank cell
1140 // to this state, instead of a blank cell
1137 target_cell.code_mirror.clearHistory();
1141 target_cell.code_mirror.clearHistory();
1138 source_cell.element.remove();
1142 source_cell.element.remove();
1139 this.select(i);
1143 this.select(i);
1140 var cursor = source_cell.code_mirror.getCursor();
1144 var cursor = source_cell.code_mirror.getCursor();
1141 target_cell.code_mirror.setCursor(cursor);
1145 target_cell.code_mirror.setCursor(cursor);
1142 this.set_dirty(true);
1146 this.set_dirty(true);
1143 }
1147 }
1144 }
1148 }
1145 };
1149 };
1146
1150
1147 Notebook.prototype._warn_heading = function () {
1151 Notebook.prototype._warn_heading = function () {
1148 /**
1152 /**
1149 * warn about heading cells being removed
1153 * warn about heading cells being removed
1150 */
1154 */
1151 dialog.modal({
1155 dialog.modal({
1152 notebook: this,
1156 notebook: this,
1153 keyboard_manager: this.keyboard_manager,
1157 keyboard_manager: this.keyboard_manager,
1154 title : "Use markdown headings",
1158 title : "Use markdown headings",
1155 body : $("<p/>").text(
1159 body : $("<p/>").text(
1156 'IPython no longer uses special heading cells. ' +
1160 'IPython no longer uses special heading cells. ' +
1157 'Instead, write your headings in Markdown cells using # characters:'
1161 'Instead, write your headings in Markdown cells using # characters:'
1158 ).append($('<pre/>').text(
1162 ).append($('<pre/>').text(
1159 '## This is a level 2 heading'
1163 '## This is a level 2 heading'
1160 )),
1164 )),
1161 buttons : {
1165 buttons : {
1162 "OK" : {}
1166 "OK" : {}
1163 }
1167 }
1164 });
1168 });
1165 };
1169 };
1166
1170
1167 /**
1171 /**
1168 * Turn a cell into a markdown cell with a heading.
1172 * Turn a cell into a markdown cell with a heading.
1169 *
1173 *
1170 * @method to_heading
1174 * @method to_heading
1171 * @param {Number} [index] A cell's index
1175 * @param {Number} [index] A cell's index
1172 * @param {Number} [level] A heading level (e.g., 1 for h1)
1176 * @param {Number} [level] A heading level (e.g., 1 for h1)
1173 */
1177 */
1174 Notebook.prototype.to_heading = function (index, level) {
1178 Notebook.prototype.to_heading = function (index, level) {
1175 this.to_markdown(index);
1179 this.to_markdown(index);
1176 level = level || 1;
1180 level = level || 1;
1177 var i = this.index_or_selected(index);
1181 var i = this.index_or_selected(index);
1178 if (this.is_valid_cell_index(i)) {
1182 if (this.is_valid_cell_index(i)) {
1179 var cell = this.get_cell(i);
1183 var cell = this.get_cell(i);
1180 cell.set_heading_level(level);
1184 cell.set_heading_level(level);
1181 this.set_dirty(true);
1185 this.set_dirty(true);
1182 }
1186 }
1183 };
1187 };
1184
1188
1185
1189
1186 // Cut/Copy/Paste
1190 // Cut/Copy/Paste
1187
1191
1188 /**
1192 /**
1189 * Enable UI elements for pasting cells.
1193 * Enable UI elements for pasting cells.
1190 *
1194 *
1191 * @method enable_paste
1195 * @method enable_paste
1192 */
1196 */
1193 Notebook.prototype.enable_paste = function () {
1197 Notebook.prototype.enable_paste = function () {
1194 var that = this;
1198 var that = this;
1195 if (!this.paste_enabled) {
1199 if (!this.paste_enabled) {
1196 $('#paste_cell_replace').removeClass('disabled')
1200 $('#paste_cell_replace').removeClass('disabled')
1197 .on('click', function () {that.paste_cell_replace();});
1201 .on('click', function () {that.paste_cell_replace();});
1198 $('#paste_cell_above').removeClass('disabled')
1202 $('#paste_cell_above').removeClass('disabled')
1199 .on('click', function () {that.paste_cell_above();});
1203 .on('click', function () {that.paste_cell_above();});
1200 $('#paste_cell_below').removeClass('disabled')
1204 $('#paste_cell_below').removeClass('disabled')
1201 .on('click', function () {that.paste_cell_below();});
1205 .on('click', function () {that.paste_cell_below();});
1202 this.paste_enabled = true;
1206 this.paste_enabled = true;
1203 }
1207 }
1204 };
1208 };
1205
1209
1206 /**
1210 /**
1207 * Disable UI elements for pasting cells.
1211 * Disable UI elements for pasting cells.
1208 *
1212 *
1209 * @method disable_paste
1213 * @method disable_paste
1210 */
1214 */
1211 Notebook.prototype.disable_paste = function () {
1215 Notebook.prototype.disable_paste = function () {
1212 if (this.paste_enabled) {
1216 if (this.paste_enabled) {
1213 $('#paste_cell_replace').addClass('disabled').off('click');
1217 $('#paste_cell_replace').addClass('disabled').off('click');
1214 $('#paste_cell_above').addClass('disabled').off('click');
1218 $('#paste_cell_above').addClass('disabled').off('click');
1215 $('#paste_cell_below').addClass('disabled').off('click');
1219 $('#paste_cell_below').addClass('disabled').off('click');
1216 this.paste_enabled = false;
1220 this.paste_enabled = false;
1217 }
1221 }
1218 };
1222 };
1219
1223
1220 /**
1224 /**
1221 * Cut a cell.
1225 * Cut a cell.
1222 *
1226 *
1223 * @method cut_cell
1227 * @method cut_cell
1224 */
1228 */
1225 Notebook.prototype.cut_cell = function () {
1229 Notebook.prototype.cut_cell = function () {
1226 this.copy_cell();
1230 this.copy_cell();
1227 this.delete_cell();
1231 this.delete_cell();
1228 };
1232 };
1229
1233
1230 /**
1234 /**
1231 * Copy a cell.
1235 * Copy a cell.
1232 *
1236 *
1233 * @method copy_cell
1237 * @method copy_cell
1234 */
1238 */
1235 Notebook.prototype.copy_cell = function () {
1239 Notebook.prototype.copy_cell = function () {
1236 var cell = this.get_selected_cell();
1240 var cell = this.get_selected_cell();
1237 this.clipboard = cell.toJSON();
1241 this.clipboard = cell.toJSON();
1238 // remove undeletable status from the copied cell
1242 // remove undeletable status from the copied cell
1239 if (this.clipboard.metadata.deletable !== undefined) {
1243 if (this.clipboard.metadata.deletable !== undefined) {
1240 delete this.clipboard.metadata.deletable;
1244 delete this.clipboard.metadata.deletable;
1241 }
1245 }
1242 this.enable_paste();
1246 this.enable_paste();
1243 };
1247 };
1244
1248
1245 /**
1249 /**
1246 * Replace the selected cell with a cell in the clipboard.
1250 * Replace the selected cell with a cell in the clipboard.
1247 *
1251 *
1248 * @method paste_cell_replace
1252 * @method paste_cell_replace
1249 */
1253 */
1250 Notebook.prototype.paste_cell_replace = function () {
1254 Notebook.prototype.paste_cell_replace = function () {
1251 if (this.clipboard !== null && this.paste_enabled) {
1255 if (this.clipboard !== null && this.paste_enabled) {
1252 var cell_data = this.clipboard;
1256 var cell_data = this.clipboard;
1253 var new_cell = this.insert_cell_above(cell_data.cell_type);
1257 var new_cell = this.insert_cell_above(cell_data.cell_type);
1254 new_cell.fromJSON(cell_data);
1258 new_cell.fromJSON(cell_data);
1255 var old_cell = this.get_next_cell(new_cell);
1259 var old_cell = this.get_next_cell(new_cell);
1256 this.delete_cell(this.find_cell_index(old_cell));
1260 this.delete_cell(this.find_cell_index(old_cell));
1257 this.select(this.find_cell_index(new_cell));
1261 this.select(this.find_cell_index(new_cell));
1258 }
1262 }
1259 };
1263 };
1260
1264
1261 /**
1265 /**
1262 * Paste a cell from the clipboard above the selected cell.
1266 * Paste a cell from the clipboard above the selected cell.
1263 *
1267 *
1264 * @method paste_cell_above
1268 * @method paste_cell_above
1265 */
1269 */
1266 Notebook.prototype.paste_cell_above = function () {
1270 Notebook.prototype.paste_cell_above = function () {
1267 if (this.clipboard !== null && this.paste_enabled) {
1271 if (this.clipboard !== null && this.paste_enabled) {
1268 var cell_data = this.clipboard;
1272 var cell_data = this.clipboard;
1269 var new_cell = this.insert_cell_above(cell_data.cell_type);
1273 var new_cell = this.insert_cell_above(cell_data.cell_type);
1270 new_cell.fromJSON(cell_data);
1274 new_cell.fromJSON(cell_data);
1271 new_cell.focus_cell();
1275 new_cell.focus_cell();
1272 }
1276 }
1273 };
1277 };
1274
1278
1275 /**
1279 /**
1276 * Paste a cell from the clipboard below the selected cell.
1280 * Paste a cell from the clipboard below the selected cell.
1277 *
1281 *
1278 * @method paste_cell_below
1282 * @method paste_cell_below
1279 */
1283 */
1280 Notebook.prototype.paste_cell_below = function () {
1284 Notebook.prototype.paste_cell_below = function () {
1281 if (this.clipboard !== null && this.paste_enabled) {
1285 if (this.clipboard !== null && this.paste_enabled) {
1282 var cell_data = this.clipboard;
1286 var cell_data = this.clipboard;
1283 var new_cell = this.insert_cell_below(cell_data.cell_type);
1287 var new_cell = this.insert_cell_below(cell_data.cell_type);
1284 new_cell.fromJSON(cell_data);
1288 new_cell.fromJSON(cell_data);
1285 new_cell.focus_cell();
1289 new_cell.focus_cell();
1286 }
1290 }
1287 };
1291 };
1288
1292
1289 // Split/merge
1293 // Split/merge
1290
1294
1291 /**
1295 /**
1292 * Split the selected cell into two, at the cursor.
1296 * Split the selected cell into two, at the cursor.
1293 *
1297 *
1294 * @method split_cell
1298 * @method split_cell
1295 */
1299 */
1296 Notebook.prototype.split_cell = function () {
1300 Notebook.prototype.split_cell = function () {
1297 var cell = this.get_selected_cell();
1301 var cell = this.get_selected_cell();
1298 if (cell.is_splittable()) {
1302 if (cell.is_splittable()) {
1299 var texta = cell.get_pre_cursor();
1303 var texta = cell.get_pre_cursor();
1300 var textb = cell.get_post_cursor();
1304 var textb = cell.get_post_cursor();
1301 cell.set_text(textb);
1305 cell.set_text(textb);
1302 var new_cell = this.insert_cell_above(cell.cell_type);
1306 var new_cell = this.insert_cell_above(cell.cell_type);
1303 // Unrender the new cell so we can call set_text.
1307 // Unrender the new cell so we can call set_text.
1304 new_cell.unrender();
1308 new_cell.unrender();
1305 new_cell.set_text(texta);
1309 new_cell.set_text(texta);
1306 }
1310 }
1307 };
1311 };
1308
1312
1309 /**
1313 /**
1310 * Combine the selected cell into the cell above it.
1314 * Combine the selected cell into the cell above it.
1311 *
1315 *
1312 * @method merge_cell_above
1316 * @method merge_cell_above
1313 */
1317 */
1314 Notebook.prototype.merge_cell_above = function () {
1318 Notebook.prototype.merge_cell_above = function () {
1315 var index = this.get_selected_index();
1319 var index = this.get_selected_index();
1316 var cell = this.get_cell(index);
1320 var cell = this.get_cell(index);
1317 var render = cell.rendered;
1321 var render = cell.rendered;
1318 if (!cell.is_mergeable()) {
1322 if (!cell.is_mergeable()) {
1319 return;
1323 return;
1320 }
1324 }
1321 if (index > 0) {
1325 if (index > 0) {
1322 var upper_cell = this.get_cell(index-1);
1326 var upper_cell = this.get_cell(index-1);
1323 if (!upper_cell.is_mergeable()) {
1327 if (!upper_cell.is_mergeable()) {
1324 return;
1328 return;
1325 }
1329 }
1326 var upper_text = upper_cell.get_text();
1330 var upper_text = upper_cell.get_text();
1327 var text = cell.get_text();
1331 var text = cell.get_text();
1328 if (cell instanceof codecell.CodeCell) {
1332 if (cell instanceof codecell.CodeCell) {
1329 cell.set_text(upper_text+'\n'+text);
1333 cell.set_text(upper_text+'\n'+text);
1330 } else {
1334 } else {
1331 cell.unrender(); // Must unrender before we set_text.
1335 cell.unrender(); // Must unrender before we set_text.
1332 cell.set_text(upper_text+'\n\n'+text);
1336 cell.set_text(upper_text+'\n\n'+text);
1333 if (render) {
1337 if (render) {
1334 // The rendered state of the final cell should match
1338 // The rendered state of the final cell should match
1335 // that of the original selected cell;
1339 // that of the original selected cell;
1336 cell.render();
1340 cell.render();
1337 }
1341 }
1338 }
1342 }
1339 this.delete_cell(index-1);
1343 this.delete_cell(index-1);
1340 this.select(this.find_cell_index(cell));
1344 this.select(this.find_cell_index(cell));
1341 }
1345 }
1342 };
1346 };
1343
1347
1344 /**
1348 /**
1345 * Combine the selected cell into the cell below it.
1349 * Combine the selected cell into the cell below it.
1346 *
1350 *
1347 * @method merge_cell_below
1351 * @method merge_cell_below
1348 */
1352 */
1349 Notebook.prototype.merge_cell_below = function () {
1353 Notebook.prototype.merge_cell_below = function () {
1350 var index = this.get_selected_index();
1354 var index = this.get_selected_index();
1351 var cell = this.get_cell(index);
1355 var cell = this.get_cell(index);
1352 var render = cell.rendered;
1356 var render = cell.rendered;
1353 if (!cell.is_mergeable()) {
1357 if (!cell.is_mergeable()) {
1354 return;
1358 return;
1355 }
1359 }
1356 if (index < this.ncells()-1) {
1360 if (index < this.ncells()-1) {
1357 var lower_cell = this.get_cell(index+1);
1361 var lower_cell = this.get_cell(index+1);
1358 if (!lower_cell.is_mergeable()) {
1362 if (!lower_cell.is_mergeable()) {
1359 return;
1363 return;
1360 }
1364 }
1361 var lower_text = lower_cell.get_text();
1365 var lower_text = lower_cell.get_text();
1362 var text = cell.get_text();
1366 var text = cell.get_text();
1363 if (cell instanceof codecell.CodeCell) {
1367 if (cell instanceof codecell.CodeCell) {
1364 cell.set_text(text+'\n'+lower_text);
1368 cell.set_text(text+'\n'+lower_text);
1365 } else {
1369 } else {
1366 cell.unrender(); // Must unrender before we set_text.
1370 cell.unrender(); // Must unrender before we set_text.
1367 cell.set_text(text+'\n\n'+lower_text);
1371 cell.set_text(text+'\n\n'+lower_text);
1368 if (render) {
1372 if (render) {
1369 // The rendered state of the final cell should match
1373 // The rendered state of the final cell should match
1370 // that of the original selected cell;
1374 // that of the original selected cell;
1371 cell.render();
1375 cell.render();
1372 }
1376 }
1373 }
1377 }
1374 this.delete_cell(index+1);
1378 this.delete_cell(index+1);
1375 this.select(this.find_cell_index(cell));
1379 this.select(this.find_cell_index(cell));
1376 }
1380 }
1377 };
1381 };
1378
1382
1379
1383
1380 // Cell collapsing and output clearing
1384 // Cell collapsing and output clearing
1381
1385
1382 /**
1386 /**
1383 * Hide a cell's output.
1387 * Hide a cell's output.
1384 *
1388 *
1385 * @method collapse_output
1389 * @method collapse_output
1386 * @param {Number} index A cell's numeric index
1390 * @param {Number} index A cell's numeric index
1387 */
1391 */
1388 Notebook.prototype.collapse_output = function (index) {
1392 Notebook.prototype.collapse_output = function (index) {
1389 var i = this.index_or_selected(index);
1393 var i = this.index_or_selected(index);
1390 var cell = this.get_cell(i);
1394 var cell = this.get_cell(i);
1391 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1395 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1392 cell.collapse_output();
1396 cell.collapse_output();
1393 this.set_dirty(true);
1397 this.set_dirty(true);
1394 }
1398 }
1395 };
1399 };
1396
1400
1397 /**
1401 /**
1398 * Hide each code cell's output area.
1402 * Hide each code cell's output area.
1399 *
1403 *
1400 * @method collapse_all_output
1404 * @method collapse_all_output
1401 */
1405 */
1402 Notebook.prototype.collapse_all_output = function () {
1406 Notebook.prototype.collapse_all_output = function () {
1403 this.get_cells().map(function (cell, i) {
1407 this.get_cells().map(function (cell, i) {
1404 if (cell instanceof codecell.CodeCell) {
1408 if (cell instanceof codecell.CodeCell) {
1405 cell.collapse_output();
1409 cell.collapse_output();
1406 }
1410 }
1407 });
1411 });
1408 // this should not be set if the `collapse` key is removed from nbformat
1412 // this should not be set if the `collapse` key is removed from nbformat
1409 this.set_dirty(true);
1413 this.set_dirty(true);
1410 };
1414 };
1411
1415
1412 /**
1416 /**
1413 * Show a cell's output.
1417 * Show a cell's output.
1414 *
1418 *
1415 * @method expand_output
1419 * @method expand_output
1416 * @param {Number} index A cell's numeric index
1420 * @param {Number} index A cell's numeric index
1417 */
1421 */
1418 Notebook.prototype.expand_output = function (index) {
1422 Notebook.prototype.expand_output = function (index) {
1419 var i = this.index_or_selected(index);
1423 var i = this.index_or_selected(index);
1420 var cell = this.get_cell(i);
1424 var cell = this.get_cell(i);
1421 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1425 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1422 cell.expand_output();
1426 cell.expand_output();
1423 this.set_dirty(true);
1427 this.set_dirty(true);
1424 }
1428 }
1425 };
1429 };
1426
1430
1427 /**
1431 /**
1428 * Expand each code cell's output area, and remove scrollbars.
1432 * Expand each code cell's output area, and remove scrollbars.
1429 *
1433 *
1430 * @method expand_all_output
1434 * @method expand_all_output
1431 */
1435 */
1432 Notebook.prototype.expand_all_output = function () {
1436 Notebook.prototype.expand_all_output = function () {
1433 this.get_cells().map(function (cell, i) {
1437 this.get_cells().map(function (cell, i) {
1434 if (cell instanceof codecell.CodeCell) {
1438 if (cell instanceof codecell.CodeCell) {
1435 cell.expand_output();
1439 cell.expand_output();
1436 }
1440 }
1437 });
1441 });
1438 // this should not be set if the `collapse` key is removed from nbformat
1442 // this should not be set if the `collapse` key is removed from nbformat
1439 this.set_dirty(true);
1443 this.set_dirty(true);
1440 };
1444 };
1441
1445
1442 /**
1446 /**
1443 * Clear the selected CodeCell's output area.
1447 * Clear the selected CodeCell's output area.
1444 *
1448 *
1445 * @method clear_output
1449 * @method clear_output
1446 * @param {Number} index A cell's numeric index
1450 * @param {Number} index A cell's numeric index
1447 */
1451 */
1448 Notebook.prototype.clear_output = function (index) {
1452 Notebook.prototype.clear_output = function (index) {
1449 var i = this.index_or_selected(index);
1453 var i = this.index_or_selected(index);
1450 var cell = this.get_cell(i);
1454 var cell = this.get_cell(i);
1451 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1455 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1452 cell.clear_output();
1456 cell.clear_output();
1453 this.set_dirty(true);
1457 this.set_dirty(true);
1454 }
1458 }
1455 };
1459 };
1456
1460
1457 /**
1461 /**
1458 * Clear each code cell's output area.
1462 * Clear each code cell's output area.
1459 *
1463 *
1460 * @method clear_all_output
1464 * @method clear_all_output
1461 */
1465 */
1462 Notebook.prototype.clear_all_output = function () {
1466 Notebook.prototype.clear_all_output = function () {
1463 this.get_cells().map(function (cell, i) {
1467 this.get_cells().map(function (cell, i) {
1464 if (cell instanceof codecell.CodeCell) {
1468 if (cell instanceof codecell.CodeCell) {
1465 cell.clear_output();
1469 cell.clear_output();
1466 }
1470 }
1467 });
1471 });
1468 this.set_dirty(true);
1472 this.set_dirty(true);
1469 };
1473 };
1470
1474
1471 /**
1475 /**
1472 * Scroll the selected CodeCell's output area.
1476 * Scroll the selected CodeCell's output area.
1473 *
1477 *
1474 * @method scroll_output
1478 * @method scroll_output
1475 * @param {Number} index A cell's numeric index
1479 * @param {Number} index A cell's numeric index
1476 */
1480 */
1477 Notebook.prototype.scroll_output = function (index) {
1481 Notebook.prototype.scroll_output = function (index) {
1478 var i = this.index_or_selected(index);
1482 var i = this.index_or_selected(index);
1479 var cell = this.get_cell(i);
1483 var cell = this.get_cell(i);
1480 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1484 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1481 cell.scroll_output();
1485 cell.scroll_output();
1482 this.set_dirty(true);
1486 this.set_dirty(true);
1483 }
1487 }
1484 };
1488 };
1485
1489
1486 /**
1490 /**
1487 * Expand each code cell's output area, and add a scrollbar for long output.
1491 * Expand each code cell's output area, and add a scrollbar for long output.
1488 *
1492 *
1489 * @method scroll_all_output
1493 * @method scroll_all_output
1490 */
1494 */
1491 Notebook.prototype.scroll_all_output = function () {
1495 Notebook.prototype.scroll_all_output = function () {
1492 this.get_cells().map(function (cell, i) {
1496 this.get_cells().map(function (cell, i) {
1493 if (cell instanceof codecell.CodeCell) {
1497 if (cell instanceof codecell.CodeCell) {
1494 cell.scroll_output();
1498 cell.scroll_output();
1495 }
1499 }
1496 });
1500 });
1497 // this should not be set if the `collapse` key is removed from nbformat
1501 // this should not be set if the `collapse` key is removed from nbformat
1498 this.set_dirty(true);
1502 this.set_dirty(true);
1499 };
1503 };
1500
1504
1501 /** Toggle whether a cell's output is collapsed or expanded.
1505 /** Toggle whether a cell's output is collapsed or expanded.
1502 *
1506 *
1503 * @method toggle_output
1507 * @method toggle_output
1504 * @param {Number} index A cell's numeric index
1508 * @param {Number} index A cell's numeric index
1505 */
1509 */
1506 Notebook.prototype.toggle_output = function (index) {
1510 Notebook.prototype.toggle_output = function (index) {
1507 var i = this.index_or_selected(index);
1511 var i = this.index_or_selected(index);
1508 var cell = this.get_cell(i);
1512 var cell = this.get_cell(i);
1509 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1513 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1510 cell.toggle_output();
1514 cell.toggle_output();
1511 this.set_dirty(true);
1515 this.set_dirty(true);
1512 }
1516 }
1513 };
1517 };
1514
1518
1515 /**
1519 /**
1516 * Hide/show the output of all cells.
1520 * Hide/show the output of all cells.
1517 *
1521 *
1518 * @method toggle_all_output
1522 * @method toggle_all_output
1519 */
1523 */
1520 Notebook.prototype.toggle_all_output = function () {
1524 Notebook.prototype.toggle_all_output = function () {
1521 this.get_cells().map(function (cell, i) {
1525 this.get_cells().map(function (cell, i) {
1522 if (cell instanceof codecell.CodeCell) {
1526 if (cell instanceof codecell.CodeCell) {
1523 cell.toggle_output();
1527 cell.toggle_output();
1524 }
1528 }
1525 });
1529 });
1526 // this should not be set if the `collapse` key is removed from nbformat
1530 // this should not be set if the `collapse` key is removed from nbformat
1527 this.set_dirty(true);
1531 this.set_dirty(true);
1528 };
1532 };
1529
1533
1530 /**
1534 /**
1531 * Toggle a scrollbar for long cell outputs.
1535 * Toggle a scrollbar for long cell outputs.
1532 *
1536 *
1533 * @method toggle_output_scroll
1537 * @method toggle_output_scroll
1534 * @param {Number} index A cell's numeric index
1538 * @param {Number} index A cell's numeric index
1535 */
1539 */
1536 Notebook.prototype.toggle_output_scroll = function (index) {
1540 Notebook.prototype.toggle_output_scroll = function (index) {
1537 var i = this.index_or_selected(index);
1541 var i = this.index_or_selected(index);
1538 var cell = this.get_cell(i);
1542 var cell = this.get_cell(i);
1539 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1543 if (cell !== null && (cell instanceof codecell.CodeCell)) {
1540 cell.toggle_output_scroll();
1544 cell.toggle_output_scroll();
1541 this.set_dirty(true);
1545 this.set_dirty(true);
1542 }
1546 }
1543 };
1547 };
1544
1548
1545 /**
1549 /**
1546 * Toggle the scrolling of long output on all cells.
1550 * Toggle the scrolling of long output on all cells.
1547 *
1551 *
1548 * @method toggle_all_output_scrolling
1552 * @method toggle_all_output_scrolling
1549 */
1553 */
1550 Notebook.prototype.toggle_all_output_scroll = function () {
1554 Notebook.prototype.toggle_all_output_scroll = function () {
1551 this.get_cells().map(function (cell, i) {
1555 this.get_cells().map(function (cell, i) {
1552 if (cell instanceof codecell.CodeCell) {
1556 if (cell instanceof codecell.CodeCell) {
1553 cell.toggle_output_scroll();
1557 cell.toggle_output_scroll();
1554 }
1558 }
1555 });
1559 });
1556 // this should not be set if the `collapse` key is removed from nbformat
1560 // this should not be set if the `collapse` key is removed from nbformat
1557 this.set_dirty(true);
1561 this.set_dirty(true);
1558 };
1562 };
1559
1563
1560 // Other cell functions: line numbers, ...
1564 // Other cell functions: line numbers, ...
1561
1565
1562 /**
1566 /**
1563 * Toggle line numbers in the selected cell's input area.
1567 * Toggle line numbers in the selected cell's input area.
1564 *
1568 *
1565 * @method cell_toggle_line_numbers
1569 * @method cell_toggle_line_numbers
1566 */
1570 */
1567 Notebook.prototype.cell_toggle_line_numbers = function() {
1571 Notebook.prototype.cell_toggle_line_numbers = function() {
1568 this.get_selected_cell().toggle_line_numbers();
1572 this.get_selected_cell().toggle_line_numbers();
1569 };
1573 };
1570
1574
1571 /**
1575 /**
1572 * Set the codemirror mode for all code cells, including the default for
1576 * Set the codemirror mode for all code cells, including the default for
1573 * new code cells.
1577 * new code cells.
1574 *
1578 *
1575 * @method set_codemirror_mode
1579 * @method set_codemirror_mode
1576 */
1580 */
1577 Notebook.prototype.set_codemirror_mode = function(newmode){
1581 Notebook.prototype.set_codemirror_mode = function(newmode){
1578 if (newmode === this.codemirror_mode) {
1582 if (newmode === this.codemirror_mode) {
1579 return;
1583 return;
1580 }
1584 }
1581 this.codemirror_mode = newmode;
1585 this.codemirror_mode = newmode;
1582 codecell.CodeCell.options_default.cm_config.mode = newmode;
1586 codecell.CodeCell.options_default.cm_config.mode = newmode;
1583 var modename = newmode.mode || newmode.name || newmode;
1587 var modename = newmode.mode || newmode.name || newmode;
1584
1588
1585 var that = this;
1589 var that = this;
1586 utils.requireCodeMirrorMode(modename, function () {
1590 utils.requireCodeMirrorMode(modename, function () {
1587 that.get_cells().map(function(cell, i) {
1591 that.get_cells().map(function(cell, i) {
1588 if (cell.cell_type === 'code'){
1592 if (cell.cell_type === 'code'){
1589 cell.code_mirror.setOption('mode', newmode);
1593 cell.code_mirror.setOption('mode', newmode);
1590 // This is currently redundant, because cm_config ends up as
1594 // This is currently redundant, because cm_config ends up as
1591 // codemirror's own .options object, but I don't want to
1595 // codemirror's own .options object, but I don't want to
1592 // rely on that.
1596 // rely on that.
1593 cell.cm_config.mode = newmode;
1597 cell.cm_config.mode = newmode;
1594 }
1598 }
1595 });
1599 });
1596 });
1600 });
1597 };
1601 };
1598
1602
1599 // Session related things
1603 // Session related things
1600
1604
1601 /**
1605 /**
1602 * Start a new session and set it on each code cell.
1606 * Start a new session and set it on each code cell.
1603 *
1607 *
1604 * @method start_session
1608 * @method start_session
1605 */
1609 */
1606 Notebook.prototype.start_session = function (kernel_name) {
1610 Notebook.prototype.start_session = function (kernel_name) {
1607 if (this._session_starting) {
1611 if (this._session_starting) {
1608 throw new session.SessionAlreadyStarting();
1612 throw new session.SessionAlreadyStarting();
1609 }
1613 }
1610 this._session_starting = true;
1614 this._session_starting = true;
1611
1615
1612 var options = {
1616 var options = {
1613 base_url: this.base_url,
1617 base_url: this.base_url,
1614 ws_url: this.ws_url,
1618 ws_url: this.ws_url,
1615 notebook_path: this.notebook_path,
1619 notebook_path: this.notebook_path,
1616 notebook_name: this.notebook_name,
1620 notebook_name: this.notebook_name,
1617 kernel_name: kernel_name,
1621 kernel_name: kernel_name,
1618 notebook: this
1622 notebook: this
1619 };
1623 };
1620
1624
1621 var success = $.proxy(this._session_started, this);
1625 var success = $.proxy(this._session_started, this);
1622 var failure = $.proxy(this._session_start_failed, this);
1626 var failure = $.proxy(this._session_start_failed, this);
1623
1627
1624 if (this.session !== null) {
1628 if (this.session !== null) {
1625 this.session.restart(options, success, failure);
1629 this.session.restart(options, success, failure);
1626 } else {
1630 } else {
1627 this.session = new session.Session(options);
1631 this.session = new session.Session(options);
1628 this.session.start(success, failure);
1632 this.session.start(success, failure);
1629 }
1633 }
1630 };
1634 };
1631
1635
1632
1636
1633 /**
1637 /**
1634 * Once a session is started, link the code cells to the kernel and pass the
1638 * Once a session is started, link the code cells to the kernel and pass the
1635 * comm manager to the widget manager
1639 * comm manager to the widget manager
1636 *
1640 *
1637 */
1641 */
1638 Notebook.prototype._session_started = function (){
1642 Notebook.prototype._session_started = function (){
1639 this._session_starting = false;
1643 this._session_starting = false;
1640 this.kernel = this.session.kernel;
1644 this.kernel = this.session.kernel;
1641 var ncells = this.ncells();
1645 var ncells = this.ncells();
1642 for (var i=0; i<ncells; i++) {
1646 for (var i=0; i<ncells; i++) {
1643 var cell = this.get_cell(i);
1647 var cell = this.get_cell(i);
1644 if (cell instanceof codecell.CodeCell) {
1648 if (cell instanceof codecell.CodeCell) {
1645 cell.set_kernel(this.session.kernel);
1649 cell.set_kernel(this.session.kernel);
1646 }
1650 }
1647 }
1651 }
1648 };
1652 };
1649 Notebook.prototype._session_start_failed = function (jqxhr, status, error){
1653 Notebook.prototype._session_start_failed = function (jqxhr, status, error){
1650 this._session_starting = false;
1654 this._session_starting = false;
1651 utils.log_ajax_error(jqxhr, status, error);
1655 utils.log_ajax_error(jqxhr, status, error);
1652 };
1656 };
1653
1657
1654 /**
1658 /**
1655 * Prompt the user to restart the IPython kernel.
1659 * Prompt the user to restart the IPython kernel.
1656 *
1660 *
1657 * @method restart_kernel
1661 * @method restart_kernel
1658 */
1662 */
1659 Notebook.prototype.restart_kernel = function () {
1663 Notebook.prototype.restart_kernel = function () {
1660 var that = this;
1664 var that = this;
1661 dialog.modal({
1665 dialog.modal({
1662 notebook: this,
1666 notebook: this,
1663 keyboard_manager: this.keyboard_manager,
1667 keyboard_manager: this.keyboard_manager,
1664 title : "Restart kernel or continue running?",
1668 title : "Restart kernel or continue running?",
1665 body : $("<p/>").text(
1669 body : $("<p/>").text(
1666 'Do you want to restart the current kernel? You will lose all variables defined in it.'
1670 'Do you want to restart the current kernel? You will lose all variables defined in it.'
1667 ),
1671 ),
1668 buttons : {
1672 buttons : {
1669 "Continue running" : {},
1673 "Continue running" : {},
1670 "Restart" : {
1674 "Restart" : {
1671 "class" : "btn-danger",
1675 "class" : "btn-danger",
1672 "click" : function() {
1676 "click" : function() {
1673 that.kernel.restart();
1677 that.kernel.restart();
1674 }
1678 }
1675 }
1679 }
1676 }
1680 }
1677 });
1681 });
1678 };
1682 };
1679
1683
1680 /**
1684 /**
1681 * Execute or render cell outputs and go into command mode.
1685 * Execute or render cell outputs and go into command mode.
1682 *
1686 *
1683 * @method execute_cell
1687 * @method execute_cell
1684 */
1688 */
1685 Notebook.prototype.execute_cell = function () {
1689 Notebook.prototype.execute_cell = function () {
1686 /**
1690 /**
1687 * mode = shift, ctrl, alt
1691 * mode = shift, ctrl, alt
1688 */
1692 */
1689 var cell = this.get_selected_cell();
1693 var cell = this.get_selected_cell();
1690
1694
1691 cell.execute();
1695 cell.execute();
1692 this.command_mode();
1696 this.command_mode();
1693 this.set_dirty(true);
1697 this.set_dirty(true);
1694 };
1698 };
1695
1699
1696 /**
1700 /**
1697 * Execute or render cell outputs and insert a new cell below.
1701 * Execute or render cell outputs and insert a new cell below.
1698 *
1702 *
1699 * @method execute_cell_and_insert_below
1703 * @method execute_cell_and_insert_below
1700 */
1704 */
1701 Notebook.prototype.execute_cell_and_insert_below = function () {
1705 Notebook.prototype.execute_cell_and_insert_below = function () {
1702 var cell = this.get_selected_cell();
1706 var cell = this.get_selected_cell();
1703 var cell_index = this.find_cell_index(cell);
1707 var cell_index = this.find_cell_index(cell);
1704
1708
1705 cell.execute();
1709 cell.execute();
1706
1710
1707 // If we are at the end always insert a new cell and return
1711 // If we are at the end always insert a new cell and return
1708 if (cell_index === (this.ncells()-1)) {
1712 if (cell_index === (this.ncells()-1)) {
1709 this.command_mode();
1713 this.command_mode();
1710 this.insert_cell_below();
1714 this.insert_cell_below();
1711 this.select(cell_index+1);
1715 this.select(cell_index+1);
1712 this.edit_mode();
1716 this.edit_mode();
1713 this.scroll_to_bottom();
1717 this.scroll_to_bottom();
1714 this.set_dirty(true);
1718 this.set_dirty(true);
1715 return;
1719 return;
1716 }
1720 }
1717
1721
1718 this.command_mode();
1722 this.command_mode();
1719 this.insert_cell_below();
1723 this.insert_cell_below();
1720 this.select(cell_index+1);
1724 this.select(cell_index+1);
1721 this.edit_mode();
1725 this.edit_mode();
1722 this.set_dirty(true);
1726 this.set_dirty(true);
1723 };
1727 };
1724
1728
1725 /**
1729 /**
1726 * Execute or render cell outputs and select the next cell.
1730 * Execute or render cell outputs and select the next cell.
1727 *
1731 *
1728 * @method execute_cell_and_select_below
1732 * @method execute_cell_and_select_below
1729 */
1733 */
1730 Notebook.prototype.execute_cell_and_select_below = function () {
1734 Notebook.prototype.execute_cell_and_select_below = function () {
1731
1735
1732 var cell = this.get_selected_cell();
1736 var cell = this.get_selected_cell();
1733 var cell_index = this.find_cell_index(cell);
1737 var cell_index = this.find_cell_index(cell);
1734
1738
1735 cell.execute();
1739 cell.execute();
1736
1740
1737 // If we are at the end always insert a new cell and return
1741 // If we are at the end always insert a new cell and return
1738 if (cell_index === (this.ncells()-1)) {
1742 if (cell_index === (this.ncells()-1)) {
1739 this.command_mode();
1743 this.command_mode();
1740 this.insert_cell_below();
1744 this.insert_cell_below();
1741 this.select(cell_index+1);
1745 this.select(cell_index+1);
1742 this.edit_mode();
1746 this.edit_mode();
1743 this.scroll_to_bottom();
1747 this.scroll_to_bottom();
1744 this.set_dirty(true);
1748 this.set_dirty(true);
1745 return;
1749 return;
1746 }
1750 }
1747
1751
1748 this.command_mode();
1752 this.command_mode();
1749 this.select(cell_index+1);
1753 this.select(cell_index+1);
1750 this.focus_cell();
1754 this.focus_cell();
1751 this.set_dirty(true);
1755 this.set_dirty(true);
1752 };
1756 };
1753
1757
1754 /**
1758 /**
1755 * Execute all cells below the selected cell.
1759 * Execute all cells below the selected cell.
1756 *
1760 *
1757 * @method execute_cells_below
1761 * @method execute_cells_below
1758 */
1762 */
1759 Notebook.prototype.execute_cells_below = function () {
1763 Notebook.prototype.execute_cells_below = function () {
1760 this.execute_cell_range(this.get_selected_index(), this.ncells());
1764 this.execute_cell_range(this.get_selected_index(), this.ncells());
1761 this.scroll_to_bottom();
1765 this.scroll_to_bottom();
1762 };
1766 };
1763
1767
1764 /**
1768 /**
1765 * Execute all cells above the selected cell.
1769 * Execute all cells above the selected cell.
1766 *
1770 *
1767 * @method execute_cells_above
1771 * @method execute_cells_above
1768 */
1772 */
1769 Notebook.prototype.execute_cells_above = function () {
1773 Notebook.prototype.execute_cells_above = function () {
1770 this.execute_cell_range(0, this.get_selected_index());
1774 this.execute_cell_range(0, this.get_selected_index());
1771 };
1775 };
1772
1776
1773 /**
1777 /**
1774 * Execute all cells.
1778 * Execute all cells.
1775 *
1779 *
1776 * @method execute_all_cells
1780 * @method execute_all_cells
1777 */
1781 */
1778 Notebook.prototype.execute_all_cells = function () {
1782 Notebook.prototype.execute_all_cells = function () {
1779 this.execute_cell_range(0, this.ncells());
1783 this.execute_cell_range(0, this.ncells());
1780 this.scroll_to_bottom();
1784 this.scroll_to_bottom();
1781 };
1785 };
1782
1786
1783 /**
1787 /**
1784 * Execute a contiguous range of cells.
1788 * Execute a contiguous range of cells.
1785 *
1789 *
1786 * @method execute_cell_range
1790 * @method execute_cell_range
1787 * @param {Number} start Index of the first cell to execute (inclusive)
1791 * @param {Number} start Index of the first cell to execute (inclusive)
1788 * @param {Number} end Index of the last cell to execute (exclusive)
1792 * @param {Number} end Index of the last cell to execute (exclusive)
1789 */
1793 */
1790 Notebook.prototype.execute_cell_range = function (start, end) {
1794 Notebook.prototype.execute_cell_range = function (start, end) {
1791 this.command_mode();
1795 this.command_mode();
1792 for (var i=start; i<end; i++) {
1796 for (var i=start; i<end; i++) {
1793 this.select(i);
1797 this.select(i);
1794 this.execute_cell();
1798 this.execute_cell();
1795 }
1799 }
1796 };
1800 };
1797
1801
1798 // Persistance and loading
1802 // Persistance and loading
1799
1803
1800 /**
1804 /**
1801 * Getter method for this notebook's name.
1805 * Getter method for this notebook's name.
1802 *
1806 *
1803 * @method get_notebook_name
1807 * @method get_notebook_name
1804 * @return {String} This notebook's name (excluding file extension)
1808 * @return {String} This notebook's name (excluding file extension)
1805 */
1809 */
1806 Notebook.prototype.get_notebook_name = function () {
1810 Notebook.prototype.get_notebook_name = function () {
1807 var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
1811 var nbname = this.notebook_name.substring(0,this.notebook_name.length-6);
1808 return nbname;
1812 return nbname;
1809 };
1813 };
1810
1814
1811 /**
1815 /**
1812 * Setter method for this notebook's name.
1816 * Setter method for this notebook's name.
1813 *
1817 *
1814 * @method set_notebook_name
1818 * @method set_notebook_name
1815 * @param {String} name A new name for this notebook
1819 * @param {String} name A new name for this notebook
1816 */
1820 */
1817 Notebook.prototype.set_notebook_name = function (name) {
1821 Notebook.prototype.set_notebook_name = function (name) {
1818 var parent = utils.url_path_split(this.notebook_path)[0];
1822 var parent = utils.url_path_split(this.notebook_path)[0];
1819 this.notebook_name = name;
1823 this.notebook_name = name;
1820 this.notebook_path = utils.url_path_join(parent, name);
1824 this.notebook_path = utils.url_path_join(parent, name);
1821 };
1825 };
1822
1826
1823 /**
1827 /**
1824 * Check that a notebook's name is valid.
1828 * Check that a notebook's name is valid.
1825 *
1829 *
1826 * @method test_notebook_name
1830 * @method test_notebook_name
1827 * @param {String} nbname A name for this notebook
1831 * @param {String} nbname A name for this notebook
1828 * @return {Boolean} True if the name is valid, false if invalid
1832 * @return {Boolean} True if the name is valid, false if invalid
1829 */
1833 */
1830 Notebook.prototype.test_notebook_name = function (nbname) {
1834 Notebook.prototype.test_notebook_name = function (nbname) {
1831 nbname = nbname || '';
1835 nbname = nbname || '';
1832 if (nbname.length>0 && !this.notebook_name_blacklist_re.test(nbname)) {
1836 if (nbname.length>0 && !this.notebook_name_blacklist_re.test(nbname)) {
1833 return true;
1837 return true;
1834 } else {
1838 } else {
1835 return false;
1839 return false;
1836 }
1840 }
1837 };
1841 };
1838
1842
1839 /**
1843 /**
1840 * Load a notebook from JSON (.ipynb).
1844 * Load a notebook from JSON (.ipynb).
1841 *
1845 *
1842 * @method fromJSON
1846 * @method fromJSON
1843 * @param {Object} data JSON representation of a notebook
1847 * @param {Object} data JSON representation of a notebook
1844 */
1848 */
1845 Notebook.prototype.fromJSON = function (data) {
1849 Notebook.prototype.fromJSON = function (data) {
1846
1850
1847 var content = data.content;
1851 var content = data.content;
1848 var ncells = this.ncells();
1852 var ncells = this.ncells();
1849 var i;
1853 var i;
1850 for (i=0; i<ncells; i++) {
1854 for (i=0; i<ncells; i++) {
1851 // Always delete cell 0 as they get renumbered as they are deleted.
1855 // Always delete cell 0 as they get renumbered as they are deleted.
1852 this.delete_cell(0);
1856 this.delete_cell(0);
1853 }
1857 }
1854 // Save the metadata and name.
1858 // Save the metadata and name.
1855 this.metadata = content.metadata;
1859 this.metadata = content.metadata;
1856 this.notebook_name = data.name;
1860 this.notebook_name = data.name;
1857 this.notebook_path = data.path;
1861 this.notebook_path = data.path;
1858 var trusted = true;
1862 var trusted = true;
1859
1863
1860 // Trigger an event changing the kernel spec - this will set the default
1864 // Trigger an event changing the kernel spec - this will set the default
1861 // codemirror mode
1865 // codemirror mode
1862 if (this.metadata.kernelspec !== undefined) {
1866 if (this.metadata.kernelspec !== undefined) {
1863 this.events.trigger('spec_changed.Kernel', this.metadata.kernelspec);
1867 this.events.trigger('spec_changed.Kernel', this.metadata.kernelspec);
1864 }
1868 }
1865
1869
1866 // Set the codemirror mode from language_info metadata
1870 // Set the codemirror mode from language_info metadata
1867 if (this.metadata.language_info !== undefined) {
1871 if (this.metadata.language_info !== undefined) {
1868 var langinfo = this.metadata.language_info;
1872 var langinfo = this.metadata.language_info;
1869 // Mode 'null' should be plain, unhighlighted text.
1873 // Mode 'null' should be plain, unhighlighted text.
1870 var cm_mode = langinfo.codemirror_mode || langinfo.name || 'null';
1874 var cm_mode = langinfo.codemirror_mode || langinfo.name || 'null';
1871 this.set_codemirror_mode(cm_mode);
1875 this.set_codemirror_mode(cm_mode);
1872 }
1876 }
1873
1877
1874 var new_cells = content.cells;
1878 var new_cells = content.cells;
1875 ncells = new_cells.length;
1879 ncells = new_cells.length;
1876 var cell_data = null;
1880 var cell_data = null;
1877 var new_cell = null;
1881 var new_cell = null;
1878 for (i=0; i<ncells; i++) {
1882 for (i=0; i<ncells; i++) {
1879 cell_data = new_cells[i];
1883 cell_data = new_cells[i];
1880 new_cell = this.insert_cell_at_index(cell_data.cell_type, i);
1884 new_cell = this.insert_cell_at_index(cell_data.cell_type, i);
1881 new_cell.fromJSON(cell_data);
1885 new_cell.fromJSON(cell_data);
1882 if (new_cell.cell_type == 'code' && !new_cell.output_area.trusted) {
1886 if (new_cell.cell_type == 'code' && !new_cell.output_area.trusted) {
1883 trusted = false;
1887 trusted = false;
1884 }
1888 }
1885 }
1889 }
1886 if (trusted !== this.trusted) {
1890 if (trusted !== this.trusted) {
1887 this.trusted = trusted;
1891 this.trusted = trusted;
1888 this.events.trigger("trust_changed.Notebook", trusted);
1892 this.events.trigger("trust_changed.Notebook", trusted);
1889 }
1893 }
1890 };
1894 };
1891
1895
1892 /**
1896 /**
1893 * Dump this notebook into a JSON-friendly object.
1897 * Dump this notebook into a JSON-friendly object.
1894 *
1898 *
1895 * @method toJSON
1899 * @method toJSON
1896 * @return {Object} A JSON-friendly representation of this notebook.
1900 * @return {Object} A JSON-friendly representation of this notebook.
1897 */
1901 */
1898 Notebook.prototype.toJSON = function () {
1902 Notebook.prototype.toJSON = function () {
1899 /**
1903 /**
1900 * remove the conversion indicator, which only belongs in-memory
1904 * remove the conversion indicator, which only belongs in-memory
1901 */
1905 */
1902 delete this.metadata.orig_nbformat;
1906 delete this.metadata.orig_nbformat;
1903 delete this.metadata.orig_nbformat_minor;
1907 delete this.metadata.orig_nbformat_minor;
1904
1908
1905 var cells = this.get_cells();
1909 var cells = this.get_cells();
1906 var ncells = cells.length;
1910 var ncells = cells.length;
1907 var cell_array = new Array(ncells);
1911 var cell_array = new Array(ncells);
1908 var trusted = true;
1912 var trusted = true;
1909 for (var i=0; i<ncells; i++) {
1913 for (var i=0; i<ncells; i++) {
1910 var cell = cells[i];
1914 var cell = cells[i];
1911 if (cell.cell_type == 'code' && !cell.output_area.trusted) {
1915 if (cell.cell_type == 'code' && !cell.output_area.trusted) {
1912 trusted = false;
1916 trusted = false;
1913 }
1917 }
1914 cell_array[i] = cell.toJSON();
1918 cell_array[i] = cell.toJSON();
1915 }
1919 }
1916 var data = {
1920 var data = {
1917 cells: cell_array,
1921 cells: cell_array,
1918 metadata: this.metadata,
1922 metadata: this.metadata,
1919 nbformat: this.nbformat,
1923 nbformat: this.nbformat,
1920 nbformat_minor: this.nbformat_minor
1924 nbformat_minor: this.nbformat_minor
1921 };
1925 };
1922 if (trusted != this.trusted) {
1926 if (trusted != this.trusted) {
1923 this.trusted = trusted;
1927 this.trusted = trusted;
1924 this.events.trigger("trust_changed.Notebook", trusted);
1928 this.events.trigger("trust_changed.Notebook", trusted);
1925 }
1929 }
1926 return data;
1930 return data;
1927 };
1931 };
1928
1932
1929 /**
1933 /**
1930 * Start an autosave timer, for periodically saving the notebook.
1934 * Start an autosave timer, for periodically saving the notebook.
1931 *
1935 *
1932 * @method set_autosave_interval
1936 * @method set_autosave_interval
1933 * @param {Integer} interval the autosave interval in milliseconds
1937 * @param {Integer} interval the autosave interval in milliseconds
1934 */
1938 */
1935 Notebook.prototype.set_autosave_interval = function (interval) {
1939 Notebook.prototype.set_autosave_interval = function (interval) {
1936 var that = this;
1940 var that = this;
1937 // clear previous interval, so we don't get simultaneous timers
1941 // clear previous interval, so we don't get simultaneous timers
1938 if (this.autosave_timer) {
1942 if (this.autosave_timer) {
1939 clearInterval(this.autosave_timer);
1943 clearInterval(this.autosave_timer);
1940 }
1944 }
1941 if (!this.writable) {
1945 if (!this.writable) {
1942 // disable autosave if not writable
1946 // disable autosave if not writable
1943 interval = 0;
1947 interval = 0;
1944 }
1948 }
1945
1949
1946 this.autosave_interval = this.minimum_autosave_interval = interval;
1950 this.autosave_interval = this.minimum_autosave_interval = interval;
1947 if (interval) {
1951 if (interval) {
1948 this.autosave_timer = setInterval(function() {
1952 this.autosave_timer = setInterval(function() {
1949 if (that.dirty) {
1953 if (that.dirty) {
1950 that.save_notebook();
1954 that.save_notebook();
1951 }
1955 }
1952 }, interval);
1956 }, interval);
1953 this.events.trigger("autosave_enabled.Notebook", interval);
1957 this.events.trigger("autosave_enabled.Notebook", interval);
1954 } else {
1958 } else {
1955 this.autosave_timer = null;
1959 this.autosave_timer = null;
1956 this.events.trigger("autosave_disabled.Notebook");
1960 this.events.trigger("autosave_disabled.Notebook");
1957 }
1961 }
1958 };
1962 };
1959
1963
1960 /**
1964 /**
1961 * Save this notebook on the server. This becomes a notebook instance's
1965 * Save this notebook on the server. This becomes a notebook instance's
1962 * .save_notebook method *after* the entire notebook has been loaded.
1966 * .save_notebook method *after* the entire notebook has been loaded.
1963 *
1967 *
1964 * @method save_notebook
1968 * @method save_notebook
1965 */
1969 */
1966 Notebook.prototype.save_notebook = function () {
1970 Notebook.prototype.save_notebook = function () {
1967 if (!this._fully_loaded) {
1971 if (!this._fully_loaded) {
1968 this.events.trigger('notebook_save_failed.Notebook',
1972 this.events.trigger('notebook_save_failed.Notebook',
1969 new Error("Load failed, save is disabled")
1973 new Error("Load failed, save is disabled")
1970 );
1974 );
1971 return;
1975 return;
1972 } else if (!this.writable) {
1976 } else if (!this.writable) {
1973 this.events.trigger('notebook_save_failed.Notebook',
1977 this.events.trigger('notebook_save_failed.Notebook',
1974 new Error("Notebook is read-only")
1978 new Error("Notebook is read-only")
1975 );
1979 );
1976 return;
1980 return;
1977 }
1981 }
1978
1982
1979 // Create a JSON model to be sent to the server.
1983 // Create a JSON model to be sent to the server.
1980 var model = {
1984 var model = {
1981 type : "notebook",
1985 type : "notebook",
1982 content : this.toJSON()
1986 content : this.toJSON()
1983 };
1987 };
1984 // time the ajax call for autosave tuning purposes.
1988 // time the ajax call for autosave tuning purposes.
1985 var start = new Date().getTime();
1989 var start = new Date().getTime();
1986
1990
1987 var that = this;
1991 var that = this;
1988 return this.contents.save(this.notebook_path, model).then(
1992 return this.contents.save(this.notebook_path, model).then(
1989 $.proxy(this.save_notebook_success, this, start),
1993 $.proxy(this.save_notebook_success, this, start),
1990 function (error) {
1994 function (error) {
1991 that.events.trigger('notebook_save_failed.Notebook', error);
1995 that.events.trigger('notebook_save_failed.Notebook', error);
1992 }
1996 }
1993 );
1997 );
1994 };
1998 };
1995
1999
1996 /**
2000 /**
1997 * Success callback for saving a notebook.
2001 * Success callback for saving a notebook.
1998 *
2002 *
1999 * @method save_notebook_success
2003 * @method save_notebook_success
2000 * @param {Integer} start Time when the save request start
2004 * @param {Integer} start Time when the save request start
2001 * @param {Object} data JSON representation of a notebook
2005 * @param {Object} data JSON representation of a notebook
2002 */
2006 */
2003 Notebook.prototype.save_notebook_success = function (start, data) {
2007 Notebook.prototype.save_notebook_success = function (start, data) {
2004 this.set_dirty(false);
2008 this.set_dirty(false);
2005 if (data.message) {
2009 if (data.message) {
2006 // save succeeded, but validation failed.
2010 // save succeeded, but validation failed.
2007 var body = $("<div>");
2011 var body = $("<div>");
2008 var title = "Notebook validation failed";
2012 var title = "Notebook validation failed";
2009
2013
2010 body.append($("<p>").text(
2014 body.append($("<p>").text(
2011 "The save operation succeeded," +
2015 "The save operation succeeded," +
2012 " but the notebook does not appear to be valid." +
2016 " but the notebook does not appear to be valid." +
2013 " The validation error was:"
2017 " The validation error was:"
2014 )).append($("<div>").addClass("validation-error").append(
2018 )).append($("<div>").addClass("validation-error").append(
2015 $("<pre>").text(data.message)
2019 $("<pre>").text(data.message)
2016 ));
2020 ));
2017 dialog.modal({
2021 dialog.modal({
2018 notebook: this,
2022 notebook: this,
2019 keyboard_manager: this.keyboard_manager,
2023 keyboard_manager: this.keyboard_manager,
2020 title: title,
2024 title: title,
2021 body: body,
2025 body: body,
2022 buttons : {
2026 buttons : {
2023 OK : {
2027 OK : {
2024 "class" : "btn-primary"
2028 "class" : "btn-primary"
2025 }
2029 }
2026 }
2030 }
2027 });
2031 });
2028 }
2032 }
2029 this.events.trigger('notebook_saved.Notebook');
2033 this.events.trigger('notebook_saved.Notebook');
2030 this._update_autosave_interval(start);
2034 this._update_autosave_interval(start);
2031 if (this._checkpoint_after_save) {
2035 if (this._checkpoint_after_save) {
2032 this.create_checkpoint();
2036 this.create_checkpoint();
2033 this._checkpoint_after_save = false;
2037 this._checkpoint_after_save = false;
2034 }
2038 }
2035 };
2039 };
2036
2040
2037 /**
2041 /**
2038 * update the autosave interval based on how long the last save took
2042 * update the autosave interval based on how long the last save took
2039 *
2043 *
2040 * @method _update_autosave_interval
2044 * @method _update_autosave_interval
2041 * @param {Integer} timestamp when the save request started
2045 * @param {Integer} timestamp when the save request started
2042 */
2046 */
2043 Notebook.prototype._update_autosave_interval = function (start) {
2047 Notebook.prototype._update_autosave_interval = function (start) {
2044 var duration = (new Date().getTime() - start);
2048 var duration = (new Date().getTime() - start);
2045 if (this.autosave_interval) {
2049 if (this.autosave_interval) {
2046 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
2050 // new save interval: higher of 10x save duration or parameter (default 30 seconds)
2047 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
2051 var interval = Math.max(10 * duration, this.minimum_autosave_interval);
2048 // round to 10 seconds, otherwise we will be setting a new interval too often
2052 // round to 10 seconds, otherwise we will be setting a new interval too often
2049 interval = 10000 * Math.round(interval / 10000);
2053 interval = 10000 * Math.round(interval / 10000);
2050 // set new interval, if it's changed
2054 // set new interval, if it's changed
2051 if (interval != this.autosave_interval) {
2055 if (interval != this.autosave_interval) {
2052 this.set_autosave_interval(interval);
2056 this.set_autosave_interval(interval);
2053 }
2057 }
2054 }
2058 }
2055 };
2059 };
2056
2060
2057 /**
2061 /**
2058 * Explicitly trust the output of this notebook.
2062 * Explicitly trust the output of this notebook.
2059 *
2063 *
2060 * @method trust_notebook
2064 * @method trust_notebook
2061 */
2065 */
2062 Notebook.prototype.trust_notebook = function () {
2066 Notebook.prototype.trust_notebook = function () {
2063 var body = $("<div>").append($("<p>")
2067 var body = $("<div>").append($("<p>")
2064 .text("A trusted IPython notebook may execute hidden malicious code ")
2068 .text("A trusted IPython notebook may execute hidden malicious code ")
2065 .append($("<strong>")
2069 .append($("<strong>")
2066 .append(
2070 .append(
2067 $("<em>").text("when you open it")
2071 $("<em>").text("when you open it")
2068 )
2072 )
2069 ).append(".").append(
2073 ).append(".").append(
2070 " Selecting trust will immediately reload this notebook in a trusted state."
2074 " Selecting trust will immediately reload this notebook in a trusted state."
2071 ).append(
2075 ).append(
2072 " For more information, see the "
2076 " For more information, see the "
2073 ).append($("<a>").attr("href", "http://ipython.org/ipython-doc/2/notebook/security.html")
2077 ).append($("<a>").attr("href", "http://ipython.org/ipython-doc/2/notebook/security.html")
2074 .text("IPython security documentation")
2078 .text("IPython security documentation")
2075 ).append(".")
2079 ).append(".")
2076 );
2080 );
2077
2081
2078 var nb = this;
2082 var nb = this;
2079 dialog.modal({
2083 dialog.modal({
2080 notebook: this,
2084 notebook: this,
2081 keyboard_manager: this.keyboard_manager,
2085 keyboard_manager: this.keyboard_manager,
2082 title: "Trust this notebook?",
2086 title: "Trust this notebook?",
2083 body: body,
2087 body: body,
2084
2088
2085 buttons: {
2089 buttons: {
2086 Cancel : {},
2090 Cancel : {},
2087 Trust : {
2091 Trust : {
2088 class : "btn-danger",
2092 class : "btn-danger",
2089 click : function () {
2093 click : function () {
2090 var cells = nb.get_cells();
2094 var cells = nb.get_cells();
2091 for (var i = 0; i < cells.length; i++) {
2095 for (var i = 0; i < cells.length; i++) {
2092 var cell = cells[i];
2096 var cell = cells[i];
2093 if (cell.cell_type == 'code') {
2097 if (cell.cell_type == 'code') {
2094 cell.output_area.trusted = true;
2098 cell.output_area.trusted = true;
2095 }
2099 }
2096 }
2100 }
2097 nb.events.on('notebook_saved.Notebook', function () {
2101 nb.events.on('notebook_saved.Notebook', function () {
2098 window.location.reload();
2102 window.location.reload();
2099 });
2103 });
2100 nb.save_notebook();
2104 nb.save_notebook();
2101 }
2105 }
2102 }
2106 }
2103 }
2107 }
2104 });
2108 });
2105 };
2109 };
2106
2110
2107 Notebook.prototype.copy_notebook = function () {
2111 Notebook.prototype.copy_notebook = function () {
2108 var that = this;
2112 var that = this;
2109 var base_url = this.base_url;
2113 var base_url = this.base_url;
2110 var w = window.open();
2114 var w = window.open();
2111 var parent = utils.url_path_split(this.notebook_path)[0];
2115 var parent = utils.url_path_split(this.notebook_path)[0];
2112 this.contents.copy(this.notebook_path, parent).then(
2116 this.contents.copy(this.notebook_path, parent).then(
2113 function (data) {
2117 function (data) {
2114 w.location = utils.url_join_encode(
2118 w.location = utils.url_join_encode(
2115 base_url, 'notebooks', data.path
2119 base_url, 'notebooks', data.path
2116 );
2120 );
2117 },
2121 },
2118 function(error) {
2122 function(error) {
2119 w.close();
2123 w.close();
2120 that.events.trigger('notebook_copy_failed', error);
2124 that.events.trigger('notebook_copy_failed', error);
2121 }
2125 }
2122 );
2126 );
2123 };
2127 };
2124
2128
2125 Notebook.prototype.rename = function (new_name) {
2129 Notebook.prototype.rename = function (new_name) {
2126 if (!new_name.match(/\.ipynb$/)) {
2130 if (!new_name.match(/\.ipynb$/)) {
2127 new_name = new_name + ".ipynb";
2131 new_name = new_name + ".ipynb";
2128 }
2132 }
2129
2133
2130 var that = this;
2134 var that = this;
2131 var parent = utils.url_path_split(this.notebook_path)[0];
2135 var parent = utils.url_path_split(this.notebook_path)[0];
2132 var new_path = utils.url_path_join(parent, new_name);
2136 var new_path = utils.url_path_join(parent, new_name);
2133 return this.contents.rename(this.notebook_path, new_path).then(
2137 return this.contents.rename(this.notebook_path, new_path).then(
2134 function (json) {
2138 function (json) {
2135 that.notebook_name = json.name;
2139 that.notebook_name = json.name;
2136 that.notebook_path = json.path;
2140 that.notebook_path = json.path;
2137 that.session.rename_notebook(json.path);
2141 that.session.rename_notebook(json.path);
2138 that.events.trigger('notebook_renamed.Notebook', json);
2142 that.events.trigger('notebook_renamed.Notebook', json);
2139 }
2143 }
2140 );
2144 );
2141 };
2145 };
2142
2146
2143 Notebook.prototype.delete = function () {
2147 Notebook.prototype.delete = function () {
2144 this.contents.delete(this.notebook_path);
2148 this.contents.delete(this.notebook_path);
2145 };
2149 };
2146
2150
2147 /**
2151 /**
2148 * Request a notebook's data from the server.
2152 * Request a notebook's data from the server.
2149 *
2153 *
2150 * @method load_notebook
2154 * @method load_notebook
2151 * @param {String} notebook_path A notebook to load
2155 * @param {String} notebook_path A notebook to load
2152 */
2156 */
2153 Notebook.prototype.load_notebook = function (notebook_path) {
2157 Notebook.prototype.load_notebook = function (notebook_path) {
2154 this.notebook_path = notebook_path;
2158 this.notebook_path = notebook_path;
2155 this.notebook_name = utils.url_path_split(this.notebook_path)[1];
2159 this.notebook_name = utils.url_path_split(this.notebook_path)[1];
2156 this.events.trigger('notebook_loading.Notebook');
2160 this.events.trigger('notebook_loading.Notebook');
2157 this.contents.get(notebook_path, {type: 'notebook'}).then(
2161 this.contents.get(notebook_path, {type: 'notebook'}).then(
2158 $.proxy(this.load_notebook_success, this),
2162 $.proxy(this.load_notebook_success, this),
2159 $.proxy(this.load_notebook_error, this)
2163 $.proxy(this.load_notebook_error, this)
2160 );
2164 );
2161 };
2165 };
2162
2166
2163 /**
2167 /**
2164 * Success callback for loading a notebook from the server.
2168 * Success callback for loading a notebook from the server.
2165 *
2169 *
2166 * Load notebook data from the JSON response.
2170 * Load notebook data from the JSON response.
2167 *
2171 *
2168 * @method load_notebook_success
2172 * @method load_notebook_success
2169 * @param {Object} data JSON representation of a notebook
2173 * @param {Object} data JSON representation of a notebook
2170 */
2174 */
2171 Notebook.prototype.load_notebook_success = function (data) {
2175 Notebook.prototype.load_notebook_success = function (data) {
2172 var failed, msg;
2176 var failed, msg;
2173 try {
2177 try {
2174 this.fromJSON(data);
2178 this.fromJSON(data);
2175 } catch (e) {
2179 } catch (e) {
2176 failed = e;
2180 failed = e;
2177 console.log("Notebook failed to load from JSON:", e);
2181 console.log("Notebook failed to load from JSON:", e);
2178 }
2182 }
2179 if (failed || data.message) {
2183 if (failed || data.message) {
2180 // *either* fromJSON failed or validation failed
2184 // *either* fromJSON failed or validation failed
2181 var body = $("<div>");
2185 var body = $("<div>");
2182 var title;
2186 var title;
2183 if (failed) {
2187 if (failed) {
2184 title = "Notebook failed to load";
2188 title = "Notebook failed to load";
2185 body.append($("<p>").text(
2189 body.append($("<p>").text(
2186 "The error was: "
2190 "The error was: "
2187 )).append($("<div>").addClass("js-error").text(
2191 )).append($("<div>").addClass("js-error").text(
2188 failed.toString()
2192 failed.toString()
2189 )).append($("<p>").text(
2193 )).append($("<p>").text(
2190 "See the error console for details."
2194 "See the error console for details."
2191 ));
2195 ));
2192 } else {
2196 } else {
2193 title = "Notebook validation failed";
2197 title = "Notebook validation failed";
2194 }
2198 }
2195
2199
2196 if (data.message) {
2200 if (data.message) {
2197 if (failed) {
2201 if (failed) {
2198 msg = "The notebook also failed validation:";
2202 msg = "The notebook also failed validation:";
2199 } else {
2203 } else {
2200 msg = "An invalid notebook may not function properly." +
2204 msg = "An invalid notebook may not function properly." +
2201 " The validation error was:";
2205 " The validation error was:";
2202 }
2206 }
2203 body.append($("<p>").text(
2207 body.append($("<p>").text(
2204 msg
2208 msg
2205 )).append($("<div>").addClass("validation-error").append(
2209 )).append($("<div>").addClass("validation-error").append(
2206 $("<pre>").text(data.message)
2210 $("<pre>").text(data.message)
2207 ));
2211 ));
2208 }
2212 }
2209
2213
2210 dialog.modal({
2214 dialog.modal({
2211 notebook: this,
2215 notebook: this,
2212 keyboard_manager: this.keyboard_manager,
2216 keyboard_manager: this.keyboard_manager,
2213 title: title,
2217 title: title,
2214 body: body,
2218 body: body,
2215 buttons : {
2219 buttons : {
2216 OK : {
2220 OK : {
2217 "class" : "btn-primary"
2221 "class" : "btn-primary"
2218 }
2222 }
2219 }
2223 }
2220 });
2224 });
2221 }
2225 }
2222 if (this.ncells() === 0) {
2226 if (this.ncells() === 0) {
2223 this.insert_cell_below('code');
2227 this.insert_cell_below('code');
2224 this.edit_mode(0);
2228 this.edit_mode(0);
2225 } else {
2229 } else {
2226 this.select(0);
2230 this.select(0);
2227 this.handle_command_mode(this.get_cell(0));
2231 this.handle_command_mode(this.get_cell(0));
2228 }
2232 }
2229 this.set_dirty(false);
2233 this.set_dirty(false);
2230 this.scroll_to_top();
2234 this.scroll_to_top();
2231 this.writable = data.writable || false;
2235 this.writable = data.writable || false;
2232 var nbmodel = data.content;
2236 var nbmodel = data.content;
2233 var orig_nbformat = nbmodel.metadata.orig_nbformat;
2237 var orig_nbformat = nbmodel.metadata.orig_nbformat;
2234 var orig_nbformat_minor = nbmodel.metadata.orig_nbformat_minor;
2238 var orig_nbformat_minor = nbmodel.metadata.orig_nbformat_minor;
2235 if (orig_nbformat !== undefined && nbmodel.nbformat !== orig_nbformat) {
2239 if (orig_nbformat !== undefined && nbmodel.nbformat !== orig_nbformat) {
2236 var src;
2240 var src;
2237 if (nbmodel.nbformat > orig_nbformat) {
2241 if (nbmodel.nbformat > orig_nbformat) {
2238 src = " an older notebook format ";
2242 src = " an older notebook format ";
2239 } else {
2243 } else {
2240 src = " a newer notebook format ";
2244 src = " a newer notebook format ";
2241 }
2245 }
2242
2246
2243 msg = "This notebook has been converted from" + src +
2247 msg = "This notebook has been converted from" + src +
2244 "(v"+orig_nbformat+") to the current notebook " +
2248 "(v"+orig_nbformat+") to the current notebook " +
2245 "format (v"+nbmodel.nbformat+"). The next time you save this notebook, the " +
2249 "format (v"+nbmodel.nbformat+"). The next time you save this notebook, the " +
2246 "current notebook format will be used.";
2250 "current notebook format will be used.";
2247
2251
2248 if (nbmodel.nbformat > orig_nbformat) {
2252 if (nbmodel.nbformat > orig_nbformat) {
2249 msg += " Older versions of IPython may not be able to read the new format.";
2253 msg += " Older versions of IPython may not be able to read the new format.";
2250 } else {
2254 } else {
2251 msg += " Some features of the original notebook may not be available.";
2255 msg += " Some features of the original notebook may not be available.";
2252 }
2256 }
2253 msg += " To preserve the original version, close the " +
2257 msg += " To preserve the original version, close the " +
2254 "notebook without saving it.";
2258 "notebook without saving it.";
2255 dialog.modal({
2259 dialog.modal({
2256 notebook: this,
2260 notebook: this,
2257 keyboard_manager: this.keyboard_manager,
2261 keyboard_manager: this.keyboard_manager,
2258 title : "Notebook converted",
2262 title : "Notebook converted",
2259 body : msg,
2263 body : msg,
2260 buttons : {
2264 buttons : {
2261 OK : {
2265 OK : {
2262 class : "btn-primary"
2266 class : "btn-primary"
2263 }
2267 }
2264 }
2268 }
2265 });
2269 });
2266 } else if (this.nbformat_minor < nbmodel.nbformat_minor) {
2270 } else if (this.nbformat_minor < nbmodel.nbformat_minor) {
2267 this.nbformat_minor = nbmodel.nbformat_minor;
2271 this.nbformat_minor = nbmodel.nbformat_minor;
2268 }
2272 }
2269
2273
2270 // Create the session after the notebook is completely loaded to prevent
2274 // Create the session after the notebook is completely loaded to prevent
2271 // code execution upon loading, which is a security risk.
2275 // code execution upon loading, which is a security risk.
2272 if (this.session === null) {
2276 if (this.session === null) {
2273 var kernelspec = this.metadata.kernelspec || {};
2277 var kernelspec = this.metadata.kernelspec || {};
2274 var kernel_name = kernelspec.name;
2278 var kernel_name = kernelspec.name;
2275
2279
2276 this.start_session(kernel_name);
2280 this.start_session(kernel_name);
2277 }
2281 }
2278 // load our checkpoint list
2282 // load our checkpoint list
2279 this.list_checkpoints();
2283 this.list_checkpoints();
2280
2284
2281 // load toolbar state
2285 // load toolbar state
2282 if (this.metadata.celltoolbar) {
2286 if (this.metadata.celltoolbar) {
2283 celltoolbar.CellToolbar.global_show();
2287 celltoolbar.CellToolbar.global_show();
2284 celltoolbar.CellToolbar.activate_preset(this.metadata.celltoolbar);
2288 celltoolbar.CellToolbar.activate_preset(this.metadata.celltoolbar);
2285 } else {
2289 } else {
2286 celltoolbar.CellToolbar.global_hide();
2290 celltoolbar.CellToolbar.global_hide();
2287 }
2291 }
2288
2292
2289 if (!this.writable) {
2293 if (!this.writable) {
2290 this.set_autosave_interval(0);
2294 this.set_autosave_interval(0);
2291 this.events.trigger('notebook_read_only.Notebook');
2295 this.events.trigger('notebook_read_only.Notebook');
2292 }
2296 }
2293
2297
2294 // now that we're fully loaded, it is safe to restore save functionality
2298 // now that we're fully loaded, it is safe to restore save functionality
2295 this._fully_loaded = true;
2299 this._fully_loaded = true;
2296 this.events.trigger('notebook_loaded.Notebook');
2300 this.events.trigger('notebook_loaded.Notebook');
2297 };
2301 };
2298
2302
2299 /**
2303 /**
2300 * Failure callback for loading a notebook from the server.
2304 * Failure callback for loading a notebook from the server.
2301 *
2305 *
2302 * @method load_notebook_error
2306 * @method load_notebook_error
2303 * @param {Error} error
2307 * @param {Error} error
2304 */
2308 */
2305 Notebook.prototype.load_notebook_error = function (error) {
2309 Notebook.prototype.load_notebook_error = function (error) {
2306 this.events.trigger('notebook_load_failed.Notebook', error);
2310 this.events.trigger('notebook_load_failed.Notebook', error);
2307 var msg;
2311 var msg;
2308 if (error.name === utils.XHR_ERROR && error.xhr.status === 500) {
2312 if (error.name === utils.XHR_ERROR && error.xhr.status === 500) {
2309 utils.log_ajax_error(error.xhr, error.xhr_status, error.xhr_error);
2313 utils.log_ajax_error(error.xhr, error.xhr_status, error.xhr_error);
2310 msg = "An unknown error occurred while loading this notebook. " +
2314 msg = "An unknown error occurred while loading this notebook. " +
2311 "This version can load notebook formats " +
2315 "This version can load notebook formats " +
2312 "v" + this.nbformat + " or earlier. See the server log for details.";
2316 "v" + this.nbformat + " or earlier. See the server log for details.";
2313 } else {
2317 } else {
2314 msg = error.message;
2318 msg = error.message;
2315 }
2319 }
2316 dialog.modal({
2320 dialog.modal({
2317 notebook: this,
2321 notebook: this,
2318 keyboard_manager: this.keyboard_manager,
2322 keyboard_manager: this.keyboard_manager,
2319 title: "Error loading notebook",
2323 title: "Error loading notebook",
2320 body : msg,
2324 body : msg,
2321 buttons : {
2325 buttons : {
2322 "OK": {}
2326 "OK": {}
2323 }
2327 }
2324 });
2328 });
2325 };
2329 };
2326
2330
2327 /********************* checkpoint-related *********************/
2331 /********************* checkpoint-related *********************/
2328
2332
2329 /**
2333 /**
2330 * Save the notebook then immediately create a checkpoint.
2334 * Save the notebook then immediately create a checkpoint.
2331 *
2335 *
2332 * @method save_checkpoint
2336 * @method save_checkpoint
2333 */
2337 */
2334 Notebook.prototype.save_checkpoint = function () {
2338 Notebook.prototype.save_checkpoint = function () {
2335 this._checkpoint_after_save = true;
2339 this._checkpoint_after_save = true;
2336 this.save_notebook();
2340 this.save_notebook();
2337 };
2341 };
2338
2342
2339 /**
2343 /**
2340 * Add a checkpoint for this notebook.
2344 * Add a checkpoint for this notebook.
2341 * for use as a callback from checkpoint creation.
2345 * for use as a callback from checkpoint creation.
2342 *
2346 *
2343 * @method add_checkpoint
2347 * @method add_checkpoint
2344 */
2348 */
2345 Notebook.prototype.add_checkpoint = function (checkpoint) {
2349 Notebook.prototype.add_checkpoint = function (checkpoint) {
2346 var found = false;
2350 var found = false;
2347 for (var i = 0; i < this.checkpoints.length; i++) {
2351 for (var i = 0; i < this.checkpoints.length; i++) {
2348 var existing = this.checkpoints[i];
2352 var existing = this.checkpoints[i];
2349 if (existing.id == checkpoint.id) {
2353 if (existing.id == checkpoint.id) {
2350 found = true;
2354 found = true;
2351 this.checkpoints[i] = checkpoint;
2355 this.checkpoints[i] = checkpoint;
2352 break;
2356 break;
2353 }
2357 }
2354 }
2358 }
2355 if (!found) {
2359 if (!found) {
2356 this.checkpoints.push(checkpoint);
2360 this.checkpoints.push(checkpoint);
2357 }
2361 }
2358 this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
2362 this.last_checkpoint = this.checkpoints[this.checkpoints.length - 1];
2359 };
2363 };
2360
2364
2361 /**
2365 /**
2362 * List checkpoints for this notebook.
2366 * List checkpoints for this notebook.
2363 *
2367 *
2364 * @method list_checkpoints
2368 * @method list_checkpoints
2365 */
2369 */
2366 Notebook.prototype.list_checkpoints = function () {
2370 Notebook.prototype.list_checkpoints = function () {
2367 var that = this;
2371 var that = this;
2368 this.contents.list_checkpoints(this.notebook_path).then(
2372 this.contents.list_checkpoints(this.notebook_path).then(
2369 $.proxy(this.list_checkpoints_success, this),
2373 $.proxy(this.list_checkpoints_success, this),
2370 function(error) {
2374 function(error) {
2371 that.events.trigger('list_checkpoints_failed.Notebook', error);
2375 that.events.trigger('list_checkpoints_failed.Notebook', error);
2372 }
2376 }
2373 );
2377 );
2374 };
2378 };
2375
2379
2376 /**
2380 /**
2377 * Success callback for listing checkpoints.
2381 * Success callback for listing checkpoints.
2378 *
2382 *
2379 * @method list_checkpoint_success
2383 * @method list_checkpoint_success
2380 * @param {Object} data JSON representation of a checkpoint
2384 * @param {Object} data JSON representation of a checkpoint
2381 */
2385 */
2382 Notebook.prototype.list_checkpoints_success = function (data) {
2386 Notebook.prototype.list_checkpoints_success = function (data) {
2383 this.checkpoints = data;
2387 this.checkpoints = data;
2384 if (data.length) {
2388 if (data.length) {
2385 this.last_checkpoint = data[data.length - 1];
2389 this.last_checkpoint = data[data.length - 1];
2386 } else {
2390 } else {
2387 this.last_checkpoint = null;
2391 this.last_checkpoint = null;
2388 }
2392 }
2389 this.events.trigger('checkpoints_listed.Notebook', [data]);
2393 this.events.trigger('checkpoints_listed.Notebook', [data]);
2390 };
2394 };
2391
2395
2392 /**
2396 /**
2393 * Create a checkpoint of this notebook on the server from the most recent save.
2397 * Create a checkpoint of this notebook on the server from the most recent save.
2394 *
2398 *
2395 * @method create_checkpoint
2399 * @method create_checkpoint
2396 */
2400 */
2397 Notebook.prototype.create_checkpoint = function () {
2401 Notebook.prototype.create_checkpoint = function () {
2398 var that = this;
2402 var that = this;
2399 this.contents.create_checkpoint(this.notebook_path).then(
2403 this.contents.create_checkpoint(this.notebook_path).then(
2400 $.proxy(this.create_checkpoint_success, this),
2404 $.proxy(this.create_checkpoint_success, this),
2401 function (error) {
2405 function (error) {
2402 that.events.trigger('checkpoint_failed.Notebook', error);
2406 that.events.trigger('checkpoint_failed.Notebook', error);
2403 }
2407 }
2404 );
2408 );
2405 };
2409 };
2406
2410
2407 /**
2411 /**
2408 * Success callback for creating a checkpoint.
2412 * Success callback for creating a checkpoint.
2409 *
2413 *
2410 * @method create_checkpoint_success
2414 * @method create_checkpoint_success
2411 * @param {Object} data JSON representation of a checkpoint
2415 * @param {Object} data JSON representation of a checkpoint
2412 */
2416 */
2413 Notebook.prototype.create_checkpoint_success = function (data) {
2417 Notebook.prototype.create_checkpoint_success = function (data) {
2414 this.add_checkpoint(data);
2418 this.add_checkpoint(data);
2415 this.events.trigger('checkpoint_created.Notebook', data);
2419 this.events.trigger('checkpoint_created.Notebook', data);
2416 };
2420 };
2417
2421
2418 Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
2422 Notebook.prototype.restore_checkpoint_dialog = function (checkpoint) {
2419 var that = this;
2423 var that = this;
2420 checkpoint = checkpoint || this.last_checkpoint;
2424 checkpoint = checkpoint || this.last_checkpoint;
2421 if ( ! checkpoint ) {
2425 if ( ! checkpoint ) {
2422 console.log("restore dialog, but no checkpoint to restore to!");
2426 console.log("restore dialog, but no checkpoint to restore to!");
2423 return;
2427 return;
2424 }
2428 }
2425 var body = $('<div/>').append(
2429 var body = $('<div/>').append(
2426 $('<p/>').addClass("p-space").text(
2430 $('<p/>').addClass("p-space").text(
2427 "Are you sure you want to revert the notebook to " +
2431 "Are you sure you want to revert the notebook to " +
2428 "the latest checkpoint?"
2432 "the latest checkpoint?"
2429 ).append(
2433 ).append(
2430 $("<strong/>").text(
2434 $("<strong/>").text(
2431 " This cannot be undone."
2435 " This cannot be undone."
2432 )
2436 )
2433 )
2437 )
2434 ).append(
2438 ).append(
2435 $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
2439 $('<p/>').addClass("p-space").text("The checkpoint was last updated at:")
2436 ).append(
2440 ).append(
2437 $('<p/>').addClass("p-space").text(
2441 $('<p/>').addClass("p-space").text(
2438 Date(checkpoint.last_modified)
2442 Date(checkpoint.last_modified)
2439 ).css("text-align", "center")
2443 ).css("text-align", "center")
2440 );
2444 );
2441
2445
2442 dialog.modal({
2446 dialog.modal({
2443 notebook: this,
2447 notebook: this,
2444 keyboard_manager: this.keyboard_manager,
2448 keyboard_manager: this.keyboard_manager,
2445 title : "Revert notebook to checkpoint",
2449 title : "Revert notebook to checkpoint",
2446 body : body,
2450 body : body,
2447 buttons : {
2451 buttons : {
2448 Revert : {
2452 Revert : {
2449 class : "btn-danger",
2453 class : "btn-danger",
2450 click : function () {
2454 click : function () {
2451 that.restore_checkpoint(checkpoint.id);
2455 that.restore_checkpoint(checkpoint.id);
2452 }
2456 }
2453 },
2457 },
2454 Cancel : {}
2458 Cancel : {}
2455 }
2459 }
2456 });
2460 });
2457 };
2461 };
2458
2462
2459 /**
2463 /**
2460 * Restore the notebook to a checkpoint state.
2464 * Restore the notebook to a checkpoint state.
2461 *
2465 *
2462 * @method restore_checkpoint
2466 * @method restore_checkpoint
2463 * @param {String} checkpoint ID
2467 * @param {String} checkpoint ID
2464 */
2468 */
2465 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2469 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2466 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2470 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2467 var that = this;
2471 var that = this;
2468 this.contents.restore_checkpoint(this.notebook_path, checkpoint).then(
2472 this.contents.restore_checkpoint(this.notebook_path, checkpoint).then(
2469 $.proxy(this.restore_checkpoint_success, this),
2473 $.proxy(this.restore_checkpoint_success, this),
2470 function (error) {
2474 function (error) {
2471 that.events.trigger('checkpoint_restore_failed.Notebook', error);
2475 that.events.trigger('checkpoint_restore_failed.Notebook', error);
2472 }
2476 }
2473 );
2477 );
2474 };
2478 };
2475
2479
2476 /**
2480 /**
2477 * Success callback for restoring a notebook to a checkpoint.
2481 * Success callback for restoring a notebook to a checkpoint.
2478 *
2482 *
2479 * @method restore_checkpoint_success
2483 * @method restore_checkpoint_success
2480 */
2484 */
2481 Notebook.prototype.restore_checkpoint_success = function () {
2485 Notebook.prototype.restore_checkpoint_success = function () {
2482 this.events.trigger('checkpoint_restored.Notebook');
2486 this.events.trigger('checkpoint_restored.Notebook');
2483 this.load_notebook(this.notebook_path);
2487 this.load_notebook(this.notebook_path);
2484 };
2488 };
2485
2489
2486 /**
2490 /**
2487 * Delete a notebook checkpoint.
2491 * Delete a notebook checkpoint.
2488 *
2492 *
2489 * @method delete_checkpoint
2493 * @method delete_checkpoint
2490 * @param {String} checkpoint ID
2494 * @param {String} checkpoint ID
2491 */
2495 */
2492 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2496 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2493 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2497 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2494 var that = this;
2498 var that = this;
2495 this.contents.delete_checkpoint(this.notebook_path, checkpoint).then(
2499 this.contents.delete_checkpoint(this.notebook_path, checkpoint).then(
2496 $.proxy(this.delete_checkpoint_success, this),
2500 $.proxy(this.delete_checkpoint_success, this),
2497 function (error) {
2501 function (error) {
2498 that.events.trigger('checkpoint_delete_failed.Notebook', error);
2502 that.events.trigger('checkpoint_delete_failed.Notebook', error);
2499 }
2503 }
2500 );
2504 );
2501 };
2505 };
2502
2506
2503 /**
2507 /**
2504 * Success callback for deleting a notebook checkpoint
2508 * Success callback for deleting a notebook checkpoint
2505 *
2509 *
2506 * @method delete_checkpoint_success
2510 * @method delete_checkpoint_success
2507 */
2511 */
2508 Notebook.prototype.delete_checkpoint_success = function () {
2512 Notebook.prototype.delete_checkpoint_success = function () {
2509 this.events.trigger('checkpoint_deleted.Notebook');
2513 this.events.trigger('checkpoint_deleted.Notebook');
2510 this.load_notebook(this.notebook_path);
2514 this.load_notebook(this.notebook_path);
2511 };
2515 };
2512
2516
2513
2517
2514 // For backwards compatability.
2518 // For backwards compatability.
2515 IPython.Notebook = Notebook;
2519 IPython.Notebook = Notebook;
2516
2520
2517 return {'Notebook': Notebook};
2521 return {'Notebook': Notebook};
2518 });
2522 });
@@ -1,487 +1,488 b''
1 """A ZMQ-based subclass of InteractiveShell.
1 """A ZMQ-based subclass of InteractiveShell.
2
2
3 This code is meant to ease the refactoring of the base InteractiveShell into
3 This code is meant to ease the refactoring of the base InteractiveShell into
4 something with a cleaner architecture for 2-process use, without actually
4 something with a cleaner architecture for 2-process use, without actually
5 breaking InteractiveShell itself. So we're doing something a bit ugly, where
5 breaking InteractiveShell itself. So we're doing something a bit ugly, where
6 we subclass and override what we want to fix. Once this is working well, we
6 we subclass and override what we want to fix. Once this is working well, we
7 can go back to the base class and refactor the code for a cleaner inheritance
7 can go back to the base class and refactor the code for a cleaner inheritance
8 implementation that doesn't rely on so much monkeypatching.
8 implementation that doesn't rely on so much monkeypatching.
9
9
10 But this lets us maintain a fully working IPython as we develop the new
10 But this lets us maintain a fully working IPython as we develop the new
11 machinery. This should thus be thought of as scaffolding.
11 machinery. This should thus be thought of as scaffolding.
12 """
12 """
13
13
14 # Copyright (c) IPython Development Team.
14 # Copyright (c) IPython Development Team.
15 # Distributed under the terms of the Modified BSD License.
15 # Distributed under the terms of the Modified BSD License.
16
16
17 from __future__ import print_function
17 from __future__ import print_function
18
18
19 import os
19 import os
20 import sys
20 import sys
21 import time
21 import time
22
22
23 from zmq.eventloop import ioloop
23 from zmq.eventloop import ioloop
24
24
25 from IPython.core.interactiveshell import (
25 from IPython.core.interactiveshell import (
26 InteractiveShell, InteractiveShellABC
26 InteractiveShell, InteractiveShellABC
27 )
27 )
28 from IPython.core import page
28 from IPython.core import page
29 from IPython.core.autocall import ZMQExitAutocall
29 from IPython.core.autocall import ZMQExitAutocall
30 from IPython.core.displaypub import DisplayPublisher
30 from IPython.core.displaypub import DisplayPublisher
31 from IPython.core.error import UsageError
31 from IPython.core.error import UsageError
32 from IPython.core.magics import MacroToEdit, CodeMagics
32 from IPython.core.magics import MacroToEdit, CodeMagics
33 from IPython.core.magic import magics_class, line_magic, Magics
33 from IPython.core.magic import magics_class, line_magic, Magics
34 from IPython.core.payloadpage import install_payload_page
34 from IPython.core.payloadpage import install_payload_page
35 from IPython.core.usage import default_gui_banner
35 from IPython.core.usage import default_gui_banner
36 from IPython.display import display, Javascript
36 from IPython.display import display, Javascript
37 from IPython.kernel.inprocess.socket import SocketABC
37 from IPython.kernel.inprocess.socket import SocketABC
38 from IPython.kernel import (
38 from IPython.kernel import (
39 get_connection_file, get_connection_info, connect_qtconsole
39 get_connection_file, get_connection_info, connect_qtconsole
40 )
40 )
41 from IPython.testing.skipdoctest import skip_doctest
41 from IPython.testing.skipdoctest import skip_doctest
42 from IPython.utils import openpy
42 from IPython.utils import openpy
43 from IPython.utils.jsonutil import json_clean, encode_images
43 from IPython.utils.jsonutil import json_clean, encode_images
44 from IPython.utils.process import arg_split
44 from IPython.utils.process import arg_split
45 from IPython.utils import py3compat
45 from IPython.utils import py3compat
46 from IPython.utils.py3compat import unicode_type
46 from IPython.utils.py3compat import unicode_type
47 from IPython.utils.traitlets import Instance, Type, Dict, CBool, CBytes, Any
47 from IPython.utils.traitlets import Instance, Type, Dict, CBool, CBytes, Any
48 from IPython.utils.warn import error
48 from IPython.utils.warn import error
49 from IPython.kernel.zmq.displayhook import ZMQShellDisplayHook
49 from IPython.kernel.zmq.displayhook import ZMQShellDisplayHook
50 from IPython.kernel.zmq.datapub import ZMQDataPublisher
50 from IPython.kernel.zmq.datapub import ZMQDataPublisher
51 from IPython.kernel.zmq.session import extract_header
51 from IPython.kernel.zmq.session import extract_header
52 from .session import Session
52 from .session import Session
53
53
54 #-----------------------------------------------------------------------------
54 #-----------------------------------------------------------------------------
55 # Functions and classes
55 # Functions and classes
56 #-----------------------------------------------------------------------------
56 #-----------------------------------------------------------------------------
57
57
58 class ZMQDisplayPublisher(DisplayPublisher):
58 class ZMQDisplayPublisher(DisplayPublisher):
59 """A display publisher that publishes data using a ZeroMQ PUB socket."""
59 """A display publisher that publishes data using a ZeroMQ PUB socket."""
60
60
61 session = Instance(Session)
61 session = Instance(Session)
62 pub_socket = Instance(SocketABC)
62 pub_socket = Instance(SocketABC)
63 parent_header = Dict({})
63 parent_header = Dict({})
64 topic = CBytes(b'display_data')
64 topic = CBytes(b'display_data')
65
65
66 def set_parent(self, parent):
66 def set_parent(self, parent):
67 """Set the parent for outbound messages."""
67 """Set the parent for outbound messages."""
68 self.parent_header = extract_header(parent)
68 self.parent_header = extract_header(parent)
69
69
70 def _flush_streams(self):
70 def _flush_streams(self):
71 """flush IO Streams prior to display"""
71 """flush IO Streams prior to display"""
72 sys.stdout.flush()
72 sys.stdout.flush()
73 sys.stderr.flush()
73 sys.stderr.flush()
74
74
75 def publish(self, data, metadata=None, source=None):
75 def publish(self, data, metadata=None, source=None):
76 self._flush_streams()
76 self._flush_streams()
77 if metadata is None:
77 if metadata is None:
78 metadata = {}
78 metadata = {}
79 self._validate_data(data, metadata)
79 self._validate_data(data, metadata)
80 content = {}
80 content = {}
81 content['data'] = encode_images(data)
81 content['data'] = encode_images(data)
82 content['metadata'] = metadata
82 content['metadata'] = metadata
83 self.session.send(
83 self.session.send(
84 self.pub_socket, u'display_data', json_clean(content),
84 self.pub_socket, u'display_data', json_clean(content),
85 parent=self.parent_header, ident=self.topic,
85 parent=self.parent_header, ident=self.topic,
86 )
86 )
87
87
88 def clear_output(self, wait=False):
88 def clear_output(self, wait=False):
89 content = dict(wait=wait)
89 content = dict(wait=wait)
90 self._flush_streams()
90 self._flush_streams()
91 self.session.send(
91 self.session.send(
92 self.pub_socket, u'clear_output', content,
92 self.pub_socket, u'clear_output', content,
93 parent=self.parent_header, ident=self.topic,
93 parent=self.parent_header, ident=self.topic,
94 )
94 )
95
95
96 @magics_class
96 @magics_class
97 class KernelMagics(Magics):
97 class KernelMagics(Magics):
98 #------------------------------------------------------------------------
98 #------------------------------------------------------------------------
99 # Magic overrides
99 # Magic overrides
100 #------------------------------------------------------------------------
100 #------------------------------------------------------------------------
101 # Once the base class stops inheriting from magic, this code needs to be
101 # Once the base class stops inheriting from magic, this code needs to be
102 # moved into a separate machinery as well. For now, at least isolate here
102 # moved into a separate machinery as well. For now, at least isolate here
103 # the magics which this class needs to implement differently from the base
103 # the magics which this class needs to implement differently from the base
104 # class, or that are unique to it.
104 # class, or that are unique to it.
105
105
106 _find_edit_target = CodeMagics._find_edit_target
106 _find_edit_target = CodeMagics._find_edit_target
107
107
108 @skip_doctest
108 @skip_doctest
109 @line_magic
109 @line_magic
110 def edit(self, parameter_s='', last_call=['','']):
110 def edit(self, parameter_s='', last_call=['','']):
111 """Bring up an editor and execute the resulting code.
111 """Bring up an editor and execute the resulting code.
112
112
113 Usage:
113 Usage:
114 %edit [options] [args]
114 %edit [options] [args]
115
115
116 %edit runs an external text editor. You will need to set the command for
116 %edit runs an external text editor. You will need to set the command for
117 this editor via the ``TerminalInteractiveShell.editor`` option in your
117 this editor via the ``TerminalInteractiveShell.editor`` option in your
118 configuration file before it will work.
118 configuration file before it will work.
119
119
120 This command allows you to conveniently edit multi-line code right in
120 This command allows you to conveniently edit multi-line code right in
121 your IPython session.
121 your IPython session.
122
122
123 If called without arguments, %edit opens up an empty editor with a
123 If called without arguments, %edit opens up an empty editor with a
124 temporary file and will execute the contents of this file when you
124 temporary file and will execute the contents of this file when you
125 close it (don't forget to save it!).
125 close it (don't forget to save it!).
126
126
127 Options:
127 Options:
128
128
129 -n <number>
129 -n <number>
130 Open the editor at a specified line number. By default, the IPython
130 Open the editor at a specified line number. By default, the IPython
131 editor hook uses the unix syntax 'editor +N filename', but you can
131 editor hook uses the unix syntax 'editor +N filename', but you can
132 configure this by providing your own modified hook if your favorite
132 configure this by providing your own modified hook if your favorite
133 editor supports line-number specifications with a different syntax.
133 editor supports line-number specifications with a different syntax.
134
134
135 -p
135 -p
136 Call the editor with the same data as the previous time it was used,
136 Call the editor with the same data as the previous time it was used,
137 regardless of how long ago (in your current session) it was.
137 regardless of how long ago (in your current session) it was.
138
138
139 -r
139 -r
140 Use 'raw' input. This option only applies to input taken from the
140 Use 'raw' input. This option only applies to input taken from the
141 user's history. By default, the 'processed' history is used, so that
141 user's history. By default, the 'processed' history is used, so that
142 magics are loaded in their transformed version to valid Python. If
142 magics are loaded in their transformed version to valid Python. If
143 this option is given, the raw input as typed as the command line is
143 this option is given, the raw input as typed as the command line is
144 used instead. When you exit the editor, it will be executed by
144 used instead. When you exit the editor, it will be executed by
145 IPython's own processor.
145 IPython's own processor.
146
146
147 Arguments:
147 Arguments:
148
148
149 If arguments are given, the following possibilites exist:
149 If arguments are given, the following possibilites exist:
150
150
151 - The arguments are numbers or pairs of colon-separated numbers (like
151 - The arguments are numbers or pairs of colon-separated numbers (like
152 1 4:8 9). These are interpreted as lines of previous input to be
152 1 4:8 9). These are interpreted as lines of previous input to be
153 loaded into the editor. The syntax is the same of the %macro command.
153 loaded into the editor. The syntax is the same of the %macro command.
154
154
155 - If the argument doesn't start with a number, it is evaluated as a
155 - If the argument doesn't start with a number, it is evaluated as a
156 variable and its contents loaded into the editor. You can thus edit
156 variable and its contents loaded into the editor. You can thus edit
157 any string which contains python code (including the result of
157 any string which contains python code (including the result of
158 previous edits).
158 previous edits).
159
159
160 - If the argument is the name of an object (other than a string),
160 - If the argument is the name of an object (other than a string),
161 IPython will try to locate the file where it was defined and open the
161 IPython will try to locate the file where it was defined and open the
162 editor at the point where it is defined. You can use ``%edit function``
162 editor at the point where it is defined. You can use ``%edit function``
163 to load an editor exactly at the point where 'function' is defined,
163 to load an editor exactly at the point where 'function' is defined,
164 edit it and have the file be executed automatically.
164 edit it and have the file be executed automatically.
165
165
166 If the object is a macro (see %macro for details), this opens up your
166 If the object is a macro (see %macro for details), this opens up your
167 specified editor with a temporary file containing the macro's data.
167 specified editor with a temporary file containing the macro's data.
168 Upon exit, the macro is reloaded with the contents of the file.
168 Upon exit, the macro is reloaded with the contents of the file.
169
169
170 Note: opening at an exact line is only supported under Unix, and some
170 Note: opening at an exact line is only supported under Unix, and some
171 editors (like kedit and gedit up to Gnome 2.8) do not understand the
171 editors (like kedit and gedit up to Gnome 2.8) do not understand the
172 '+NUMBER' parameter necessary for this feature. Good editors like
172 '+NUMBER' parameter necessary for this feature. Good editors like
173 (X)Emacs, vi, jed, pico and joe all do.
173 (X)Emacs, vi, jed, pico and joe all do.
174
174
175 - If the argument is not found as a variable, IPython will look for a
175 - If the argument is not found as a variable, IPython will look for a
176 file with that name (adding .py if necessary) and load it into the
176 file with that name (adding .py if necessary) and load it into the
177 editor. It will execute its contents with execfile() when you exit,
177 editor. It will execute its contents with execfile() when you exit,
178 loading any code in the file into your interactive namespace.
178 loading any code in the file into your interactive namespace.
179
179
180 Unlike in the terminal, this is designed to use a GUI editor, and we do
180 Unlike in the terminal, this is designed to use a GUI editor, and we do
181 not know when it has closed. So the file you edit will not be
181 not know when it has closed. So the file you edit will not be
182 automatically executed or printed.
182 automatically executed or printed.
183
183
184 Note that %edit is also available through the alias %ed.
184 Note that %edit is also available through the alias %ed.
185 """
185 """
186
186
187 opts,args = self.parse_options(parameter_s,'prn:')
187 opts,args = self.parse_options(parameter_s,'prn:')
188
188
189 try:
189 try:
190 filename, lineno, _ = CodeMagics._find_edit_target(self.shell, args, opts, last_call)
190 filename, lineno, _ = CodeMagics._find_edit_target(self.shell, args, opts, last_call)
191 except MacroToEdit as e:
191 except MacroToEdit as e:
192 # TODO: Implement macro editing over 2 processes.
192 # TODO: Implement macro editing over 2 processes.
193 print("Macro editing not yet implemented in 2-process model.")
193 print("Macro editing not yet implemented in 2-process model.")
194 return
194 return
195
195
196 # Make sure we send to the client an absolute path, in case the working
196 # Make sure we send to the client an absolute path, in case the working
197 # directory of client and kernel don't match
197 # directory of client and kernel don't match
198 filename = os.path.abspath(filename)
198 filename = os.path.abspath(filename)
199
199
200 payload = {
200 payload = {
201 'source' : 'edit_magic',
201 'source' : 'edit_magic',
202 'filename' : filename,
202 'filename' : filename,
203 'line_number' : lineno
203 'line_number' : lineno
204 }
204 }
205 self.shell.payload_manager.write_payload(payload)
205 self.shell.payload_manager.write_payload(payload)
206
206
207 # A few magics that are adapted to the specifics of using pexpect and a
207 # A few magics that are adapted to the specifics of using pexpect and a
208 # remote terminal
208 # remote terminal
209
209
210 @line_magic
210 @line_magic
211 def clear(self, arg_s):
211 def clear(self, arg_s):
212 """Clear the terminal."""
212 """Clear the terminal."""
213 if os.name == 'posix':
213 if os.name == 'posix':
214 self.shell.system("clear")
214 self.shell.system("clear")
215 else:
215 else:
216 self.shell.system("cls")
216 self.shell.system("cls")
217
217
218 if os.name == 'nt':
218 if os.name == 'nt':
219 # This is the usual name in windows
219 # This is the usual name in windows
220 cls = line_magic('cls')(clear)
220 cls = line_magic('cls')(clear)
221
221
222 # Terminal pagers won't work over pexpect, but we do have our own pager
222 # Terminal pagers won't work over pexpect, but we do have our own pager
223
223
224 @line_magic
224 @line_magic
225 def less(self, arg_s):
225 def less(self, arg_s):
226 """Show a file through the pager.
226 """Show a file through the pager.
227
227
228 Files ending in .py are syntax-highlighted."""
228 Files ending in .py are syntax-highlighted."""
229 if not arg_s:
229 if not arg_s:
230 raise UsageError('Missing filename.')
230 raise UsageError('Missing filename.')
231
231
232 cont = open(arg_s).read()
232 cont = open(arg_s).read()
233 if arg_s.endswith('.py'):
233 if arg_s.endswith('.py'):
234 cont = self.shell.pycolorize(openpy.read_py_file(arg_s, skip_encoding_cookie=False))
234 cont = self.shell.pycolorize(openpy.read_py_file(arg_s, skip_encoding_cookie=False))
235 else:
235 else:
236 cont = open(arg_s).read()
236 cont = open(arg_s).read()
237 page.page(cont)
237 page.page(cont)
238
238
239 more = line_magic('more')(less)
239 more = line_magic('more')(less)
240
240
241 # Man calls a pager, so we also need to redefine it
241 # Man calls a pager, so we also need to redefine it
242 if os.name == 'posix':
242 if os.name == 'posix':
243 @line_magic
243 @line_magic
244 def man(self, arg_s):
244 def man(self, arg_s):
245 """Find the man page for the given command and display in pager."""
245 """Find the man page for the given command and display in pager."""
246 page.page(self.shell.getoutput('man %s | col -b' % arg_s,
246 page.page(self.shell.getoutput('man %s | col -b' % arg_s,
247 split=False))
247 split=False))
248
248
249 @line_magic
249 @line_magic
250 def connect_info(self, arg_s):
250 def connect_info(self, arg_s):
251 """Print information for connecting other clients to this kernel
251 """Print information for connecting other clients to this kernel
252
252
253 It will print the contents of this session's connection file, as well as
253 It will print the contents of this session's connection file, as well as
254 shortcuts for local clients.
254 shortcuts for local clients.
255
255
256 In the simplest case, when called from the most recently launched kernel,
256 In the simplest case, when called from the most recently launched kernel,
257 secondary clients can be connected, simply with:
257 secondary clients can be connected, simply with:
258
258
259 $> ipython <app> --existing
259 $> ipython <app> --existing
260
260
261 """
261 """
262
262
263 from IPython.core.application import BaseIPythonApplication as BaseIPApp
263 from IPython.core.application import BaseIPythonApplication as BaseIPApp
264
264
265 if BaseIPApp.initialized():
265 if BaseIPApp.initialized():
266 app = BaseIPApp.instance()
266 app = BaseIPApp.instance()
267 security_dir = app.profile_dir.security_dir
267 security_dir = app.profile_dir.security_dir
268 profile = app.profile
268 profile = app.profile
269 else:
269 else:
270 profile = 'default'
270 profile = 'default'
271 security_dir = ''
271 security_dir = ''
272
272
273 try:
273 try:
274 connection_file = get_connection_file()
274 connection_file = get_connection_file()
275 info = get_connection_info(unpack=False)
275 info = get_connection_info(unpack=False)
276 except Exception as e:
276 except Exception as e:
277 error("Could not get connection info: %r" % e)
277 error("Could not get connection info: %r" % e)
278 return
278 return
279
279
280 # add profile flag for non-default profile
280 # add profile flag for non-default profile
281 profile_flag = "--profile %s" % profile if profile != 'default' else ""
281 profile_flag = "--profile %s" % profile if profile != 'default' else ""
282
282
283 # if it's in the security dir, truncate to basename
283 # if it's in the security dir, truncate to basename
284 if security_dir == os.path.dirname(connection_file):
284 if security_dir == os.path.dirname(connection_file):
285 connection_file = os.path.basename(connection_file)
285 connection_file = os.path.basename(connection_file)
286
286
287
287
288 print (info + '\n')
288 print (info + '\n')
289 print ("Paste the above JSON into a file, and connect with:\n"
289 print ("Paste the above JSON into a file, and connect with:\n"
290 " $> ipython <app> --existing <file>\n"
290 " $> ipython <app> --existing <file>\n"
291 "or, if you are local, you can connect with just:\n"
291 "or, if you are local, you can connect with just:\n"
292 " $> ipython <app> --existing {0} {1}\n"
292 " $> ipython <app> --existing {0} {1}\n"
293 "or even just:\n"
293 "or even just:\n"
294 " $> ipython <app> --existing {1}\n"
294 " $> ipython <app> --existing {1}\n"
295 "if this is the most recent IPython session you have started.".format(
295 "if this is the most recent IPython session you have started.".format(
296 connection_file, profile_flag
296 connection_file, profile_flag
297 )
297 )
298 )
298 )
299
299
300 @line_magic
300 @line_magic
301 def qtconsole(self, arg_s):
301 def qtconsole(self, arg_s):
302 """Open a qtconsole connected to this kernel.
302 """Open a qtconsole connected to this kernel.
303
303
304 Useful for connecting a qtconsole to running notebooks, for better
304 Useful for connecting a qtconsole to running notebooks, for better
305 debugging.
305 debugging.
306 """
306 """
307
307
308 # %qtconsole should imply bind_kernel for engines:
308 # %qtconsole should imply bind_kernel for engines:
309 try:
309 try:
310 from IPython.parallel import bind_kernel
310 from IPython.parallel import bind_kernel
311 except ImportError:
311 except ImportError:
312 # technically possible, because parallel has higher pyzmq min-version
312 # technically possible, because parallel has higher pyzmq min-version
313 pass
313 pass
314 else:
314 else:
315 bind_kernel()
315 bind_kernel()
316
316
317 try:
317 try:
318 p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix'))
318 p = connect_qtconsole(argv=arg_split(arg_s, os.name=='posix'))
319 except Exception as e:
319 except Exception as e:
320 error("Could not start qtconsole: %r" % e)
320 error("Could not start qtconsole: %r" % e)
321 return
321 return
322
322
323 @line_magic
323 @line_magic
324 def autosave(self, arg_s):
324 def autosave(self, arg_s):
325 """Set the autosave interval in the notebook (in seconds).
325 """Set the autosave interval in the notebook (in seconds).
326
326
327 The default value is 120, or two minutes.
327 The default value is 120, or two minutes.
328 ``%autosave 0`` will disable autosave.
328 ``%autosave 0`` will disable autosave.
329
329
330 This magic only has an effect when called from the notebook interface.
330 This magic only has an effect when called from the notebook interface.
331 It has no effect when called in a startup file.
331 It has no effect when called in a startup file.
332 """
332 """
333
333
334 try:
334 try:
335 interval = int(arg_s)
335 interval = int(arg_s)
336 except ValueError:
336 except ValueError:
337 raise UsageError("%%autosave requires an integer, got %r" % arg_s)
337 raise UsageError("%%autosave requires an integer, got %r" % arg_s)
338
338
339 # javascript wants milliseconds
339 # javascript wants milliseconds
340 milliseconds = 1000 * interval
340 milliseconds = 1000 * interval
341 display(Javascript("IPython.notebook.set_autosave_interval(%i)" % milliseconds),
341 display(Javascript("IPython.notebook.set_autosave_interval(%i)" % milliseconds),
342 include=['application/javascript']
342 include=['application/javascript']
343 )
343 )
344 if interval:
344 if interval:
345 print("Autosaving every %i seconds" % interval)
345 print("Autosaving every %i seconds" % interval)
346 else:
346 else:
347 print("Autosave disabled")
347 print("Autosave disabled")
348
348
349
349
350 class ZMQInteractiveShell(InteractiveShell):
350 class ZMQInteractiveShell(InteractiveShell):
351 """A subclass of InteractiveShell for ZMQ."""
351 """A subclass of InteractiveShell for ZMQ."""
352
352
353 displayhook_class = Type(ZMQShellDisplayHook)
353 displayhook_class = Type(ZMQShellDisplayHook)
354 display_pub_class = Type(ZMQDisplayPublisher)
354 display_pub_class = Type(ZMQDisplayPublisher)
355 data_pub_class = Type(ZMQDataPublisher)
355 data_pub_class = Type(ZMQDataPublisher)
356 kernel = Any()
356 kernel = Any()
357 parent_header = Any()
357 parent_header = Any()
358
358
359 def _banner1_default(self):
359 def _banner1_default(self):
360 return default_gui_banner
360 return default_gui_banner
361
361
362 # Override the traitlet in the parent class, because there's no point using
362 # Override the traitlet in the parent class, because there's no point using
363 # readline for the kernel. Can be removed when the readline code is moved
363 # readline for the kernel. Can be removed when the readline code is moved
364 # to the terminal frontend.
364 # to the terminal frontend.
365 colors_force = CBool(True)
365 colors_force = CBool(True)
366 readline_use = CBool(False)
366 readline_use = CBool(False)
367 # autoindent has no meaning in a zmqshell, and attempting to enable it
367 # autoindent has no meaning in a zmqshell, and attempting to enable it
368 # will print a warning in the absence of readline.
368 # will print a warning in the absence of readline.
369 autoindent = CBool(False)
369 autoindent = CBool(False)
370
370
371 exiter = Instance(ZMQExitAutocall)
371 exiter = Instance(ZMQExitAutocall)
372 def _exiter_default(self):
372 def _exiter_default(self):
373 return ZMQExitAutocall(self)
373 return ZMQExitAutocall(self)
374
374
375 def _exit_now_changed(self, name, old, new):
375 def _exit_now_changed(self, name, old, new):
376 """stop eventloop when exit_now fires"""
376 """stop eventloop when exit_now fires"""
377 if new:
377 if new:
378 loop = ioloop.IOLoop.instance()
378 loop = ioloop.IOLoop.instance()
379 loop.add_timeout(time.time()+0.1, loop.stop)
379 loop.add_timeout(time.time()+0.1, loop.stop)
380
380
381 keepkernel_on_exit = None
381 keepkernel_on_exit = None
382
382
383 # Over ZeroMQ, GUI control isn't done with PyOS_InputHook as there is no
383 # Over ZeroMQ, GUI control isn't done with PyOS_InputHook as there is no
384 # interactive input being read; we provide event loop support in ipkernel
384 # interactive input being read; we provide event loop support in ipkernel
385 @staticmethod
385 @staticmethod
386 def enable_gui(gui):
386 def enable_gui(gui):
387 from .eventloops import enable_gui as real_enable_gui
387 from .eventloops import enable_gui as real_enable_gui
388 try:
388 try:
389 real_enable_gui(gui)
389 real_enable_gui(gui)
390 except ValueError as e:
390 except ValueError as e:
391 raise UsageError("%s" % e)
391 raise UsageError("%s" % e)
392
392
393 def init_environment(self):
393 def init_environment(self):
394 """Configure the user's environment.
394 """Configure the user's environment.
395
395
396 """
396 """
397 env = os.environ
397 env = os.environ
398 # These two ensure 'ls' produces nice coloring on BSD-derived systems
398 # These two ensure 'ls' produces nice coloring on BSD-derived systems
399 env['TERM'] = 'xterm-color'
399 env['TERM'] = 'xterm-color'
400 env['CLICOLOR'] = '1'
400 env['CLICOLOR'] = '1'
401 # Since normal pagers don't work at all (over pexpect we don't have
401 # Since normal pagers don't work at all (over pexpect we don't have
402 # single-key control of the subprocess), try to disable paging in
402 # single-key control of the subprocess), try to disable paging in
403 # subprocesses as much as possible.
403 # subprocesses as much as possible.
404 env['PAGER'] = 'cat'
404 env['PAGER'] = 'cat'
405 env['GIT_PAGER'] = 'cat'
405 env['GIT_PAGER'] = 'cat'
406
406
407 # And install the payload version of page.
407 # And install the payload version of page.
408 install_payload_page()
408 install_payload_page()
409
409
410 def ask_exit(self):
410 def ask_exit(self):
411 """Engage the exit actions."""
411 """Engage the exit actions."""
412 self.exit_now = (not self.keepkernel_on_exit)
412 self.exit_now = (not self.keepkernel_on_exit)
413 payload = dict(
413 payload = dict(
414 source='ask_exit',
414 source='ask_exit',
415 keepkernel=self.keepkernel_on_exit,
415 keepkernel=self.keepkernel_on_exit,
416 )
416 )
417 self.payload_manager.write_payload(payload)
417 self.payload_manager.write_payload(payload)
418
418
419 def _showtraceback(self, etype, evalue, stb):
419 def _showtraceback(self, etype, evalue, stb):
420 # try to preserve ordering of tracebacks and print statements
420 # try to preserve ordering of tracebacks and print statements
421 sys.stdout.flush()
421 sys.stdout.flush()
422 sys.stderr.flush()
422 sys.stderr.flush()
423
423
424 exc_content = {
424 exc_content = {
425 u'traceback' : stb,
425 u'traceback' : stb,
426 u'ename' : unicode_type(etype.__name__),
426 u'ename' : unicode_type(etype.__name__),
427 u'evalue' : py3compat.safe_unicode(evalue),
427 u'evalue' : py3compat.safe_unicode(evalue),
428 }
428 }
429
429
430 dh = self.displayhook
430 dh = self.displayhook
431 # Send exception info over pub socket for other clients than the caller
431 # Send exception info over pub socket for other clients than the caller
432 # to pick up
432 # to pick up
433 topic = None
433 topic = None
434 if dh.topic:
434 if dh.topic:
435 topic = dh.topic.replace(b'execute_result', b'error')
435 topic = dh.topic.replace(b'execute_result', b'error')
436
436
437 exc_msg = dh.session.send(dh.pub_socket, u'error', json_clean(exc_content), dh.parent_header, ident=topic)
437 exc_msg = dh.session.send(dh.pub_socket, u'error', json_clean(exc_content), dh.parent_header, ident=topic)
438
438
439 # FIXME - Hack: store exception info in shell object. Right now, the
439 # FIXME - Hack: store exception info in shell object. Right now, the
440 # caller is reading this info after the fact, we need to fix this logic
440 # caller is reading this info after the fact, we need to fix this logic
441 # to remove this hack. Even uglier, we need to store the error status
441 # to remove this hack. Even uglier, we need to store the error status
442 # here, because in the main loop, the logic that sets it is being
442 # here, because in the main loop, the logic that sets it is being
443 # skipped because runlines swallows the exceptions.
443 # skipped because runlines swallows the exceptions.
444 exc_content[u'status'] = u'error'
444 exc_content[u'status'] = u'error'
445 self._reply_content = exc_content
445 self._reply_content = exc_content
446 # /FIXME
446 # /FIXME
447
447
448 return exc_content
448 return exc_content
449
449
450 def set_next_input(self, text):
450 def set_next_input(self, text, replace=False):
451 """Send the specified text to the frontend to be presented at the next
451 """Send the specified text to the frontend to be presented at the next
452 input cell."""
452 input cell."""
453 payload = dict(
453 payload = dict(
454 source='set_next_input',
454 source='set_next_input',
455 text=text
455 text=text,
456 replace=replace,
456 )
457 )
457 self.payload_manager.write_payload(payload)
458 self.payload_manager.write_payload(payload)
458
459
459 def set_parent(self, parent):
460 def set_parent(self, parent):
460 """Set the parent header for associating output with its triggering input"""
461 """Set the parent header for associating output with its triggering input"""
461 self.parent_header = parent
462 self.parent_header = parent
462 self.displayhook.set_parent(parent)
463 self.displayhook.set_parent(parent)
463 self.display_pub.set_parent(parent)
464 self.display_pub.set_parent(parent)
464 self.data_pub.set_parent(parent)
465 self.data_pub.set_parent(parent)
465 try:
466 try:
466 sys.stdout.set_parent(parent)
467 sys.stdout.set_parent(parent)
467 except AttributeError:
468 except AttributeError:
468 pass
469 pass
469 try:
470 try:
470 sys.stderr.set_parent(parent)
471 sys.stderr.set_parent(parent)
471 except AttributeError:
472 except AttributeError:
472 pass
473 pass
473
474
474 def get_parent(self):
475 def get_parent(self):
475 return self.parent_header
476 return self.parent_header
476
477
477 #-------------------------------------------------------------------------
478 #-------------------------------------------------------------------------
478 # Things related to magics
479 # Things related to magics
479 #-------------------------------------------------------------------------
480 #-------------------------------------------------------------------------
480
481
481 def init_magics(self):
482 def init_magics(self):
482 super(ZMQInteractiveShell, self).init_magics()
483 super(ZMQInteractiveShell, self).init_magics()
483 self.register_magics(KernelMagics)
484 self.register_magics(KernelMagics)
484 self.magics_manager.register_alias('ed', 'edit')
485 self.magics_manager.register_alias('ed', 'edit')
485
486
486
487
487 InteractiveShellABC.register(ZMQInteractiveShell)
488 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now