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_simple_hook_with_explicit_format(tmpdir):
nb_file = str(tmpdir.join('notebook.ipynb'))
py_file = str(tmpdir.join('notebook.py'))
nbformat.write(new_notebook(cells=[new_markdown_cell('Some text')]), nb_file)
nb = jupytext.read(nb_file)
jupytext.write(nb, py_file, fmt='py:percent')
with open(py_file) as fp:
text = fp.read()
assert '# %% [markdown]' in text.splitlines()
assert '# Some text' in text.splitlines()
def test_vim_folding_markers(tmpdir):
tmp_ipynb = str(tmpdir.join('nb.ipynb'))
tmp_py = str(tmpdir.join('nb.py'))
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# Default Vim folding markers
cm.default_cell_markers = '{{{,}}}'
cm.default_jupytext_formats = 'ipynb,py'
nb = new_notebook(cells=[new_code_cell("""# region
'''Sample cell with region markers'''
'''End of the cell'''
# end region"""),
new_code_cell('a = 1\n\n\nb = 1')])
cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
nb2 = cm.get('nb.ipynb')['content']
compare_notebooks(nb2, nb)
nb3 = read(tmp_py)
assert nb3.metadata['jupytext']['cell_markers'] == '{{{,}}}'
with open(tmp_py) as fp:
def test_cell_metadata_differ():
nb1 = new_notebook(cells=[new_code_cell('1'),
new_code_cell('2', metadata={'additional': 'metadata1'})])
nb2 = new_notebook(cells=[new_code_cell('1'),
new_code_cell('2', metadata={'additional': 'metadata2'})])
with pytest.raises(NotebookDifference) as exception_info:
compare_notebooks(nb2, nb1, raise_on_first_difference=False)
assert "Cell metadata 'additional' differ" in exception_info.value.args[0]
def test_single_triple_quote_works(no_jupytext_version_number, text='''# ---
# jupyter:
# jupytext:
# cell_markers: '"""'
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# ---
# %%
print("hello")
''', notebook=new_notebook(cells=[new_code_cell('print("hello")')])):
compare_notebooks(jupytext.reads(text, 'py'), notebook)
def round_trip_cell_metadata(cell_metadata):
nb = new_notebook(metadata={'jupytext': {'main_language': 'python'}},
cells=[new_code_cell('1 + 1', metadata=cell_metadata)])
text = jupytext.writes(nb, 'Rmd')
nb2 = jupytext.reads(text, 'Rmd')
compare_notebooks(nb2, nb)
def test_raise_on_different_metadata():
ref = new_notebook(metadata={'kernelspec': {'language': 'python', 'name': 'python', 'display_name': 'Python'}},
cells=[new_markdown_cell('Cell one')])
test = new_notebook(metadata={'kernelspec': {'language': 'R', 'name': 'R', 'display_name': 'R'}},
cells=[new_markdown_cell('Cell one')])
with pytest.raises(NotebookDifference):
compare_notebooks(test, ref, 'md')
def test_notebook_with_empty_cells(blank_cells):
notebook = new_notebook(cells=[new_markdown_cell('markdown cell one')] +
[new_code_cell('')] * blank_cells +
[new_markdown_cell('markdown cell two')] +
[new_code_cell('')] * blank_cells,
metadata={'jupytext': {'main_language': 'python'}})
script = jupytext.writes(notebook, 'py')
notebook2 = jupytext.reads(script, 'py')
compare_notebooks(notebook2, notebook)
def test_combine():
nb_source = new_notebook(
cells=[new_markdown_cell('Markdown text'),
new_code_cell('a=3'),
new_code_cell('a+1'),
new_code_cell('a+1'),
new_markdown_cell('Markdown text'),
new_code_cell('a+2')])
nb_outputs = new_notebook(
cells=[new_markdown_cell('Markdown text'),
new_code_cell('a=3'),
new_code_cell('a+1'),
new_code_cell('a+2'),
new_markdown_cell('Markdown text')])
nb_outputs.cells[2].outputs = ['4']
nb_outputs.cells[3].outputs = ['5']
def test_tags_in_rmd(rmd='''---
jupyter:
jupytext:
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.1'
jupytext_version: 1.2.3
---
```{python tags=c("parameters")}
p = 1
''', nb=new_notebook(cells=[new_code_cell('p = 1', metadata={'tags': ['parameters']})])): nb2 = jupytext.reads(rmd, 'Rmd') compare_notebooks(nb2, nb)
def write_nb(root, nb_name, cells):
"""Write a jupyter notebook to disk.
Takes a given a root directory, a notebook name, and a list of cells.
"""
nb = new_notebook(cells=cells,
metadata={
'language': 'python',
})
nb_path = os.path.join(root, '%s.ipynb' % nb_name)
with codecs.open(nb_path, encoding='utf-8', mode='w') as nb_file:
nbformat.write(nb, nb_file, NB_VERSION)
print("Created Jupyter notebook at:\n%s" % nb_path)