How to use the jupytext.read 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_cli.py View on Github external
def test_sync_pandoc(nb_file, tmpdir, capsys):
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
    tmp_md = str(tmpdir.join('notebook.md'))
    nb = read(nb_file)
    write(nb, tmp_ipynb)

    # Test that sync issues a warning when the notebook is not paired
    jupytext(['--sync', tmp_ipynb])
    _, err = capsys.readouterr()
    assert 'is not a paired notebook' in err

    # Now with a pairing information
    nb.metadata.setdefault('jupytext', {})['formats'] = 'ipynb,md:pandoc'
    write(nb, tmp_ipynb)

    # Test that missing files are created
    jupytext(['--sync', tmp_ipynb])

    assert os.path.isfile(tmp_md)
    compare_notebooks(read(tmp_md), nb, 'md:pandoc')
github mwouts / jupytext / tests / test_black.py View on Github external
def test_apply_black_on_python_notebooks(nb_file, tmpdir):
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
    tmp_py = str(tmpdir.join('notebook.py'))
    copyfile(nb_file, tmp_ipynb)

    jupytext(args=[tmp_ipynb, '--to', 'py:percent'])
    system('black', tmp_py)
    jupytext(args=[tmp_py, '--to', 'ipynb', '--update'])

    nb1 = read(nb_file)
    nb2 = read(tmp_ipynb)
    nb3 = read(tmp_py)

    assert len(nb1.cells) == len(nb2.cells)
    assert len(nb1.cells) == len(nb3.cells)
    for c1, c2 in zip(nb1.cells, nb2.cells):
        # same content (almost)
        assert black_invariant(c1.source) == black_invariant(c2.source)
        # python representation is pep8
        assert 'lines_to_next_cell' not in c2.metadata
        # outputs are preserved
        assert c1.cell_type == c2.cell_type
        if c1.cell_type == 'code':
            compare(c1.outputs, c2.outputs)

    compare(nb1.metadata, nb2.metadata)
github mwouts / jupytext / tests / test_lf_crlf.py View on Github external
def test_lf_file(tmpdir, text):
    tmp_md = str(tmpdir.join('file.md'))
    with open(tmp_md, 'w', newline='\n') as fp:
        fp.write(text)

    nb = jupytext.read(tmp_md)
    assert 'use_crlf' not in nb.metadata['jupytext']

    jupytext.write(nb, tmp_md)
    with open(tmp_md) as fp:
        compare(fp.read(), text)
github mwouts / jupytext / tests / test_read_write_functions.py View on Github external
def test_read_py_percent_from_stream():
    def stream():
        return StringIO(u"""# %%
1 + 1
""")

    nb = jupytext.read(stream())
    nb2 = jupytext.read(stream(), fmt='py:percent')
    compare(nb2, nb)
github mwouts / jupytext / tests / test_cli.py View on Github external
def test_set_formats(py_file, tmpdir):
    tmp_py = str(tmpdir.join('notebook.py'))
    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))

    copyfile(py_file, tmp_py)

    jupytext([tmp_py, '--set-formats', 'ipynb,py:light'])
    nb = read(tmp_ipynb)
    assert nb.metadata['jupytext']['formats'] == 'ipynb,py:light'
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
def test_outdated_text_notebook(nb_file, tmpdir):
    # 1. write py ipynb
    tmp_ipynb = u'notebook.ipynb'
    tmp_nbpy = u'notebook.py'

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

    # open ipynb, save py, reopen
    nb = jupytext.read(nb_file)
    cm.save(model=dict(type='notebook', content=nb), path=tmp_nbpy)
    model_py = cm.get(tmp_nbpy, load_alternative_format=False)
    model_ipynb = cm.get(tmp_ipynb, load_alternative_format=False)

    # 2. check that time of ipynb <= py
    assert model_ipynb['last_modified'] <= model_py['last_modified']

    # 3. wait some time
    time.sleep(0.5)

    # 4. touch ipynb
    with open(str(tmpdir.join(tmp_ipynb)), 'a'):
        os.utime(str(tmpdir.join(tmp_ipynb)), None)

    # 5. test error
    with pytest.raises(HTTPError):
github mwouts / jupytext / tests / test_cli.py View on Github external
def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir):
    # cannot encode magic parameters in markdown yet
    if 'magic' in nb_file and fmt == 'md':
        return

    tmp_ipynb = str(tmpdir.join('notebook.ipynb'))
    copyfile(nb_file, tmp_ipynb)
    ext = long_form_one_format(fmt)['extension']
    tmp_text = str(tmpdir.join('notebook' + ext))

    jupytext(['--to', fmt, tmp_ipynb])
    jupytext(['--to', 'ipynb', '--update', tmp_text])

    nb_org = read(nb_file)
    nb_now = read(tmp_ipynb)
    compare(nb_now, nb_org)
github mwouts / jupytext / tests / test_jupytext_errors.py View on Github external
def test_read_wrong_ext(tmpdir, nb_file='notebook.ext'):
    nb_file = tmpdir.join(nb_file)
    nb_file.write('{}')
    with pytest.raises(JupytextFormatError):
        jupytext.read(str(nb_file))
github mwouts / jupytext / tests / test_cli.py View on Github external
def test_set_kernel_inplace(py_file, tmpdir):
    tmp_py = str(tmpdir.join('notebook.py'))

    copyfile(py_file, tmp_py)

    jupytext([tmp_py, '--set-kernel', '-'])

    nb = read(tmp_py)
    kernel_name = nb.metadata['kernelspec']['name']
    assert normpath(get_kernel_spec(kernel_name).argv[0]) in ['python', normpath(sys.executable)]
github snorkel-team / snorkel-tutorials / scripts / build.py View on Github external
def check_notebook(notebook: Notebook) -> None:
    assert os.path.exists(notebook.py), f"No file {notebook.py}"
    os.environ["IS_TEST"] = "true"
    logging.info(f"Checking links in [{notebook.py}]")
    check_links(notebook.py)
    notebook_actual = jupytext.read(notebook.ipynb, fmt=dict(extension="ipynb"))
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as f:
        logging.info(f"Executing notebook [{notebook.py}]")
        call_jupytext(notebook, f.name, to_ipynb=True)
        notebook_expected = jupytext.read(f.name, fmt=dict(extension="ipynb"))
        # notebook_metadata_filter gets flipped during execution. Remove it to ensure
        # all metadata is tested.
        notebook_actual.metadata.get("jupytext", {}).pop(
            "notebook_metadata_filter", None
        )
        notebook_expected.metadata.get("jupytext", {}).pop(
            "notebook_metadata_filter", None
        )
        compare_notebooks(notebook_actual, notebook_expected)