Skip to content

Commit

Permalink
?_nofacets=1 option, closes #1350
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed May 31, 2021
1 parent f7d3e76 commit c5ae119
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
16 changes: 9 additions & 7 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,14 @@ async def data(
)
)

for facet in facet_instances:
(
instance_facet_results,
instance_facets_timed_out,
) = await facet.facet_results()
facet_results.update(instance_facet_results)
facets_timed_out.extend(instance_facets_timed_out)
if not request.args.get("_nofacets"):
for facet in facet_instances:
(
instance_facet_results,
instance_facets_timed_out,
) = await facet.facet_results()
facet_results.update(instance_facet_results)
facets_timed_out.extend(instance_facets_timed_out)

# Figure out columns and rows for the query
columns = [r[0] for r in results.description]
Expand Down Expand Up @@ -828,6 +829,7 @@ async def data(
self.ds.setting("suggest_facets")
and self.ds.setting("allow_facet")
and not _next
and not request.args.get("_nofacets")
):
for facet in facet_instances:
suggested_facets.extend(await facet.suggest())
Expand Down
2 changes: 2 additions & 0 deletions docs/facets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ If Datasette detects that a column is a foreign key, the ``"label"`` property wi

The default number of facet results returned is 30, controlled by the :ref:`setting_default_facet_size` setting. You can increase this on an individual page by adding ``?_facet_size=100`` to the query string, up to a maximum of :ref:`setting_max_returned_rows` (which defaults to 1000).

.. _facets_metadata:

Facets in metadata.json
-----------------------

Expand Down
3 changes: 3 additions & 0 deletions docs/json_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ Special table arguments
``?_facet_size=100``
Increase the number of facet results returned for each facet. Use ``?_facet_size=max`` for the maximum available size, determined by :ref:`setting_max_returned_rows`.

``?_nofacets=1``
Disable all facets and facet suggestions for this page, including any defined by :ref:`facets_metadata`.

``?_trace=1``
Turns on tracing for this page: SQL queries executed during the request will
be gathered and included in the response, either in a new ``"_traces"`` key
Expand Down
14 changes: 14 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,20 @@ def test_suggest_facets_off():
assert [] == client.get("/fixtures/facetable.json").json["suggested_facets"]


@pytest.mark.parametrize("nofacets", (True, False))
def test_nofacets(app_client, nofacets):
path = "/fixtures/facetable.json?_facet=state"
if nofacets:
path += "&_nofacets=1"
response = app_client.get(path)
if nofacets:
assert response.json["suggested_facets"] == []
assert response.json["facet_results"] == {}
else:
assert response.json["suggested_facets"] != []
assert response.json["facet_results"] != {}


def test_expand_labels(app_client):
response = app_client.get(
"/fixtures/facetable.json?_shape=object&_labels=1&_size=2"
Expand Down

0 comments on commit c5ae119

Please sign in to comment.