How to use the jupytext.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_contentsmanager.py View on Github external
cm.default_jupytext_formats = 'ipynb,py'

    nb = new_notebook(cells=[new_code_cell("""# {{{
'''Sample cell with region markers'''
'''End of the cell'''
# }}}"""),
                             new_code_cell('a = 1\n\n\nb = 1')])
    cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')

    assert os.path.isfile(tmp_ipynb)
    assert os.path.isfile(tmp_py)

    nb2 = cm.get('nb.ipynb')['content']
    compare_notebooks(nb2, nb)

    nb3 = read(tmp_py)
    assert nb3.metadata['jupytext']['cell_markers'] == 'region,endregion'

    with open(tmp_py) as fp:
        text = fp.read()

    # Remove YAML header
    text = re.sub(re.compile(r'# ---.*# ---\n\n', re.DOTALL), '', text)

    compare(text, """# {{{
'''Sample cell with region markers'''
github mwouts / jupytext / tests / test_contentsmanager.py View on Github external
tmp_py = str(tmpdir.join('nb.py'))

    cm = jupytext.TextFileContentsManager()
    cm.root_dir = str(tmpdir)

    # set default py format
    cm.preferred_jupytext_formats_save = "py:percent"

    # every new file is paired
    cm.default_jupytext_formats = "ipynb,py"

    # the text files don't need a YAML header
    cm.default_notebook_metadata_filter = "-all"
    cm.default_cell_metadata_filter = "-all"

    nb = read(nb_file)
    model_ipynb = cm.save(model=dict(content=nb, type='notebook'), path='nb.ipynb')

    assert os.path.isfile(tmp_ipynb)
    assert os.path.isfile(tmp_py)

    os.remove(tmp_ipynb)

    # reopen and save nb.py
    model = cm.get('nb.py')
    cm.save(model=model, path='nb.py')

    # ipynb is re-created
    assert os.path.isfile(tmp_ipynb)

    # save time of ipynb is that of py file
    assert model_ipynb['last_modified'] == model['last_modified']
github mwouts / jupytext / jupytext / cli.py View on Github external
info = os.lstat(alt_path)
        if not max_mtime_inputs or info.st_mtime > max_mtime_inputs:
            max_mtime_inputs = info.st_mtime
            latest_inputs, input_fmt = alt_path, alt_fmt

        if alt_path.endswith('.ipynb'):
            if not max_mtime_outputs or info.st_mtime > max_mtime_outputs:
                max_mtime_outputs = info.st_mtime
                latest_outputs = alt_path

    if latest_outputs and latest_outputs != latest_inputs:
        log("[jupytext] Loading input cells from '{}'".format(latest_inputs))
        inputs = notebook if latest_inputs == nb_file else read(latest_inputs, fmt=input_fmt)
        check_file_version(inputs, latest_inputs, latest_outputs)
        log("[jupytext] Loading output cells from '{}'".format(latest_outputs))
        outputs = notebook if latest_outputs == nb_file else read(latest_outputs)
        combine_inputs_with_outputs(inputs, outputs, fmt=input_fmt)
        return inputs, latest_inputs, latest_outputs

    log("[jupytext] Loading notebook from '{}'".format(latest_inputs))
    if latest_inputs != nb_file:
        notebook = read(latest_inputs, fmt=input_fmt)
    return notebook, latest_inputs, latest_outputs
github mwouts / jupytext / jupytext / cli.py View on Github external
# b. Output to the desired file or format
    if nb_dest:
        if nb_dest == nb_file and not dest_fmt:
            dest_fmt = fmt

        # Test consistency between dest name and output format
        if dest_fmt and nb_dest != '-':
            base_path(nb_dest, dest_fmt)

        # Describe what jupytext is doing
        if os.path.isfile(nb_dest) and args.update:
            if not nb_dest.endswith('.ipynb'):
                raise ValueError('--update is only for ipynb files')
            action = ' (destination file updated)'
            check_file_version(notebook, nb_file, nb_dest)
            combine_inputs_with_outputs(notebook, read(nb_dest), fmt=fmt)
        elif os.path.isfile(nb_dest):
            action = ' (destination file replaced)'
        else:
            action = ''

        log('[jupytext] Writing {nb_dest}{format}{action}'
            .format(nb_dest=nb_dest,
                    format=' in format ' + short_form_one_format(
                        dest_fmt) if dest_fmt and 'format_name' in dest_fmt else '',
                    action=action))
        write(notebook, nb_dest, fmt=dest_fmt)
        if args.pre_commit:
            system('git', 'add', nb_dest)

    # c. Synchronize paired notebooks
    if args.sync:
github mwouts / jupytext / jupytext / cli.py View on Github external
for alt_path, alt_fmt in paired_paths(nb_file, fmt, formats):
        if not os.path.isfile(alt_path):
            continue
        info = os.lstat(alt_path)
        if not max_mtime_inputs or info.st_mtime > max_mtime_inputs:
            max_mtime_inputs = info.st_mtime
            latest_inputs, input_fmt = alt_path, alt_fmt

        if alt_path.endswith('.ipynb'):
            if not max_mtime_outputs or info.st_mtime > max_mtime_outputs:
                max_mtime_outputs = info.st_mtime
                latest_outputs = alt_path

    if latest_outputs and latest_outputs != latest_inputs:
        log("[jupytext] Loading input cells from '{}'".format(latest_inputs))
        inputs = notebook if latest_inputs == nb_file else read(latest_inputs, fmt=input_fmt)
        check_file_version(inputs, latest_inputs, latest_outputs)
        log("[jupytext] Loading output cells from '{}'".format(latest_outputs))
        outputs = notebook if latest_outputs == nb_file else read(latest_outputs)
        combine_inputs_with_outputs(inputs, outputs, fmt=input_fmt)
        return inputs, latest_inputs, latest_outputs

    log("[jupytext] Loading notebook from '{}'".format(latest_inputs))
    if latest_inputs != nb_file:
        notebook = read(latest_inputs, fmt=input_fmt)
    return notebook, latest_inputs, latest_outputs
github mwouts / jupytext / jupytext / cli.py View on Github external
if not max_mtime_outputs or info.st_mtime > max_mtime_outputs:
                max_mtime_outputs = info.st_mtime
                latest_outputs = alt_path

    if latest_outputs and latest_outputs != latest_inputs:
        log("[jupytext] Loading input cells from '{}'".format(latest_inputs))
        inputs = notebook if latest_inputs == nb_file else read(latest_inputs, fmt=input_fmt)
        check_file_version(inputs, latest_inputs, latest_outputs)
        log("[jupytext] Loading output cells from '{}'".format(latest_outputs))
        outputs = notebook if latest_outputs == nb_file else read(latest_outputs)
        combine_inputs_with_outputs(inputs, outputs, fmt=input_fmt)
        return inputs, latest_inputs, latest_outputs

    log("[jupytext] Loading notebook from '{}'".format(latest_inputs))
    if latest_inputs != nb_file:
        notebook = read(latest_inputs, fmt=input_fmt)
    return notebook, latest_inputs, latest_outputs
github mwouts / jupytext / jupytext / cli.py View on Github external
try:
            tmp = NamedTemporaryFile(**tmp_file_args)
        except TypeError:
            # NamedTemporaryFile does not have an 'encoding' argument on pypy
            tmp_file_args.pop('encoding')
            tmp = NamedTemporaryFile(**tmp_file_args)
        try:
            tmp.write(text)
            tmp.close()

            exec_command([cmd if cmd != '{}' else tmp.name for cmd in command])

            if not update:
                return notebook

            piped_notebook = read(tmp.name, fmt=fmt)
        finally:
            os.remove(tmp.name)
    else:
        cmd_output = exec_command(command, text.encode('utf-8'))

        if not update:
            return notebook

        if not cmd_output:
            sys.stderr.write("[jupytext] The command '{}' had no output. As a result, the notebook is empty. "
                             "Is this expected? If not, use --check rather than --pipe for this command."
                             .format(command))

        piped_notebook = reads(cmd_output.decode('utf-8'), fmt)

    if fmt['extension'] != '.ipynb':