New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nbdiff-web-export to export HTML diff using just command line #552
Open
trams
wants to merge
7
commits into
jupyter:master
Choose a base branch
from
trams:export_to_html
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a1f12b6
Add nbdiff-web-export to export a diff inside a rendered HTML
trams f38f36c
Fix missing file bug
trams 1dea358
Add the basic test nbdiff can handle missing files
trams 39c73ee
Remove unrelated comment
trams 9785571
nbdiff-web-export will embed the script if --nbdime-url is missing
trams 067c83e
fix broken checkbox label link
vidartf 5b9e1ff
Align some of the changes with existign conventions
vidartf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import sys | ||
import os | ||
import json | ||
|
||
from jinja2 import FileSystemLoader, Environment | ||
|
||
from ..args import ( | ||
Path, | ||
ConfigBackedParser, | ||
add_generic_args) | ||
|
||
from ..nbdiffapp import ( | ||
list_changed_file_pairs, | ||
resolve_diff_args, | ||
_build_diff) | ||
|
||
|
||
here = os.path.abspath(os.path.dirname(__file__)) | ||
static_path = os.path.join(here, 'static') | ||
template_path = os.path.join(here, 'templates') | ||
|
||
|
||
def build_arg_parser(): | ||
""" | ||
Creates an argument parser for the diff tool, that also lets the | ||
user specify a port and displays a help message. | ||
""" | ||
description = 'Difftool for Nbdime.' | ||
parser = ConfigBackedParser( | ||
description=description, | ||
add_help=True | ||
) | ||
add_generic_args(parser) | ||
parser.add_argument( | ||
"base", help="the base notebook filename OR base git-revision.", | ||
type=Path, | ||
nargs='?', default='HEAD', | ||
) | ||
parser.add_argument( | ||
"remote", help="the remote modified notebook filename OR remote git-revision.", | ||
type=Path, | ||
nargs='?', default=None, | ||
) | ||
parser.add_argument( | ||
"paths", help="filter diffs for git-revisions based on path", | ||
type=Path, | ||
nargs='*', default=None, | ||
) | ||
parser.add_argument( | ||
"--nbdime_url", | ||
type=Path, | ||
default="", | ||
help="URL to nbdime.js" | ||
) | ||
parser.add_argument( | ||
"--output-dir", | ||
type=Path, | ||
default="output/", | ||
help="a path to an output dir" | ||
) | ||
return parser | ||
|
||
|
||
def main_export(opts): | ||
env = Environment(loader=FileSystemLoader([template_path]), autoescape=False) | ||
outputdir = opts.output_dir | ||
nbdime_url = opts.nbdime_url | ||
if not nbdime_url: | ||
nbdime_url = "nbdime.js" | ||
import shutil | ||
shutil.copy(os.path.join(static_path, "nbdime.js"), os.path.join(outputdir, nbdime_url)) | ||
|
||
base, remote, paths = resolve_diff_args(opts) | ||
index = 1 | ||
for fbase, fremote in list_changed_file_pairs(base, remote, paths): | ||
# on_null="minimal" is crucial cause web renderer expects | ||
# base_notebook to be a valid notebook even if it is missing | ||
try: | ||
base_notebook, remote_notebook, diff = _build_diff(fbase, fremote, on_null="minimal") | ||
except ValueError as e: | ||
print(e, file=sys.stderr) | ||
return 1 | ||
data = json.dumps(dict( | ||
base=base_notebook, | ||
diff=diff | ||
)) | ||
|
||
template = env.get_template("diffembedded.html") | ||
rendered = template.render( | ||
data=data, | ||
nbdime_url=nbdime_url) | ||
outputfilename = os.path.join(outputdir, "diff" + str(index) + ".html") | ||
with open(outputfilename, "w") as f: | ||
f.write(rendered) | ||
index += 1 | ||
return 0 | ||
|
||
|
||
def main(args=None): | ||
if args is None: | ||
args = sys.argv[1:] | ||
opts = build_arg_parser().parse_args(args) | ||
return main_export(opts) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
|
||
<title>nbdime - diff and merge your Jupyter notebooks</title> | ||
|
||
</head> | ||
|
||
|
||
<!-- TODO: make nbdime.init() setup the forms/input user interface? --> | ||
|
||
<body> | ||
|
||
<div id="nbdime-header" class="nbdime-Diff"> | ||
<h3>Notebook Diff</h3> | ||
<script id='diff-and-base' type="application/json">{{ data|tojson|safe }}</script> | ||
<div id="nbdime-header-buttonrow"> | ||
<input id="nbdime-hide-unchanged" type="checkbox"><label for="cbox1">Hide unchanged cells</label></input> | ||
<button id="nbdime-trust" style="display: none">Trust outputs</button> | ||
<button id="nbdime-close" type="checkbox" style="display: none">Close tool</button> | ||
<button id="nbdime-export" type="checkbox" style="display: none">Export diff</button> | ||
</div> | ||
<div id=nbdime-header-banner> | ||
<span id="nbdime-header-base">Base</span> | ||
<span id="nbdime-header-remote">Remote</span> | ||
</div> | ||
</div> <!-- ndime-header --> | ||
|
||
<div id="nbdime-root" class="nbdime-root"> | ||
</div> | ||
|
||
<script src="{{ nbdime_url }}"></script> | ||
vidartf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<noscript>Nbdime relies on javascript for diff/merge views!</noscript> | ||
</body> | ||
</html> |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I just copy pasted the test with a comment