def test_header_in_html_comment(): text = """<!-- --- jupyter: title: Sample header --- --> """ lines = text.splitlines() metadata, _, cell, _ = header_to_metadata_and_cell(lines, "") assert metadata == {"title": "Sample header"} assert cell is None
def test_header_to_metadata_and_cell_no_blank_line(): text = """--- title: Sample header --- Header is not followed by a blank line """ lines = text.splitlines() metadata, _, cell, pos = header_to_metadata_and_cell(lines, '') assert metadata == {} assert cell.cell_type == 'raw' assert cell.source == """--- title: Sample header ---""" assert cell.metadata == {'lines_to_next_cell': 0} assert lines[pos].startswith('Header is')
def test_header_to_metadata_and_cell_metadata(): text = """--- title: Sample header jupyter: mainlanguage: python --- """ lines = text.splitlines() metadata, _, cell, pos = header_to_metadata_and_cell(lines, '') assert metadata == {'mainlanguage': 'python'} assert cell.cell_type == 'raw' assert cell.source == """--- title: Sample header ---""" assert cell.metadata == {'lines_to_next_cell': 0} assert pos == len(lines)
def test_header_to_metadata_and_cell_metadata(): text = """--- title: Sample header jupyter: mainlanguage: python --- """ lines = text.splitlines() metadata, _, cell, pos = header_to_metadata_and_cell(lines, "") assert metadata == {"mainlanguage": "python"} assert cell.cell_type == "raw" assert (cell.source == """--- title: Sample header ---""") assert cell.metadata == {"lines_to_next_cell": 0} assert pos == len(lines)
def test_header_to_metadata_and_cell_blank_line(): text = """--- title: Sample header --- Header is followed by a blank line """ lines = text.splitlines() metadata, _, cell, pos = header_to_metadata_and_cell(lines, "") assert metadata == {} assert cell.cell_type == "raw" assert (cell.source == """--- title: Sample header ---""") assert cell.metadata == {} assert lines[pos].startswith("Header is")
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_jupytext_markdown_similar_to_nbconvert(nb_file): """Test that the nbconvert export for a notebook matches Jupytext's one""" nb = jupytext.read(nb_file) # Remove cell outputs and metadata for cell in nb.cells: cell.outputs = [] cell.execution_count = None cell.metadata = {} md_jupytext = jupytext.writes(nb, fmt="md") import nbconvert md_nbconvert, _ = nbconvert.export(nbconvert.MarkdownExporter, nb) # our expectations # nbconvert file has no YAML header md_jupytext_lines = md_jupytext.splitlines() _, _, raw_cell, pos = header_to_metadata_and_cell(md_jupytext_lines, "") md_jupytext = "\n".join(md_jupytext_lines[pos:]) + "\n" if raw_cell is not None: md_jupytext = raw_cell.source + "\n\n" + md_jupytext # region comments are not in nbconvert md_jupytext = md_jupytext.replace("<!-- #region -->\n", "").replace("<!-- #endregion -->\n", "") # Jupytext uses HTML comments to keep track of raw cells md_jupytext = (md_jupytext.replace("\n<!-- #raw -->\n", "").replace( "<!-- #raw -->\n", "").replace("\n<!-- #endraw -->\n", "")) # nbconvert file may start with an empty line md_jupytext = md_jupytext.lstrip("\n") md_nbconvert = md_nbconvert.lstrip("\n") # Jupytext may not always have two blank lines between cells like Jupyter nbconvert md_jupytext = md_jupytext.replace("\n\n\n", "\n\n") md_nbconvert = md_nbconvert.replace("\n\n\n", "\n\n") jupytext.compare.compare(md_nbconvert, md_jupytext)
def test_save_to_percent_format(nb_file, tmpdir): tmp_ipynb = 'notebook.ipynb' tmp_jl = 'notebook.jl' cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) cm.preferred_jupytext_formats_save = 'jl:percent' nb = jupytext.readf(nb_file) nb['metadata']['jupytext'] = {'formats': 'ipynb,jl'} # save to ipynb and jl cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb) # read jl file with open(str(tmpdir.join(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_save_to_percent_format(nb_file, tmpdir): tmp_ipynb = 'notebook.ipynb' tmp_jl = 'notebook.jl' nb = jupytext.readf(nb_file) cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) cm.preferred_jupytext_formats_save = 'jl:percent' nb['metadata']['jupytext_formats'] = 'ipynb,jl' # open python, save with mock.patch('jupytext.header.INSERT_AND_CHECK_VERSION_NUMBER', True): cm.save(model=dict(type='notebook', content=nb), path=tmp_ipynb) # read jl file with open(str(tmpdir.join(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'