jupytext/combine.py
def combine_inputs_with_outputs(nb_source, nb_outputs, fmt=None):
    """Copy outputs and metadata of the second notebook into
    the first one, for cells that have matching inputs"""

    output_code_cells = [cell for cell in nb_outputs.cells if cell.cell_type == 'code']
    output_other_cells = [cell for cell in nb_outputs.cells if cell.cell_type != 'code']

    # nbformat version number taken from the notebook with outputs
    assert nb_outputs.nbformat == nb_source.nbformat, \
        "The notebook with outputs is in format {}.{}, please upgrade it to {}.x".format(
            nb_outputs.nbformat, nb_outputs.nbformat_minor, nb_source.nbformat)
    nb_source.nbformat_minor = nb_outputs.nbformat_minor

    fmt = long_form_one_format(fmt)
    text_repr = nb_source.metadata.get('jupytext', {}).get('text_representation', {})
    ext = fmt.get('extension') or text_repr.get('extension')
    format_name = fmt.get('format_name') or text_repr.get('format_name')

    nb_source.metadata = restore_filtered_metadata(
        nb_source.metadata,
        nb_outputs.metadata,
        nb_source.metadata.get('jupytext', {}).get('notebook_metadata_filter'),
        _DEFAULT_NOTEBOOK_METADATA)

    source_is_md_version_one = ext in ['.md', '.markdown', '.Rmd'] and text_repr.get('format_version') == '1.0'
    if nb_source.metadata.get('jupytext', {}).get('formats') or ext in ['.md', '.markdown', '.Rmd']:
        nb_source.metadata.get('jupytext', {}).pop('text_representation', None)

    if not nb_source.metadata.get('jupytext', {}):
        nb_source.metadata.pop('jupytext', {})

    if format_name in ['nomarker', 'sphinx'] or source_is_md_version_one:
        cell_metadata_filter = '-all'
    else:
        cell_metadata_filter = nb_source.metadata.get('jupytext', {}).get('cell_metadata_filter')

    for cell in nb_source.cells:
        # Remove outputs to warranty that trust of returned notebook is that of second notebook
        if cell.cell_type == 'code':
            cell.execution_count = None
            cell.outputs = []

            for i, ocell in enumerate(output_code_cells):
                if same_content(cell.source, ocell.source):
                    # Fill outputs with that of second notebook
                    cell.execution_count = ocell.execution_count
                    cell.outputs = ocell.outputs

                    # Restore the filtered output cell metadata
                    cell.metadata = restore_filtered_metadata(cell.metadata, ocell.metadata,
                                                              cell_metadata_filter, _IGNORE_CELL_METADATA)

                    output_code_cells = output_code_cells[(i + 1):]
                    break
        else:
            for i, ocell in enumerate(output_other_cells):
                if cell.cell_type == ocell.cell_type and same_content(cell.source, ocell.source):
                    # The 'spin' format does not allow metadata on non-code cells
                    cell.metadata = restore_filtered_metadata(cell.metadata, ocell.metadata,
                                                              '-all' if format_name == 'spin' else cell_metadata_filter,
                                                              _IGNORE_CELL_METADATA)

                    output_other_cells = output_other_cells[(i + 1):]
                    break

    return nb_source
Copy outputs and metadata of the second notebook into
    the first one, for cells that have matching inputs
Copy outputs and metadata of the second notebook into
the first one, for cells that have matching inputs
Copy outputs and metadata of the second notebook into the first one for cells that have matching inputs
def combine_inputs_with_outputs(nb_source, nb_outputs, fmt=None):
    output_code_cells = [cell for cell in nb_outputs.cells if cell.cell_type == 'code']
    output_other_cells = [cell for cell in nb_outputs.cells if cell.cell_type != 'code']
    assert nb_outputs.nbformat == nb_source.nbformat, \
        "The notebook with outputs is in format {}.{}, please upgrade it to {}.x".format(
            nb_outputs.nbformat, nb_outputs.nbformat_minor, nb_source.nbformat)
    nb_source.nbformat_minor = nb_outputs.nbformat_minor
    fmt = long_form_one_format(fmt)
    text_repr = nb_source.metadata.get('jupytext', {}).get('text_representation', {})
    ext = fmt.get('extension') or text_repr.get('extension')
    format_name = fmt.get('format_name') or text_repr.get('format_name')
    nb_source.metadata = restore_filtered_metadata(
        nb_source.metadata,
        nb_outputs.metadata,
        nb_source.metadata.get('jupytext', {}).get('notebook_metadata_filter'),
        _DEFAULT_NOTEBOOK_METADATA)
    source_is_md_version_one = ext in ['.md', '.markdown', '.Rmd'] and text_repr.get('format_version') == '1.0'
    if nb_source.metadata.get('jupytext', {}).get('formats') or ext in ['.md', '.markdown', '.Rmd']:
        nb_source.metadata.get('jupytext', {}).pop('text_representation', None)
    if not nb_source.metadata.get('jupytext', {}):
        nb_source.metadata.pop('jupytext', {})
    if format_name in ['nomarker', 'sphinx'] or source_is_md_version_one:
        cell_metadata_filter = '-all'
    else:
        cell_metadata_filter = nb_source.metadata.get('jupytext', {}).get('cell_metadata_filter')
    for cell in nb_source.cells:
        if cell.cell_type == 'code':
            cell.execution_count = None
            cell.outputs = []
            for i, ocell in enumerate(output_code_cells):
                if same_content(cell.source, ocell.source):
                    cell.execution_count = ocell.execution_count
                    cell.outputs = ocell.outputs
                    cell.metadata = restore_filtered_metadata(cell.metadata, ocell.metadata,
                                                              cell_metadata_filter, _IGNORE_CELL_METADATA)
                    output_code_cells = output_code_cells[(i + 1):]
                    break
        else:
            for i, ocell in enumerate(output_other_cells):
                if cell.cell_type == ocell.cell_type and same_content(cell.source, ocell.source):
                    cell.metadata = restore_filtered_metadata(cell.metadata, ocell.metadata,
                                                              '-all' if format_name == 'spin' else cell_metadata_filter,
                                                              _IGNORE_CELL_METADATA)
                    output_other_cells = output_other_cells[(i + 1):]
                    break
    return nb_source
