##// END OF EJS Templates
Make nbformat.(read|write) accept file-like object or path
Thomas Kluyver -
Show More
@@ -5,6 +5,8 b' Use this module to read or write notebook files as particular nbformat versions.'
5
5
6 # Copyright (c) IPython Development Team.
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 import io
9 from IPython.utils import py3compat
8
10
9 from IPython.utils.log import get_logger
11 from IPython.utils.log import get_logger
10
12
@@ -112,8 +114,8 b' def read(fp, as_version, **kwargs):'
112
114
113 Parameters
115 Parameters
114 ----------
116 ----------
115 fp : file
117 fp : file or str
116 Any file-like object with a read method.
118 Any file-like object with a read method, or a path to a file.
117 as_version: int
119 as_version: int
118 The version of the notebook format to return.
120 The version of the notebook format to return.
119 The notebook will be converted, if necessary.
121 The notebook will be converted, if necessary.
@@ -124,6 +126,10 b' def read(fp, as_version, **kwargs):'
124 nb : NotebookNode
126 nb : NotebookNode
125 The notebook that was read.
127 The notebook that was read.
126 """
128 """
129 if isinstance(fp, py3compat.string_types):
130 with io.open(fp, encoding='utf-8') as f:
131 return read(f, as_version, **kwargs)
132
127 return reads(fp.read(), as_version, **kwargs)
133 return reads(fp.read(), as_version, **kwargs)
128
134
129
135
@@ -136,14 +142,19 b' def write(nb, fp, version=NO_CONVERT, **kwargs):'
136 ----------
142 ----------
137 nb : NotebookNode
143 nb : NotebookNode
138 The notebook to write.
144 The notebook to write.
139 fp : file
145 fp : file or str
140 Any file-like object with a write method that accepts unicode.
146 Any file-like object with a write method that accepts unicode, or
147 a path to write a file.
141 version : int, optional
148 version : int, optional
142 The nbformat version to write.
149 The nbformat version to write.
143 If nb is not this version, it will be converted.
150 If nb is not this version, it will be converted.
144 If unspecified, or specified as nbformat.NO_CONVERT,
151 If unspecified, or specified as nbformat.NO_CONVERT,
145 the notebook's own version will be used and no conversion performed.
152 the notebook's own version will be used and no conversion performed.
146 """
153 """
154 if isinstance(fp, py3compat.string_types):
155 with open(fp, 'w', encoding='utf-8') as f:
156 return write(nb, f, version=version, **kwargs)
157
147 s = writes(nb, version, **kwargs)
158 s = writes(nb, version, **kwargs)
148 if isinstance(s, bytes):
159 if isinstance(s, bytes):
149 s = s.decode('utf8')
160 s = s.decode('utf8')
@@ -4,11 +4,13 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import json
6 import json
7 import os
7
8
8 from .base import TestsBase
9 from .base import TestsBase
9
10
11 from IPython.utils.tempdir import TemporaryDirectory
10 from ..reader import get_version
12 from ..reader import get_version
11 from IPython.nbformat import read, current_nbformat, writes
13 from IPython.nbformat import read, current_nbformat, writes, write
12
14
13
15
14 class TestAPI(TestsBase):
16 class TestAPI(TestsBase):
@@ -35,3 +37,13 b' class TestAPI(TestsBase):'
35 nb2 = json.loads(jsons)
37 nb2 = json.loads(jsons)
36 (major, minor) = get_version(nb2)
38 (major, minor) = get_version(nb2)
37 self.assertEqual(major, 2)
39 self.assertEqual(major, 2)
40
41 def test_read_write_path(self):
42 """read() and write() take filesystem paths"""
43 path = os.path.join(self._get_files_path(), u'test4.ipynb')
44 nb = read(path, as_version=4)
45
46 with TemporaryDirectory() as td:
47 dest = os.path.join(td, 'echidna.ipynb')
48 write(nb, dest)
49 assert os.path.isfile(dest)
General Comments 0
You need to be logged in to leave comments. Login now