Last active
May 11, 2022 13:10
-
-
Save chongshenng/9b7eb6af66ba034f60ff63a82dc90226 to your computer and use it in GitHub Desktop.
Python function to print text diffs with html using Google's diff-match-patch
This file contains hidden or 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
| # Import tools for displaying Htmls | |
| from IPython.display import display, HTML | |
| # Import library for string diff | |
| import diff_match_patch as dmp_module | |
| def print_text_diff(text1, text2): | |
| # Compute the diff | |
| dmp = dmp_module.diff_match_patch() | |
| diff = dmp.diff_main(text1, text2) | |
| # Return a pretty Html sequence | |
| htmlSnippet = dmp.diff_prettyHtml(diff) | |
| return display(HTML(htmlSnippet)) | |
| text1 = "Hello world!" | |
| text2 = "Goodbye world!" | |
| print_text_diff(text1, text2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment