Skip to content

Commit

Permalink
Trust .Rmd notebook if .ipynb is trusted #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 12, 2018
1 parent 5e99a99 commit d2ebd96
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
50 changes: 26 additions & 24 deletions nbrmd/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,32 @@ def __init__(self, **kwargs):
def _read_notebook(self, os_path, as_version=4):
"""Read a notebook from an os path."""
file, ext = os.path.splitext(os_path)
if ext == '.ipynb':
return super(RmdFileContentsManager, self) \
._read_notebook(os_path, as_version)

if ext == '.Rmd':
with mock.patch('nbformat.reads', _nbrmd_reads):
return super(RmdFileContentsManager, self) \
nb = super(RmdFileContentsManager, self) \
._read_notebook(os_path, as_version)
elif ext == '.md':
else: # ext == '.md':
with mock.patch('nbformat.reads', _nbrmd_md_reads):
return super(RmdFileContentsManager, self) \
nb = super(RmdFileContentsManager, self) \
._read_notebook(os_path, as_version)
else:
return super(RmdFileContentsManager, self) \
._read_notebook(os_path, as_version)

# Read outputs from .ipynb version if available
if ext != '.ipynb':
os_path_ipynb = file + '.ipynb'
try:
nb_outputs = self._read_notebook(
os_path_ipynb, as_version=as_version)
combine.combine_inputs_with_outputs(nb, nb_outputs)
if self.notary.check_signature(nb_outputs):
self.notary.sign(nb)
except HTTPError:
pass

return nb

def _save_notebook(self, os_path, nb):
"""Save a notebook to an os_path."""
Expand Down Expand Up @@ -97,28 +112,15 @@ def get(self, path, content=True, type=None, format=None):
(type == 'notebook' or
(type is None and
any([path.endswith(ext) for ext in self.nb_extensions]))):
nb = self._notebook_model(path, content=content)

# Read outputs from .ipynb version if available
if content and not path.endswith('.ipynb'):
file, ext = os.path.splitext(path)
path_ipynb = file + '.ipynb'
if self.exists(path_ipynb):
try:
nb_outputs = self._notebook_model(
path_ipynb, content=content)
combine.combine_inputs_with_outputs(
nb['content'],
nb_outputs['content'])
except HTTPError:
pass

return nb

return self._notebook_model(path, content=content)
else:
return super(RmdFileContentsManager, self) \
.get(path, content, type, format)

def trust_notebook(self, path):
file, ext = os.path.splitext(path)
super(RmdFileContentsManager, self).trust_notebook(file + '.ipynb')

def rename_file(self, old_path, new_path):
old_file, org_ext = os.path.splitext(old_path)
new_file, new_ext = os.path.splitext(new_path)
Expand Down
21 changes: 14 additions & 7 deletions nbrmd/combine.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from .chunk_options import _ignore_metadata


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'''

remaining_output_cells = nb_outputs.get('cells', [])
for cell in nb_source.get('cells', []):
remaining_output_cells = nb_outputs.cells
for cell in nb_source.cells:
for i, ocell in enumerate(remaining_output_cells):
if cell.get('cell_type') == 'code' \
and ocell.get('cell_type') == 'code' \
and cell.get('source') == ocell.get('source'):
cell['execution_count'] = ocell.get('execution_count')
cell['outputs'] = ocell.get('outputs', None)
if cell.cell_type == 'code' \
and ocell.cell_type == 'code' \
and cell.source == ocell.source:
cell.execution_count = ocell.execution_count
cell.outputs = ocell.outputs

m = ocell.metadata
cell.metadata.update({k: m[k] for k in m
if m in _ignore_metadata})
remaining_output_cells = remaining_output_cells[(i + 1):]
break

0 comments on commit d2ebd96

Please sign in to comment.