##// END OF EJS Templates
update IPython.core to nbformat 4...
MinRK -
Show More
@@ -2651,12 +2651,12 b' class InteractiveShell(SingletonConfigurable):'
2651 if fname.endswith('.ipynb'):
2651 if fname.endswith('.ipynb'):
2652 from IPython.nbformat import current
2652 from IPython.nbformat import current
2653 with open(fname) as f:
2653 with open(fname) as f:
2654 nb = current.read(f, 'json')
2654 nb = current.read(f)
2655 if not nb.worksheets:
2655 if not nb.cells:
2656 return
2656 return
2657 for cell in nb.worksheets[0].cells:
2657 for cell in nb.cells:
2658 if cell.cell_type == 'code':
2658 if cell.cell_type == 'code':
2659 yield cell.input
2659 yield cell.source
2660 else:
2660 else:
2661 with open(fname) as f:
2661 with open(fname) as f:
2662 yield f.read()
2662 yield f.read()
@@ -1,25 +1,12 b''
1 """Implementation of basic magic functions.
1 """Implementation of basic magic functions."""
2 """
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 #-----------------------------------------------------------------------------
14 from __future__ import print_function
3 from __future__ import print_function
15
4
16 # Stdlib
17 import io
5 import io
18 import json
6 import json
19 import sys
7 import sys
20 from pprint import pformat
8 from pprint import pformat
21
9
22 # Our own packages
23 from IPython.core import magic_arguments, page
10 from IPython.core import magic_arguments, page
24 from IPython.core.error import UsageError
11 from IPython.core.error import UsageError
25 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
12 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
@@ -30,9 +17,6 b' from IPython.utils.path import unquote_filename'
30 from IPython.utils.py3compat import unicode_type
17 from IPython.utils.py3compat import unicode_type
31 from IPython.utils.warn import warn, error
18 from IPython.utils.warn import warn, error
32
19
33 #-----------------------------------------------------------------------------
34 # Magics class implementation
35 #-----------------------------------------------------------------------------
36
20
37 class MagicsDisplay(object):
21 class MagicsDisplay(object):
38 def __init__(self, magics_manager):
22 def __init__(self, magics_manager):
@@ -599,13 +583,6 b' Defaulting color scheme to \'NoColor\'"""'
599 'file extension will write the notebook as a Python script'
583 'file extension will write the notebook as a Python script'
600 )
584 )
601 @magic_arguments.argument(
585 @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 )
608 @magic_arguments.argument(
609 'filename', type=unicode_type,
586 'filename', type=unicode_type,
610 help='Notebook name or filename'
587 help='Notebook name or filename'
611 )
588 )
@@ -613,41 +590,20 b' Defaulting color scheme to \'NoColor\'"""'
613 def notebook(self, s):
590 def notebook(self, s):
614 """Export and convert IPython notebooks.
591 """Export and convert IPython notebooks.
615
592
616 This function can export the current IPython history to a notebook file
593 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
594 For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
618 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".
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).
622 """
596 """
623 args = magic_arguments.parse_argstring(self.notebook, s)
597 args = magic_arguments.parse_argstring(self.notebook, s)
624
598
625 from IPython.nbformat import current
599 from IPython.nbformat import current
626 args.filename = unquote_filename(args.filename)
600 args.filename = unquote_filename(args.filename)
627 if args.export:
601 if args.export:
628 fname, name, format = current.parse_filename(args.filename)
629 cells = []
602 cells = []
630 hist = list(self.shell.history_manager.get_range())
603 hist = list(self.shell.history_manager.get_range())
631 for session, prompt_number, input in hist[:-1]:
604 for session, prompt_number, input in hist[:-1]:
632 cells.append(current.new_code_cell(prompt_number=prompt_number,
605 cells.append(current.new_code_cell(prompt_number=prompt_number,
633 input=input))
606 input=input))
634 worksheet = current.new_worksheet(cells=cells)
607 nb = current.new_notebook(cells=cells)
635 nb = current.new_notebook(name=name,worksheets=[worksheet])
608 with io.open(args.filename, 'w', encoding='utf-8') as f:
636 with io.open(fname, 'w', encoding='utf-8') as f:
609 current.write(nb, 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)
@@ -632,35 +632,6 b' class NotebookExportMagicTests(TestCase):'
632 _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
632 _ip.ex(py3compat.u_format(u"u = {u}'héllo'"))
633 _ip.magic("notebook -e %s" % outfile)
633 _ip.magic("notebook -e %s" % outfile)
634
634
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
664
635
665 def test_env():
636 def test_env():
666 env = _ip.magic("env")
637 env = _ip.magic("env")
@@ -377,14 +377,12 b' tclass.py: deleting object: C-third'
377 """Test %run notebook.ipynb"""
377 """Test %run notebook.ipynb"""
378 from IPython.nbformat import current
378 from IPython.nbformat import current
379 nb = current.new_notebook(
379 nb = current.new_notebook(
380 worksheets=[
380 cells=[
381 current.new_worksheet(cells=[
381 current.new_markdown_cell("The Ultimate Question of Everything"),
382 current.new_text_cell("The Ultimate Question of Everything"),
382 current.new_code_cell("answer=42")
383 current.new_code_cell("answer=42")
384 ])
385 ]
383 ]
386 )
384 )
387 src = current.writes(nb, 'json')
385 src = current.writes(nb)
388 self.mktmp(src, ext='.ipynb')
386 self.mktmp(src, ext='.ipynb')
389
387
390 _ip.magic("run %s" % self.fname)
388 _ip.magic("run %s" % self.fname)
General Comments 0
You need to be logged in to leave comments. Login now