##// END OF EJS Templates
strip transient values to/from nb files...
MinRK -
Show More
@@ -34,7 +34,7 b''
34 "type": "string"
34 "type": "string"
35 },
35 },
36 "orig_nbformat": {
36 "orig_nbformat": {
37 "description": "Original notebook format (major number) before converting the notebook between versions.",
37 "description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
38 "type": "integer",
38 "type": "integer",
39 "minimum": 1
39 "minimum": 1
40 }
40 }
@@ -10,7 +10,7 b' from IPython.utils import py3compat'
10
10
11 from .nbbase import from_dict
11 from .nbbase import from_dict
12 from .rwbase import (
12 from .rwbase import (
13 NotebookReader, NotebookWriter, rejoin_lines, split_lines
13 NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
14 )
14 )
15
15
16
16
@@ -30,7 +30,9 b' class JSONReader(NotebookReader):'
30 return nb
30 return nb
31
31
32 def to_notebook(self, d, **kwargs):
32 def to_notebook(self, d, **kwargs):
33 return rejoin_lines(from_dict(d))
33 nb = rejoin_lines(from_dict(d))
34 nb = strip_transient(nb)
35 return nb
34
36
35
37
36 class JSONWriter(NotebookWriter):
38 class JSONWriter(NotebookWriter):
@@ -40,8 +42,11 b' class JSONWriter(NotebookWriter):'
40 kwargs['indent'] = 1
42 kwargs['indent'] = 1
41 kwargs['sort_keys'] = True
43 kwargs['sort_keys'] = True
42 kwargs['separators'] = (',',': ')
44 kwargs['separators'] = (',',': ')
45 # don't modify in-memory dict
46 nb = copy.deepcopy(nb)
43 if kwargs.pop('split_lines', True):
47 if kwargs.pop('split_lines', True):
44 nb = split_lines(copy.deepcopy(nb))
48 nb = split_lines(nb)
49 nb = strip_transient(nb)
45 return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8')
50 return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8')
46
51
47
52
@@ -75,6 +75,18 b' def split_lines(nb):'
75 return nb
75 return nb
76
76
77
77
78 def strip_transient(nb):
79 """Strip transient values that shouldn't be stored in files.
80
81 This should be called in *both* read and write.
82 """
83 nb.metadata.pop('orig_nbformat', None)
84 nb.metadata.pop('orig_nbformat_minor', None)
85 for cell in nb.cells:
86 cell.metadata.pop('trusted', None)
87 return nb
88
89
78 class NotebookReader(object):
90 class NotebookReader(object):
79 """A class for reading notebooks."""
91 """A class for reading notebooks."""
80
92
General Comments 0
You need to be logged in to leave comments. Login now