Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v1/nbjson.py: 81%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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# -----------------------------------------------------------------------------
18from __future__ import annotations
20import json
22from .nbbase import from_dict
23from .rwbase import NotebookReader, NotebookWriter
25# -----------------------------------------------------------------------------
26# Code
27# -----------------------------------------------------------------------------
30class JSONReader(NotebookReader):
31 """A JSON notebook reader."""
33 def reads(self, s, **kwargs):
34 """Convert a string to a notebook object."""
35 nb = json.loads(s, **kwargs)
36 return self.to_notebook(nb, **kwargs)
38 def to_notebook(self, d, **kwargs):
39 """Convert from a raw JSON dict to a nested NotebookNode structure."""
40 return from_dict(d)
43class JSONWriter(NotebookWriter):
44 """A JSON notebook writer."""
46 def writes(self, nb, **kwargs):
47 """Convert a notebook object to a string."""
48 kwargs["indent"] = 4
49 return json.dumps(nb, **kwargs)
52_reader = JSONReader()
53_writer = JSONWriter()
55reads = _reader.reads
56read = _reader.read
57to_notebook = _reader.to_notebook
58write = _writer.write
59writes = _writer.writes