How to use the jupytext.TextFileContentsManager 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_contentsmanager.py View on Github external
def test_vscode_pycharm_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 VScode/PyCharm folding markers
    cm.default_cell_markers = 'region,endregion'
    cm.default_jupytext_formats = 'ipynb,py'

    nb = new_notebook(cells=[new_code_cell("""# {{{
'''Sample cell with region markers'''
'''End of the cell'''
# }}}"""),
                             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)
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_notebook_extensions(tmpdir):
    tmp_py = str(tmpdir.join('script.py'))
    tmp_rmd = str(tmpdir.join('notebook.Rmd'))
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))

    nb = new_notebook()
    write(nb, tmp_py)
    write(nb, tmp_rmd)
    write(nb, tmp_ipynb)

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    cm.notebook_extensions = 'ipynb,Rmd'
    model = cm.get('notebook.ipynb')
    assert model['type'] == 'notebook'

    model = cm.get('notebook.Rmd')
    assert model['type'] == 'notebook'

    model = cm.get('script.py')
    assert model['type'] == 'file'
github mwouts / jupytext / tests / test_load_multiple.py View on Github external
tmp_nbpy = 'notebook.py'

    with open(str(tmpdir.join(tmp_nbpy)), 'w') as fp:
        fp.write("""# ---
# jupyter:
#   jupytext_formats: ipynb,py
#   jupytext_format_version: '1.2'
# ---

# New cell
""")

    nb = new_notebook(metadata={'jupytext_formats': 'ipynb,py'})
    jupytext.write(nb, str(tmpdir.join(tmp_ipynb)))

    cm = jupytext.TextFileContentsManager()
    cm.default_jupytext_formats = 'ipynb,py'
    cm.root_dir = str(tmpdir)

    nb = cm.get(tmp_ipynb)
    cells = nb['content']['cells']
    assert len(cells) == 1
    assert cells[0].cell_type == 'markdown'
    assert cells[0].source == 'New cell'
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_load_save_rename_nbpy_default_config(nb_file, tmpdir):
    tmp_ipynb = 'notebook.ipynb'
    tmp_nbpy = 'notebook.nb.py'

    cm = jupytext.TextFileContentsManager()
    cm.default_jupytext_formats = 'ipynb,.nb.py'
    cm.root_dir = str(tmpdir)

    # open ipynb, save nb.py, reopen
    nb = jupytext.read(nb_file)

    cm.save(model=dict(type='notebook', content=nb), path=tmp_nbpy)
    nbpy = cm.get(tmp_nbpy)
    compare_notebooks(nbpy['content'], nb)

    # open ipynb
    nbipynb = cm.get(tmp_ipynb)
    compare_notebooks(nbipynb['content'], nb)

    # save ipynb
    cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_load_save_percent_format(nb_file, tmpdir):
    tmp_py = 'notebook.py'
    with open(nb_file) as stream:
        text_py = stream.read()
    with open(str(tmpdir.join(tmp_py)), 'w') as stream:
        stream.write(text_py)

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # open python, save
    nb = cm.get(tmp_py)['content']
    del nb.metadata['jupytext']['notebook_metadata_filter']
    cm.save(model=dict(type='notebook', content=nb), path=tmp_py)

    # compare the new file with original one
    with open(str(tmpdir.join(tmp_py))) as stream:
        text_py2 = stream.read()

    # do we find 'percent' in the header?
    header = text_py2[:-len(text_py)]
    assert any(['percent' in line for line in header.splitlines()])

    # Remove the YAML header
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_save_to_light_percent_sphinx_format(nb_file, tmpdir):
    tmp_ipynb = 'notebook.ipynb'
    tmp_lgt_py = 'notebook.lgt.py'
    tmp_pct_py = 'notebook.pct.py'
    tmp_spx_py = 'notebook.spx.py'

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    nb = jupytext.read(nb_file)
    nb['metadata']['jupytext'] = {'formats': 'ipynb,.pct.py:percent,.lgt.py:light,.spx.py:sphinx'}

    # save to ipynb and three python flavors
    cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)

    # read files
    with open(str(tmpdir.join(tmp_pct_py))) as stream:
        assert read_format_from_metadata(stream.read(), '.py') == 'percent'

    with open(str(tmpdir.join(tmp_lgt_py))) as stream:
        assert read_format_from_metadata(stream.read(), '.py') == 'light'

    with open(str(tmpdir.join(tmp_spx_py))) as stream:
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_create_contentsmanager():
    jupytext.TextFileContentsManager()
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_python_kernel_preserves_R_files(nb_file, tmpdir):
    """Opening a R file with a Jupyter server that has no R kernel should not modify the file"""
    tmp_r_file = str(tmpdir.join('script.R'))
    with open(nb_file) as fp:
        script = fp.read()
    with open(tmp_r_file, 'w') as fp:
        fp.write(script)

    # create contents manager
    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # open notebook, set Python kernel and save
    model = cm.get('script.R')
    model['content'].metadata['kernelspec'] = kernelspec_from_language('python')
    cm.save(model=model, path='script.R')

    with open(tmp_r_file) as fp:
        script2 = fp.read()

    compare(script2, script)
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_set_then_change_auto_formats(tmpdir, nb_file):
    tmp_ipynb = str(tmpdir.join('nb.ipynb'))
    tmp_py = str(tmpdir.join('nb.py'))
    tmp_rmd = str(tmpdir.join('nb.Rmd'))
    nb = new_notebook(metadata=read(nb_file).metadata)

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # Pair ipynb/py and save
    nb.metadata['jupytext'] = {'formats': 'ipynb,auto:light'}
    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
    assert 'nb.py' in cm.paired_notebooks
    assert 'nb.auto' not in cm.paired_notebooks
    assert os.path.isfile(tmp_py)
    assert read(tmp_ipynb).metadata['jupytext']['formats'] == 'ipynb,py:light'

    # Pair ipynb/Rmd and save
    time.sleep(0.5)
    nb.metadata['jupytext'] = {'formats': 'ipynb,Rmd'}
    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
    assert 'nb.Rmd' in cm.paired_notebooks
    assert 'nb.py' not in cm.paired_notebooks
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_save_load_paired_md_pandoc_notebook(nb_file, tmpdir):
    tmp_ipynb = 'notebook.ipynb'
    tmp_md = 'notebook.md'

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # open ipynb, save with cm, reopen
    nb = jupytext.read(nb_file)
    nb.metadata['jupytext'] = {'formats': 'ipynb,md:pandoc'}

    cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb)
    nb_md = cm.get(tmp_md)

    compare_notebooks(nb_md['content'], nb, 'md:pandoc')
    assert nb_md['content'].metadata['jupytext']['formats'] == 'ipynb,md:pandoc'