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

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

18 

19import json 

20 

21from .nbbase import from_dict 

22from .rwbase import NotebookReader, NotebookWriter 

23 

24# ----------------------------------------------------------------------------- 

25# Code 

26# ----------------------------------------------------------------------------- 

27 

28 

29class JSONReader(NotebookReader): 

30 """A JSON notebook reader.""" 

31 

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) 

36 

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

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

39 return from_dict(d) 

40 

41 

42class JSONWriter(NotebookWriter): 

43 """A JSON notebook writer.""" 

44 

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

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

47 kwargs["indent"] = 4 

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

49 

50 

51_reader = JSONReader() 

52_writer = JSONWriter() 

53 

54reads = _reader.reads 

55read = _reader.read 

56to_notebook = _reader.to_notebook 

57write = _writer.write 

58writes = _writer.writes