Repository navigation
Expand file tree
/
Copy pathtest_ipynb_nbformat3.py
More file actions
189 lines (144 loc) · 5.42 KB
/
Copy pathtest_ipynb_nbformat3.py
File metadata and controls
189 lines (144 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
"""Regression tests for reading notebooks saved in the nbformat 3 layout.
nbformat 3 (IPython 3, Jupyter 4.0) nests cells under ``worksheets[*].cells`` and
stores code cell text under ``input``. Reading only the nbformat 4 layout produced
an empty document with a success exit code, so every cell was silently dropped.
"""
import io
import json
from markitdown import MarkItDown, StreamInfo
from markitdown.converters._ipynb_converter import IpynbConverter
_NBFORMAT_3 = {
"metadata": {},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"language": "python",
"input": ["print('hello')\n"],
"outputs": [],
},
{
"cell_type": "markdown",
"source": ["# Quarterly notes\n", "\n", "Revenue grew.\n"],
},
]
}
],
}
# The same notebook in the nbformat 4 layout: flat `cells`, and `source` everywhere.
_NBFORMAT_4 = {
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5,
"cells": [
{"cell_type": "code", "source": ["print('hello')\n"], "outputs": []},
{
"cell_type": "markdown",
"source": ["# Quarterly notes\n", "\n", "Revenue grew.\n"],
},
],
}
def _convert(notebook: dict):
data = json.dumps(notebook).encode("utf-8")
return IpynbConverter().convert(
io.BytesIO(data), StreamInfo(extension=".ipynb", charset="utf-8")
)
def test_nbformat_3_notebook_is_not_empty() -> None:
"""An accepted notebook must never convert to an empty document."""
result = _convert(_NBFORMAT_3)
assert result.markdown.strip(), "nbformat 3 notebook converted to nothing"
def test_nbformat_3_code_cells_use_their_input_key() -> None:
result = _convert(_NBFORMAT_3)
assert "```python\nprint('hello')" in result.markdown
def test_nbformat_3_markdown_cells_are_kept_and_title_extracted() -> None:
result = _convert(_NBFORMAT_3)
assert "# Quarterly notes" in result.markdown
assert "Revenue grew." in result.markdown
assert result.title == "Quarterly notes"
def test_nbformat_3_matches_nbformat_4_for_the_same_notebook() -> None:
"""The two layouts describe the same document and must produce the same Markdown."""
assert _convert(_NBFORMAT_3).markdown == _convert(_NBFORMAT_4).markdown
def test_nbformat_3_converts_through_the_public_entry_point() -> None:
"""The whole stack must see the content, not fall through to plain text."""
data = json.dumps(_NBFORMAT_3).encode("utf-8")
result = MarkItDown().convert_stream(
io.BytesIO(data), stream_info=StreamInfo(extension=".ipynb")
)
assert "print('hello')" in result.markdown
assert "Revenue grew." in result.markdown
def test_nbformat_3_with_several_worksheets_keeps_every_cell() -> None:
notebook = {
"metadata": {},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{"cells": [{"cell_type": "code", "input": ["first = 1\n"], "outputs": []}]},
{
"cells": [
{"cell_type": "code", "input": ["second = 2\n"], "outputs": []}
]
},
],
}
markdown = _convert(notebook).markdown
assert "first = 1" in markdown
assert "second = 2" in markdown
def test_nbformat_3_title_from_metadata_still_wins() -> None:
notebook = dict(_NBFORMAT_3, metadata={"title": "From metadata"})
assert _convert(notebook).title == "From metadata"
def test_nbformat_4_is_unchanged() -> None:
"""The nbformat 4 path must be untouched by this change."""
result = _convert(_NBFORMAT_4)
assert "```python\nprint('hello')" in result.markdown
assert result.title == "Quarterly notes"
def test_nbformat_3_heading_cells_are_kept_as_markdown_headings() -> None:
"""v3 heading cells must not vanish; they map to the '#' form that v4 uses."""
notebook = {
"metadata": {},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"source": ["Quarterly notes\n"],
},
{"cell_type": "code", "input": ["print('hello')\n"], "outputs": []},
]
}
],
}
result = _convert(notebook)
assert "# Quarterly notes" in result.markdown
assert result.title == "Quarterly notes"
def test_nbformat_3_single_string_source_still_yields_title() -> None:
notebook = {
"metadata": {},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": "# Quarterly notes\n\nRevenue grew.\n",
}
]
}
],
}
result = _convert(notebook)
assert result.title == "Quarterly notes"
assert "Revenue grew." in result.markdown
def test_null_cells_fall_back_to_worksheets() -> None:
notebook = dict(_NBFORMAT_3, cells=None)
result = _convert(notebook)
assert "print('hello')" in result.markdown
assert "Revenue grew." in result.markdown