How to use the jupytext.write function in jupytext

To help you get started, we’ve selected a few jupytext examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mwouts / jupytext / tests / test_cli.py View on Github external
tmp_ipynb = str(tmpdir.join('nb with spaces.ipynb'))
    tmp_py = str(tmpdir.join('nb with spaces.py'))
    nb = new_notebook(cells=[])

    git = git_in_tmpdir(tmpdir)
    git('init')
    git('status')
    hook = str(tmpdir.join('.git/hooks/pre-commit'))
    with open(hook, 'w') as fp:
        fp.write('#!/bin/sh\n'
                 'jupytext --to py:light --pre-commit\n')

    st = os.stat(hook)
    os.chmod(hook, st.st_mode | stat.S_IEXEC)

    write(nb, tmp_ipynb)
    assert os.path.isfile(tmp_ipynb)
    assert not os.path.isfile(tmp_py)

    git('add', 'nb with spaces.ipynb')
    git('status')
    git('commit', '-m', 'created')
    git('status')

    assert 'nb with spaces.py' in git('ls-tree', '-r', 'master', '--name-only')
    assert os.path.isfile(tmp_py)
github mwouts / jupytext / tests / test_read_write_functions.py View on Github external
def test_no_error_on_path_object(tmpdir):
    nb_file = Path(str(tmpdir.join('notebook.ipynb')))
    md_file = nb_file.with_suffix('.md')
    nbformat.write(new_notebook(cells=[new_markdown_cell('Some text')]), str(nb_file))

    nb = jupytext.read(nb_file)
    jupytext.write(nb, md_file)
github mwouts / jupytext / tests / test_read_simple_nomarker.py View on Github external
def test_jupytext_cli_bare(tmpdir):
    tmp_py = str(tmpdir.join('test.py'))
    tmp_ipynb = str(tmpdir.join('test.ipynb'))
    write(new_notebook(cells=[new_code_cell('1 + 1')]), tmp_ipynb)
    with pytest.warns(DeprecationWarning, match='nomarker'):
        jupytext_cli([tmp_ipynb, '--to', 'py:bare'])
    assert os.path.isfile(tmp_py)
github mwouts / jupytext / tests / test_cli.py View on Github external
# Test that missing files are created
    jupytext(['--sync', tmp_ipynb])

    assert os.path.isfile(tmp_py)
    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)
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
cm.save(model=dict(type='notebook', content=nb('saved from cm')), path='notebook.ipynb')
    compare_notebooks(jupytext.read(tmp_ipynb), nb('saved from cm'))
    compare_notebooks(jupytext.read(tmp_md), nb('saved from cm'))
    compare_notebooks(jupytext.read(tmp_py), nb('saved from cm'))

    jupytext.write(nb('md edited'), tmp_md)
    model = cm.get('notebook.ipynb')
    compare_notebooks(model['content'], nb('md edited'))

    cm.save(model=model, path='notebook.ipynb')
    compare_notebooks(jupytext.read(tmp_ipynb), nb('md edited'))
    compare_notebooks(jupytext.read(tmp_md), nb('md edited'))
    compare_notebooks(jupytext.read(tmp_py), nb('md edited'))

    jupytext.write(nb('py edited'), tmp_py)

    # Loading the md file give the content of that file
    model = cm.get('notebook.md')
    compare_notebooks(model['content'], nb('md edited'))

    # Loading the ipynb files gives the content of the most recent text file
    model = cm.get('notebook.ipynb')
    compare_notebooks(model['content'], nb('py edited'))

    cm.save(model=model, path='notebook.ipynb')
    compare_notebooks(jupytext.read(tmp_ipynb), nb('py edited'))
    compare_notebooks(jupytext.read(tmp_md), nb('py edited'))
    compare_notebooks(jupytext.read(tmp_py), nb('py edited'))

    model_ipynb = cm.get('notebook.ipynb', content=False, load_alternative_format=False)
    model_md = cm.get('notebook.md', content=False, load_alternative_format=False)
github mwouts / jupytext / tests / test_black.py View on Github external
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',
              '--pipe', 'black', '--check', 'flake8'])
    assert os.path.isfile(tmp_py)
    nb_now = read(tmp_py)
    nb_now.metadata = metadata
    compare(nb_now, nb_black)
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_rename_inconsistent_path(tmpdir):
    org_file = str(tmpdir.join('notebook_suffix.ipynb'))
    new_file = str(tmpdir.join('new.ipynb'))
    jupytext.write(new_notebook(metadata={'jupytext': {'formats': '_suffix.ipynb'}}), org_file)

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    # Read notebook, and learn about its format
    cm.get('notebook_suffix.ipynb')
    with pytest.raises(HTTPError):
        cm.rename_file('notebook_suffix.ipynb', 'new.ipynb')

    assert not os.path.isfile(new_file)
    assert os.path.isfile(org_file)
github mwouts / jupytext / tests / test_read_simple_ipynb.py View on Github external
def test_save_ipynb_with_jupytext_has_final_newline(tmpdir):
    nb = new_notebook()
    file_jupytext = str(tmpdir.join('jupytext.ipynb'))
    file_nbformat = str(tmpdir.join('nbformat.ipynb'))

    jupytext.write(nb, file_jupytext)
    with open(file_nbformat, 'w') as fp:
        nbformat.write(nb, fp)

    with open(file_jupytext) as fp:
        text_jupytext = fp.read()

    with open(file_nbformat) as fp:
        text_nbformat = fp.read()

    compare(text_jupytext, text_nbformat)
github mwouts / jupytext / tests / test_read_write_functions.py View on Github external
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()
github mwouts / jupytext / tests / test_cli.py View on Github external
def tmp_ipynb(tmpdir):
    tmp_file = str(tmpdir.join('notebook.ipynb'))
    write(new_notebook(), tmp_file)
    return tmp_file