Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v2/convert.py: 18%
17 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Code for converting notebooks to and from the v2 format.
3Authors:
5* Brian Granger
6* Jonathan Frederic
7"""
9# -----------------------------------------------------------------------------
10# Copyright (C) 2008-2011 The IPython Development Team
11#
12# Distributed under the terms of the BSD License. The full license is in
13# the file LICENSE, distributed as part of this software.
14# -----------------------------------------------------------------------------
16# -----------------------------------------------------------------------------
17# Imports
18# -----------------------------------------------------------------------------
20from .nbbase import new_code_cell, new_notebook, new_text_cell, new_worksheet
22# -----------------------------------------------------------------------------
23# Code
24# -----------------------------------------------------------------------------
27def upgrade(nb, from_version=1):
28 """Convert a notebook to the v2 format.
30 Parameters
31 ----------
32 nb : NotebookNode
33 The Python representation of the notebook to convert.
34 from_version : int
35 The version of the notebook to convert from.
36 """
37 if from_version == 1:
38 newnb = new_notebook()
39 ws = new_worksheet()
40 for cell in nb.cells:
41 if cell.cell_type == "code":
42 newcell = new_code_cell(
43 input=cell.get("code"), prompt_number=cell.get("prompt_number")
44 )
45 elif cell.cell_type == "text":
46 newcell = new_text_cell("markdown", source=cell.get("text"))
47 ws.cells.append(newcell)
48 newnb.worksheets.append(ws)
49 return newnb
50 else:
51 raise ValueError("Cannot convert a notebook from v%s to v2" % from_version)
54def downgrade(nb):
55 """Convert a v2 notebook to v1.
57 Parameters
58 ----------
59 nb : NotebookNode
60 The Python representation of the notebook to convert.
61 """
62 msg = "Downgrade from notebook v2 to v1 is not supported."
63 raise Exception(msg)