1- """Implementation of basic magic functions.
2- """
3- #-----------------------------------------------------------------------------
4- # Copyright (c) 2012 The IPython Development Team.
5- #
6- # Distributed under the terms of the Modified BSD License.
7- #
8- # The full license is in the file COPYING.txt, distributed with this software.
9- #-----------------------------------------------------------------------------
10-
11- #-----------------------------------------------------------------------------
12- # Imports
13- #-----------------------------------------------------------------------------
1+ """Implementation of basic magic functions."""
2+
143from __future__ import print_function
154
16- # Stdlib
175import io
186import json
197import sys
208from pprint import pformat
219
22- # Our own packages
2310from IPython .core import magic_arguments , page
2411from IPython .core .error import UsageError
2512from IPython .core .magic import Magics , magics_class , line_magic , magic_escapes
3017from IPython .utils .py3compat import unicode_type
3118from IPython .utils .warn import warn , error
3219
33- #-----------------------------------------------------------------------------
34- # Magics class implementation
35- #-----------------------------------------------------------------------------
3620
3721class MagicsDisplay (object ):
3822 def __init__ (self , magics_manager ):
@@ -598,13 +582,6 @@ def precision(self, s=''):
598582 'of "notebook" and a format of "json". Likewise using a ".py" '
599583 'file extension will write the notebook as a Python script'
600584 )
601- @magic_arguments .argument (
602- '-f' , '--format' ,
603- help = 'Convert an existing IPython notebook to a new format. This option '
604- 'specifies the new format and can have the values: json, py. '
605- 'The target filename is chosen automatically based on the new '
606- 'format. The filename argument gives the name of the source file.'
607- )
608585 @magic_arguments .argument (
609586 'filename' , type = unicode_type ,
610587 help = 'Notebook name or filename'
@@ -613,41 +590,20 @@ def precision(self, s=''):
613590 def notebook (self , s ):
614591 """Export and convert IPython notebooks.
615592
616- This function can export the current IPython history to a notebook file
617- or can convert an existing notebook file into a different format. For
618- example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
619- To export the history to "foo.py" do "%notebook -e foo.py". To convert
620- "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
621- formats include (json/ipynb, py).
593+ This function can export the current IPython history to a notebook file.
594+ For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
595+ To export the history to "foo.py" do "%notebook -e foo.py".
622596 """
623597 args = magic_arguments .parse_argstring (self .notebook , s )
624598
625599 from IPython .nbformat import current
626600 args .filename = unquote_filename (args .filename )
627601 if args .export :
628- fname , name , format = current .parse_filename (args .filename )
629602 cells = []
630603 hist = list (self .shell .history_manager .get_range ())
631604 for session , prompt_number , input in hist [:- 1 ]:
632605 cells .append (current .new_code_cell (prompt_number = prompt_number ,
633606 input = input ))
634- worksheet = current .new_worksheet (cells = cells )
635- nb = current .new_notebook (name = name ,worksheets = [worksheet ])
636- with io .open (fname , 'w' , encoding = 'utf-8' ) as f :
637- current .write (nb , f , format );
638- elif args .format is not None :
639- old_fname , old_name , old_format = current .parse_filename (args .filename )
640- new_format = args .format
641- if new_format == u'xml' :
642- raise ValueError ('Notebooks cannot be written as xml.' )
643- elif new_format == u'ipynb' or new_format == u'json' :
644- new_fname = old_name + u'.ipynb'
645- new_format = u'json'
646- elif new_format == u'py' :
647- new_fname = old_name + u'.py'
648- else :
649- raise ValueError ('Invalid notebook format: %s' % new_format )
650- with io .open (old_fname , 'r' , encoding = 'utf-8' ) as f :
651- nb = current .read (f , old_format )
652- with io .open (new_fname , 'w' , encoding = 'utf-8' ) as f :
653- current .write (nb , f , new_format )
607+ nb = current .new_notebook (cells = cells )
608+ with io .open (args .filename , 'w' , encoding = 'utf-8' ) as f :
609+ current .write (nb , f )
0 commit comments