Export and convert IPython notebooks. This function can export the current IPython history to a notebook file. For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb". The -e or --export flag is deprecated in IPython 5.2, and will be removed in th
(self, s)
| 568 | ) |
| 569 | @line_magic |
| 570 | def notebook(self, s): |
| 571 | """Export and convert IPython notebooks. |
| 572 | |
| 573 | This function can export the current IPython history to a notebook file. |
| 574 | For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb". |
| 575 | |
| 576 | The -e or --export flag is deprecated in IPython 5.2, and will be |
| 577 | removed in the future. |
| 578 | """ |
| 579 | args = magic_arguments.parse_argstring(self.notebook, s) |
| 580 | |
| 581 | from nbformat import write, v4 |
| 582 | |
| 583 | cells = [] |
| 584 | hist = list(self.shell.history_manager.get_range()) |
| 585 | if(len(hist)<=1): |
| 586 | raise ValueError('History is empty, cannot export') |
| 587 | for session, execution_count, source in hist[:-1]: |
| 588 | cells.append(v4.new_code_cell( |
| 589 | execution_count=execution_count, |
| 590 | source=source |
| 591 | )) |
| 592 | nb = v4.new_notebook(cells=cells) |
| 593 | with io.open(args.filename, 'w', encoding='utf-8') as f: |
| 594 | write(nb, f, version=4) |
| 595 | |
| 596 | @magics_class |
| 597 | class AsyncMagics(BasicMagics): |
nothing calls this directly
no test coverage detected