Show More
@@ -3,6 +3,7 b'' | |||||
3 | from __future__ import print_function |
|
3 | from __future__ import print_function | |
4 | from __future__ import absolute_import |
|
4 | from __future__ import absolute_import | |
5 |
|
5 | |||
|
6 | import argparse | |||
6 | import io |
|
7 | import io | |
7 | import sys |
|
8 | import sys | |
8 | from pprint import pformat |
|
9 | from pprint import pformat | |
@@ -548,11 +549,7 b' Currently the magic system has the following functions:""",' | |||||
548 | @magic_arguments.magic_arguments() |
|
549 | @magic_arguments.magic_arguments() | |
549 | @magic_arguments.argument( |
|
550 | @magic_arguments.argument( | |
550 | '-e', '--export', action='store_true', default=False, |
|
551 | '-e', '--export', action='store_true', default=False, | |
551 | help='Export IPython history as a notebook. The filename argument ' |
|
552 | help=argparse.SUPPRESS | |
552 | 'is used to specify the notebook name and format. For example ' |
|
|||
553 | 'a filename of notebook.ipynb will result in a notebook name ' |
|
|||
554 | 'of "notebook" and a format of "json". Likewise using a ".py" ' |
|
|||
555 | 'file extension will write the notebook as a Python script' |
|
|||
556 | ) |
|
553 | ) | |
557 | @magic_arguments.argument( |
|
554 | @magic_arguments.argument( | |
558 | 'filename', type=unicode_type, |
|
555 | 'filename', type=unicode_type, | |
@@ -563,22 +560,25 b' Currently the magic system has the following functions:""",' | |||||
563 | """Export and convert IPython notebooks. |
|
560 | """Export and convert IPython notebooks. | |
564 |
|
561 | |||
565 | This function can export the current IPython history to a notebook file. |
|
562 | This function can export the current IPython history to a notebook file. | |
566 |
For example, to export the history to "foo.ipynb" do "%notebook |
|
563 | For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb". | |
567 |
To export the history to "foo.py" do "%notebook |
|
564 | To export the history to "foo.py" do "%notebook foo.py". | |
|
565 | ||||
|
566 | The -e or --export flag is deprecated in IPython 5.2, and will be | |||
|
567 | removed in the future. | |||
568 | """ |
|
568 | """ | |
569 | args = magic_arguments.parse_argstring(self.notebook, s) |
|
569 | args = magic_arguments.parse_argstring(self.notebook, s) | |
570 |
|
570 | |||
571 | from nbformat import write, v4 |
|
571 | from nbformat import write, v4 | |
572 | if args.export: |
|
572 | ||
573 |
|
|
573 | cells = [] | |
574 |
|
|
574 | hist = list(self.shell.history_manager.get_range()) | |
575 |
|
|
575 | if(len(hist)<=1): | |
576 |
|
|
576 | raise ValueError('History is empty, cannot export') | |
577 |
|
|
577 | for session, execution_count, source in hist[:-1]: | |
578 |
|
|
578 | cells.append(v4.new_code_cell( | |
579 |
|
|
579 | execution_count=execution_count, | |
580 |
|
|
580 | source=source | |
581 |
|
|
581 | )) | |
582 |
|
|
582 | nb = v4.new_notebook(cells=cells) | |
583 |
|
|
583 | with io.open(args.filename, 'w', encoding='utf-8') as f: | |
584 |
|
|
584 | write(nb, f, version=4) |
General Comments 0
You need to be logged in to leave comments.
Login now