예제 #1
0
def test_load_then_change_formats(tmpdir):
    tmp_ipynb = str(tmpdir.join('nb.ipynb'))
    tmp_py = str(tmpdir.join('nb.py'))
    nb = new_notebook(metadata={'jupytext': {'formats': 'ipynb,py:light'}})
    writef(nb, tmp_ipynb)

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

    model = cm.get('nb.ipynb')
    assert model['content'].metadata['jupytext']['formats'] == 'ipynb,py:light'

    cm.save(model, path='nb.ipynb')
    assert os.path.isfile(tmp_py)
    assert readf(tmp_py).metadata['jupytext']['formats'] == 'ipynb,py:light'

    time.sleep(0.5)
    del model['content'].metadata['jupytext']['formats']
    cm.save(model, path='nb.ipynb')
    # test that we have not kept the 'ipynb/py' pairing info, and that we can read the ipynb
    cm.get('nb.ipynb')
    os.remove(tmp_py)

    model['content'].metadata.setdefault('jupytext',
                                         {})['formats'] = 'ipynb,py:percent'
    cm.save(model, path='nb.ipynb')
    assert os.path.isfile(tmp_py)
    assert readf(tmp_py).metadata['jupytext']['formats'] == 'ipynb,py:percent'
    os.remove(tmp_py)

    del model['content'].metadata['jupytext']['formats']
    cm.save(model, path='nb.ipynb')
    assert not os.path.isfile(tmp_py)
예제 #2
0
def test_save_using_preferred_and_default_format_170(nb_file, tmpdir):
    nb = readf(nb_file)

    # Way 0: preferred_jupytext_formats_save, no prefix + default_jupytext_formats
    tmp_py = str(tmpdir.join('python/notebook.py'))

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.preferred_jupytext_formats_save = 'py:percent'
    cm.default_jupytext_formats = "ipynb,python//py"

    # save to ipynb and oy
    cm.save(model=dict(type='notebook', content=nb), path='notebook.ipynb')

    # read py file
    nb_py = readf(tmp_py)
    assert nb_py.metadata['jupytext']['text_representation'][
        'format_name'] == 'percent'

    # Way 1: preferred_jupytext_formats_save + default_jupytext_formats
    tmp_py = str(tmpdir.join('python/notebook.py'))

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.preferred_jupytext_formats_save = 'python//py:percent'
    cm.default_jupytext_formats = "ipynb,python//py"

    # save to ipynb and oy
    cm.save(model=dict(type='notebook', content=nb), path='notebook.ipynb')

    # read py file
    nb_py = readf(tmp_py)
    assert nb_py.metadata['jupytext']['text_representation'][
        'format_name'] == 'percent'

    # Way 2: default_jupytext_formats
    tmp_py = str(tmpdir.join('python/notebook.py'))

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)
    cm.default_jupytext_formats = "ipynb,python//py:percent"

    # save to ipynb and py
    cm.save(model=dict(type='notebook', content=nb), path='notebook.ipynb')

    # read py file
    nb_py = readf(tmp_py)
    assert nb_py.metadata['jupytext']['text_representation'][
        'format_name'] == 'percent'
예제 #3
0
def test_share_py_recreate_ipynb(tmpdir, nb_file):
    tmp_ipynb = str(tmpdir.join('nb.ipynb'))
    tmp_py = str(tmpdir.join('nb.py'))

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

    # set default py format
    cm.preferred_jupytext_formats_save = "py:percent"

    # every new file is paired
    cm.default_jupytext_formats = "ipynb,py"

    # the text files don't need a YAML header
    cm.default_notebook_metadata_filter = "-all"
    cm.default_cell_metadata_filter = "-all"

    nb = readf(nb_file)
    model_ipynb = cm.save(model=dict(content=nb, type='notebook'),
                          path='nb.ipynb')

    assert os.path.isfile(tmp_ipynb)
    assert os.path.isfile(tmp_py)

    os.remove(tmp_ipynb)

    # reopen and save nb.py
    model = cm.get('nb.py')
    cm.save(model=model, path='nb.py')

    # ipynb is re-created
    assert os.path.isfile(tmp_ipynb)

    # save time of ipynb is that of py file
    assert model_ipynb['last_modified'] == model['last_modified']
예제 #4
0
def test_set_then_change_formats(tmpdir):
    tmp_py = str(tmpdir.join('nb.py'))
    nb = new_notebook(metadata={'jupytext': {'formats': 'ipynb,py:light'}})

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

    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
    assert os.path.isfile(tmp_py)
    assert readf(tmp_py).metadata['jupytext']['formats'] == 'ipynb,py:light'
    os.remove(tmp_py)

    nb.metadata['jupytext']['formats'] = 'ipynb,py:percent'
    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
    assert os.path.isfile(tmp_py)
    assert readf(tmp_py).metadata['jupytext']['formats'] == 'ipynb,py:percent'
    os.remove(tmp_py)

    del nb.metadata['jupytext']['formats']
    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
    assert not os.path.isfile(tmp_py)
예제 #5
0
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=readf(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 readf(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
    assert 'nb.auto' not in cm.paired_notebooks
    assert os.path.isfile(tmp_rmd)
    assert readf(tmp_ipynb).metadata['jupytext']['formats'] == 'ipynb,Rmd'
    cm.get('nb.ipynb')

    # Unpair and save
    time.sleep(0.5)
    del nb.metadata['jupytext']
    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')
    assert 'nb.Rmd' not in cm.paired_notebooks
    assert 'nb.py' not in cm.paired_notebooks
    assert 'nb.auto' not in cm.paired_notebooks
    cm.get('nb.ipynb')
예제 #6
0
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)

    nb2 = cm.get('nb.ipynb')['content']
    compare_notebooks(nb, nb2)

    nb3 = readf(tmp_py)
    assert nb3.metadata['jupytext']['cell_markers'] == 'region,endregion'

    with open(tmp_py) as fp:
        text = fp.read()

    # Remove YAML header
    text = re.sub(re.compile(r'# ---.*# ---\n\n', re.DOTALL), '', text)

    compare(
        """# {{{
'''Sample cell with region markers'''
'''End of the cell'''
# }}}

# region
a = 1


b = 1
# endregion
""", text)