Skip to content

Fix CustomCssHandler path resolution, UTF-8 encoding, and 404 handling - #8149

Open
vishwapramuditha wants to merge 2 commits into
jupyter:mainfrom
vishwapramuditha:main
Open

vishwapramuditha wants to merge 2 commits into
jupyter:mainfrom
vishwapramuditha:main

Conversation

@vishwapramuditha

Copy link
Copy Markdown

References

N/A (code quality & robustness improvement for CustomCssHandler)

Code changes

  • notebook/app.py (CustomCssHandler.get):
    • Replaced the re.match("^(.*?)static", page_config["staticDir"]) string-based fallback with pathlib.Path resolution across three ordered candidates:
      1. Path(jupyterConfigDir) / "custom" / "custom.css"
      2. Path(staticDir).parent / "custom" / "custom.css"
      3. HERE / "custom" / "custom.css" (built-in package fallback)
    • Read custom.css using read_text(encoding="utf-8") instead of locale-dependent .open() to avoid encoding errors on Windows with UTF-8 content.
    • Set Content-Type: text/css when a file is served and raise web.HTTPError(404) if no candidate exists, instead of raising an unhandled FileNotFoundError (HTTP 500).
    • Removed the now-unused import re.
  • tests/test_app.py (test_custom_css_handler):
    • Added tests covering package fallback, UTF-8 custom.css overrides from jupyterConfigDir, and returning 404 when no candidate file exists.

User-facing changes

None. Existing custom.css behavior is preserved, while avoiding 500 errors when staticDir is customized or custom.css contains UTF-8 characters on Windows.

Backwards-incompatible changes

None.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant