How to use jupytext - 10 common examples

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
tmp_ipynb = str(tmpdir.join('nb with spaces.ipynb'))
    tmp_py = str(tmpdir.join('nb with spaces.py'))
    nb = new_notebook(cells=[])

    git = git_in_tmpdir(tmpdir)
    git('init')
    git('status')
    hook = str(tmpdir.join('.git/hooks/pre-commit'))
    with open(hook, 'w') as fp:
        fp.write('#!/bin/sh\n'
                 'jupytext --to py:light --pre-commit\n')

    st = os.stat(hook)
    os.chmod(hook, st.st_mode | stat.S_IEXEC)

    write(nb, tmp_ipynb)
    assert os.path.isfile(tmp_ipynb)
    assert not os.path.isfile(tmp_py)

    git('add', 'nb with spaces.ipynb')
    git('status')
    git('commit', '-m', 'created')
    git('status')

    assert 'nb with spaces.py' in git('ls-tree', '-r', 'master', '--name-only')
    assert os.path.isfile(tmp_py)
github mwouts / jupytext / tests / test_mirror.py View on Github external
def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook=False):
    dirname, basename = os.path.split(nb_file)
    file_name, org_ext = os.path.splitext(basename)
    fmt = long_form_one_format(fmt)
    notebook = jupytext.read(nb_file, fmt=fmt)
    check_auto_ext(fmt, notebook.metadata, '')
    ext = fmt['extension']
    mirror_file = os.path.join(dirname, '..', 'mirror', mirror_name, full_path(file_name, fmt))

    # it's better not to have Jupytext metadata in test notebooks:
    if fmt == 'ipynb' and 'jupytext' in notebook.metadata:  # pragma: no cover
        notebook.metadata.pop('jupytext')
        jupytext.write(nb_file, fmt=fmt)

    create_mirror_file_if_missing(mirror_file, notebook, fmt)

    # Compare the text representation of the two notebooks
    if compare_notebook:
        nb_mirror = jupytext.read(mirror_file)
        compare(nb_mirror, notebook)
github mwouts / jupytext / tests / test_read_simple_markdown.py View on Github external
def test_raw_cell_with_metadata(markdown="""
raw content

"""):
    nb = jupytext.reads(markdown, 'md')
    compare(nb.cells[0], new_raw_cell(source='raw content', metadata={'key': 'value'}))
    markdown2 = jupytext.writes(nb, 'md')
    compare(markdown2, markdown)
github mwouts / jupytext / tests / test_cell_metadata.py View on Github external
def test_title_no_metadata(text='title', value=('title', {})):
    compare(text_to_metadata(text, allow_title=True), value)
    assert metadata_to_text(*value) == text.strip()
github mwouts / jupytext / tests / test_read_simple_python.py View on Github external
pynb="""def test_read_cell_explicit_start_end(pynb='''
import pandas as pd
# +
def data():
    return pd.DataFrame({'A': [0, 1]})


data()
'''):
    nb = jupytext.reads(pynb, 'py')
    pynb2 = jupytext.writes(nb, 'py')
    compare(pynb2, pynb)
"""):
    nb = jupytext.reads(pynb, 'py')
    pynb2 = jupytext.writes(nb, 'py')
    compare(pynb2, pynb)
github mwouts / jupytext / tests / test_cell_metadata.py View on Github external
def test_parse_rmd_options(options, language_and_metadata):
    compare(rmd_options_to_metadata(options), language_and_metadata)
github mwouts / jupytext / tests / test_active_cells.py View on Github external
def test_active_ipynb_rmd_using_tags(ext, no_jupytext_version_number):
    nb = jupytext.reads(HEADER[ext] + ACTIVE_IPYNB_RMD_USING_TAG[ext], ext)
    assert len(nb.cells) == 1
    compare(jupytext.writes(nb, ext), ACTIVE_IPYNB_RMD_USING_TAG[ext])
    compare(nb.cells[0], ACTIVE_IPYNB_RMD_USING_TAG['.ipynb'])
github mwouts / jupytext / tests / test_read_simple_python.py View on Github external
def test_read_markdown_cell_with_triple_quote_307(
        script="""# This script test that commented triple quotes '''
# do not impede the correct identification of Markdown cells

# Here is Markdown cell number 2 '''
"""):
    notebook = jupytext.reads(script, 'py')
    assert len(notebook.cells) == 2
    assert notebook.cells[0].cell_type == 'markdown'
    assert notebook.cells[0].source == """This script test that commented triple quotes '''
do not impede the correct identification of Markdown cells"""
    assert notebook.cells[1].cell_type == 'markdown'
    assert notebook.cells[1].source == "Here is Markdown cell number 2 '''"

    script2 = jupytext.writes(notebook, 'py')
    compare(script2, script)
github mwouts / jupytext / tests / test_read_simple_python.py View on Github external
# +
# Final cell

1 + 1
"""):
    notebook = jupytext.reads(script, 'py')
    assert len(notebook.cells) == 3
    for cell in notebook.cells:
        lines = cell.source.splitlines()
        if len(lines) != 1:
            assert lines[0]
            assert lines[-1]

    script2 = jupytext.writes(notebook, 'py')

    compare(script2, script)
github mwouts / jupytext / tests / test_read_simple_markdown.py View on Github external
def test_markdown_cell_with_metadata(markdown="""
A long


markdown cell

"""):
    nb = jupytext.reads(markdown, 'md')
    compare(nb.cells[0], new_markdown_cell(source='A long\n\n\nmarkdown cell',
                                           metadata={'key': 'value'}))
    markdown2 = jupytext.writes(nb, 'md')
    compare(markdown2, markdown)