-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cell metadata from ipynb on markdown cells as well #99
Compare cell content up to blank lines, and restore markdown cell metadata from ipynb
- Loading branch information
Showing
1 changed file
with
38 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,46 @@ | ||
"""Combine source and outputs from two notebooks | ||
""" | ||
import re | ||
from .cell_metadata import _IGNORE_METADATA | ||
|
||
_BLANK_LINE = re.compile(r'^\s*$') | ||
|
||
|
||
def same_content(ref, test): | ||
"""Is the content of two cells the same, except for blank lines?""" | ||
ref = [line for line in ref.splitlines() if not _BLANK_LINE.match(line)] | ||
test = [line for line in test.splitlines() if not _BLANK_LINE.match(line)] | ||
return ref == test | ||
|
||
|
||
def combine_inputs_with_outputs(nb_source, nb_outputs): | ||
'''Copy outputs of the second notebook into | ||
the first one, for cells that have matching inputs''' | ||
"""Copy outputs 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'] | ||
|
||
remaining_output_cells = nb_outputs.cells | ||
for cell in nb_source.cells: | ||
if cell.cell_type != 'code': | ||
continue | ||
|
||
# Remove outputs to warranty that trust of returned | ||
# notebook is that of second notebook | ||
cell.execution_count = None | ||
cell.outputs = [] | ||
|
||
# Fill outputs with that of second notebook | ||
for i, ocell in enumerate(remaining_output_cells): | ||
if ocell.cell_type == 'code' and cell.source == ocell.source: | ||
cell.execution_count = ocell.execution_count | ||
cell.outputs = ocell.outputs | ||
|
||
ometadata = ocell.metadata | ||
cell.metadata.update({k: ometadata[k] for k in ometadata | ||
if k in _IGNORE_METADATA}) | ||
remaining_output_cells = remaining_output_cells[(i + 1):] | ||
break | ||
# 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): | ||
cell.execution_count = ocell.execution_count | ||
cell.outputs = ocell.outputs | ||
|
||
ometadata = ocell.metadata | ||
cell.metadata.update({k: ometadata[k] for k in ometadata if k in _IGNORE_METADATA}) | ||
output_code_cells = output_code_cells[(i + 1):] | ||
break | ||
else: | ||
# Fill outputs with that of second notebook | ||
for i, ocell in enumerate(output_other_cells): | ||
if cell.cell_type == ocell.cell_type and same_content(cell.source, ocell.source): | ||
ometadata = ocell.metadata | ||
cell.metadata.update({k: ometadata[k] for k in ometadata if k in _IGNORE_METADATA}) | ||
|
||
output_other_cells = output_other_cells[(i + 1):] | ||
break |