update IPython.core to nbformat 4 · pythonian4000/ipython@80dee97 · GitHub
Skip to content

Commit 80dee97

Browse files
committed
update IPython.core to nbformat 4
affects `%run` and `%notebook` removes `%notebook --format` conversion, that's what nbconvert is for.
1 parent 3954d29 commit 80dee97

4 files changed

Lines changed: 16 additions & 91 deletions

File tree

IPython/core/interactiveshell.py

Lines changed: 4 additions & 4 deletions

IPython/core/magics/basic.py

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
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+
143
from __future__ import print_function
154

16-
# Stdlib
175
import io
186
import json
197
import sys
208
from pprint import pformat
219

22-
# Our own packages
2310
from IPython.core import magic_arguments, page
2411
from IPython.core.error import UsageError
2512
from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
@@ -30,9 +17,6 @@
3017
from IPython.utils.py3compat import unicode_type
3118
from IPython.utils.warn import warn, error
3219

33-
#-----------------------------------------------------------------------------
34-
# Magics class implementation
35-
#-----------------------------------------------------------------------------
3620

3721
class 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)

IPython/core/tests/test_magic.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -632,35 +632,6 @@ def test_notebook_export_json(self):
632632
_ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
633633
_ip.magic("notebook -e %s" % outfile)
634634

635-
def test_notebook_export_py(self):
636-
with TemporaryDirectory() as td:
637-
outfile = os.path.join(td, "nb.py")
638-
_ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
639-
_ip.magic("notebook -e %s" % outfile)
640-
641-
def test_notebook_reformat_py(self):
642-
from IPython.nbformat.v3.tests.nbexamples import nb0
643-
from IPython.nbformat import current
644-
with TemporaryDirectory() as td:
645-
infile = os.path.join(td, "nb.ipynb")
646-
with io.open(infile, 'w', encoding='utf-8') as f:
647-
current.write(nb0, f, 'json')
648-
649-
_ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
650-
_ip.magic("notebook -f py %s" % infile)
651-
652-
def test_notebook_reformat_json(self):
653-
from IPython.nbformat.v3.tests.nbexamples import nb0
654-
from IPython.nbformat import current
655-
with TemporaryDirectory() as td:
656-
infile = os.path.join(td, "nb.py")
657-
with io.open(infile, 'w', encoding='utf-8') as f:
658-
current.write(nb0, f, 'py')
659-
660-
_ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
661-
_ip.magic("notebook -f ipynb %s" % infile)
662-
_ip.magic("notebook -f json %s" % infile)
663-
664635

665636
def test_env():
666637
env = _ip.magic("env")

IPython/core/tests/test_run.py

Lines changed: 4 additions & 6 deletions

0 commit comments

Comments
 (0)