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_error_update_not_ipynb(tmp_py):
with pytest.raises(ValueError, match='--update is only for ipynb files'):
jupytext([tmp_py, '--to', 'py', '--update'])
def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir):
# cannot encode magic parameters in markdown yet
if 'magic' in nb_file and fmt == 'md':
return
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
copyfile(nb_file, tmp_ipynb)
ext = long_form_one_format(fmt)['extension']
tmp_text = str(tmpdir.join('notebook' + ext))
jupytext(['--to', fmt, tmp_ipynb])
jupytext(['--to', 'ipynb', '--update', tmp_text])
nb_org = read(nb_file)
nb_now = read(tmp_ipynb)
compare(nb_now, nb_org)
def test_to_cpluplus(nb_file, tmpdir, capsys):
nb_org = str(tmpdir.join(os.path.basename(nb_file)))
copyfile(nb_file, nb_org)
nb1 = read(nb_org)
text_cpp = writes(nb1, 'cpp')
jupytext([nb_org, '--to', 'c++', '--output', '-'])
out, err = capsys.readouterr()
assert err == ''
compare(out, text_cpp)
def test_paired_paths(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
nb = read(nb_file)
nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,_light.py,_percent.py:percent'
write(nb, tmp_ipynb)
jupytext(['--paired-paths', tmp_ipynb])
out, err = capsys.readouterr()
assert not err
formats = nb.metadata.get('jupytext', {}).get('formats')
assert set(out.splitlines()).union([tmp_ipynb]) == set(
[path for path, _ in paired_paths(tmp_ipynb, 'ipynb', formats)])
def test_cli_can_infer_jupytext_format(nb_file, ext, tmpdir):
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
tmp_text = str(tmpdir.join('notebook' + ext))
nb = read(nb_file)
# Light format to Jupyter notebook
write(nb, tmp_text)
jupytext(['--to', 'notebook', tmp_text])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
# Percent format to Jupyter notebook
write(nb, tmp_text, ext + ':percent')
jupytext(['--to', 'notebook', tmp_text])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
def test_execute_readme_ok(tmpdir):
tmp_md = str(tmpdir.join('notebook.md'))
with open(tmp_md, 'w') as fp:
fp.write("""
A readme with correct instructions
```python
1 + 2
""")
jupytext(args=[tmp_md, '--execute'])
def test_utf8_out_331(capsys):
py = u"from IPython.core.display import HTML; HTML(u'\xd7')"
with mock.patch('sys.stdin', StringIO(py)):
jupytext(['--to', 'ipynb', '--execute', '-'])
out, err = capsys.readouterr()
assert err == ''
nb = reads(out, 'ipynb')
assert len(nb.cells) == 1
print(nb.cells[0].outputs)
assert nb.cells[0].outputs[0]['data']['text/html'] == u'\xd7'
def hook():
with mock.patch('jupytext.cli.system', system_in_tmpdir):
jupytext(['--to', 'py', '--pre-commit'])
def test_set_formats(py_file, tmpdir):
tmp_py = str(tmpdir.join('notebook.py'))
tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
copyfile(py_file, tmp_py)
jupytext([tmp_py, '--set-formats', 'ipynb,py:light'])
nb = read(tmp_ipynb)
assert nb.metadata['jupytext']['formats'] == 'ipynb,py:light'
def test_apply_black_through_jupytext(tmpdir, nb_file):
# Load real notebook metadata to get the 'auto' extension in --pipe-fmt to work
metadata = read(nb_file).metadata
nb_org = new_notebook(cells=[new_code_cell('1 +1')], metadata=metadata)
nb_black = new_notebook(cells=[new_code_cell('1 + 1')], metadata=metadata)
os.makedirs(str(tmpdir.join('notebook_folder')))
os.makedirs(str(tmpdir.join('script_folder')))
tmp_ipynb = str(tmpdir.join('notebook_folder').join('notebook.ipynb'))
tmp_py = str(tmpdir.join('script_folder').join('notebook.py'))
# Black in place
write(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--pipe', 'black'])
nb_now = read(tmp_ipynb)
compare(nb_now, nb_black)
# Write to another folder using dots
script_fmt = os.path.join('..', 'script_folder//py:percent')
write(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--to', script_fmt, '--pipe', 'black'])
assert os.path.isfile(tmp_py)
nb_now = read(tmp_py)
nb_now.metadata = metadata
compare(nb_now, nb_black)
os.remove(tmp_py)
# Map to another folder based on file name
write(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--from', 'notebook_folder//ipynb', '--to', 'script_folder//py:percent',