def post(self): # Assuming a request on the form "{'argname':arg}" body = json.loads(escape.to_unicode(self.request.body)) base = body['base'] if base.startswith('git:'): base_nb, remote_nb = self._get_git_notebooks(base[len('git:'):]) elif base.startswith('checkpoint:'): base_nb, remote_nb = yield self._get_checkpoint_notebooks( base[len('checkpoint:'):]) else: # Regular files, call super super(ExtensionApiDiffHandler, self).post() return # Perform actual diff and return data: try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: self.log.exception('Error diffing documents:') raise HTTPError(500, 'Error while attempting to diff documents') data = { 'base': base_nb, 'diff': thediff, } self.finish(data)
def post(self): # Assuming a request on the form "{'argname':arg}" body = json.loads(escape.to_unicode(self.request.body)) base = body['base'] if base.startswith('git:'): base_nb, remote_nb = self._get_git_notebooks(base[len('git:'):]) elif base.startswith('checkpoint:'): base_nb, remote_nb = yield self._get_checkpoint_notebooks(base[len('checkpoint:'):]) else: # Regular files, call super super(ExtensionApiDiffHandler, self).post() return # Perform actual diff and return data: try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: self.log.exception('Error diffing documents:') raise HTTPError(500, 'Error while attempting to diff documents') data = { 'base': base_nb, 'diff': thediff, } self.finish(data)
def diff_nbnode_with_cache(self, pk: int, nb: nbf.NotebookNode, uri: str = "", as_str=False, **kwargs): """Return a diff of a notebook to a cached one. Note: this will not diff markdown content, since it is not stored in the cache. """ import nbdime from nbdime.prettyprint import pretty_print_diff, PrettyPrintConfig cached_nb = self.get_cache_bundle(pk).nb nb, _ = self.create_hashed_notebook(nb) diff = nbdime.diff_notebooks(cached_nb, nb) if not as_str: return diff stream = io.StringIO() stream.writelines( ["nbdiff\n", f"--- cached pk={pk}\n", f"+++ other: {uri}\n"]) pretty_print_diff(cached_nb, diff, "nb", PrettyPrintConfig(out=stream, **kwargs)) return stream.getvalue()
def are_same_notebooks(filename1, filename2): nbdime.diffing.notebooks.set_notebook_diff_targets(sources=True, outputs=False, attachments=False, metadata=False, details=False) try: diff = nbdime.diff_notebooks(load_json(filename1), load_json(filename2)) except: return False return len(diff) == 0
def get_file_nbdiff(self, prev_content, curr_content): def read_notebook(content): if not content: return nbformat.v4.new_notebook() return nbformat.reads(content, as_version=4) try: prev_nb = read_notebook(prev_content) curr_nb = read_notebook(curr_content) thediff = diff_notebooks(prev_nb, curr_nb) except: raise data = {'base': prev_nb, 'diff': thediff} return data
def post(self): base_nb = self.get_notebook_argument("base") remote_nb = self.get_notebook_argument("remote") try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception as e: raise web.HTTPError(400, "Error while diffing documents.") data = { "base": base_nb, "diff": thediff, } self.finish(data)
def post(self): base_nb = self.get_notebook_argument("base") remote_nb = self.get_notebook_argument("remote") try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: raise web.HTTPError(400, "Error while diffing documents.") data = { "base": base_nb, "diff": thediff, } self.finish(data)
def post(self): base_nb = self.get_notebook_argument("base") remote_nb = self.get_notebook_argument("remote") try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: nbdime.log.exception("Error diffing documents:") raise web.HTTPError(500, "Error while attempting to diff documents") data = { "base": base_nb, "diff": thediff, } self.finish(data)
def post(self): base_nb = self.get_notebook_argument("base") remote_nb = self.get_notebook_argument("remote") try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: nbdime.log.exception("Error diffing documents:") raise web.HTTPError(500, "Error while attempting to diff documents") data = { "base": base_nb, "diff": thediff, } self.finish(data)
def post(self): base_nb = self.get_notebook_argument('base') remote_nb = self.get_notebook_argument('remote') try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: nbdime.log.exception('Error diffing documents:') raise web.HTTPError(500, 'Error while attempting to diff documents') data = { 'base': base_nb, 'diff': thediff, } self.finish(data)
def post(self): base_nb = self.get_notebook_argument('base') remote_nb = self.get_notebook_argument('remote') try: thediff = nbdime.diff_notebooks(base_nb, remote_nb) except Exception: nbdime.log.exception('Error diffing documents:') raise web.HTTPError(500, 'Error while attempting to diff documents') data = { 'base': base_nb, 'diff': thediff, } self.finish(data)
def test_diff_and_patch_notebooks(any_nb_pair): "Test diff/patch on any pair of notebooks in the test suite." a, b = any_nb_pair assert patch_notebook(a, diff_notebooks(a, b)) == nbformat.from_dict(b)
def post(self): base_nb = self.get_notebook_argument('base') body = json.loads(escape.to_unicode(self.request.body)) base_selected_execution = body['base_selected_execution'] remote_selected_execution = body['remote_selected_execution'] cell_index = body['cell_index'] base_notebook = {} remote_notebook = {} base_notebook['metadata'] = base_nb['metadata'] remote_notebook['metadata'] = base_nb['metadata'] base_notebook['nbformat'] = base_nb['nbformat'] remote_notebook['nbformat'] = base_nb['nbformat'] base_notebook['nbformat_minor'] = base_nb['nbformat_minor'] remote_notebook['nbformat_minor'] = base_nb['nbformat_minor'] base_notebook['cells'] = [] remote_notebook['cells'] = [] for cell_i, cell_node in enumerate(base_nb['cells']): base_prov_obj = {} remote_prov_obj = {} if (int(cell_i) == int(cell_index)): if 'provenance' in cell_node['metadata']: provenance = cell_node['metadata']['provenance'] base_selected_execution_prov = provenance[ base_selected_execution] remote_selected_execution_prov = provenance[ remote_selected_execution] base_prov_obj['source'] = base_selected_execution_prov[ 'source'] remote_prov_obj['source'] = remote_selected_execution_prov[ 'source'] base_prov_obj['outputs'] = base_selected_execution_prov[ 'outputs'] remote_prov_obj[ 'outputs'] = remote_selected_execution_prov['outputs'] base_prov_obj['execution_count'] = cell_node[ 'execution_count'] remote_prov_obj['execution_count'] = cell_node[ 'execution_count'] base_prov_obj['metadata'] = {} remote_prov_obj['metadata'] = {} base_prov_obj['cell_type'] = cell_node['cell_type'] remote_prov_obj['cell_type'] = cell_node['cell_type'] base_notebook['cells'] = [base_prov_obj] remote_notebook['cells'] = [remote_prov_obj] d = nbdime.diff(provenance[base_selected_execution], provenance[remote_selected_execution]) else: base_notebook = base_nb remote_notebook = base_nb try: thediff = nbdime.diff_notebooks(base_notebook, remote_notebook) except Exception: nbdime.log.exception('Error diffing documents:') raise web.HTTPError(500, 'Error while attempting to diff documents') data = { 'base': base_notebook, 'diff': thediff, } self.finish(data)
def test_validate_matching_notebook_diff(matching_nb_pairs): a, b = matching_nb_pairs d = diff_notebooks(a, b) validator.validate(d)
def test_validate_matching_notebook_diff(matching_nb_pairs, diff_validator): a, b = matching_nb_pairs d = diff_notebooks(a, b) diff_validator.validate(d)
def test_diff_and_patch_matching_notebooks(matching_nb_pairs): "Test diff/patch on pairs of notebooks with the same basename in the test suite." a, b = matching_nb_pairs assert patch_notebook(a, diff_notebooks(a, b)) == nbformat.from_dict(b)
def test_diff_and_patch_notebooks(any_nb_pair): "Test diff/patch on any pair of notebooks in the test suite." a, b = any_nb_pair assert patch_notebook(a, diff_notebooks(a, b)) == nbformat.from_dict(b)
def test_diff_and_patch_matching_notebooks(matching_nb_pairs): "Test diff/patch on pairs of notebooks with the same basename in the test suite." a, b = matching_nb_pairs assert patch_notebook(a, diff_notebooks(a, b)) == nbformat.from_dict(b)