Skip to content

Commit

Permalink
Show count of facet values if ?_facet_size=max, closes #1423
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 16, 2021
1 parent 2883098 commit adb5b70
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions datasette/static/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ form button[type=button] {
width: 250px;
margin-right: 15px;
}
.facet-info-total {
font-size: 0.8em;
color: #666;
padding-right: 0.25em;
}
.facet-info li,
.facet-info ul {
margin: 0;
Expand Down
4 changes: 3 additions & 1 deletion datasette/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ <h3>{{ extra_wheres_for_ui|length }} extra where clause{% if extra_wheres_for_ui
{% for facet_info in sorted_facet_results %}
<div class="facet-info facet-{{ database|to_css_class }}-{{ table|to_css_class }}-{{ facet_info.name|to_css_class }}" id="facet-{{ facet_info.name|to_css_class }}">
<p class="facet-info-name">
<strong>{{ facet_info.name }}{% if facet_info.type != "column" %} ({{ facet_info.type }}){% endif %}</strong>
<strong>{{ facet_info.name }}{% if facet_info.type != "column" %} ({{ facet_info.type }}){% endif %}
{% if show_facet_counts %} <span class="facet-info-total">{% if facet_info.truncated %}&gt;{% endif %}{{ facet_info.results|length }}</span>{% endif %}
</strong>
{% if facet_info.hideable %}
<a href="{{ facet_info.toggle_url }}" class="cross">&#x2716;</a>
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ async def table_actions():
key=lambda f: (len(f["results"]), f["name"]),
reverse=True,
),
"show_facet_counts": special_args.get("_facet_size") == "max",
"extra_wheres_for_ui": extra_wheres_for_ui,
"form_hidden_args": form_hidden_args,
"is_sortable": any(c["sortable"] for c in display_columns),
Expand Down
22 changes: 21 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def test_facet_display(app_client):
for div in divs:
actual.append(
{
"name": div.find("strong").text,
"name": div.find("strong").text.split()[0],
"items": [
{
"name": a.text,
Expand Down Expand Up @@ -1797,3 +1797,23 @@ def test_column_metadata(app_client):
soup.select("th[data-column=address]")[0]["data-column-description"]
== "The street address for the attraction"
)


@pytest.mark.parametrize("use_facet_size_max", (True, False))
def test_facet_total_shown_if_facet_max_size(use_facet_size_max):
# https://github.com/simonw/datasette/issues/1423
with make_app_client(settings={"max_returned_rows": 100}) as client:
path = "/fixtures/sortable?_facet=content&_facet=pk1"
if use_facet_size_max:
path += "&_facet_size=max"
response = client.get(path)
assert response.status == 200
fragments = (
'<span class="facet-info-total">&gt;100</span>',
'<span class="facet-info-total">8</span>',
)
for fragment in fragments:
if use_facet_size_max:
assert fragment in response.text
else:
assert fragment not in response.text

0 comments on commit adb5b70

Please sign in to comment.