Example #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)
Example #2
0
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`')
    ])
    writef(nb, tmp_py, '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
Example #3
0
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()
    writef(nb, tmp_py)
    writef(nb, tmp_rmd)
    writef(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'