Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_notebook_metadata_differ():
nb1 = new_notebook(cells=[new_code_cell('1'),
new_code_cell('2')])
nb2 = new_notebook(cells=[new_code_cell('1'),
new_code_cell('2')],
metadata={'kernelspec': {'language': 'python', 'name': 'python', 'display_name': 'Python'}})
with pytest.raises(NotebookDifference) as exception_info:
compare_notebooks(nb2, nb1, raise_on_first_difference=False)
assert "Notebook metadata differ" in exception_info.value.args[0]
def test_two_markdown_cell_with_code_works(nb=new_notebook(cells=[
new_markdown_cell("""```python
1 + 1
```"""),
new_markdown_cell("""```python
2 + 2
```""")
])):
text = jupytext.writes(nb, 'md')
nb2 = jupytext.reads(text, 'md')
compare_notebooks(nb2, nb)
compare_notebooks(read(tmp_py), nb)
assert os.path.isfile(tmp_rmd)
compare_notebooks(read(tmp_rmd), nb, 'Rmd')
write(nb, tmp_rmd, 'Rmd')
jupytext(['--sync', tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb, 'Rmd', compare_outputs=True)
write(nb, tmp_py, 'py')
jupytext(['--sync', tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb, compare_outputs=True)
# Finally we recreate the ipynb
os.remove(tmp_ipynb)
time.sleep(0.1)
jupytext(['--sync', tmp_py])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
# ipynb must be older than py file, otherwise our Contents Manager will complain
assert os.path.getmtime(tmp_ipynb) <= os.path.getmtime(tmp_py)
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)
# check that text representation exists, and is in percent format
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == 'percent'
# reload and compare with original notebook
model = cm.get(path=tmp_script)
# saving should not create a format entry #95
assert 'formats' not in model['content'].metadata.get('jupytext', {})
compare_notebooks(model['content'], nb)
def test_identity_source_write_read(nb_file, ext):
"""
Test that writing the notebook with R, and read again,
is the same as removing outputs
"""
with open(nb_file) as fp:
nb1 = nbformat.read(fp, as_version=4)
R = jupytext.writes(nb1, ext)
nb2 = jupytext.reads(R, ext)
compare_notebooks(nb2, nb1)
model = cm.get(tmp_nbpy)
# Check that format is explicit
assert model['content']['metadata']['jupytext']['formats'] == 'ipynb,py:light'
# Check contents
compare_notebooks(model['content'], nb)
# Change save format and save
model['content']['metadata']['jupytext']['formats'] == 'ipynb,py'
cm.preferred_jupytext_formats_save = 'py:percent'
cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)
# Read notebook
model = cm.get(tmp_nbpy)
compare_notebooks(model['content'], nb)
# Check that format is explicit
assert model['content']['metadata']['jupytext']['formats'] == 'ipynb,py:percent'
compare_notebooks(nb2, nb, 'Rmd', compare_outputs=True)
write(nb, tmp_py, 'py')
jupytext(['--sync', tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb, compare_outputs=True)
# Finally we recreate the ipynb
os.remove(tmp_ipynb)
time.sleep(0.1)
jupytext(['--sync', tmp_py])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
# ipynb must be older than py file, otherwise our Contents Manager will complain
assert os.path.getmtime(tmp_ipynb) <= os.path.getmtime(tmp_py)
def test_read_file_with_explicit_fmt(tmpdir):
tmp_py = str(tmpdir.join('notebook.py'))
with open(tmp_py, 'w') as fp:
fp.write("""# %%
1 + 1
""")
nb1 = jupytext.read(tmp_py)
nb2 = jupytext.read(tmp_py, fmt='py:percent')
compare_notebooks(nb2, nb1)
def test_raise_on_different_cell_content(raise_on_first_difference):
ref = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Cell two')])
test = new_notebook(cells=[new_markdown_cell('Cell one'), new_code_cell('Modified cell two')])
with pytest.raises(NotebookDifference):
compare_notebooks(test, ref, 'md', raise_on_first_difference=raise_on_first_difference)
def test_two_markdown_cell_with_no_language_code_works(nb=new_notebook(cells=[
new_markdown_cell("""```
1 + 1
```"""),
new_markdown_cell("""```
2 + 2
```""")
])):
text = jupytext.writes(nb, 'md')
nb2 = jupytext.reads(text, 'md')
compare_notebooks(nb2, nb)