Download tasks/columbia/environment/reference/clone/site/grid.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 5.51 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/columbia/environment/reference/clone/site/grid.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/columbia/environment/reference/clone/site/grid.py
-
curl -L -o grid.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/columbia/environment/reference/clone/site/grid.py
5.51 kB
| """Faceted grid engine: refinements, counts, sorting and pagination. | |
| Mirrors the source's own grid contract observed on /c/womens/ and /search/: | |
| multi-select refinements with live hit counts, five sorting options, and a | |
| `pagesize`-driven "Show More" that grows the rendered window. | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import Iterable, Sequence | |
| from catalog import PRICE_BUCKETS, REFINEMENTS, Product | |
| PAGE_SIZE = 47 | |
| PAGE_STEP = 48 | |
| # Source-observed `quickFilters` on /c/womens/: only these render as pills in | |
| # the control row; the rest stay behind the "All Filters" panel. | |
| QUICK_FILTERS = ( | |
| "c_productType", "c_bigTall", "c_size", "c_technologyBenefit", "c_activity", | |
| ) | |
| class RefinementValue: | |
| label: str | |
| hit_count: int | |
| selected: bool | |
| class Refinement: | |
| attribute_id: str | |
| label: str | |
| values: list[RefinementValue] | |
| def selected_count(self) -> int: | |
| return sum(1 for value in self.values if value.selected) | |
| class GridResult: | |
| products: list[Product] | |
| total: int | |
| shown: int | |
| refinements: list[Refinement] | |
| sort_id: str | |
| page_size: int | |
| def quick(self) -> list["Refinement"]: | |
| order = {attribute: index for index, attribute in enumerate(QUICK_FILTERS)} | |
| chosen = [r for r in self.refinements if r.attribute_id in order] | |
| chosen.sort(key=lambda r: order[r.attribute_id]) | |
| return chosen | |
| def rest(self) -> list["Refinement"]: | |
| return [r for r in self.refinements if r.attribute_id not in QUICK_FILTERS] | |
| def has_more(self) -> bool: | |
| return self.shown < self.total | |
| def next_page_size(self) -> int: | |
| return self.page_size + PAGE_STEP | |
| def remaining(self) -> int: | |
| return max(self.total - self.shown, 0) | |
| def parse_refine(values: Sequence[str]) -> dict[str, set[str]]: | |
| """Parse the source's `refine=<attribute>=<value>` repeated parameter.""" | |
| selected: dict[str, set[str]] = {} | |
| for raw in values: | |
| if "=" not in raw: | |
| continue | |
| attribute, _, value = raw.partition("=") | |
| selected.setdefault(attribute.strip(), set()).add(value.strip()) | |
| return selected | |
| def _matches(product: Product, attribute: str, wanted: set[str]) -> bool: | |
| source_field = _FIELD_FOR.get(attribute) | |
| if source_field is None: | |
| return True | |
| return bool(wanted & set(product.facet_values(source_field))) | |
| _FIELD_FOR = {attribute: field for attribute, _label, field in REFINEMENTS} | |
| _LABEL_FOR = {attribute: label for attribute, label, _field in REFINEMENTS} | |
| def _sort(products: list[Product], sort_id: str) -> list[Product]: | |
| def price(product: Product) -> float: | |
| value = product.sale_price if product.sale_price is not None else product.min_price | |
| return value if value is not None else 0.0 | |
| if sort_id == "price-low-to-high": | |
| return sorted(products, key=price) | |
| if sort_id == "price-high-to-low": | |
| return sorted(products, key=price, reverse=True) | |
| if sort_id == "newest": | |
| return sorted(products, key=lambda p: (p.grid_badge != "New", p.name)) | |
| if sort_id == "top-rated": | |
| # The source ranks by review score, which this clone did not capture. | |
| # Fall back to the stable featured order rather than inventing ratings. | |
| return list(products) | |
| return list(products) | |
| def build( | |
| products: Iterable[Product], | |
| selected: dict[str, set[str]], | |
| sort_id: str = "featured-new", | |
| page_size: int = PAGE_SIZE, | |
| ) -> GridResult: | |
| pool = list(products) | |
| def passes(product: Product, skip: str | None = None) -> bool: | |
| for attribute, wanted in selected.items(): | |
| if attribute == skip or not wanted: | |
| continue | |
| if not _matches(product, attribute, wanted): | |
| return False | |
| return True | |
| matched = [product for product in pool if passes(product)] | |
| refinements: list[Refinement] = [] | |
| for attribute, label, source_field in REFINEMENTS: | |
| # Counts for a facet are computed with that facet's own selection | |
| # removed, which is what multi-select refinement panels show. | |
| others = [product for product in pool if passes(product, skip=attribute)] | |
| counts: dict[str, int] = {} | |
| for product in others: | |
| for value in product.facet_values(source_field): | |
| counts[value] = counts.get(value, 0) + 1 | |
| if not counts: | |
| continue | |
| chosen = selected.get(attribute, set()) | |
| if attribute == "price": | |
| ordered = [label for label, _low, _high in PRICE_BUCKETS if label in counts] | |
| else: | |
| ordered = sorted(counts, key=lambda value: (-counts[value], value)) | |
| refinements.append( | |
| Refinement( | |
| attribute_id=attribute, | |
| label=label, | |
| values=[ | |
| RefinementValue(label=value, hit_count=counts[value], selected=value in chosen) | |
| for value in ordered | |
| ], | |
| ) | |
| ) | |
| ordered_products = _sort(matched, sort_id) | |
| window = ordered_products[:page_size] | |
| return GridResult( | |
| products=window, | |
| total=len(ordered_products), | |
| shown=len(window), | |
| refinements=refinements, | |
| sort_id=sort_id, | |
| page_size=page_size, | |
| ) | |