def combine_inputs_with_outputs ( nb_source , nb_outputs , fmt = None ) : output_code_cells = [ cell for cell in nb_outputs . cells if cell . cell_type == 'code' ] output_other_cells = [ cell for cell in nb_outputs . cells if cell . cell_type != 'code' ] assert nb_outputs . nbformat == nb_source . nbformat , "The notebook with outputs is in format {}.{}, please upgrade it to {}.x" . format ( nb_outputs . nbformat , nb_outputs . nbformat_minor , nb_source . nbformat ) nb_source . nbformat_minor = nb_outputs . nbformat_minor fmt = long_form_one_format ( fmt ) text_repr = nb_source . metadata . get ( 'jupytext' , { } ) . get ( 'text_representation' , { } ) ext = fmt . get ( 'extension' ) or text_repr . get ( 'extension' ) format_name = fmt . get ( 'format_name' ) or text_repr . get ( 'format_name' ) nb_source . metadata = restore_filtered_metadata ( nb_source . metadata , nb_outputs . metadata , nb_source . metadata . get ( 'jupytext' , { } ) . get ( 'notebook_metadata_filter' ) , _DEFAULT_NOTEBOOK_METADATA ) source_is_md_version_one = ext in [ '.md' , '.markdown' , '.Rmd' ] and text_repr . get ( 'format_version' ) == '1.0' if nb_source . metadata . get ( 'jupytext' , { } ) . get ( 'formats' ) or ext in [ '.md' , '.markdown' , '.Rmd' ] : nb_source . metadata . get ( 'jupytext' , { } ) . pop ( 'text_representation' , None ) if not nb_source . metadata . get ( 'jupytext' , { } ) : nb_source . metadata . pop ( 'jupytext' , { } ) if format_name in [ 'nomarker' , 'sphinx' ] or source_is_md_version_one : cell_metadata_filter = '-all' else : cell_metadata_filter = nb_source . metadata . get ( 'jupytext' , { } ) . get ( 'cell_metadata_filter' ) for cell in nb_source . cells : if cell . cell_type == 'code' : cell . execution_count = None cell . outputs = [ ] for i , ocell in enumerate ( output_code_cells ) : if same_content ( cell . source , ocell . source ) : cell . execution_count = ocell . execution_count cell . outputs = ocell . outputs cell . metadata = restore_filtered_metadata ( cell . metadata , ocell . metadata , cell_metadata_filter , _IGNORE_CELL_METADATA ) output_code_cells = output_code_cells [ ( i + 1 ) : ] break else : for i , ocell in enumerate ( output_other_cells ) : if cell . cell_type == ocell . cell_type and same_content ( cell . source , ocell . source ) : cell . metadata = restore_filtered_metadata ( cell . metadata , ocell . metadata , '-all' if format_name == 'spin' else cell_metadata_filter , _IGNORE_CELL_METADATA ) output_other_cells = output_other_cells [ ( i + 1 ) : ] break return nb_source
Copy outputs and metadata of the second notebook into
the first one, for cells that have matching inputs
Copy outputs and metadata of the second notebook into the first one for cells that have matching inputs
"""Copy outputs and metadata of the second notebook into
    the first one, for cells that have matching inputs""" # nbformat version number taken from the notebook with outputs # Remove outputs to warranty that trust of returned notebook is that of second notebook # Fill outputs with that of second notebook # Restore the filtered output cell metadata # The 'spin' format does not allow metadata on non-code cells
nb_source
nb_outputs
fmt