diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 31fa45e..7df9e29 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2651,12 +2651,12 @@ class InteractiveShell(SingletonConfigurable): if fname.endswith('.ipynb'): from IPython.nbformat import current with open(fname) as f: - nb = current.read(f, 'json') - if not nb.worksheets: + nb = current.read(f) + if not nb.cells: return - for cell in nb.worksheets[0].cells: + for cell in nb.cells: if cell.cell_type == 'code': - yield cell.input + yield cell.source else: with open(fname) as f: yield f.read() diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py index 827f72c..52e032a 100644 --- a/IPython/core/magics/basic.py +++ b/IPython/core/magics/basic.py @@ -1,25 +1,12 @@ -"""Implementation of basic magic functions. -""" -#----------------------------------------------------------------------------- -# Copyright (c) 2012 The IPython Development Team. -# -# Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- +"""Implementation of basic magic functions.""" + from __future__ import print_function -# Stdlib import io import json import sys from pprint import pformat -# Our own packages from IPython.core import magic_arguments, page from IPython.core.error import UsageError from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes @@ -30,9 +17,6 @@ from IPython.utils.path import unquote_filename from IPython.utils.py3compat import unicode_type from IPython.utils.warn import warn, error -#----------------------------------------------------------------------------- -# Magics class implementation -#----------------------------------------------------------------------------- class MagicsDisplay(object): def __init__(self, magics_manager): @@ -599,13 +583,6 @@ Defaulting color scheme to 'NoColor'""" 'file extension will write the notebook as a Python script' ) @magic_arguments.argument( - '-f', '--format', - help='Convert an existing IPython notebook to a new format. This option ' - 'specifies the new format and can have the values: json, py. ' - 'The target filename is chosen automatically based on the new ' - 'format. The filename argument gives the name of the source file.' - ) - @magic_arguments.argument( 'filename', type=unicode_type, help='Notebook name or filename' ) @@ -613,41 +590,20 @@ Defaulting color scheme to 'NoColor'""" def notebook(self, s): """Export and convert IPython notebooks. - This function can export the current IPython history to a notebook file - or can convert an existing notebook file into a different format. For - example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". - To export the history to "foo.py" do "%notebook -e foo.py". To convert - "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible - formats include (json/ipynb, py). + This function can export the current IPython history to a notebook file. + For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". + To export the history to "foo.py" do "%notebook -e foo.py". """ args = magic_arguments.parse_argstring(self.notebook, s) from IPython.nbformat import current args.filename = unquote_filename(args.filename) if args.export: - fname, name, format = current.parse_filename(args.filename) cells = [] hist = list(self.shell.history_manager.get_range()) for session, prompt_number, input in hist[:-1]: cells.append(current.new_code_cell(prompt_number=prompt_number, input=input)) - worksheet = current.new_worksheet(cells=cells) - nb = current.new_notebook(name=name,worksheets=[worksheet]) - with io.open(fname, 'w', encoding='utf-8') as f: - current.write(nb, f, format); - elif args.format is not None: - old_fname, old_name, old_format = current.parse_filename(args.filename) - new_format = args.format - if new_format == u'xml': - raise ValueError('Notebooks cannot be written as xml.') - elif new_format == u'ipynb' or new_format == u'json': - new_fname = old_name + u'.ipynb' - new_format = u'json' - elif new_format == u'py': - new_fname = old_name + u'.py' - else: - raise ValueError('Invalid notebook format: %s' % new_format) - with io.open(old_fname, 'r', encoding='utf-8') as f: - nb = current.read(f, old_format) - with io.open(new_fname, 'w', encoding='utf-8') as f: - current.write(nb, f, new_format) + nb = current.new_notebook(cells=cells) + with io.open(args.filename, 'w', encoding='utf-8') as f: + current.write(nb, f) diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index ea617bc..e28dc5c 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -632,35 +632,6 @@ class NotebookExportMagicTests(TestCase): _ip.ex(py3compat.u_format(u"u = {u}'héllo'")) _ip.magic("notebook -e %s" % outfile) - def test_notebook_export_py(self): - with TemporaryDirectory() as td: - outfile = os.path.join(td, "nb.py") - _ip.ex(py3compat.u_format(u"u = {u}'héllo'")) - _ip.magic("notebook -e %s" % outfile) - - def test_notebook_reformat_py(self): - from IPython.nbformat.v3.tests.nbexamples import nb0 - from IPython.nbformat import current - with TemporaryDirectory() as td: - infile = os.path.join(td, "nb.ipynb") - with io.open(infile, 'w', encoding='utf-8') as f: - current.write(nb0, f, 'json') - - _ip.ex(py3compat.u_format(u"u = {u}'héllo'")) - _ip.magic("notebook -f py %s" % infile) - - def test_notebook_reformat_json(self): - from IPython.nbformat.v3.tests.nbexamples import nb0 - from IPython.nbformat import current - with TemporaryDirectory() as td: - infile = os.path.join(td, "nb.py") - with io.open(infile, 'w', encoding='utf-8') as f: - current.write(nb0, f, 'py') - - _ip.ex(py3compat.u_format(u"u = {u}'héllo'")) - _ip.magic("notebook -f ipynb %s" % infile) - _ip.magic("notebook -f json %s" % infile) - def test_env(): env = _ip.magic("env") diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 2816e36..95dc4ac 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -377,14 +377,12 @@ tclass.py: deleting object: C-third """Test %run notebook.ipynb""" from IPython.nbformat import current nb = current.new_notebook( - worksheets=[ - current.new_worksheet(cells=[ - current.new_text_cell("The Ultimate Question of Everything"), - current.new_code_cell("answer=42") - ]) + cells=[ + current.new_markdown_cell("The Ultimate Question of Everything"), + current.new_code_cell("answer=42") ] ) - src = current.writes(nb, 'json') + src = current.writes(nb) self.mktmp(src, ext='.ipynb') _ip.magic("run %s" % self.fname)