def diff():
    description = '''
    Produce a diffed IPython Notebook from before and after notebooks.

    If no arguments are given, nbdiff looks for modified notebook files in
    the version control system.

    The resulting diff is presented to the user in the browser at
    http://localhost:5000.
    '''
    usage = 'nbdiff [-h] [--check] [--debug] ' +\
            '[--browser=<browser] [before after]'
    parser = argparse.ArgumentParser(
        description=description,
        usage=usage,
    )
    # TODO share this code with merge()
    parser.add_argument(
        '--browser',
        '-b',
        default=None,
        help='Browser to launch nbdiff/nbmerge in',
    )
    parser.add_argument(
        '--check',
        '-c',
        action='store_true',
        default=False,
        help='Run nbdiff algorithm but do not display the result.',
    )
    parser.add_argument(
        '--debug',
        '-d',
        action='store_true',
        default=False,
        help='Pass debug=True to the Flask server to ease debugging.',
    )
    parser.add_argument('before', nargs='?',
                        help='The notebook to diff against.')
    parser.add_argument('after', nargs='?',
                        help='The notebook to compare `before` to.')
    args = parser.parse_args()

    parser = NotebookParser()

    if args.before and args.after:
        invalid_notebooks = []

        try:
            notebook1 = parser.parse(open(args.before))
        except NotJSONError:
            invalid_notebooks.append(args.before)

        try:
            notebook2 = parser.parse(open(args.after))
        except NotJSONError:
            invalid_notebooks.append(args.after)

        if (len(invalid_notebooks) == 0):
            result = notebook_diff(notebook1, notebook2)

            filename_placeholder = "{} and {}".format(args.before, args.after)
            app.add_notebook(result, filename_placeholder)
            if not args.check:
                open_browser(args.browser)
                app.run(debug=args.debug)

        else:
            print('The notebooks could not be diffed.')
            print('There was a problem parsing the following notebook '
                  + 'files:\n' + '\n'.join(invalid_notebooks))
            return -1

    elif not (args.before or args.after):
        # No arguments have been given. Ask version control instead
        #  Try hg and git in this order.
        # The order is important because when GitAdapter fails, it sends an
        # annoying message to stdout, which is impossible to suppress.
        #  We don't want to see this message when we have a Hg repository.

        # Hg:
        try:
            vcs = HgAdapter()
        except NoVCSError as hg_err:
            # Git:
            #  use a nested try block to make sure the GitAdapter
            # is only created if the HgAdapter has failed.
            try:
                vcs = GitAdapter()
            except NoVCSError:
                #  Now we're sure we are not inside a supported repo.
                #  The GitAdapter error message has already been printed
                # and we print the HgAdapter error message
                print(hg_err.value)
                sys.exit(-1)

        modified_notebooks = vcs.get_modified_notebooks()

        if not len(modified_notebooks) == 0:
            invalid_notebooks = []
            for nbook in modified_notebooks:
                try:
                    filename = nbook[2]

                    current_notebook = parser.parse(nbook[0])
                    head_version = parser.parse(nbook[1])

                    result = notebook_diff(head_version, current_notebook)
                    app.add_notebook(result, filename)

                except NotJSONError:
                    invalid_notebooks.append(filename)

            if (0 > len(invalid_notebooks)):
                print('There was a problem parsing the following notebook '
                      + 'files:\n' + '\n'.join(invalid_notebooks))

            if (len(modified_notebooks) == len(invalid_notebooks)):
                print("There are no valid notebooks to diff.")
                return -1

            if not args.check:
                open_browser(args.browser)
                app.run(debug=False)
        else:
            print("No modified files to diff.")
            return 0
    else:
        print("Invalid number of arguments. Run nbdiff --help")
        return -1
dataset/ETHPy150Open tarmstrong/nbdiff/nbdiff/commands.py/diff/swapped operands of `>`