##// END OF EJS Templates
reformatting
Matthias Bussonnier -
Show More
@@ -1,1265 +1,1268 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Sphinx directive to support embedded IPython code.
3 Sphinx directive to support embedded IPython code.
4
4
5 IPython provides an extension for `Sphinx <http://www.sphinx-doc.org/>`_ to
5 IPython provides an extension for `Sphinx <http://www.sphinx-doc.org/>`_ to
6 highlight and run code.
6 highlight and run code.
7
7
8 This directive allows pasting of entire interactive IPython sessions, prompts
8 This directive allows pasting of entire interactive IPython sessions, prompts
9 and all, and their code will actually get re-executed at doc build time, with
9 and all, and their code will actually get re-executed at doc build time, with
10 all prompts renumbered sequentially. It also allows you to input code as a pure
10 all prompts renumbered sequentially. It also allows you to input code as a pure
11 python input by giving the argument python to the directive. The output looks
11 python input by giving the argument python to the directive. The output looks
12 like an interactive ipython section.
12 like an interactive ipython section.
13
13
14 Here is an example of how the IPython directive can
14 Here is an example of how the IPython directive can
15 **run** python code, at build time.
15 **run** python code, at build time.
16
16
17 .. ipython::
17 .. ipython::
18
18
19 In [1]: 1+1
19 In [1]: 1+1
20
20
21 In [1]: import datetime
21 In [1]: import datetime
22 ...: datetime.datetime.now()
22 ...: datetime.datetime.now()
23
23
24 It supports IPython construct that plain
24 It supports IPython construct that plain
25 Python does not understand (like magics):
25 Python does not understand (like magics):
26
26
27 .. ipython::
27 .. ipython::
28
28
29 In [0]: import time
29 In [0]: import time
30
30
31 In [0]: %timeit time.sleep(0.05)
31 In [0]: %timeit time.sleep(0.05)
32
32
33 This will also support top-level async when using IPython 7.0+
33 This will also support top-level async when using IPython 7.0+
34
34
35 .. ipython::
35 .. ipython::
36
36
37 In [2]: import asyncio
37 In [2]: import asyncio
38 ...: print('before')
38 ...: print('before')
39 ...: await asyncio.sleep(1)
39 ...: await asyncio.sleep(1)
40 ...: print('after')
40 ...: print('after')
41
41
42
42
43 The namespace will persist across multiple code chucks, Let's define a variable:
43 The namespace will persist across multiple code chucks, Let's define a variable:
44
44
45 .. ipython::
45 .. ipython::
46
46
47 In [0]: who = "World"
47 In [0]: who = "World"
48
48
49 And now say hello:
49 And now say hello:
50
50
51 .. ipython::
51 .. ipython::
52
52
53 In [0]: print('Hello,', who)
53 In [0]: print('Hello,', who)
54
54
55 If the current section raises an exception, you can add the ``:okexcept:`` flag
55 If the current section raises an exception, you can add the ``:okexcept:`` flag
56 to the current block, otherwise the build will fail.
56 to the current block, otherwise the build will fail.
57
57
58 .. ipython::
58 .. ipython::
59 :okexcept:
59 :okexcept:
60
60
61 In [1]: 1/0
61 In [1]: 1/0
62
62
63 IPython Sphinx directive module
63 IPython Sphinx directive module
64 ===============================
64 ===============================
65
65
66 To enable this directive, simply list it in your Sphinx ``conf.py`` file
66 To enable this directive, simply list it in your Sphinx ``conf.py`` file
67 (making sure the directory where you placed it is visible to sphinx, as is
67 (making sure the directory where you placed it is visible to sphinx, as is
68 needed for all Sphinx directives). For example, to enable syntax highlighting
68 needed for all Sphinx directives). For example, to enable syntax highlighting
69 and the IPython directive::
69 and the IPython directive::
70
70
71 extensions = ['IPython.sphinxext.ipython_console_highlighting',
71 extensions = ['IPython.sphinxext.ipython_console_highlighting',
72 'IPython.sphinxext.ipython_directive']
72 'IPython.sphinxext.ipython_directive']
73
73
74 The IPython directive outputs code-blocks with the language 'ipython'. So
74 The IPython directive outputs code-blocks with the language 'ipython'. So
75 if you do not have the syntax highlighting extension enabled as well, then
75 if you do not have the syntax highlighting extension enabled as well, then
76 all rendered code-blocks will be uncolored. By default this directive assumes
76 all rendered code-blocks will be uncolored. By default this directive assumes
77 that your prompts are unchanged IPython ones, but this can be customized.
77 that your prompts are unchanged IPython ones, but this can be customized.
78 The configurable options that can be placed in conf.py are:
78 The configurable options that can be placed in conf.py are:
79
79
80 ipython_savefig_dir:
80 ipython_savefig_dir:
81 The directory in which to save the figures. This is relative to the
81 The directory in which to save the figures. This is relative to the
82 Sphinx source directory. The default is `html_static_path`.
82 Sphinx source directory. The default is `html_static_path`.
83 ipython_rgxin:
83 ipython_rgxin:
84 The compiled regular expression to denote the start of IPython input
84 The compiled regular expression to denote the start of IPython input
85 lines. The default is ``re.compile('In \\[(\\d+)\\]:\\s?(.*)\\s*')``. You
85 lines. The default is ``re.compile('In \\[(\\d+)\\]:\\s?(.*)\\s*')``. You
86 shouldn't need to change this.
86 shouldn't need to change this.
87 ipython_warning_is_error: [default to True]
87 ipython_warning_is_error: [default to True]
88 Fail the build if something unexpected happen, for example if a block raise
88 Fail the build if something unexpected happen, for example if a block raise
89 an exception but does not have the `:okexcept:` flag. The exact behavior of
89 an exception but does not have the `:okexcept:` flag. The exact behavior of
90 what is considered strict, may change between the sphinx directive version.
90 what is considered strict, may change between the sphinx directive version.
91 ipython_rgxout:
91 ipython_rgxout:
92 The compiled regular expression to denote the start of IPython output
92 The compiled regular expression to denote the start of IPython output
93 lines. The default is ``re.compile('Out\\[(\\d+)\\]:\\s?(.*)\\s*')``. You
93 lines. The default is ``re.compile('Out\\[(\\d+)\\]:\\s?(.*)\\s*')``. You
94 shouldn't need to change this.
94 shouldn't need to change this.
95 ipython_promptin:
95 ipython_promptin:
96 The string to represent the IPython input prompt in the generated ReST.
96 The string to represent the IPython input prompt in the generated ReST.
97 The default is ``'In [%d]:'``. This expects that the line numbers are used
97 The default is ``'In [%d]:'``. This expects that the line numbers are used
98 in the prompt.
98 in the prompt.
99 ipython_promptout:
99 ipython_promptout:
100 The string to represent the IPython prompt in the generated ReST. The
100 The string to represent the IPython prompt in the generated ReST. The
101 default is ``'Out [%d]:'``. This expects that the line numbers are used
101 default is ``'Out [%d]:'``. This expects that the line numbers are used
102 in the prompt.
102 in the prompt.
103 ipython_mplbackend:
103 ipython_mplbackend:
104 The string which specifies if the embedded Sphinx shell should import
104 The string which specifies if the embedded Sphinx shell should import
105 Matplotlib and set the backend. The value specifies a backend that is
105 Matplotlib and set the backend. The value specifies a backend that is
106 passed to `matplotlib.use()` before any lines in `ipython_execlines` are
106 passed to `matplotlib.use()` before any lines in `ipython_execlines` are
107 executed. If not specified in conf.py, then the default value of 'agg' is
107 executed. If not specified in conf.py, then the default value of 'agg' is
108 used. To use the IPython directive without matplotlib as a dependency, set
108 used. To use the IPython directive without matplotlib as a dependency, set
109 the value to `None`. It may end up that matplotlib is still imported
109 the value to `None`. It may end up that matplotlib is still imported
110 if the user specifies so in `ipython_execlines` or makes use of the
110 if the user specifies so in `ipython_execlines` or makes use of the
111 @savefig pseudo decorator.
111 @savefig pseudo decorator.
112 ipython_execlines:
112 ipython_execlines:
113 A list of strings to be exec'd in the embedded Sphinx shell. Typical
113 A list of strings to be exec'd in the embedded Sphinx shell. Typical
114 usage is to make certain packages always available. Set this to an empty
114 usage is to make certain packages always available. Set this to an empty
115 list if you wish to have no imports always available. If specified in
115 list if you wish to have no imports always available. If specified in
116 ``conf.py`` as `None`, then it has the effect of making no imports available.
116 ``conf.py`` as `None`, then it has the effect of making no imports available.
117 If omitted from conf.py altogether, then the default value of
117 If omitted from conf.py altogether, then the default value of
118 ['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
118 ['import numpy as np', 'import matplotlib.pyplot as plt'] is used.
119 ipython_holdcount
119 ipython_holdcount
120 When the @suppress pseudo-decorator is used, the execution count can be
120 When the @suppress pseudo-decorator is used, the execution count can be
121 incremented or not. The default behavior is to hold the execution count,
121 incremented or not. The default behavior is to hold the execution count,
122 corresponding to a value of `True`. Set this to `False` to increment
122 corresponding to a value of `True`. Set this to `False` to increment
123 the execution count after each suppressed command.
123 the execution count after each suppressed command.
124
124
125 As an example, to use the IPython directive when `matplotlib` is not available,
125 As an example, to use the IPython directive when `matplotlib` is not available,
126 one sets the backend to `None`::
126 one sets the backend to `None`::
127
127
128 ipython_mplbackend = None
128 ipython_mplbackend = None
129
129
130 An example usage of the directive is:
130 An example usage of the directive is:
131
131
132 .. code-block:: rst
132 .. code-block:: rst
133
133
134 .. ipython::
134 .. ipython::
135
135
136 In [1]: x = 1
136 In [1]: x = 1
137
137
138 In [2]: y = x**2
138 In [2]: y = x**2
139
139
140 In [3]: print(y)
140 In [3]: print(y)
141
141
142 See http://matplotlib.org/sampledoc/ipython_directive.html for additional
142 See http://matplotlib.org/sampledoc/ipython_directive.html for additional
143 documentation.
143 documentation.
144
144
145 Pseudo-Decorators
145 Pseudo-Decorators
146 =================
146 =================
147
147
148 Note: Only one decorator is supported per input. If more than one decorator
148 Note: Only one decorator is supported per input. If more than one decorator
149 is specified, then only the last one is used.
149 is specified, then only the last one is used.
150
150
151 In addition to the Pseudo-Decorators/options described at the above link,
151 In addition to the Pseudo-Decorators/options described at the above link,
152 several enhancements have been made. The directive will emit a message to the
152 several enhancements have been made. The directive will emit a message to the
153 console at build-time if code-execution resulted in an exception or warning.
153 console at build-time if code-execution resulted in an exception or warning.
154 You can suppress these on a per-block basis by specifying the :okexcept:
154 You can suppress these on a per-block basis by specifying the :okexcept:
155 or :okwarning: options:
155 or :okwarning: options:
156
156
157 .. code-block:: rst
157 .. code-block:: rst
158
158
159 .. ipython::
159 .. ipython::
160 :okexcept:
160 :okexcept:
161 :okwarning:
161 :okwarning:
162
162
163 In [1]: 1/0
163 In [1]: 1/0
164 In [2]: # raise warning.
164 In [2]: # raise warning.
165
165
166 To Do
166 To Do
167 =====
167 =====
168
168
169 - Turn the ad-hoc test() function into a real test suite.
169 - Turn the ad-hoc test() function into a real test suite.
170 - Break up ipython-specific functionality from matplotlib stuff into better
170 - Break up ipython-specific functionality from matplotlib stuff into better
171 separated code.
171 separated code.
172
172
173 """
173 """
174
174
175 # Authors
175 # Authors
176 # =======
176 # =======
177 #
177 #
178 # - John D Hunter: original author.
178 # - John D Hunter: original author.
179 # - Fernando Perez: refactoring, documentation, cleanups, port to 0.11.
179 # - Fernando Perez: refactoring, documentation, cleanups, port to 0.11.
180 # - VΓ‘clavΕ milauer <eudoxos-AT-arcig.cz>: Prompt generalizations.
180 # - VΓ‘clavΕ milauer <eudoxos-AT-arcig.cz>: Prompt generalizations.
181 # - Skipper Seabold, refactoring, cleanups, pure python addition
181 # - Skipper Seabold, refactoring, cleanups, pure python addition
182
182
183 #-----------------------------------------------------------------------------
183 #-----------------------------------------------------------------------------
184 # Imports
184 # Imports
185 #-----------------------------------------------------------------------------
185 #-----------------------------------------------------------------------------
186
186
187 # Stdlib
187 # Stdlib
188 import atexit
188 import atexit
189 import errno
189 import errno
190 import os
190 import os
191 import pathlib
191 import pathlib
192 import re
192 import re
193 import sys
193 import sys
194 import tempfile
194 import tempfile
195 import ast
195 import ast
196 import warnings
196 import warnings
197 import shutil
197 import shutil
198 from io import StringIO
198 from io import StringIO
199
199
200 # Third-party
200 # Third-party
201 from docutils.parsers.rst import directives
201 from docutils.parsers.rst import directives
202 from docutils.parsers.rst import Directive
202 from docutils.parsers.rst import Directive
203 from sphinx.util import logging
203 from sphinx.util import logging
204
204
205 # Our own
205 # Our own
206 from traitlets.config import Config
206 from traitlets.config import Config
207 from IPython import InteractiveShell
207 from IPython import InteractiveShell
208 from IPython.core.profiledir import ProfileDir
208 from IPython.core.profiledir import ProfileDir
209
209
210 use_matplotlib = False
210 use_matplotlib = False
211 try:
211 try:
212 import matplotlib
212 import matplotlib
213 use_matplotlib = True
213 use_matplotlib = True
214 except Exception:
214 except Exception:
215 pass
215 pass
216
216
217 #-----------------------------------------------------------------------------
217 #-----------------------------------------------------------------------------
218 # Globals
218 # Globals
219 #-----------------------------------------------------------------------------
219 #-----------------------------------------------------------------------------
220 # for tokenizing blocks
220 # for tokenizing blocks
221 COMMENT, INPUT, OUTPUT = range(3)
221 COMMENT, INPUT, OUTPUT = range(3)
222
222
223 PSEUDO_DECORATORS = ["suppress", "verbatim", "savefig", "doctest"]
223 PSEUDO_DECORATORS = ["suppress", "verbatim", "savefig", "doctest"]
224
224
225 #-----------------------------------------------------------------------------
225 #-----------------------------------------------------------------------------
226 # Functions and class declarations
226 # Functions and class declarations
227 #-----------------------------------------------------------------------------
227 #-----------------------------------------------------------------------------
228
228
229 def block_parser(part, rgxin, rgxout, fmtin, fmtout):
229 def block_parser(part, rgxin, rgxout, fmtin, fmtout):
230 """
230 """
231 part is a string of ipython text, comprised of at most one
231 part is a string of ipython text, comprised of at most one
232 input, one output, comments, and blank lines. The block parser
232 input, one output, comments, and blank lines. The block parser
233 parses the text into a list of::
233 parses the text into a list of::
234
234
235 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
235 blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
236
236
237 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
237 where TOKEN is one of [COMMENT | INPUT | OUTPUT ] and
238 data is, depending on the type of token::
238 data is, depending on the type of token::
239
239
240 COMMENT : the comment string
240 COMMENT : the comment string
241
241
242 INPUT: the (DECORATOR, INPUT_LINE, REST) where
242 INPUT: the (DECORATOR, INPUT_LINE, REST) where
243 DECORATOR: the input decorator (or None)
243 DECORATOR: the input decorator (or None)
244 INPUT_LINE: the input as string (possibly multi-line)
244 INPUT_LINE: the input as string (possibly multi-line)
245 REST : any stdout generated by the input line (not OUTPUT)
245 REST : any stdout generated by the input line (not OUTPUT)
246
246
247 OUTPUT: the output string, possibly multi-line
247 OUTPUT: the output string, possibly multi-line
248
248
249 """
249 """
250 block = []
250 block = []
251 lines = part.split('\n')
251 lines = part.split('\n')
252 N = len(lines)
252 N = len(lines)
253 i = 0
253 i = 0
254 decorator = None
254 decorator = None
255 while 1:
255 while 1:
256
256
257 if i==N:
257 if i==N:
258 # nothing left to parse -- the last line
258 # nothing left to parse -- the last line
259 break
259 break
260
260
261 line = lines[i]
261 line = lines[i]
262 i += 1
262 i += 1
263 line_stripped = line.strip()
263 line_stripped = line.strip()
264 if line_stripped.startswith('#'):
264 if line_stripped.startswith('#'):
265 block.append((COMMENT, line))
265 block.append((COMMENT, line))
266 continue
266 continue
267
267
268 if any(
268 if any(
269 line_stripped.startswith('@' + pseudo_decorator) for pseudo_decorator in PSEUDO_DECORATORS
269 line_stripped.startswith("@" + pseudo_decorator)
270 ):
270 for pseudo_decorator in PSEUDO_DECORATORS
271 ):
271 if decorator:
272 if decorator:
272 raise RuntimeError("Applying multiple pseudo-decorators on one line is not supported")
273 raise RuntimeError(
274 "Applying multiple pseudo-decorators on one line is not supported"
275 )
273 else:
276 else:
274 decorator = line_stripped
277 decorator = line_stripped
275 continue
278 continue
276
279
277 # does this look like an input line?
280 # does this look like an input line?
278 matchin = rgxin.match(line)
281 matchin = rgxin.match(line)
279 if matchin:
282 if matchin:
280 lineno, inputline = int(matchin.group(1)), matchin.group(2)
283 lineno, inputline = int(matchin.group(1)), matchin.group(2)
281
284
282 # the ....: continuation string
285 # the ....: continuation string
283 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
286 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
284 Nc = len(continuation)
287 Nc = len(continuation)
285 # input lines can continue on for more than one line, if
288 # input lines can continue on for more than one line, if
286 # we have a '\' line continuation char or a function call
289 # we have a '\' line continuation char or a function call
287 # echo line 'print'. The input line can only be
290 # echo line 'print'. The input line can only be
288 # terminated by the end of the block or an output line, so
291 # terminated by the end of the block or an output line, so
289 # we parse out the rest of the input line if it is
292 # we parse out the rest of the input line if it is
290 # multiline as well as any echo text
293 # multiline as well as any echo text
291
294
292 rest = []
295 rest = []
293 while i<N:
296 while i<N:
294
297
295 # look ahead; if the next line is blank, or a comment, or
298 # look ahead; if the next line is blank, or a comment, or
296 # an output line, we're done
299 # an output line, we're done
297
300
298 nextline = lines[i]
301 nextline = lines[i]
299 matchout = rgxout.match(nextline)
302 matchout = rgxout.match(nextline)
300 #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation))
303 #print "nextline=%s, continuation=%s, starts=%s"%(nextline, continuation, nextline.startswith(continuation))
301 if matchout or nextline.startswith('#'):
304 if matchout or nextline.startswith('#'):
302 break
305 break
303 elif nextline.startswith(continuation):
306 elif nextline.startswith(continuation):
304 # The default ipython_rgx* treat the space following the colon as optional.
307 # The default ipython_rgx* treat the space following the colon as optional.
305 # However, If the space is there we must consume it or code
308 # However, If the space is there we must consume it or code
306 # employing the cython_magic extension will fail to execute.
309 # employing the cython_magic extension will fail to execute.
307 #
310 #
308 # This works with the default ipython_rgx* patterns,
311 # This works with the default ipython_rgx* patterns,
309 # If you modify them, YMMV.
312 # If you modify them, YMMV.
310 nextline = nextline[Nc:]
313 nextline = nextline[Nc:]
311 if nextline and nextline[0] == ' ':
314 if nextline and nextline[0] == ' ':
312 nextline = nextline[1:]
315 nextline = nextline[1:]
313
316
314 inputline += '\n' + nextline
317 inputline += '\n' + nextline
315 else:
318 else:
316 rest.append(nextline)
319 rest.append(nextline)
317 i+= 1
320 i+= 1
318
321
319 block.append((INPUT, (decorator, inputline, '\n'.join(rest))))
322 block.append((INPUT, (decorator, inputline, '\n'.join(rest))))
320 continue
323 continue
321
324
322 # if it looks like an output line grab all the text to the end
325 # if it looks like an output line grab all the text to the end
323 # of the block
326 # of the block
324 matchout = rgxout.match(line)
327 matchout = rgxout.match(line)
325 if matchout:
328 if matchout:
326 lineno, output = int(matchout.group(1)), matchout.group(2)
329 lineno, output = int(matchout.group(1)), matchout.group(2)
327 if i<N-1:
330 if i<N-1:
328 output = '\n'.join([output] + lines[i:])
331 output = '\n'.join([output] + lines[i:])
329
332
330 block.append((OUTPUT, output))
333 block.append((OUTPUT, output))
331 break
334 break
332
335
333 return block
336 return block
334
337
335
338
336 class EmbeddedSphinxShell(object):
339 class EmbeddedSphinxShell(object):
337 """An embedded IPython instance to run inside Sphinx"""
340 """An embedded IPython instance to run inside Sphinx"""
338
341
339 def __init__(self, exec_lines=None):
342 def __init__(self, exec_lines=None):
340
343
341 self.cout = StringIO()
344 self.cout = StringIO()
342
345
343 if exec_lines is None:
346 if exec_lines is None:
344 exec_lines = []
347 exec_lines = []
345
348
346 # Create config object for IPython
349 # Create config object for IPython
347 config = Config()
350 config = Config()
348 config.HistoryManager.hist_file = ':memory:'
351 config.HistoryManager.hist_file = ':memory:'
349 config.InteractiveShell.autocall = False
352 config.InteractiveShell.autocall = False
350 config.InteractiveShell.autoindent = False
353 config.InteractiveShell.autoindent = False
351 config.InteractiveShell.colors = 'NoColor'
354 config.InteractiveShell.colors = 'NoColor'
352
355
353 # create a profile so instance history isn't saved
356 # create a profile so instance history isn't saved
354 tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
357 tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
355 profname = 'auto_profile_sphinx_build'
358 profname = 'auto_profile_sphinx_build'
356 pdir = os.path.join(tmp_profile_dir,profname)
359 pdir = os.path.join(tmp_profile_dir,profname)
357 profile = ProfileDir.create_profile_dir(pdir)
360 profile = ProfileDir.create_profile_dir(pdir)
358
361
359 # Create and initialize global ipython, but don't start its mainloop.
362 # Create and initialize global ipython, but don't start its mainloop.
360 # This will persist across different EmbeddedSphinxShell instances.
363 # This will persist across different EmbeddedSphinxShell instances.
361 IP = InteractiveShell.instance(config=config, profile_dir=profile)
364 IP = InteractiveShell.instance(config=config, profile_dir=profile)
362 atexit.register(self.cleanup)
365 atexit.register(self.cleanup)
363
366
364 # Store a few parts of IPython we'll need.
367 # Store a few parts of IPython we'll need.
365 self.IP = IP
368 self.IP = IP
366 self.user_ns = self.IP.user_ns
369 self.user_ns = self.IP.user_ns
367 self.user_global_ns = self.IP.user_global_ns
370 self.user_global_ns = self.IP.user_global_ns
368
371
369 self.input = ''
372 self.input = ''
370 self.output = ''
373 self.output = ''
371 self.tmp_profile_dir = tmp_profile_dir
374 self.tmp_profile_dir = tmp_profile_dir
372
375
373 self.is_verbatim = False
376 self.is_verbatim = False
374 self.is_doctest = False
377 self.is_doctest = False
375 self.is_suppress = False
378 self.is_suppress = False
376
379
377 # Optionally, provide more detailed information to shell.
380 # Optionally, provide more detailed information to shell.
378 # this is assigned by the SetUp method of IPythonDirective
381 # this is assigned by the SetUp method of IPythonDirective
379 # to point at itself.
382 # to point at itself.
380 #
383 #
381 # So, you can access handy things at self.directive.state
384 # So, you can access handy things at self.directive.state
382 self.directive = None
385 self.directive = None
383
386
384 # on the first call to the savefig decorator, we'll import
387 # on the first call to the savefig decorator, we'll import
385 # pyplot as plt so we can make a call to the plt.gcf().savefig
388 # pyplot as plt so we can make a call to the plt.gcf().savefig
386 self._pyplot_imported = False
389 self._pyplot_imported = False
387
390
388 # Prepopulate the namespace.
391 # Prepopulate the namespace.
389 for line in exec_lines:
392 for line in exec_lines:
390 self.process_input_line(line, store_history=False)
393 self.process_input_line(line, store_history=False)
391
394
392 def cleanup(self):
395 def cleanup(self):
393 shutil.rmtree(self.tmp_profile_dir, ignore_errors=True)
396 shutil.rmtree(self.tmp_profile_dir, ignore_errors=True)
394
397
395 def clear_cout(self):
398 def clear_cout(self):
396 self.cout.seek(0)
399 self.cout.seek(0)
397 self.cout.truncate(0)
400 self.cout.truncate(0)
398
401
399 def process_input_line(self, line, store_history):
402 def process_input_line(self, line, store_history):
400 return self.process_input_lines([line], store_history=store_history)
403 return self.process_input_lines([line], store_history=store_history)
401
404
402 def process_input_lines(self, lines, store_history=True):
405 def process_input_lines(self, lines, store_history=True):
403 """process the input, capturing stdout"""
406 """process the input, capturing stdout"""
404 stdout = sys.stdout
407 stdout = sys.stdout
405 source_raw = '\n'.join(lines)
408 source_raw = '\n'.join(lines)
406 try:
409 try:
407 sys.stdout = self.cout
410 sys.stdout = self.cout
408 self.IP.run_cell(source_raw, store_history=store_history)
411 self.IP.run_cell(source_raw, store_history=store_history)
409 finally:
412 finally:
410 sys.stdout = stdout
413 sys.stdout = stdout
411
414
412 def process_image(self, decorator):
415 def process_image(self, decorator):
413 """
416 """
414 # build out an image directive like
417 # build out an image directive like
415 # .. image:: somefile.png
418 # .. image:: somefile.png
416 # :width 4in
419 # :width 4in
417 #
420 #
418 # from an input like
421 # from an input like
419 # savefig somefile.png width=4in
422 # savefig somefile.png width=4in
420 """
423 """
421 savefig_dir = self.savefig_dir
424 savefig_dir = self.savefig_dir
422 source_dir = self.source_dir
425 source_dir = self.source_dir
423 saveargs = decorator.split(' ')
426 saveargs = decorator.split(' ')
424 filename = saveargs[1]
427 filename = saveargs[1]
425 # insert relative path to image file in source
428 # insert relative path to image file in source
426 # as absolute path for Sphinx
429 # as absolute path for Sphinx
427 # sphinx expects a posix path, even on Windows
430 # sphinx expects a posix path, even on Windows
428 path = pathlib.Path(savefig_dir, filename)
431 path = pathlib.Path(savefig_dir, filename)
429 outfile = '/' + path.relative_to(source_dir).as_posix()
432 outfile = '/' + path.relative_to(source_dir).as_posix()
430
433
431 imagerows = ['.. image:: %s' % outfile]
434 imagerows = ['.. image:: %s' % outfile]
432
435
433 for kwarg in saveargs[2:]:
436 for kwarg in saveargs[2:]:
434 arg, val = kwarg.split('=')
437 arg, val = kwarg.split('=')
435 arg = arg.strip()
438 arg = arg.strip()
436 val = val.strip()
439 val = val.strip()
437 imagerows.append(' :%s: %s'%(arg, val))
440 imagerows.append(' :%s: %s'%(arg, val))
438
441
439 image_file = os.path.basename(outfile) # only return file name
442 image_file = os.path.basename(outfile) # only return file name
440 image_directive = '\n'.join(imagerows)
443 image_directive = '\n'.join(imagerows)
441 return image_file, image_directive
444 return image_file, image_directive
442
445
443 # Callbacks for each type of token
446 # Callbacks for each type of token
444 def process_input(self, data, input_prompt, lineno):
447 def process_input(self, data, input_prompt, lineno):
445 """
448 """
446 Process data block for INPUT token.
449 Process data block for INPUT token.
447
450
448 """
451 """
449 decorator, input, rest = data
452 decorator, input, rest = data
450 image_file = None
453 image_file = None
451 image_directive = None
454 image_directive = None
452
455
453 is_verbatim = decorator=='@verbatim' or self.is_verbatim
456 is_verbatim = decorator=='@verbatim' or self.is_verbatim
454 is_doctest = (decorator is not None and \
457 is_doctest = (decorator is not None and \
455 decorator.startswith('@doctest')) or self.is_doctest
458 decorator.startswith('@doctest')) or self.is_doctest
456 is_suppress = decorator=='@suppress' or self.is_suppress
459 is_suppress = decorator=='@suppress' or self.is_suppress
457 is_okexcept = decorator=='@okexcept' or self.is_okexcept
460 is_okexcept = decorator=='@okexcept' or self.is_okexcept
458 is_okwarning = decorator=='@okwarning' or self.is_okwarning
461 is_okwarning = decorator=='@okwarning' or self.is_okwarning
459 is_savefig = decorator is not None and \
462 is_savefig = decorator is not None and \
460 decorator.startswith('@savefig')
463 decorator.startswith('@savefig')
461
464
462 input_lines = input.split('\n')
465 input_lines = input.split('\n')
463 if len(input_lines) > 1:
466 if len(input_lines) > 1:
464 if input_lines[-1] != "":
467 if input_lines[-1] != "":
465 input_lines.append('') # make sure there's a blank line
468 input_lines.append('') # make sure there's a blank line
466 # so splitter buffer gets reset
469 # so splitter buffer gets reset
467
470
468 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
471 continuation = ' %s:'%''.join(['.']*(len(str(lineno))+2))
469
472
470 if is_savefig:
473 if is_savefig:
471 image_file, image_directive = self.process_image(decorator)
474 image_file, image_directive = self.process_image(decorator)
472
475
473 ret = []
476 ret = []
474 is_semicolon = False
477 is_semicolon = False
475
478
476 # Hold the execution count, if requested to do so.
479 # Hold the execution count, if requested to do so.
477 if is_suppress and self.hold_count:
480 if is_suppress and self.hold_count:
478 store_history = False
481 store_history = False
479 else:
482 else:
480 store_history = True
483 store_history = True
481
484
482 # Note: catch_warnings is not thread safe
485 # Note: catch_warnings is not thread safe
483 with warnings.catch_warnings(record=True) as ws:
486 with warnings.catch_warnings(record=True) as ws:
484 if input_lines[0].endswith(';'):
487 if input_lines[0].endswith(';'):
485 is_semicolon = True
488 is_semicolon = True
486 #for i, line in enumerate(input_lines):
489 #for i, line in enumerate(input_lines):
487
490
488 # process the first input line
491 # process the first input line
489 if is_verbatim:
492 if is_verbatim:
490 self.process_input_lines([''])
493 self.process_input_lines([''])
491 self.IP.execution_count += 1 # increment it anyway
494 self.IP.execution_count += 1 # increment it anyway
492 else:
495 else:
493 # only submit the line in non-verbatim mode
496 # only submit the line in non-verbatim mode
494 self.process_input_lines(input_lines, store_history=store_history)
497 self.process_input_lines(input_lines, store_history=store_history)
495
498
496 if not is_suppress:
499 if not is_suppress:
497 for i, line in enumerate(input_lines):
500 for i, line in enumerate(input_lines):
498 if i == 0:
501 if i == 0:
499 formatted_line = '%s %s'%(input_prompt, line)
502 formatted_line = '%s %s'%(input_prompt, line)
500 else:
503 else:
501 formatted_line = '%s %s'%(continuation, line)
504 formatted_line = '%s %s'%(continuation, line)
502 ret.append(formatted_line)
505 ret.append(formatted_line)
503
506
504 if not is_suppress and len(rest.strip()) and is_verbatim:
507 if not is_suppress and len(rest.strip()) and is_verbatim:
505 # The "rest" is the standard output of the input. This needs to be
508 # The "rest" is the standard output of the input. This needs to be
506 # added when in verbatim mode. If there is no "rest", then we don't
509 # added when in verbatim mode. If there is no "rest", then we don't
507 # add it, as the new line will be added by the processed output.
510 # add it, as the new line will be added by the processed output.
508 ret.append(rest)
511 ret.append(rest)
509
512
510 # Fetch the processed output. (This is not the submitted output.)
513 # Fetch the processed output. (This is not the submitted output.)
511 self.cout.seek(0)
514 self.cout.seek(0)
512 processed_output = self.cout.read()
515 processed_output = self.cout.read()
513 if not is_suppress and not is_semicolon:
516 if not is_suppress and not is_semicolon:
514 #
517 #
515 # In IPythonDirective.run, the elements of `ret` are eventually
518 # In IPythonDirective.run, the elements of `ret` are eventually
516 # combined such that '' entries correspond to newlines. So if
519 # combined such that '' entries correspond to newlines. So if
517 # `processed_output` is equal to '', then the adding it to `ret`
520 # `processed_output` is equal to '', then the adding it to `ret`
518 # ensures that there is a blank line between consecutive inputs
521 # ensures that there is a blank line between consecutive inputs
519 # that have no outputs, as in:
522 # that have no outputs, as in:
520 #
523 #
521 # In [1]: x = 4
524 # In [1]: x = 4
522 #
525 #
523 # In [2]: x = 5
526 # In [2]: x = 5
524 #
527 #
525 # When there is processed output, it has a '\n' at the tail end. So
528 # When there is processed output, it has a '\n' at the tail end. So
526 # adding the output to `ret` will provide the necessary spacing
529 # adding the output to `ret` will provide the necessary spacing
527 # between consecutive input/output blocks, as in:
530 # between consecutive input/output blocks, as in:
528 #
531 #
529 # In [1]: x
532 # In [1]: x
530 # Out[1]: 5
533 # Out[1]: 5
531 #
534 #
532 # In [2]: x
535 # In [2]: x
533 # Out[2]: 5
536 # Out[2]: 5
534 #
537 #
535 # When there is stdout from the input, it also has a '\n' at the
538 # When there is stdout from the input, it also has a '\n' at the
536 # tail end, and so this ensures proper spacing as well. E.g.:
539 # tail end, and so this ensures proper spacing as well. E.g.:
537 #
540 #
538 # In [1]: print x
541 # In [1]: print x
539 # 5
542 # 5
540 #
543 #
541 # In [2]: x = 5
544 # In [2]: x = 5
542 #
545 #
543 # When in verbatim mode, `processed_output` is empty (because
546 # When in verbatim mode, `processed_output` is empty (because
544 # nothing was passed to IP. Sometimes the submitted code block has
547 # nothing was passed to IP. Sometimes the submitted code block has
545 # an Out[] portion and sometimes it does not. When it does not, we
548 # an Out[] portion and sometimes it does not. When it does not, we
546 # need to ensure proper spacing, so we have to add '' to `ret`.
549 # need to ensure proper spacing, so we have to add '' to `ret`.
547 # However, if there is an Out[] in the submitted code, then we do
550 # However, if there is an Out[] in the submitted code, then we do
548 # not want to add a newline as `process_output` has stuff to add.
551 # not want to add a newline as `process_output` has stuff to add.
549 # The difficulty is that `process_input` doesn't know if
552 # The difficulty is that `process_input` doesn't know if
550 # `process_output` will be called---so it doesn't know if there is
553 # `process_output` will be called---so it doesn't know if there is
551 # Out[] in the code block. The requires that we include a hack in
554 # Out[] in the code block. The requires that we include a hack in
552 # `process_block`. See the comments there.
555 # `process_block`. See the comments there.
553 #
556 #
554 ret.append(processed_output)
557 ret.append(processed_output)
555 elif is_semicolon:
558 elif is_semicolon:
556 # Make sure there is a newline after the semicolon.
559 # Make sure there is a newline after the semicolon.
557 ret.append('')
560 ret.append('')
558
561
559 # context information
562 # context information
560 filename = "Unknown"
563 filename = "Unknown"
561 lineno = 0
564 lineno = 0
562 if self.directive.state:
565 if self.directive.state:
563 filename = self.directive.state.document.current_source
566 filename = self.directive.state.document.current_source
564 lineno = self.directive.state.document.current_line
567 lineno = self.directive.state.document.current_line
565
568
566 # Use sphinx logger for warnings
569 # Use sphinx logger for warnings
567 logger = logging.getLogger(__name__)
570 logger = logging.getLogger(__name__)
568
571
569 # output any exceptions raised during execution to stdout
572 # output any exceptions raised during execution to stdout
570 # unless :okexcept: has been specified.
573 # unless :okexcept: has been specified.
571 if not is_okexcept and (
574 if not is_okexcept and (
572 ("Traceback" in processed_output) or ("SyntaxError" in processed_output)
575 ("Traceback" in processed_output) or ("SyntaxError" in processed_output)
573 ):
576 ):
574 s = "\n>>>" + ("-" * 73) + "\n"
577 s = "\n>>>" + ("-" * 73) + "\n"
575 s += "Exception in %s at block ending on line %s\n" % (filename, lineno)
578 s += "Exception in %s at block ending on line %s\n" % (filename, lineno)
576 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
579 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
577 s += processed_output + "\n"
580 s += processed_output + "\n"
578 s += "<<<" + ("-" * 73)
581 s += "<<<" + ("-" * 73)
579 logger.warning(s)
582 logger.warning(s)
580 if self.warning_is_error:
583 if self.warning_is_error:
581 raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno))
584 raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno))
582
585
583 # output any warning raised during execution to stdout
586 # output any warning raised during execution to stdout
584 # unless :okwarning: has been specified.
587 # unless :okwarning: has been specified.
585 if not is_okwarning:
588 if not is_okwarning:
586 for w in ws:
589 for w in ws:
587 s = "\n>>>" + ("-" * 73) + "\n"
590 s = "\n>>>" + ("-" * 73) + "\n"
588 s += "Warning in %s at block ending on line %s\n" % (filename, lineno)
591 s += "Warning in %s at block ending on line %s\n" % (filename, lineno)
589 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
592 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
590 s += ("-" * 76) + "\n"
593 s += ("-" * 76) + "\n"
591 s += warnings.formatwarning(
594 s += warnings.formatwarning(
592 w.message, w.category, w.filename, w.lineno, w.line
595 w.message, w.category, w.filename, w.lineno, w.line
593 )
596 )
594 s += "<<<" + ("-" * 73)
597 s += "<<<" + ("-" * 73)
595 logger.warning(s)
598 logger.warning(s)
596 if self.warning_is_error:
599 if self.warning_is_error:
597 raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno))
600 raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno))
598
601
599 self.clear_cout()
602 self.clear_cout()
600 return (ret, input_lines, processed_output,
603 return (ret, input_lines, processed_output,
601 is_doctest, decorator, image_file, image_directive)
604 is_doctest, decorator, image_file, image_directive)
602
605
603
606
604 def process_output(self, data, output_prompt, input_lines, output,
607 def process_output(self, data, output_prompt, input_lines, output,
605 is_doctest, decorator, image_file):
608 is_doctest, decorator, image_file):
606 """
609 """
607 Process data block for OUTPUT token.
610 Process data block for OUTPUT token.
608
611
609 """
612 """
610 # Recall: `data` is the submitted output, and `output` is the processed
613 # Recall: `data` is the submitted output, and `output` is the processed
611 # output from `input_lines`.
614 # output from `input_lines`.
612
615
613 TAB = ' ' * 4
616 TAB = ' ' * 4
614
617
615 if is_doctest and output is not None:
618 if is_doctest and output is not None:
616
619
617 found = output # This is the processed output
620 found = output # This is the processed output
618 found = found.strip()
621 found = found.strip()
619 submitted = data.strip()
622 submitted = data.strip()
620
623
621 if self.directive is None:
624 if self.directive is None:
622 source = 'Unavailable'
625 source = 'Unavailable'
623 content = 'Unavailable'
626 content = 'Unavailable'
624 else:
627 else:
625 source = self.directive.state.document.current_source
628 source = self.directive.state.document.current_source
626 content = self.directive.content
629 content = self.directive.content
627 # Add tabs and join into a single string.
630 # Add tabs and join into a single string.
628 content = '\n'.join([TAB + line for line in content])
631 content = '\n'.join([TAB + line for line in content])
629
632
630 # Make sure the output contains the output prompt.
633 # Make sure the output contains the output prompt.
631 ind = found.find(output_prompt)
634 ind = found.find(output_prompt)
632 if ind < 0:
635 if ind < 0:
633 e = ('output does not contain output prompt\n\n'
636 e = ('output does not contain output prompt\n\n'
634 'Document source: {0}\n\n'
637 'Document source: {0}\n\n'
635 'Raw content: \n{1}\n\n'
638 'Raw content: \n{1}\n\n'
636 'Input line(s):\n{TAB}{2}\n\n'
639 'Input line(s):\n{TAB}{2}\n\n'
637 'Output line(s):\n{TAB}{3}\n\n')
640 'Output line(s):\n{TAB}{3}\n\n')
638 e = e.format(source, content, '\n'.join(input_lines),
641 e = e.format(source, content, '\n'.join(input_lines),
639 repr(found), TAB=TAB)
642 repr(found), TAB=TAB)
640 raise RuntimeError(e)
643 raise RuntimeError(e)
641 found = found[len(output_prompt):].strip()
644 found = found[len(output_prompt):].strip()
642
645
643 # Handle the actual doctest comparison.
646 # Handle the actual doctest comparison.
644 if decorator.strip() == '@doctest':
647 if decorator.strip() == '@doctest':
645 # Standard doctest
648 # Standard doctest
646 if found != submitted:
649 if found != submitted:
647 e = ('doctest failure\n\n'
650 e = ('doctest failure\n\n'
648 'Document source: {0}\n\n'
651 'Document source: {0}\n\n'
649 'Raw content: \n{1}\n\n'
652 'Raw content: \n{1}\n\n'
650 'On input line(s):\n{TAB}{2}\n\n'
653 'On input line(s):\n{TAB}{2}\n\n'
651 'we found output:\n{TAB}{3}\n\n'
654 'we found output:\n{TAB}{3}\n\n'
652 'instead of the expected:\n{TAB}{4}\n\n')
655 'instead of the expected:\n{TAB}{4}\n\n')
653 e = e.format(source, content, '\n'.join(input_lines),
656 e = e.format(source, content, '\n'.join(input_lines),
654 repr(found), repr(submitted), TAB=TAB)
657 repr(found), repr(submitted), TAB=TAB)
655 raise RuntimeError(e)
658 raise RuntimeError(e)
656 else:
659 else:
657 self.custom_doctest(decorator, input_lines, found, submitted)
660 self.custom_doctest(decorator, input_lines, found, submitted)
658
661
659 # When in verbatim mode, this holds additional submitted output
662 # When in verbatim mode, this holds additional submitted output
660 # to be written in the final Sphinx output.
663 # to be written in the final Sphinx output.
661 # https://github.com/ipython/ipython/issues/5776
664 # https://github.com/ipython/ipython/issues/5776
662 out_data = []
665 out_data = []
663
666
664 is_verbatim = decorator=='@verbatim' or self.is_verbatim
667 is_verbatim = decorator=='@verbatim' or self.is_verbatim
665 if is_verbatim and data.strip():
668 if is_verbatim and data.strip():
666 # Note that `ret` in `process_block` has '' as its last element if
669 # Note that `ret` in `process_block` has '' as its last element if
667 # the code block was in verbatim mode. So if there is no submitted
670 # the code block was in verbatim mode. So if there is no submitted
668 # output, then we will have proper spacing only if we do not add
671 # output, then we will have proper spacing only if we do not add
669 # an additional '' to `out_data`. This is why we condition on
672 # an additional '' to `out_data`. This is why we condition on
670 # `and data.strip()`.
673 # `and data.strip()`.
671
674
672 # The submitted output has no output prompt. If we want the
675 # The submitted output has no output prompt. If we want the
673 # prompt and the code to appear, we need to join them now
676 # prompt and the code to appear, we need to join them now
674 # instead of adding them separately---as this would create an
677 # instead of adding them separately---as this would create an
675 # undesired newline. How we do this ultimately depends on the
678 # undesired newline. How we do this ultimately depends on the
676 # format of the output regex. I'll do what works for the default
679 # format of the output regex. I'll do what works for the default
677 # prompt for now, and we might have to adjust if it doesn't work
680 # prompt for now, and we might have to adjust if it doesn't work
678 # in other cases. Finally, the submitted output does not have
681 # in other cases. Finally, the submitted output does not have
679 # a trailing newline, so we must add it manually.
682 # a trailing newline, so we must add it manually.
680 out_data.append("{0} {1}\n".format(output_prompt, data))
683 out_data.append("{0} {1}\n".format(output_prompt, data))
681
684
682 return out_data
685 return out_data
683
686
684 def process_comment(self, data):
687 def process_comment(self, data):
685 """Process data fPblock for COMMENT token."""
688 """Process data fPblock for COMMENT token."""
686 if not self.is_suppress:
689 if not self.is_suppress:
687 return [data]
690 return [data]
688
691
689 def save_image(self, image_file):
692 def save_image(self, image_file):
690 """
693 """
691 Saves the image file to disk.
694 Saves the image file to disk.
692 """
695 """
693 self.ensure_pyplot()
696 self.ensure_pyplot()
694 command = 'plt.gcf().savefig("%s")'%image_file
697 command = 'plt.gcf().savefig("%s")'%image_file
695 #print 'SAVEFIG', command # dbg
698 #print 'SAVEFIG', command # dbg
696 self.process_input_line('bookmark ipy_thisdir', store_history=False)
699 self.process_input_line('bookmark ipy_thisdir', store_history=False)
697 self.process_input_line('cd -b ipy_savedir', store_history=False)
700 self.process_input_line('cd -b ipy_savedir', store_history=False)
698 self.process_input_line(command, store_history=False)
701 self.process_input_line(command, store_history=False)
699 self.process_input_line('cd -b ipy_thisdir', store_history=False)
702 self.process_input_line('cd -b ipy_thisdir', store_history=False)
700 self.process_input_line('bookmark -d ipy_thisdir', store_history=False)
703 self.process_input_line('bookmark -d ipy_thisdir', store_history=False)
701 self.clear_cout()
704 self.clear_cout()
702
705
703 def process_block(self, block):
706 def process_block(self, block):
704 """
707 """
705 process block from the block_parser and return a list of processed lines
708 process block from the block_parser and return a list of processed lines
706 """
709 """
707 ret = []
710 ret = []
708 output = None
711 output = None
709 input_lines = None
712 input_lines = None
710 lineno = self.IP.execution_count
713 lineno = self.IP.execution_count
711
714
712 input_prompt = self.promptin % lineno
715 input_prompt = self.promptin % lineno
713 output_prompt = self.promptout % lineno
716 output_prompt = self.promptout % lineno
714 image_file = None
717 image_file = None
715 image_directive = None
718 image_directive = None
716
719
717 found_input = False
720 found_input = False
718 for token, data in block:
721 for token, data in block:
719 if token == COMMENT:
722 if token == COMMENT:
720 out_data = self.process_comment(data)
723 out_data = self.process_comment(data)
721 elif token == INPUT:
724 elif token == INPUT:
722 found_input = True
725 found_input = True
723 (out_data, input_lines, output, is_doctest,
726 (out_data, input_lines, output, is_doctest,
724 decorator, image_file, image_directive) = \
727 decorator, image_file, image_directive) = \
725 self.process_input(data, input_prompt, lineno)
728 self.process_input(data, input_prompt, lineno)
726 elif token == OUTPUT:
729 elif token == OUTPUT:
727 if not found_input:
730 if not found_input:
728
731
729 TAB = ' ' * 4
732 TAB = ' ' * 4
730 linenumber = 0
733 linenumber = 0
731 source = 'Unavailable'
734 source = 'Unavailable'
732 content = 'Unavailable'
735 content = 'Unavailable'
733 if self.directive:
736 if self.directive:
734 linenumber = self.directive.state.document.current_line
737 linenumber = self.directive.state.document.current_line
735 source = self.directive.state.document.current_source
738 source = self.directive.state.document.current_source
736 content = self.directive.content
739 content = self.directive.content
737 # Add tabs and join into a single string.
740 # Add tabs and join into a single string.
738 content = '\n'.join([TAB + line for line in content])
741 content = '\n'.join([TAB + line for line in content])
739
742
740 e = ('\n\nInvalid block: Block contains an output prompt '
743 e = ('\n\nInvalid block: Block contains an output prompt '
741 'without an input prompt.\n\n'
744 'without an input prompt.\n\n'
742 'Document source: {0}\n\n'
745 'Document source: {0}\n\n'
743 'Content begins at line {1}: \n\n{2}\n\n'
746 'Content begins at line {1}: \n\n{2}\n\n'
744 'Problematic block within content: \n\n{TAB}{3}\n\n')
747 'Problematic block within content: \n\n{TAB}{3}\n\n')
745 e = e.format(source, linenumber, content, block, TAB=TAB)
748 e = e.format(source, linenumber, content, block, TAB=TAB)
746
749
747 # Write, rather than include in exception, since Sphinx
750 # Write, rather than include in exception, since Sphinx
748 # will truncate tracebacks.
751 # will truncate tracebacks.
749 sys.stdout.write(e)
752 sys.stdout.write(e)
750 raise RuntimeError('An invalid block was detected.')
753 raise RuntimeError('An invalid block was detected.')
751 out_data = \
754 out_data = \
752 self.process_output(data, output_prompt, input_lines,
755 self.process_output(data, output_prompt, input_lines,
753 output, is_doctest, decorator,
756 output, is_doctest, decorator,
754 image_file)
757 image_file)
755 if out_data:
758 if out_data:
756 # Then there was user submitted output in verbatim mode.
759 # Then there was user submitted output in verbatim mode.
757 # We need to remove the last element of `ret` that was
760 # We need to remove the last element of `ret` that was
758 # added in `process_input`, as it is '' and would introduce
761 # added in `process_input`, as it is '' and would introduce
759 # an undesirable newline.
762 # an undesirable newline.
760 assert(ret[-1] == '')
763 assert(ret[-1] == '')
761 del ret[-1]
764 del ret[-1]
762
765
763 if out_data:
766 if out_data:
764 ret.extend(out_data)
767 ret.extend(out_data)
765
768
766 # save the image files
769 # save the image files
767 if image_file is not None:
770 if image_file is not None:
768 self.save_image(image_file)
771 self.save_image(image_file)
769
772
770 return ret, image_directive
773 return ret, image_directive
771
774
772 def ensure_pyplot(self):
775 def ensure_pyplot(self):
773 """
776 """
774 Ensures that pyplot has been imported into the embedded IPython shell.
777 Ensures that pyplot has been imported into the embedded IPython shell.
775
778
776 Also, makes sure to set the backend appropriately if not set already.
779 Also, makes sure to set the backend appropriately if not set already.
777
780
778 """
781 """
779 # We are here if the @figure pseudo decorator was used. Thus, it's
782 # We are here if the @figure pseudo decorator was used. Thus, it's
780 # possible that we could be here even if python_mplbackend were set to
783 # possible that we could be here even if python_mplbackend were set to
781 # `None`. That's also strange and perhaps worthy of raising an
784 # `None`. That's also strange and perhaps worthy of raising an
782 # exception, but for now, we just set the backend to 'agg'.
785 # exception, but for now, we just set the backend to 'agg'.
783
786
784 if not self._pyplot_imported:
787 if not self._pyplot_imported:
785 if 'matplotlib.backends' not in sys.modules:
788 if 'matplotlib.backends' not in sys.modules:
786 # Then ipython_matplotlib was set to None but there was a
789 # Then ipython_matplotlib was set to None but there was a
787 # call to the @figure decorator (and ipython_execlines did
790 # call to the @figure decorator (and ipython_execlines did
788 # not set a backend).
791 # not set a backend).
789 #raise Exception("No backend was set, but @figure was used!")
792 #raise Exception("No backend was set, but @figure was used!")
790 import matplotlib
793 import matplotlib
791 matplotlib.use('agg')
794 matplotlib.use('agg')
792
795
793 # Always import pyplot into embedded shell.
796 # Always import pyplot into embedded shell.
794 self.process_input_line('import matplotlib.pyplot as plt',
797 self.process_input_line('import matplotlib.pyplot as plt',
795 store_history=False)
798 store_history=False)
796 self._pyplot_imported = True
799 self._pyplot_imported = True
797
800
798 def process_pure_python(self, content):
801 def process_pure_python(self, content):
799 """
802 """
800 content is a list of strings. it is unedited directive content
803 content is a list of strings. it is unedited directive content
801
804
802 This runs it line by line in the InteractiveShell, prepends
805 This runs it line by line in the InteractiveShell, prepends
803 prompts as needed capturing stderr and stdout, then returns
806 prompts as needed capturing stderr and stdout, then returns
804 the content as a list as if it were ipython code
807 the content as a list as if it were ipython code
805 """
808 """
806 output = []
809 output = []
807 savefig = False # keep up with this to clear figure
810 savefig = False # keep up with this to clear figure
808 multiline = False # to handle line continuation
811 multiline = False # to handle line continuation
809 multiline_start = None
812 multiline_start = None
810 fmtin = self.promptin
813 fmtin = self.promptin
811
814
812 ct = 0
815 ct = 0
813
816
814 for lineno, line in enumerate(content):
817 for lineno, line in enumerate(content):
815
818
816 line_stripped = line.strip()
819 line_stripped = line.strip()
817 if not len(line):
820 if not len(line):
818 output.append(line)
821 output.append(line)
819 continue
822 continue
820
823
821 # handle decorators
824 # handle decorators
822 if line_stripped.startswith('@'):
825 if line_stripped.startswith('@'):
823 output.extend([line])
826 output.extend([line])
824 if 'savefig' in line:
827 if 'savefig' in line:
825 savefig = True # and need to clear figure
828 savefig = True # and need to clear figure
826 continue
829 continue
827
830
828 # handle comments
831 # handle comments
829 if line_stripped.startswith('#'):
832 if line_stripped.startswith('#'):
830 output.extend([line])
833 output.extend([line])
831 continue
834 continue
832
835
833 # deal with lines checking for multiline
836 # deal with lines checking for multiline
834 continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2))
837 continuation = u' %s:'% ''.join(['.']*(len(str(ct))+2))
835 if not multiline:
838 if not multiline:
836 modified = u"%s %s" % (fmtin % ct, line_stripped)
839 modified = u"%s %s" % (fmtin % ct, line_stripped)
837 output.append(modified)
840 output.append(modified)
838 ct += 1
841 ct += 1
839 try:
842 try:
840 ast.parse(line_stripped)
843 ast.parse(line_stripped)
841 output.append(u'')
844 output.append(u'')
842 except Exception: # on a multiline
845 except Exception: # on a multiline
843 multiline = True
846 multiline = True
844 multiline_start = lineno
847 multiline_start = lineno
845 else: # still on a multiline
848 else: # still on a multiline
846 modified = u'%s %s' % (continuation, line)
849 modified = u'%s %s' % (continuation, line)
847 output.append(modified)
850 output.append(modified)
848
851
849 # if the next line is indented, it should be part of multiline
852 # if the next line is indented, it should be part of multiline
850 if len(content) > lineno + 1:
853 if len(content) > lineno + 1:
851 nextline = content[lineno + 1]
854 nextline = content[lineno + 1]
852 if len(nextline) - len(nextline.lstrip()) > 3:
855 if len(nextline) - len(nextline.lstrip()) > 3:
853 continue
856 continue
854 try:
857 try:
855 mod = ast.parse(
858 mod = ast.parse(
856 '\n'.join(content[multiline_start:lineno+1]))
859 '\n'.join(content[multiline_start:lineno+1]))
857 if isinstance(mod.body[0], ast.FunctionDef):
860 if isinstance(mod.body[0], ast.FunctionDef):
858 # check to see if we have the whole function
861 # check to see if we have the whole function
859 for element in mod.body[0].body:
862 for element in mod.body[0].body:
860 if isinstance(element, ast.Return):
863 if isinstance(element, ast.Return):
861 multiline = False
864 multiline = False
862 else:
865 else:
863 output.append(u'')
866 output.append(u'')
864 multiline = False
867 multiline = False
865 except Exception:
868 except Exception:
866 pass
869 pass
867
870
868 if savefig: # clear figure if plotted
871 if savefig: # clear figure if plotted
869 self.ensure_pyplot()
872 self.ensure_pyplot()
870 self.process_input_line('plt.clf()', store_history=False)
873 self.process_input_line('plt.clf()', store_history=False)
871 self.clear_cout()
874 self.clear_cout()
872 savefig = False
875 savefig = False
873
876
874 return output
877 return output
875
878
876 def custom_doctest(self, decorator, input_lines, found, submitted):
879 def custom_doctest(self, decorator, input_lines, found, submitted):
877 """
880 """
878 Perform a specialized doctest.
881 Perform a specialized doctest.
879
882
880 """
883 """
881 from .custom_doctests import doctests
884 from .custom_doctests import doctests
882
885
883 args = decorator.split()
886 args = decorator.split()
884 doctest_type = args[1]
887 doctest_type = args[1]
885 if doctest_type in doctests:
888 if doctest_type in doctests:
886 doctests[doctest_type](self, args, input_lines, found, submitted)
889 doctests[doctest_type](self, args, input_lines, found, submitted)
887 else:
890 else:
888 e = "Invalid option to @doctest: {0}".format(doctest_type)
891 e = "Invalid option to @doctest: {0}".format(doctest_type)
889 raise Exception(e)
892 raise Exception(e)
890
893
891
894
892 class IPythonDirective(Directive):
895 class IPythonDirective(Directive):
893
896
894 has_content = True
897 has_content = True
895 required_arguments = 0
898 required_arguments = 0
896 optional_arguments = 4 # python, suppress, verbatim, doctest
899 optional_arguments = 4 # python, suppress, verbatim, doctest
897 final_argumuent_whitespace = True
900 final_argumuent_whitespace = True
898 option_spec = { 'python': directives.unchanged,
901 option_spec = { 'python': directives.unchanged,
899 'suppress' : directives.flag,
902 'suppress' : directives.flag,
900 'verbatim' : directives.flag,
903 'verbatim' : directives.flag,
901 'doctest' : directives.flag,
904 'doctest' : directives.flag,
902 'okexcept': directives.flag,
905 'okexcept': directives.flag,
903 'okwarning': directives.flag
906 'okwarning': directives.flag
904 }
907 }
905
908
906 shell = None
909 shell = None
907
910
908 seen_docs = set()
911 seen_docs = set()
909
912
910 def get_config_options(self):
913 def get_config_options(self):
911 # contains sphinx configuration variables
914 # contains sphinx configuration variables
912 config = self.state.document.settings.env.config
915 config = self.state.document.settings.env.config
913
916
914 # get config variables to set figure output directory
917 # get config variables to set figure output directory
915 savefig_dir = config.ipython_savefig_dir
918 savefig_dir = config.ipython_savefig_dir
916 source_dir = self.state.document.settings.env.srcdir
919 source_dir = self.state.document.settings.env.srcdir
917 savefig_dir = os.path.join(source_dir, savefig_dir)
920 savefig_dir = os.path.join(source_dir, savefig_dir)
918
921
919 # get regex and prompt stuff
922 # get regex and prompt stuff
920 rgxin = config.ipython_rgxin
923 rgxin = config.ipython_rgxin
921 rgxout = config.ipython_rgxout
924 rgxout = config.ipython_rgxout
922 warning_is_error= config.ipython_warning_is_error
925 warning_is_error= config.ipython_warning_is_error
923 promptin = config.ipython_promptin
926 promptin = config.ipython_promptin
924 promptout = config.ipython_promptout
927 promptout = config.ipython_promptout
925 mplbackend = config.ipython_mplbackend
928 mplbackend = config.ipython_mplbackend
926 exec_lines = config.ipython_execlines
929 exec_lines = config.ipython_execlines
927 hold_count = config.ipython_holdcount
930 hold_count = config.ipython_holdcount
928
931
929 return (savefig_dir, source_dir, rgxin, rgxout,
932 return (savefig_dir, source_dir, rgxin, rgxout,
930 promptin, promptout, mplbackend, exec_lines, hold_count, warning_is_error)
933 promptin, promptout, mplbackend, exec_lines, hold_count, warning_is_error)
931
934
932 def setup(self):
935 def setup(self):
933 # Get configuration values.
936 # Get configuration values.
934 (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
937 (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout,
935 mplbackend, exec_lines, hold_count, warning_is_error) = self.get_config_options()
938 mplbackend, exec_lines, hold_count, warning_is_error) = self.get_config_options()
936
939
937 try:
940 try:
938 os.makedirs(savefig_dir)
941 os.makedirs(savefig_dir)
939 except OSError as e:
942 except OSError as e:
940 if e.errno != errno.EEXIST:
943 if e.errno != errno.EEXIST:
941 raise
944 raise
942
945
943 if self.shell is None:
946 if self.shell is None:
944 # We will be here many times. However, when the
947 # We will be here many times. However, when the
945 # EmbeddedSphinxShell is created, its interactive shell member
948 # EmbeddedSphinxShell is created, its interactive shell member
946 # is the same for each instance.
949 # is the same for each instance.
947
950
948 if mplbackend and 'matplotlib.backends' not in sys.modules and use_matplotlib:
951 if mplbackend and 'matplotlib.backends' not in sys.modules and use_matplotlib:
949 import matplotlib
952 import matplotlib
950 matplotlib.use(mplbackend)
953 matplotlib.use(mplbackend)
951
954
952 # Must be called after (potentially) importing matplotlib and
955 # Must be called after (potentially) importing matplotlib and
953 # setting its backend since exec_lines might import pylab.
956 # setting its backend since exec_lines might import pylab.
954 self.shell = EmbeddedSphinxShell(exec_lines)
957 self.shell = EmbeddedSphinxShell(exec_lines)
955
958
956 # Store IPython directive to enable better error messages
959 # Store IPython directive to enable better error messages
957 self.shell.directive = self
960 self.shell.directive = self
958
961
959 # reset the execution count if we haven't processed this doc
962 # reset the execution count if we haven't processed this doc
960 #NOTE: this may be borked if there are multiple seen_doc tmp files
963 #NOTE: this may be borked if there are multiple seen_doc tmp files
961 #check time stamp?
964 #check time stamp?
962 if not self.state.document.current_source in self.seen_docs:
965 if not self.state.document.current_source in self.seen_docs:
963 self.shell.IP.history_manager.reset()
966 self.shell.IP.history_manager.reset()
964 self.shell.IP.execution_count = 1
967 self.shell.IP.execution_count = 1
965 self.seen_docs.add(self.state.document.current_source)
968 self.seen_docs.add(self.state.document.current_source)
966
969
967 # and attach to shell so we don't have to pass them around
970 # and attach to shell so we don't have to pass them around
968 self.shell.rgxin = rgxin
971 self.shell.rgxin = rgxin
969 self.shell.rgxout = rgxout
972 self.shell.rgxout = rgxout
970 self.shell.promptin = promptin
973 self.shell.promptin = promptin
971 self.shell.promptout = promptout
974 self.shell.promptout = promptout
972 self.shell.savefig_dir = savefig_dir
975 self.shell.savefig_dir = savefig_dir
973 self.shell.source_dir = source_dir
976 self.shell.source_dir = source_dir
974 self.shell.hold_count = hold_count
977 self.shell.hold_count = hold_count
975 self.shell.warning_is_error = warning_is_error
978 self.shell.warning_is_error = warning_is_error
976
979
977 # setup bookmark for saving figures directory
980 # setup bookmark for saving figures directory
978 self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir,
981 self.shell.process_input_line('bookmark ipy_savedir %s'%savefig_dir,
979 store_history=False)
982 store_history=False)
980 self.shell.clear_cout()
983 self.shell.clear_cout()
981
984
982 return rgxin, rgxout, promptin, promptout
985 return rgxin, rgxout, promptin, promptout
983
986
984 def teardown(self):
987 def teardown(self):
985 # delete last bookmark
988 # delete last bookmark
986 self.shell.process_input_line('bookmark -d ipy_savedir',
989 self.shell.process_input_line('bookmark -d ipy_savedir',
987 store_history=False)
990 store_history=False)
988 self.shell.clear_cout()
991 self.shell.clear_cout()
989
992
990 def run(self):
993 def run(self):
991 debug = False
994 debug = False
992
995
993 #TODO, any reason block_parser can't be a method of embeddable shell
996 #TODO, any reason block_parser can't be a method of embeddable shell
994 # then we wouldn't have to carry these around
997 # then we wouldn't have to carry these around
995 rgxin, rgxout, promptin, promptout = self.setup()
998 rgxin, rgxout, promptin, promptout = self.setup()
996
999
997 options = self.options
1000 options = self.options
998 self.shell.is_suppress = 'suppress' in options
1001 self.shell.is_suppress = 'suppress' in options
999 self.shell.is_doctest = 'doctest' in options
1002 self.shell.is_doctest = 'doctest' in options
1000 self.shell.is_verbatim = 'verbatim' in options
1003 self.shell.is_verbatim = 'verbatim' in options
1001 self.shell.is_okexcept = 'okexcept' in options
1004 self.shell.is_okexcept = 'okexcept' in options
1002 self.shell.is_okwarning = 'okwarning' in options
1005 self.shell.is_okwarning = 'okwarning' in options
1003
1006
1004 # handle pure python code
1007 # handle pure python code
1005 if 'python' in self.arguments:
1008 if 'python' in self.arguments:
1006 content = self.content
1009 content = self.content
1007 self.content = self.shell.process_pure_python(content)
1010 self.content = self.shell.process_pure_python(content)
1008
1011
1009 # parts consists of all text within the ipython-block.
1012 # parts consists of all text within the ipython-block.
1010 # Each part is an input/output block.
1013 # Each part is an input/output block.
1011 parts = '\n'.join(self.content).split('\n\n')
1014 parts = '\n'.join(self.content).split('\n\n')
1012
1015
1013 lines = ['.. code-block:: ipython', '']
1016 lines = ['.. code-block:: ipython', '']
1014 figures = []
1017 figures = []
1015
1018
1016 # Use sphinx logger for warnings
1019 # Use sphinx logger for warnings
1017 logger = logging.getLogger(__name__)
1020 logger = logging.getLogger(__name__)
1018
1021
1019 for part in parts:
1022 for part in parts:
1020 block = block_parser(part, rgxin, rgxout, promptin, promptout)
1023 block = block_parser(part, rgxin, rgxout, promptin, promptout)
1021 if len(block):
1024 if len(block):
1022 rows, figure = self.shell.process_block(block)
1025 rows, figure = self.shell.process_block(block)
1023 for row in rows:
1026 for row in rows:
1024 lines.extend([' {0}'.format(line)
1027 lines.extend([' {0}'.format(line)
1025 for line in row.split('\n')])
1028 for line in row.split('\n')])
1026
1029
1027 if figure is not None:
1030 if figure is not None:
1028 figures.append(figure)
1031 figures.append(figure)
1029 else:
1032 else:
1030 message = 'Code input with no code at {}, line {}'\
1033 message = 'Code input with no code at {}, line {}'\
1031 .format(
1034 .format(
1032 self.state.document.current_source,
1035 self.state.document.current_source,
1033 self.state.document.current_line)
1036 self.state.document.current_line)
1034 if self.shell.warning_is_error:
1037 if self.shell.warning_is_error:
1035 raise RuntimeError(message)
1038 raise RuntimeError(message)
1036 else:
1039 else:
1037 logger.warning(message)
1040 logger.warning(message)
1038
1041
1039 for figure in figures:
1042 for figure in figures:
1040 lines.append('')
1043 lines.append('')
1041 lines.extend(figure.split('\n'))
1044 lines.extend(figure.split('\n'))
1042 lines.append('')
1045 lines.append('')
1043
1046
1044 if len(lines) > 2:
1047 if len(lines) > 2:
1045 if debug:
1048 if debug:
1046 print('\n'.join(lines))
1049 print('\n'.join(lines))
1047 else:
1050 else:
1048 # This has to do with input, not output. But if we comment
1051 # This has to do with input, not output. But if we comment
1049 # these lines out, then no IPython code will appear in the
1052 # these lines out, then no IPython code will appear in the
1050 # final output.
1053 # final output.
1051 self.state_machine.insert_input(
1054 self.state_machine.insert_input(
1052 lines, self.state_machine.input_lines.source(0))
1055 lines, self.state_machine.input_lines.source(0))
1053
1056
1054 # cleanup
1057 # cleanup
1055 self.teardown()
1058 self.teardown()
1056
1059
1057 return []
1060 return []
1058
1061
1059 # Enable as a proper Sphinx directive
1062 # Enable as a proper Sphinx directive
1060 def setup(app):
1063 def setup(app):
1061 setup.app = app
1064 setup.app = app
1062
1065
1063 app.add_directive('ipython', IPythonDirective)
1066 app.add_directive('ipython', IPythonDirective)
1064 app.add_config_value('ipython_savefig_dir', 'savefig', 'env')
1067 app.add_config_value('ipython_savefig_dir', 'savefig', 'env')
1065 app.add_config_value('ipython_warning_is_error', True, 'env')
1068 app.add_config_value('ipython_warning_is_error', True, 'env')
1066 app.add_config_value('ipython_rgxin',
1069 app.add_config_value('ipython_rgxin',
1067 re.compile(r'In \[(\d+)\]:\s?(.*)\s*'), 'env')
1070 re.compile(r'In \[(\d+)\]:\s?(.*)\s*'), 'env')
1068 app.add_config_value('ipython_rgxout',
1071 app.add_config_value('ipython_rgxout',
1069 re.compile(r'Out\[(\d+)\]:\s?(.*)\s*'), 'env')
1072 re.compile(r'Out\[(\d+)\]:\s?(.*)\s*'), 'env')
1070 app.add_config_value('ipython_promptin', 'In [%d]:', 'env')
1073 app.add_config_value('ipython_promptin', 'In [%d]:', 'env')
1071 app.add_config_value('ipython_promptout', 'Out[%d]:', 'env')
1074 app.add_config_value('ipython_promptout', 'Out[%d]:', 'env')
1072
1075
1073 # We could just let matplotlib pick whatever is specified as the default
1076 # We could just let matplotlib pick whatever is specified as the default
1074 # backend in the matplotlibrc file, but this would cause issues if the
1077 # backend in the matplotlibrc file, but this would cause issues if the
1075 # backend didn't work in headless environments. For this reason, 'agg'
1078 # backend didn't work in headless environments. For this reason, 'agg'
1076 # is a good default backend choice.
1079 # is a good default backend choice.
1077 app.add_config_value('ipython_mplbackend', 'agg', 'env')
1080 app.add_config_value('ipython_mplbackend', 'agg', 'env')
1078
1081
1079 # If the user sets this config value to `None`, then EmbeddedSphinxShell's
1082 # If the user sets this config value to `None`, then EmbeddedSphinxShell's
1080 # __init__ method will treat it as [].
1083 # __init__ method will treat it as [].
1081 execlines = ['import numpy as np']
1084 execlines = ['import numpy as np']
1082 if use_matplotlib:
1085 if use_matplotlib:
1083 execlines.append('import matplotlib.pyplot as plt')
1086 execlines.append('import matplotlib.pyplot as plt')
1084 app.add_config_value('ipython_execlines', execlines, 'env')
1087 app.add_config_value('ipython_execlines', execlines, 'env')
1085
1088
1086 app.add_config_value('ipython_holdcount', True, 'env')
1089 app.add_config_value('ipython_holdcount', True, 'env')
1087
1090
1088 metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
1091 metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
1089 return metadata
1092 return metadata
1090
1093
1091 # Simple smoke test, needs to be converted to a proper automatic test.
1094 # Simple smoke test, needs to be converted to a proper automatic test.
1092 def test():
1095 def test():
1093
1096
1094 examples = [
1097 examples = [
1095 r"""
1098 r"""
1096 In [9]: pwd
1099 In [9]: pwd
1097 Out[9]: '/home/jdhunter/py4science/book'
1100 Out[9]: '/home/jdhunter/py4science/book'
1098
1101
1099 In [10]: cd bookdata/
1102 In [10]: cd bookdata/
1100 /home/jdhunter/py4science/book/bookdata
1103 /home/jdhunter/py4science/book/bookdata
1101
1104
1102 In [2]: from pylab import *
1105 In [2]: from pylab import *
1103
1106
1104 In [2]: ion()
1107 In [2]: ion()
1105
1108
1106 In [3]: im = imread('stinkbug.png')
1109 In [3]: im = imread('stinkbug.png')
1107
1110
1108 @savefig mystinkbug.png width=4in
1111 @savefig mystinkbug.png width=4in
1109 In [4]: imshow(im)
1112 In [4]: imshow(im)
1110 Out[4]: <matplotlib.image.AxesImage object at 0x39ea850>
1113 Out[4]: <matplotlib.image.AxesImage object at 0x39ea850>
1111
1114
1112 """,
1115 """,
1113 r"""
1116 r"""
1114
1117
1115 In [1]: x = 'hello world'
1118 In [1]: x = 'hello world'
1116
1119
1117 # string methods can be
1120 # string methods can be
1118 # used to alter the string
1121 # used to alter the string
1119 @doctest
1122 @doctest
1120 In [2]: x.upper()
1123 In [2]: x.upper()
1121 Out[2]: 'HELLO WORLD'
1124 Out[2]: 'HELLO WORLD'
1122
1125
1123 @verbatim
1126 @verbatim
1124 In [3]: x.st<TAB>
1127 In [3]: x.st<TAB>
1125 x.startswith x.strip
1128 x.startswith x.strip
1126 """,
1129 """,
1127 r"""
1130 r"""
1128
1131
1129 In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
1132 In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
1130 .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
1133 .....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
1131
1134
1132 In [131]: print url.split('&')
1135 In [131]: print url.split('&')
1133 ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv']
1136 ['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22', 'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv']
1134
1137
1135 In [60]: import urllib
1138 In [60]: import urllib
1136
1139
1137 """,
1140 """,
1138 r"""\
1141 r"""\
1139
1142
1140 In [133]: import numpy.random
1143 In [133]: import numpy.random
1141
1144
1142 @suppress
1145 @suppress
1143 In [134]: numpy.random.seed(2358)
1146 In [134]: numpy.random.seed(2358)
1144
1147
1145 @doctest
1148 @doctest
1146 In [135]: numpy.random.rand(10,2)
1149 In [135]: numpy.random.rand(10,2)
1147 Out[135]:
1150 Out[135]:
1148 array([[ 0.64524308, 0.59943846],
1151 array([[ 0.64524308, 0.59943846],
1149 [ 0.47102322, 0.8715456 ],
1152 [ 0.47102322, 0.8715456 ],
1150 [ 0.29370834, 0.74776844],
1153 [ 0.29370834, 0.74776844],
1151 [ 0.99539577, 0.1313423 ],
1154 [ 0.99539577, 0.1313423 ],
1152 [ 0.16250302, 0.21103583],
1155 [ 0.16250302, 0.21103583],
1153 [ 0.81626524, 0.1312433 ],
1156 [ 0.81626524, 0.1312433 ],
1154 [ 0.67338089, 0.72302393],
1157 [ 0.67338089, 0.72302393],
1155 [ 0.7566368 , 0.07033696],
1158 [ 0.7566368 , 0.07033696],
1156 [ 0.22591016, 0.77731835],
1159 [ 0.22591016, 0.77731835],
1157 [ 0.0072729 , 0.34273127]])
1160 [ 0.0072729 , 0.34273127]])
1158
1161
1159 """,
1162 """,
1160
1163
1161 r"""
1164 r"""
1162 In [106]: print x
1165 In [106]: print x
1163 jdh
1166 jdh
1164
1167
1165 In [109]: for i in range(10):
1168 In [109]: for i in range(10):
1166 .....: print i
1169 .....: print i
1167 .....:
1170 .....:
1168 .....:
1171 .....:
1169 0
1172 0
1170 1
1173 1
1171 2
1174 2
1172 3
1175 3
1173 4
1176 4
1174 5
1177 5
1175 6
1178 6
1176 7
1179 7
1177 8
1180 8
1178 9
1181 9
1179 """,
1182 """,
1180
1183
1181 r"""
1184 r"""
1182
1185
1183 In [144]: from pylab import *
1186 In [144]: from pylab import *
1184
1187
1185 In [145]: ion()
1188 In [145]: ion()
1186
1189
1187 # use a semicolon to suppress the output
1190 # use a semicolon to suppress the output
1188 @savefig test_hist.png width=4in
1191 @savefig test_hist.png width=4in
1189 In [151]: hist(np.random.randn(10000), 100);
1192 In [151]: hist(np.random.randn(10000), 100);
1190
1193
1191
1194
1192 @savefig test_plot.png width=4in
1195 @savefig test_plot.png width=4in
1193 In [151]: plot(np.random.randn(10000), 'o');
1196 In [151]: plot(np.random.randn(10000), 'o');
1194 """,
1197 """,
1195
1198
1196 r"""
1199 r"""
1197 # use a semicolon to suppress the output
1200 # use a semicolon to suppress the output
1198 In [151]: plt.clf()
1201 In [151]: plt.clf()
1199
1202
1200 @savefig plot_simple.png width=4in
1203 @savefig plot_simple.png width=4in
1201 In [151]: plot([1,2,3])
1204 In [151]: plot([1,2,3])
1202
1205
1203 @savefig hist_simple.png width=4in
1206 @savefig hist_simple.png width=4in
1204 In [151]: hist(np.random.randn(10000), 100);
1207 In [151]: hist(np.random.randn(10000), 100);
1205
1208
1206 """,
1209 """,
1207 r"""
1210 r"""
1208 # update the current fig
1211 # update the current fig
1209 In [151]: ylabel('number')
1212 In [151]: ylabel('number')
1210
1213
1211 In [152]: title('normal distribution')
1214 In [152]: title('normal distribution')
1212
1215
1213
1216
1214 @savefig hist_with_text.png
1217 @savefig hist_with_text.png
1215 In [153]: grid(True)
1218 In [153]: grid(True)
1216
1219
1217 @doctest float
1220 @doctest float
1218 In [154]: 0.1 + 0.2
1221 In [154]: 0.1 + 0.2
1219 Out[154]: 0.3
1222 Out[154]: 0.3
1220
1223
1221 @doctest float
1224 @doctest float
1222 In [155]: np.arange(16).reshape(4,4)
1225 In [155]: np.arange(16).reshape(4,4)
1223 Out[155]:
1226 Out[155]:
1224 array([[ 0, 1, 2, 3],
1227 array([[ 0, 1, 2, 3],
1225 [ 4, 5, 6, 7],
1228 [ 4, 5, 6, 7],
1226 [ 8, 9, 10, 11],
1229 [ 8, 9, 10, 11],
1227 [12, 13, 14, 15]])
1230 [12, 13, 14, 15]])
1228
1231
1229 In [1]: x = np.arange(16, dtype=float).reshape(4,4)
1232 In [1]: x = np.arange(16, dtype=float).reshape(4,4)
1230
1233
1231 In [2]: x[0,0] = np.inf
1234 In [2]: x[0,0] = np.inf
1232
1235
1233 In [3]: x[0,1] = np.nan
1236 In [3]: x[0,1] = np.nan
1234
1237
1235 @doctest float
1238 @doctest float
1236 In [4]: x
1239 In [4]: x
1237 Out[4]:
1240 Out[4]:
1238 array([[ inf, nan, 2., 3.],
1241 array([[ inf, nan, 2., 3.],
1239 [ 4., 5., 6., 7.],
1242 [ 4., 5., 6., 7.],
1240 [ 8., 9., 10., 11.],
1243 [ 8., 9., 10., 11.],
1241 [ 12., 13., 14., 15.]])
1244 [ 12., 13., 14., 15.]])
1242
1245
1243
1246
1244 """,
1247 """,
1245 ]
1248 ]
1246 # skip local-file depending first example:
1249 # skip local-file depending first example:
1247 examples = examples[1:]
1250 examples = examples[1:]
1248
1251
1249 #ipython_directive.DEBUG = True # dbg
1252 #ipython_directive.DEBUG = True # dbg
1250 #options = dict(suppress=True) # dbg
1253 #options = dict(suppress=True) # dbg
1251 options = {}
1254 options = {}
1252 for example in examples:
1255 for example in examples:
1253 content = example.split('\n')
1256 content = example.split('\n')
1254 IPythonDirective('debug', arguments=None, options=options,
1257 IPythonDirective('debug', arguments=None, options=options,
1255 content=content, lineno=0,
1258 content=content, lineno=0,
1256 content_offset=None, block_text=None,
1259 content_offset=None, block_text=None,
1257 state=None, state_machine=None,
1260 state=None, state_machine=None,
1258 )
1261 )
1259
1262
1260 # Run test suite as a script
1263 # Run test suite as a script
1261 if __name__=='__main__':
1264 if __name__=='__main__':
1262 if not os.path.isdir('_static'):
1265 if not os.path.isdir('_static'):
1263 os.mkdir('_static')
1266 os.mkdir('_static')
1264 test()
1267 test()
1265 print('All OK? Check figures in _static/')
1268 print('All OK? Check figures in _static/')
General Comments 0
You need to be logged in to leave comments. Login now