8000 `difflib`-generated HTML file does not use monospaced font · Issue #131204 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tfpf opened this issue Mar 13, 2025 · 2 comments · Fixed by #131221
Closed

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

tfpf opened this issue Mar 13, 2025 · 2 comments · Fixed by #131221
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tfpf
Copy link
tfpf commented Mar 13, 2025

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

@tfpf tfpf added the type-bug An unexpected behavior, bug, or error label Mar 13, 2025
@hugovk
8000 Copy link
Member
hugovk commented Mar 13, 2025

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/

@picnixz picnixz added the stdlib Python modules in the Lib dir label Mar 13, 2025
@picnixz
Copy link
Member
picnixz commented Mar 13, 2025

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

@picnixz picnixz changed the title bug: difflib-generated HTML file does not use monospaced font difflib-generated HTML file does not use monospaced font Mar 13, 2025
hugovk added a commit that referenced this issue Mar 14, 2025
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
hugovk pushed a commit that referenced this issue Mar 14, 2025
hugovk pushed a commit that referenced this issue Mar 14, 2025
mikec9 pushed a commit to mikec9/cpython that referenced this issue Mar 14, 2025
plashchynski pushed a commit to plashchynski/cpython that referenced this issue Mar 17, 2025
…ython#131221)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…ython#131221)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0