Skip to content

difflib-generated HTML file does not use monospaced font #131204

Closed

Description

@tfpf

Bug report

Bug description:

difflib.HtmlDiff.make_file writes an HTML file with a CSS rule specifying Courier as the font to use. On systems which don't have it installed, browsers may fall back to the system default font, which likely won't be monospaced.

import difflib
import webbrowser

hd = difflib.HtmlDiff()
html = hd.make_file(["a", "aab", "c" + "f" * 20, "eg", "ry"], ["aab", "f" * 19, "ry"])
with open("diff.html", "w") as writer:
    print(html, file=writer)
webbrowser.open("diff.html")

On my system (which does not have Courier), the default font is Signika, which is what Firefox used to render the text.

Image

  • Python 3.13.2 (main, Feb 5 2025, 08:05:21) [GCC 14.2.1 20250128]
  • glibc 2.41+r9+ga900dbaf70f0-1
  • Arch Linux (64-bit)

I think is is sufficient to do:

diff --git a/Lib/difflib.py b/Lib/difflib.py
index bc09aa128aa..11d286a4aa8 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1633,7 +1633,7 @@ def _line_pair_iterator():
 
 _styles = """
         :root {color-scheme: light dark}
-        table.diff {font-family:Courier; border:medium;}
+        table.diff {font-family:monospace; border:medium;}
         .diff_header {background-color:#e0e0e0}
         td.diff_header {text-align:right}
         .diff_next {background-color:#c0c0c0}

and will follow up with a pull request soon.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Mar 13, 2025
  2. hugovk commented on Mar 13, 2025

    @hugovk
    Member

    Shall we use the System Font Stack for better cross-platform support?

    font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;

    https://systemfontstack.com/

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 13, 2025
  4. picnixz commented on Mar 13, 2025

    @picnixz
    Member

    (I think we can consider it as a bugfix as it hinders readability)

  5. changed the title [-]bug: `difflib`-generated HTML file does not use monospaced font[/-] [+]`difflib`-generated HTML file does not use monospaced font[/+] on Mar 13, 2025
  6. added 3 commits that reference this issue on Mar 14, 2025
  7. added a commit that references this issue on Mar 14, 2025
  8. added a commit that references this issue on Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions