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(tmpdir):
nb_file = str(tmpdir.join('notebook.ipynb'))
md_file = str(tmpdir.join('notebook.md'))
nbformat.write(new_notebook(cells=[new_markdown_cell('Some text')]), nb_file)
nb = jupytext.read(nb_file)
jupytext.write(nb, md_file)
with open(md_file) as fp:
text = fp.read()
assert 'Some text' in text.splitlines()
def test_inactive_cell_using_noeval(text='''This is text
```python .noeval
# This is python code.
# It should not become a code cell
'''): expected = new_notebook(cells=[new_markdown_cell(text[:-1])]) nb = jupytext.reads(text, 'md') compare_notebooks(nb, expected) text2 = jupytext.writes(nb, 'md') compare(text2, text)
def test_default_cell_markers_in_contents_manager(tmpdir):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_py = str(tmpdir.join('notebook.py'))
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.default_cell_markers = "'''"
nb = new_notebook(cells=[new_code_cell('1 + 1'), new_markdown_cell('a\nlong\ncell')],
metadata={'jupytext': {'formats': 'ipynb,py:percent',
'notebook_metadata_filter': '-all'}})
cm.save(model=dict(type='notebook', content=nb), path='notebook.ipynb')
assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
with open(tmp_py) as fp:
text = fp.read()
compare(text, """# %%
1 + 1
# %% [markdown]
'''
a
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_markdown_cell_with_code_works(nb=new_notebook(cells=[
new_markdown_cell("""```python
1 + 1
```""")])):
text = jupytext.writes(nb, 'Rmd')
nb2 = jupytext.reads(text, 'Rmd')
compare_notebooks(nb2, nb)
def nb(text):
return new_notebook(cells=[new_markdown_cell(text)],
metadata={'jupytext': {'formats': 'ipynb,md,py'}})
def test_cell_markers_option_in_contents_manager(tmpdir):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_py = str(tmpdir.join('notebook.py'))
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
nb = new_notebook(cells=[new_code_cell('1 + 1'), new_markdown_cell('a\nlong\ncell')],
metadata={'jupytext': {'formats': 'ipynb,py:percent',
'notebook_metadata_filter': '-all',
'cell_markers': "'''"}})
cm.save(model=dict(type='notebook', content=nb), path='notebook.ipynb')
assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
with open(tmp_py) as fp:
text = fp.read()
compare(text, """# %%
1 + 1
# %% [markdown]
'''
def test_noeval_followed_by_code_works(text='''```python .noeval
# Not a code cell in Jupyter
1 + 1
''', expected=new_notebook(cells=[new_markdown_cell('''```python .noeval
new_code_cell('1 + 1')])):
nb = jupytext.reads(text, 'md')
compare_notebooks(nb, expected)
text2 = jupytext.writes(nb, 'md')
compare(text2, text)
def test_rst2md_option(tmpdir):
tmp_py = str(tmpdir.join('notebook.py'))
# Write notebook in sphinx format
nb = new_notebook(cells=[new_markdown_cell('A short sphinx notebook'),
new_markdown_cell(':math:`1+1`')])
write(nb, tmp_py, fmt='py:sphinx')
cm = jupytext.TextFileContentsManager()
cm.sphinx_convert_rst2md = True
cm.root_dir = str(tmpdir)
nb2 = cm.get('notebook.py')['content']
# Was rst to md conversion effective?
assert nb2.cells[2].source == '$1+1$'
assert nb2.metadata['jupytext']['rst2md'] is False
def add_book_info():
for nb_name in iter_notebooks():
nb_file = os.path.join(NOTEBOOK_DIR, nb_name)
nb = nbformat.read(nb_file, as_version=4)
is_comment = lambda cell: cell.source.startswith(BOOK_COMMENT)
if is_comment(nb.cells[0]):
print('- amending comment for {0}'.format(nb_name))
nb.cells[0].source = BOOK_INFO
else:
print('- inserting comment for {0}'.format(nb_name))
nb.cells.insert(0, new_markdown_cell(BOOK_INFO))
nbformat.write(nb, nb_file)