这是indexloc提供的服务,不要输入任何密码
Skip to content

Move CSS from static file to Python module #58

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

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed

- Fixed an error generating reprs when the default system encoding is `gbk`

### Performance

- Avoid stringifying long lists that will definitely be truncated in the repr (~20% speedup when testing with a 25-image Sentinel-2 collection)

### Changed

- CSS is loaded from a Python module instead of a static file

## [0.1.0] - 2025-01-10

### Breaking Changes
Expand Down
26 changes: 2 additions & 24 deletions eerepr/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from eerepr.config import Config
from eerepr.html import convert_to_html, escape_object
from eerepr.style import CSS

REPR_HTML = "_repr_html_"
EEObject = Union[ee.Element, ee.ComputedObject]
Expand All @@ -19,28 +20,6 @@
options = Config()


def _load_file(package: str, resource: str) -> str:
"""
Compatibility wrapper for deprecated `importlib.resources.read_text`.

Replace with `importlib.resources.files` once support for Python < 3.9 is dropped.
"""
try:
# Python >= 3.9
from importlib.resources import files

return files(package).joinpath(resource).read_text()
except ImportError:
from importlib.resources import read_text

return read_text(package, resource)


@lru_cache(maxsize=1)
def _load_css() -> str:
return _load_file("eerepr.static.css", "style.css")


def _attach_html_repr(cls: type, repr: Any) -> None:
"""Add a HTML repr method to an EE class. Only overwrite the method if it was set by
this function.
Expand All @@ -66,12 +45,11 @@ def _repr_html_(obj: EEObject) -> str:
"""Generate an HTML representation of an EE object."""
# Escape all strings in object info to prevent injection
info = escape_object(obj.getInfo())
css = _load_css()
body = convert_to_html(info)

return (
"<div>"
f"<style>{css}</style>"
f"<style>{CSS}</style>"
"<div class='eerepr'>"
f"<ul>{body}</ul>"
"</div>"
Expand Down
Empty file removed eerepr/static/__init__.py
Empty file.
Empty file removed eerepr/static/css/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion eerepr/static/css/style.css → eerepr/style.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:root {
CSS = r""":root {
--font-color-primary: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--font-color-secondary: var(--jp-content-font-color2, rgba(0, 0, 0, 0.7));
--font-color-accent: rgba(123, 31, 162, 1);
Expand Down Expand Up @@ -85,3 +85,4 @@
.eerepr details summary {
list-style-type: none;
}
"""