mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Facet results now have "truncated" field
To indicate if there was more than 20 distinct values. Refs #255
This commit is contained in:
parent
a82175276c
commit
142a550a99
3 changed files with 128 additions and 95 deletions
|
|
@ -95,12 +95,15 @@
|
|||
<p>Suggested facets: {% for facet in suggested_facets %}<a href="{{ facet.toggle_url }}">{{ facet.name }}</a> {% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% for facet_name, facet_values in facet_results.items() %}
|
||||
{% for facet_name, facet_info in facet_results.items() %}
|
||||
<p><strong>{{ facet_name }}</strong></p>
|
||||
<ul>
|
||||
{% for facet_value in facet_values %}
|
||||
{% for facet_value in facet_info.results %}
|
||||
<li><a href="{{ facet_value.toggle_url }}">{{ facet_value.value }}</a> ({{ facet_value.count }})</li>
|
||||
{% endfor %}
|
||||
{% if facet_info.truncated %}
|
||||
<li>...</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
{% endfor %}
|
||||
|
||||
|
|
|
|||
|
|
@ -490,6 +490,7 @@ class TableView(RowTableShared):
|
|||
)
|
||||
|
||||
# facets support
|
||||
FACET_SIZE = 20
|
||||
try:
|
||||
facets = request.args["_facet"]
|
||||
except KeyError:
|
||||
|
|
@ -499,18 +500,23 @@ class TableView(RowTableShared):
|
|||
facet_sql = """
|
||||
select {col} as value, count(*) as count
|
||||
{from_sql} {and_or_where} {col} is not null
|
||||
group by {col} order by count desc limit 20
|
||||
group by {col} order by count desc limit {limit}
|
||||
""".format(
|
||||
col=escape_sqlite(column),
|
||||
from_sql=from_sql,
|
||||
and_or_where='and' if where_clauses else 'where',
|
||||
limit=FACET_SIZE+1,
|
||||
)
|
||||
try:
|
||||
facet_rows = await self.execute(
|
||||
name, facet_sql, params, truncate=False, custom_time_limit=200
|
||||
)
|
||||
facet_results[column] = []
|
||||
for row in facet_rows:
|
||||
facet_results[column] = {
|
||||
"name": column,
|
||||
"results": [],
|
||||
"truncated": len(facet_rows) > FACET_SIZE,
|
||||
}
|
||||
for row in facet_rows[:FACET_SIZE]:
|
||||
selected = str(other_args.get(column)) == str(row["value"])
|
||||
if selected:
|
||||
toggle_path = path_with_removed_args(
|
||||
|
|
@ -520,7 +526,7 @@ class TableView(RowTableShared):
|
|||
toggle_path = path_with_added_args(
|
||||
request, {column: row["value"]}
|
||||
)
|
||||
facet_results[column].append({
|
||||
facet_results[column]["results"].append({
|
||||
"value": row["value"],
|
||||
"count": row["count"],
|
||||
"toggle_url": urllib.parse.urljoin(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue