Show More
@@ -38,7 +38,6 b' def page(strng, start=0, screen_lines=0, pager_cmd=None):' | |||||
38 | source='page', |
|
38 | source='page', | |
39 | data=data, |
|
39 | data=data, | |
40 | start=start, |
|
40 | start=start, | |
41 | screen_lines=screen_lines, |
|
|||
42 | ) |
|
41 | ) | |
43 | shell.payload_manager.write_payload(payload) |
|
42 | shell.payload_manager.write_payload(payload) | |
44 |
|
43 |
@@ -102,72 +102,6 b' class KernelMagics(Magics):' | |||||
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 |
|
||||
106 | @line_magic |
|
|||
107 | def doctest_mode(self, parameter_s=''): |
|
|||
108 | """Toggle doctest mode on and off. |
|
|||
109 |
|
||||
110 | This mode is intended to make IPython behave as much as possible like a |
|
|||
111 | plain Python shell, from the perspective of how its prompts, exceptions |
|
|||
112 | and output look. This makes it easy to copy and paste parts of a |
|
|||
113 | session into doctests. It does so by: |
|
|||
114 |
|
||||
115 | - Changing the prompts to the classic ``>>>`` ones. |
|
|||
116 | - Changing the exception reporting mode to 'Plain'. |
|
|||
117 | - Disabling pretty-printing of output. |
|
|||
118 |
|
||||
119 | Note that IPython also supports the pasting of code snippets that have |
|
|||
120 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
|||
121 | doctests from files or docstrings (even if they have leading |
|
|||
122 | whitespace), and the code will execute correctly. You can then use |
|
|||
123 | '%history -t' to see the translated history; this will give you the |
|
|||
124 | input after removal of all the leading prompts and whitespace, which |
|
|||
125 | can be pasted back into an editor. |
|
|||
126 |
|
||||
127 | With these features, you can switch into this mode easily whenever you |
|
|||
128 | need to do testing and changes to doctests, without having to leave |
|
|||
129 | your existing IPython session. |
|
|||
130 | """ |
|
|||
131 |
|
||||
132 | from IPython.utils.ipstruct import Struct |
|
|||
133 |
|
||||
134 | # Shorthands |
|
|||
135 | shell = self.shell |
|
|||
136 | disp_formatter = self.shell.display_formatter |
|
|||
137 | ptformatter = disp_formatter.formatters['text/plain'] |
|
|||
138 | # dstore is a data store kept in the instance metadata bag to track any |
|
|||
139 | # changes we make, so we can undo them later. |
|
|||
140 | dstore = shell.meta.setdefault('doctest_mode', Struct()) |
|
|||
141 | save_dstore = dstore.setdefault |
|
|||
142 |
|
||||
143 | # save a few values we'll need to recover later |
|
|||
144 | mode = save_dstore('mode', False) |
|
|||
145 | save_dstore('rc_pprint', ptformatter.pprint) |
|
|||
146 | save_dstore('rc_active_types',disp_formatter.active_types) |
|
|||
147 | save_dstore('xmode', shell.InteractiveTB.mode) |
|
|||
148 |
|
||||
149 | if mode == False: |
|
|||
150 | # turn on |
|
|||
151 | ptformatter.pprint = False |
|
|||
152 | disp_formatter.active_types = ['text/plain'] |
|
|||
153 | shell.magic('xmode Plain') |
|
|||
154 | else: |
|
|||
155 | # turn off |
|
|||
156 | ptformatter.pprint = dstore.rc_pprint |
|
|||
157 | disp_formatter.active_types = dstore.rc_active_types |
|
|||
158 | shell.magic("xmode " + dstore.xmode) |
|
|||
159 |
|
||||
160 | # Store new mode and inform on console |
|
|||
161 | dstore.mode = bool(1-int(mode)) |
|
|||
162 | mode_label = ['OFF','ON'][dstore.mode] |
|
|||
163 | print('Doctest mode is:', mode_label) |
|
|||
164 |
|
||||
165 | # Send the payload back so that clients can modify their prompt display |
|
|||
166 | payload = dict( |
|
|||
167 | source='doctest_mode', |
|
|||
168 | mode=dstore.mode) |
|
|||
169 | shell.payload_manager.write_payload(payload) |
|
|||
170 |
|
||||
171 |
|
105 | |||
172 | _find_edit_target = CodeMagics._find_edit_target |
|
106 | _find_edit_target = CodeMagics._find_edit_target | |
173 |
|
107 | |||
@@ -473,25 +407,11 b' class ZMQInteractiveShell(InteractiveShell):' | |||||
473 | # And install the payload version of page. |
|
407 | # And install the payload version of page. | |
474 | install_payload_page() |
|
408 | install_payload_page() | |
475 |
|
409 | |||
476 | def auto_rewrite_input(self, cmd): |
|
|||
477 | """Called to show the auto-rewritten input for autocall and friends. |
|
|||
478 |
|
||||
479 | FIXME: this payload is currently not correctly processed by the |
|
|||
480 | frontend. |
|
|||
481 | """ |
|
|||
482 | new = self.prompt_manager.render('rewrite') + cmd |
|
|||
483 | payload = dict( |
|
|||
484 | source='auto_rewrite_input', |
|
|||
485 | transformed_input=new, |
|
|||
486 | ) |
|
|||
487 | self.payload_manager.write_payload(payload) |
|
|||
488 |
|
||||
489 | def ask_exit(self): |
|
410 | def ask_exit(self): | |
490 | """Engage the exit actions.""" |
|
411 | """Engage the exit actions.""" | |
491 | self.exit_now = (not self.keepkernel_on_exit) |
|
412 | self.exit_now = (not self.keepkernel_on_exit) | |
492 | payload = dict( |
|
413 | payload = dict( | |
493 | source='ask_exit', |
|
414 | source='ask_exit', | |
494 | exit=True, |
|
|||
495 | keepkernel=self.keepkernel_on_exit, |
|
415 | keepkernel=self.keepkernel_on_exit, | |
496 | ) |
|
416 | ) | |
497 | self.payload_manager.write_payload(payload) |
|
417 | self.payload_manager.write_payload(payload) |
@@ -103,7 +103,7 b' class IPythonWidget(FrontendWidget):' | |||||
103 |
|
103 | |||
104 | # IPythonWidget protected class variables. |
|
104 | # IPythonWidget protected class variables. | |
105 | _PromptBlock = namedtuple('_PromptBlock', ['block', 'length', 'number']) |
|
105 | _PromptBlock = namedtuple('_PromptBlock', ['block', 'length', 'number']) | |
106 |
_payload_source_edit = 'edit |
|
106 | _payload_source_edit = 'edit' | |
107 | _payload_source_exit = 'ask_exit' |
|
107 | _payload_source_exit = 'ask_exit' | |
108 | _payload_source_next_input = 'set_next_input' |
|
108 | _payload_source_next_input = 'set_next_input' | |
109 | _payload_source_page = 'page' |
|
109 | _payload_source_page = 'page' |
@@ -355,13 +355,11 b' Message type: ``execute_reply``::' | |||||
355 | When status is 'ok', the following extra fields are present:: |
|
355 | When status is 'ok', the following extra fields are present:: | |
356 |
|
356 | |||
357 | { |
|
357 | { | |
358 | # 'payload' will be a list of payload dicts. |
|
358 | # 'payload' will be a list of payload dicts, and is optional. | |
359 | # Each execution payload is a dict with string keys that may have been |
|
359 | # payloads are considered deprecated. | |
360 | # produced by the code being executed. It is retrieved by the kernel at |
|
|||
361 | # the end of the execution and sent back to the front end, which can take |
|
|||
362 | # action on it as needed. |
|
|||
363 | # The only requirement of each payload dict is that it have a 'source' key, |
|
360 | # The only requirement of each payload dict is that it have a 'source' key, | |
364 |
# which is a string classifying the payload (e.g. 'page |
|
361 | # which is a string classifying the payload (e.g. 'page'). | |
|
362 | ||||
365 | 'payload' : list(dict), |
|
363 | 'payload' : list(dict), | |
366 |
|
364 | |||
367 | # Results for the user_expressions. |
|
365 | # Results for the user_expressions. | |
@@ -372,24 +370,6 b" When status is 'ok', the following extra fields are present::" | |||||
372 |
|
370 | |||
373 | ``user_variables`` is removed, use user_expressions instead. |
|
371 | ``user_variables`` is removed, use user_expressions instead. | |
374 |
|
372 | |||
375 | .. admonition:: Execution payloads |
|
|||
376 |
|
||||
377 | The notion of an 'execution payload' is different from a return value of a |
|
|||
378 | given set of code, which normally is just displayed on the execute_result stream |
|
|||
379 | through the PUB socket. The idea of a payload is to allow special types of |
|
|||
380 | code, typically magics, to populate a data container in the IPython kernel |
|
|||
381 | that will be shipped back to the caller via this channel. The kernel |
|
|||
382 | has an API for this in the PayloadManager:: |
|
|||
383 |
|
||||
384 | ip.payload_manager.write_payload(payload_dict) |
|
|||
385 |
|
||||
386 | which appends a dictionary to the list of payloads. |
|
|||
387 |
|
||||
388 | The payload API is not yet stabilized, |
|
|||
389 | and should probably not be supported by non-Python kernels at this time. |
|
|||
390 | In such cases, the payload list should always be empty. |
|
|||
391 |
|
||||
392 |
|
||||
393 | When status is 'error', the following extra fields are present:: |
|
373 | When status is 'error', the following extra fields are present:: | |
394 |
|
374 | |||
395 | { |
|
375 | { | |
@@ -411,6 +391,72 b" When status is 'error', the following extra fields are present::" | |||||
411 | When status is 'abort', there are for now no additional data fields. This |
|
391 | When status is 'abort', there are for now no additional data fields. This | |
412 | happens when the kernel was interrupted by a signal. |
|
392 | happens when the kernel was interrupted by a signal. | |
413 |
|
393 | |||
|
394 | Payloads | |||
|
395 | ******** | |||
|
396 | ||||
|
397 | .. admonition:: Execution payloads | |||
|
398 | ||||
|
399 | Payloads are considered deprecated, though their replacement is not yet implemented. | |||
|
400 | ||||
|
401 | Payloads are a way to trigger frontend actions from the kernel. Current payloads: | |||
|
402 | ||||
|
403 | **page**: display data in a pager. | |||
|
404 | ||||
|
405 | Pager output is used for introspection, or other displayed information that's not considered output. | |||
|
406 | Pager payloads are generally displayed in a separate pane, that can be viewed alongside code, | |||
|
407 | and are not included in notebook documents. | |||
|
408 | ||||
|
409 | .. sourcecode:: python | |||
|
410 | ||||
|
411 | { | |||
|
412 | "source": "page", | |||
|
413 | # mime-bundle of data to display in the pager. | |||
|
414 | # Must include text/plain. | |||
|
415 | "data": mimebundle, | |||
|
416 | # line offset to start from | |||
|
417 | "start": int, | |||
|
418 | } | |||
|
419 | ||||
|
420 | **set_next_input**: create a new output | |||
|
421 | ||||
|
422 | used to create new cells in the notebook, | |||
|
423 | or set the next input in a console interface. | |||
|
424 | The main example being ``%load``. | |||
|
425 | ||||
|
426 | .. sourcecode:: python | |||
|
427 | ||||
|
428 | { | |||
|
429 | "source": "set_next_input", | |||
|
430 | # the text contents of the cell to create | |||
|
431 | "text": "some cell content", | |||
|
432 | } | |||
|
433 | ||||
|
434 | **edit**: open a file for editing. | |||
|
435 | ||||
|
436 | Triggered by `%edit`. Only the QtConsole currently supports edit payloads. | |||
|
437 | ||||
|
438 | .. sourcecode:: python | |||
|
439 | ||||
|
440 | { | |||
|
441 | "source": "edit", | |||
|
442 | "filename": "/path/to/file.py", # the file to edit | |||
|
443 | "line_number": int, # the line number to start with | |||
|
444 | } | |||
|
445 | ||||
|
446 | **ask_exit**: instruct the frontend to prompt the user for exit | |||
|
447 | ||||
|
448 | Allows the kernel to request exit, e.g. via ``%exit`` in IPython. | |||
|
449 | Only for console frontends. | |||
|
450 | ||||
|
451 | .. sourcecode:: python | |||
|
452 | ||||
|
453 | { | |||
|
454 | "source": "ask_exit", | |||
|
455 | # whether the kernel should be left running, only closing the client | |||
|
456 | "keepkernel": bool, | |||
|
457 | } | |||
|
458 | ||||
|
459 | ||||
414 | .. _msging_inspection: |
|
460 | .. _msging_inspection: | |
415 |
|
461 | |||
416 | Introspection |
|
462 | Introspection |
General Comments 0
You need to be logged in to leave comments.
Login now