Skip to content
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

Faster icons #303

Merged
merged 1 commit into from
Oct 15, 2024
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ examples = [
# installation via the Git URL, because build artifacts aren't
# version-controlled.
[tool.setuptools.package-data]
viser = ["py.typed", "*.pyi", "_icons/tabler-icons.tar", "client/**/*", "client/**/.*"]
viser = ["py.typed", "*.pyi", "_icons/tabler-icons.zip", "client/**/*", "client/**/.*"]
# </>

[tool.setuptools.exclude-package-data]
Expand Down
13 changes: 7 additions & 6 deletions src/viser/_icons.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import tarfile
import zipfile
from functools import lru_cache
from pathlib import Path

from ._icons_enum import IconName

ICONS_DIR = Path(__file__).absolute().parent / "_icons"


@lru_cache(maxsize=32)
def svg_from_icon(icon_name: IconName) -> str:
"""Read an icon and return it as a UTF string; we expect this to be an
<svg /> tag."""
assert isinstance(icon_name, str)
icons_tarball = ICONS_DIR / "tabler-icons.tar"
icons_zipfile = ICONS_DIR / "tabler-icons.zip"

with tarfile.open(icons_tarball) as tar:
icon_file = tar.extractfile(f"{icon_name}.svg")
assert icon_file is not None
out = icon_file.read()
with zipfile.ZipFile(icons_zipfile) as zip_file:
with zip_file.open(f"{icon_name}.svg") as icon_file:
out = icon_file.read()

return out.decode("utf-8")
Binary file removed src/viser/_icons/tabler-icons.tar
Binary file not shown.
Binary file added src/viser/_icons/tabler-icons.zip
Binary file not shown.
8 changes: 5 additions & 3 deletions src/viser/_icons_generate_enum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Helper script for dumping Tabler icon names into a Literal type."""

import tarfile
import zipfile
from pathlib import Path

HERE_DIR = Path(__file__).absolute().parent
Expand All @@ -17,8 +17,10 @@ def enum_name_from_icon(name: str) -> str:


if __name__ == "__main__":
with tarfile.open(ICON_DIR / "tabler-icons.tar") as tar:
icon_names = sorted([name.partition(".svg")[0] for name in tar.getnames()])
with zipfile.ZipFile(ICON_DIR / "tabler-icons.zip") as zip_file:
icon_names = sorted(
[Path(name).stem for name in zip_file.namelist() if name.endswith(".svg")]
)

# Generate stub file. This is used by type checkers.
(HERE_DIR / "_icons_enum.pyi").write_text(
Expand Down
Loading