Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
add magic command textdiff
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Apr 23, 2015
1 parent 9e99b8a commit e7257e7
Show file tree
Hide file tree
Showing 10 changed files with 1,001 additions and 22 deletions.
848 changes: 848 additions & 0 deletions _doc/notebooks/view_differences.ipynb

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions _doc/sphinxdoc/source/blog/2015/2015-04-23_textdiff.rst
@@ -0,0 +1,15 @@


.. _b-textdiff:

.. blogpost::
:title: A magic command to visualize differences between two files in a notebook
:keywords: diffview, difflib.js, differences, textdiff
:date: 2015-04-23
:categories: javascript, notebook

The magic command :meth:`textdiff <pyensae.file_helper.magic_file.MagicFile.textdiff>`
makes it easy to display the differences between two files or two strings.

.. image:: diffviewm.png

Binary file added _doc/sphinxdoc/source/blog/2015/diffviewm.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions _unittests/ut_files/test_files.py
Expand Up @@ -195,6 +195,20 @@ def test_htmlhelp(self):
assert "it can also contain several files separated by" in res
assert "@param" in res

def test_textdiff(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

from IPython.core.display import Javascript
mg = MagicFile()
mg.add_context(
{"f1": "STRING1\nSTRING2", "f2": "STRING1\nSTRING3"})
cmd = "f1 f2"
res = mg.textdiff(cmd)
assert isinstance(res, Javascript)


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions _unittests/ut_notebooks/test_magic_commands_about_files.py
Expand Up @@ -38,9 +38,9 @@
from pyquickhelper import get_temp_folder, fLOG


class TestNotebookRunner (unittest.TestCase):
class TestNotebookRunnerMagicCommand (unittest.TestCase):

def test_notebook_runner(self):
def test_notebook_runner_magic_command(self):
fLOG(
__file__,
self._testMethodName,
Expand Down
4 changes: 2 additions & 2 deletions _unittests/ut_notebooks/test_pyensae_StockPrices.py
Expand Up @@ -38,9 +38,9 @@
from pyquickhelper import get_temp_folder, fLOG


class TestNotebookRunner (unittest.TestCase):
class TestNotebookRunnerStockPrices (unittest.TestCase):

def test_notebook_runner(self):
def test_notebook_runner_stock_prices(self):
fLOG(
__file__,
self._testMethodName,
Expand Down
4 changes: 2 additions & 2 deletions _unittests/ut_notebooks/test_pyensae_flat2db3.py
Expand Up @@ -38,9 +38,9 @@
from pyquickhelper import get_temp_folder, fLOG


class TestNotebookRunner (unittest.TestCase):
class TestNotebookRunnerFlat2Db3 (unittest.TestCase):

def test_notebook_runner(self):
def test_notebook_runner_flat2db3(self):
fLOG(
__file__,
self._testMethodName,
Expand Down
4 changes: 2 additions & 2 deletions _unittests/ut_notebooks/test_pyensae_sql_magic.py
Expand Up @@ -38,9 +38,9 @@
from pyquickhelper import get_temp_folder, fLOG


class TestNotebookRunner (unittest.TestCase):
class TestNotebookRunnerSqlMagic (unittest.TestCase):

def test_notebook_runner(self):
def test_notebook_runner_sql_magic(self):
fLOG(
__file__,
self._testMethodName,
Expand Down
86 changes: 86 additions & 0 deletions _unittests/ut_notebooks/test_view_differences.py
@@ -0,0 +1,86 @@
"""
@brief test log(time=7s)
"""

import sys
import os
import unittest
import re

try:
import src
import pyquickhelper
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import src
import pyquickhelper


from pyquickhelper.ipythonhelper.notebook_helper import run_notebook
from pyquickhelper import get_temp_folder, fLOG


class TestNotebookRunner (unittest.TestCase):

def test_notebook_runner(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
notebook = os.path.split(
__file__)[-1].replace(".ipynb", "").replace(".py", "")[5:]
temp = get_temp_folder(__file__, "temp_" + notebook)
nbfile = os.path.join(
temp,
"..",
"..",
"..",
"_doc",
"notebooks",
"%s.ipynb" %
notebook)
if not os.path.exists(nbfile):
raise FileNotFoundError(nbfile)
addpath = [os.path.normpath(os.path.join(temp, "..", "..", "..", "src")),
os.path.normpath(
os.path.join(
temp,
"..",
"..",
"..",
"..",
"pyquickhelper",
"src")),
]
outfile = os.path.join(temp, "out_notebook.ipynb")
assert not os.path.exists(outfile)

out = run_notebook(
nbfile,
working_dir=temp,
outfilename=outfile,
additional_path=addpath)
fLOG(out)
assert os.path.exists(outfile)


if __name__ == "__main__":
unittest.main()
44 changes: 30 additions & 14 deletions src/pyensae/file_helper/magic_file.py
Expand Up @@ -8,7 +8,7 @@
import pandas

from IPython.core.magic import magics_class, line_magic, cell_magic
from IPython.core.display import HTML, display_javascript
from IPython.core.display import HTML, display_html

from pyquickhelper.filehelper.synchelper import explore_folder_iterfile, explore_folder_iterfile_repo
from pyquickhelper import MagicCommandParser, run_cmd, zip_files, gzip_files, zip7_files, MagicClassWithHelpers
Expand Down Expand Up @@ -340,14 +340,25 @@ def hhelp(self, line):
print(docstring2html(obj, format=format))

@staticmethod
def filediff_parser():
def textdiff_parser():
"""
defines the way to parse the magic command ``%filediff``
defines the way to parse the magic command ``%textdiff``
"""
parser = MagicCommandParser(
description='show the differences between two files')
parser.add_argument('f1', type=str, help='first file')
parser.add_argument('f2', type=str, help='second file')
description='show the differences between two files, two text')
parser.add_argument('f1', type=str, help='first file or text or url')
parser.add_argument('f2', type=str, help='second file or text or url')
parser.add_argument(
'-c',
'--context',
default="",
help='context view, empty to see everything, > 0 to see only a couple of lines around the changes')
parser.add_argument(
'-i',
'--inline',
action="store_true",
default=False,
help='True=one column (inline) or False=two columns')
parser.add_argument(
'-e',
'--encoding',
Expand All @@ -356,19 +367,24 @@ def filediff_parser():
return parser

@line_magic
def filediff(self, line):
def textdiff(self, line):
"""
defines ``%filediff``
which displays differences between two text files,
it based on `create_visual_diff_through_html_files <http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx/pyquickhelper/filehelper/visual_sync.html?highlight=create#pyquickhelper.filehelper.visual_sync.create_visual_diff_through_html_files>`_
defines ``%textdiff``
which displays differences between two text files, two strings, two urls,
it is based on `create_visual_diff_through_html_files <http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx/pyquickhelper/filehelper/visual_sync.html?highlight=create#pyquickhelper.filehelper.visual_sync.create_visual_diff_through_html_files>`_
Check blog post :ref:`b-textdiff` to see an example.
"""
parser = self.get_parser(MagicFile.filediff_parser, "filediff")
parser = self.get_parser(MagicFile.textdiff_parser, "textdiff")
args = self.get_args(line, parser)

if args is not None:
html, js = create_visual_diff_through_html_files(args.f1, args.f2, encoding=args.encoding, notebook=True)
display_javascript(js)
return html
html, js = create_visual_diff_through_html_files(args.f1, args.f2, encoding=args.encoding, notebook=True,
context_size=None if args.context in [
None, ""] else int(args.context),
inline_view=args.inline)
display_html(html)
return js


def register_file_magics():
Expand Down

0 comments on commit e7257e7

Please sign in to comment.