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

21 statements  

1"""Read and write notebooks in JSON format. 

2 

3Authors: 

4 

5* Brian Granger 

6""" 

7 

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# ----------------------------------------------------------------------------- 

14 

15# ----------------------------------------------------------------------------- 

16# Imports 

17# ----------------------------------------------------------------------------- 

18from __future__ import annotations 

19 

20import json 

21 

22from .nbbase import from_dict 

23from .rwbase import NotebookReader, NotebookWriter 

24 

25# ----------------------------------------------------------------------------- 

26# Code 

27# ----------------------------------------------------------------------------- 

28 

29 

30class JSONReader(NotebookReader): 

31 """A JSON notebook reader.""" 

32 

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) 

37 

38 def to_notebook(self, d, **kwargs): 

39 """Convert from a raw JSON dict to a nested NotebookNode structure.""" 

40 return from_dict(d) 

41 

42 

43class JSONWriter(NotebookWriter): 

44 """A JSON notebook writer.""" 

45 

46 def writes(self, nb, **kwargs): 

47 """Convert a notebook object to a string.""" 

48 kwargs["indent"] = 4 

49 return json.dumps(nb, **kwargs) 

50 

51 

52_reader = JSONReader() 

53_writer = JSONWriter() 

54 

55reads = _reader.reads 

56read = _reader.read 

57to_notebook = _reader.to_notebook 

58write = _writer.write 

59writes = _writer.writes