Debian Codegraph @

tests/functional/simple_notebooks/test_ipynb_to_myst.py

raw symbols SCIP
1 import jsonscip-python python python-stdlib 3.11 json/__init__:
2 import unittest.mockscip-python python python-stdlib 3.11 `unittest.mock`/__init__: as mock
3 from textwrapscip-python python python-stdlib 3.11 textwrap/__init__: import dedentscip-python python python-stdlib 3.11 textwrap/dedent().
4
5 import pytestscip-python python pytest 9.1.1 pytest/__init__:
6 from nbformat.v4.nbbasescip-python python nbformat 5.10.4 `nbformat.v4.nbbase`/__init__: import new_notebookscip-python python nbformat 5.10.4 `nbformat.v4.nbbase`/new_notebook().
7 from tornado.webscip-python python tornado 6.5.5 `tornado.web`/__init__: import HTTPError
8
9 import jupytext
10 from jupytext.cliscip-python python jupytext 1.16.4 `jupytext.cli`/__init__: import jupytext as jupytext_cli
11 from jupytext.comparescip-python python jupytext 1.16.4 `jupytext.compare`/__init__: import compare
12 from jupytext.formatsscip-python python jupytext 1.16.4 `jupytext.formats`/__init__: import (
13 JupytextFormatError,
14 get_format_implementation,
15 guess_format,
16 )
17 from jupytext.mystscip-python python jupytext 1.16.4 `jupytext.myst`/__init__: import (
18 CODE_DIRECTIVE,
19 MystMetadataParsingError,
20 matches_mystnb,
21 myst_extensions,
22 myst_to_notebook,
23 )
24
25
26 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
27 def test_bad_notebook_metadatascip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_bad_notebook_metadata().():
28 """Test exception raised if notebook metadata cannot be parsed."""
29 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(MystMetadataParsingError):
30 myst_to_notebook(
31 dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
32 """\
33 ---
34 {{a
35 ---
36 """
37 )
38 )
39
40
41 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
42 def test_bad_code_metadatascip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_bad_code_metadata().():
43 """Test exception raised if cell metadata cannot be parsed."""
44 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(MystMetadataParsingError):
45 myst_to_notebook(
46 dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
47 """\
48 ```{0}
49 ---
50 {{a
51 ---
52 ```
53 """
54 ).format(CODE_DIRECTIVE)
55 )
56
57
58 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
59 def test_bad_markdown_metadatascip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_bad_markdown_metadata().():
60 """Test exception raised if markdown metadata cannot be parsed."""
61 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(MystMetadataParsingError):
62 myst_to_notebook(
63 dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
64 """\
65 +++ {{a
66 """
67 )
68 )
69
70
71 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
72 def test_bad_markdown_metadata2scip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_bad_markdown_metadata2().():
73 """Test exception raised if markdown metadata is not a dict."""
74 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(MystMetadataParsingError):
75 myst_to_notebook(
76 dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
77 """\
78 +++ [1, 2]
79 """
80 )
81 )
82
83
84 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
85 def test_matches_mystnbscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_matches_mystnb().():
86 assert matches_mystnb("") is False
87 assert matches_mystnb("```{code-cell}\n```") is False
88 assert matches_mystnb("---\njupytext: true\n---") is False
89 for ext in myst_extensions(no_md=True):
90 assert matches_mystnb("", ext=ext) is True
91 text = dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
92 """\
93 ---
94 {{a
95 ---
96 ```{code-cell}
97 :b: {{c
98 ```
99 """
100 )
101 assert matches_mystnb(text) is True
102 text = dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
103 """\
104 ---
105 jupytext:
106 text_representation:
107 format_name: myst
108 extension: .md
109 ---
110 """
111 )
112 assert matches_mystnb(text) is True
113 text = dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
114 """\
115 ---
116 a: 1
117 ---
118 > ```{code-cell}
119 ```
120 """
121 )
122 assert matches_mystnb(text) is True
123 assert guess_format(text, ".md") == ("myst", {})
124
125
126 def test_not_installedscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_not_installed().():
127 with mock.patch("jupytext.formats.JUPYTEXT_FORMATS", return_value=[]):
128 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(JupytextFormatError):
129 get_format_implementation(".myst")
130
131
132 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
133 def test_add_source_mapscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_add_source_map().():
134 notebook = myst_to_notebook(
135 dedentscip-python python python-stdlib 3.11 textwrap/dedent().(
136 """\
137 ---
138 a: 1
139 ---
140 abc
141 +++
142 def
143 ```{0}
144 ---
145 b: 2
146 ---
147 c = 3
148 ```
149 xyz
150 """
151 ).format(CODE_DIRECTIVE),
152 add_source_map=True,
153 )
154 assert notebook.metadata.source_map == [3, 5, 7, 12]
155
156
157 PLEASE_INSTALL_MYSTscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/PLEASE_INSTALL_MYST. = "The MyST Markdown format requires .*"
158
159
160 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_no_myst
161 def test_meaningfull_error_write_myst_missingscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_write_myst_missing().(tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_write_myst_missing().(tmpdir)):
162 nb_file = tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_write_myst_missing().(tmpdir).join("notebook.ipynb")
163 jupytext.write(new_notebookscip-python python nbformat 5.10.4 `nbformat.v4.nbbase`/new_notebook().(), strscip-python python python-stdlib 3.11 builtins/str#(nb_file))
164
165 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#, match=PLEASE_INSTALL_MYSTscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/PLEASE_INSTALL_MYST.):
166 jupytext_cli([strscip-python python python-stdlib 3.11 builtins/str#(nb_file), "--to", "md:myst"])
167
168
169 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_no_myst
170 def test_meaningfull_error_open_myst_missingscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_open_myst_missing().(tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_open_myst_missing().(tmpdir)):
171 md_file = tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_open_myst_missing().(tmpdir).join("notebook.md")
172 md_file.write(
173 """---
174 jupytext:
175 text_representation:
176 extension: '.md'
177 format_name: myst
178 kernelspec:
179 display_name: Python 3
180 language: python
181 name: python3
182 ---
183
184 1 + 1
185 """
186 )
187
188 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#, match=PLEASE_INSTALL_MYSTscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/PLEASE_INSTALL_MYST.):
189 jupytext_cli([strscip-python python python-stdlib 3.11 builtins/str#(md_file), "--to", "ipynb"])
190
191 cm = jupytext.TextFileContentsManager()
192 cm.root_dir = strscip-python python python-stdlib 3.11 builtins/str#(tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_meaningfull_error_open_myst_missing().(tmpdir))
193
194 with pytestscip-python python pytest 9.1.1 pytest/__init__:.raises(HTTPError, match=PLEASE_INSTALL_MYSTscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/PLEASE_INSTALL_MYST.):
195 cm.get("notebook.md")
196
197
198 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.requires_myst
199 @pytestscip-python python pytest 9.1.1 pytest/__init__:.mark.parametrize("language_info", ["none", "std", "no_pygments_lexer"])
200 def test_myst_representation_same_cli_or_contents_managerscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(
201 tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(tmpdir), cwd_tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(cwd_tmpdir), notebook_with_outputsscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(notebook_with_outputs), language_infoscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(language_info)
202 ):
203 """This test gives some information on #759. As of Jupytext 1.11.1, in the MyST Markdown format,
204 the code cells have an ipython3 lexer when the notebook "language_info" metadata has "ipython3"
205 as the pygments_lexer. This information comes from the kernel and ATM it is not clear how the user
206 can choose to include it or not in the md file."""
207
208 nb = notebook_with_outputsscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(notebook_with_outputs)
209 if language_infoscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(language_info) != "none":
210 nb["metadata"]["language_info"] = {
211 "codemirror_mode": {"name": "ipython", "version": 3},
212 "file_extension": ".py",
213 "mimetype": "text/x-python",
214 "name": "python",
215 "nbconvert_exporter": "python",
216 "pygments_lexer": "ipython3",
217 "version": "3.7.3",
218 }
219 if language_infoscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(language_info) == "no_pygments_lexer":
220 del nb["metadata"]["language_info"]["pygments_lexer"]
221
222 # Writing the notebook with the Python API
223 text_api = jupytext.writes(nb, fmt="md:myst")
224
225 # How do code cells look like?
226 code_cells = {
227 line for line in text_api.splitlines() if line.startswith("```{code-cell")
228 }
229
230 if language_infoscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(language_info) == "std":
231 assert code_cells == {"```{code-cell} ipython3"}
232 else:
233 assert code_cells == {"```{code-cell}"}
234
235 # We get the same file with the command line jupytext
236 tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(tmpdir).mkdir("cli").join("notebook.ipynb").write(jsonscip-python python python-stdlib 3.11 json/__init__:.dumpsscip-python python python-stdlib 3.11 json/dumps().(nb))
237 jupytext_cli(["--to", "md:myst", "cli/notebook.ipynb"])
238 text_cli = tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(tmpdir).join("cli").join("notebook.md").read()
239
240 compare(text_cli, text_api)
241
242 # Or with the contents manager
243 cm = jupytext.TextFileContentsManager()
244 cm.formats = "ipynb,md:myst"
245 cm.root_dir = strscip-python python python-stdlib 3.11 builtins/str#(tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(tmpdir).mkdir("contents_manager"))
246
247 cm.save(model=dictscip-python python python-stdlib 3.11 builtins/dict#(content=nb, type="notebook"), path="notebook.ipynb")
248 text_cm = tmpdirscip-python python jupytext 1.16.4 `tests.functional.simple_notebooks.test_ipynb_to_myst`/test_myst_representation_same_cli_or_contents_manager().(tmpdir).join("contents_manager").join("notebook.md").read()
249
250 compare(text_cm, text_api)