Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v1/nbjson.py: 75%
20 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
1"""Read and write notebooks in JSON format.
3Authors:
5* Brian Granger
6"""
8# -----------------------------------------------------------------------------
9# Copyright (C) 2008-2011 The IPython Development Team
10#
11# Distributed under the terms of the BSD License. The full license is in
12# the file LICENSE, distributed as part of this software.
13# -----------------------------------------------------------------------------
15# -----------------------------------------------------------------------------
16# Imports
17# -----------------------------------------------------------------------------
19import json
21from .nbbase import from_dict
22from .rwbase import NotebookReader, NotebookWriter
24# -----------------------------------------------------------------------------
25# Code
26# -----------------------------------------------------------------------------
29class JSONReader(NotebookReader):
30 """A JSON notebook reader."""
32 def reads(self, s, **kwargs):
33 """Convert a string to a notebook object."""
34 nb = json.loads(s, **kwargs)
35 return self.to_notebook(nb, **kwargs)
37 def to_notebook(self, d, **kwargs):
38 """Convert from a raw JSON dict to a nested NotebookNode structure."""
39 return from_dict(d)
42class JSONWriter(NotebookWriter):
43 """A JSON notebook writer."""
45 def writes(self, nb, **kwargs):
46 """Convert a notebook object to a string."""
47 kwargs["indent"] = 4
48 return json.dumps(nb, **kwargs)
51_reader = JSONReader()
52_writer = JSONWriter()
54reads = _reader.reads
55read = _reader.read
56to_notebook = _reader.to_notebook
57write = _writer.write
58writes = _writer.writes