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'}}) write(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 read(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 read(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)
def test_hide_notebook_metadata(tmpdir, no_jupytext_version_number): tmpdir.join(".jupytext").write("hide_notebook_metadata = true") tmp_ipynb = tmpdir.join("notebook.ipynb") tmp_md = tmpdir.join("notebook.md") nb = new_notebook(cells=[new_code_cell("1 + 1")], metadata={"jupytext": { "formats": "ipynb,md" }}) write(nb, str(tmp_ipynb)) jupytext([str(tmp_ipynb), "--sync"]) with open(str(tmp_md)) as stream: text_md = stream.read() compare( text_md, """<!-- --- jupyter: jupytext: formats: ipynb,md hide_notebook_metadata: true --- --> ```python 1 + 1 ``` """, )
def test_sync_config_does_not_create_formats_metadata(tmpdir, cwd_tmpdir, python_notebook): tmpdir.join("jupytext.yml").write("""formats: "ipynb,py:percent" """) write(python_notebook, "test.ipynb") jupytext(["--sync", "test.ipynb"]) nb = read("test.py") assert "formats" not in nb.metadata["jupytext"]
def test_save_using_preferred_and_default_format(config, tmpdir): tmpdir.join(".jupytext.yml").write(config) tmp_ipynb = tmpdir.join("notebook.ipynb") tmp_py = tmpdir.join("python").join("notebook.py") nb = new_notebook(cells=[new_code_cell("1 + 1")]) write(nb, str(tmp_ipynb)) jupytext([str(tmp_ipynb), "--sync"]) # read py file nb_py = read(str(tmp_py)) assert nb_py.metadata["jupytext"]["text_representation"]["format_name"] == "percent"
def test_download_file_318(tmpdir): tmp_ipynb = str(tmpdir.join('notebook.ipynb')) tmp_py = str(tmpdir.join('notebook.py')) nb = new_notebook() nb.metadata['jupytext'] = {'formats': 'ipynb,py'} write(nb, tmp_ipynb) write(nb, tmp_py) cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) cm.notebook_extensions = 'ipynb' model = cm.get('notebook.ipynb', content=True, type=None, format=None) assert model['type'] == 'notebook'
def test_markdown_and_r_extensions(tmpdir): tmp_r = str(tmpdir.join('script.r')) tmp_markdown = str(tmpdir.join('notebook.markdown')) nb = new_notebook() write(nb, tmp_r) write(nb, tmp_markdown) cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) model = cm.get('script.r') assert model['type'] == 'notebook' model = cm.get('notebook.markdown') assert model['type'] == 'notebook'
def test_preferred_jupytext_formats_save(tmpdir): tmpdir.join(".jupytext.yml").write("preferred_jupytext_formats_save: jl:percent") tmp_ipynb = tmpdir.join("notebook.ipynb") tmp_jl = tmpdir.join("notebook.jl") nb = new_notebook( cells=[new_code_cell("1 + 1")], metadata={"jupytext": {"formats": "ipynb,jl"}} ) write(nb, str(tmp_ipynb)) jupytext([str(tmp_ipynb), "--sync"]) with open(str(tmp_jl)) as stream: text_jl = stream.read() # Parse the YAML header metadata, _, _, _ = header_to_metadata_and_cell(text_jl.splitlines(), "#") assert metadata["jupytext"]["formats"] == "ipynb,jl:percent"
def test_multiple_formats_771(tmpdir, cwd_tmpdir, python_notebook): tmpdir.join("jupytext.toml").write( """formats = "notebooks///ipynb,notebooks///py,scripts///py:percent" """) notebooks_dir = tmpdir.mkdir("notebooks") scripts_dir = tmpdir.join("scripts") write(python_notebook, str(notebooks_dir.join("notebook.ipynb"))) jupytext(["--sync", "notebooks/notebook.ipynb"]) assert notebooks_dir.join("notebook.py").isfile() assert scripts_dir.join("notebook.py").isfile() notebooks_dir.join("module.py").write("1 + 1\n") jupytext(["--sync", "notebooks/module.py"]) assert notebooks_dir.join("module.ipynb").isfile() assert scripts_dir.join("module.py").isfile()
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`') ]) write(nb, tmp_py, fmt='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
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'