Skip to content

Instantly share code, notes, and snippets.

@chongshenng
Last active May 11, 2022 13:10
Show Gist options
  • Select an option

  • Save chongshenng/9b7eb6af66ba034f60ff63a82dc90226 to your computer and use it in GitHub Desktop.

Select an option

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
# 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