simonw / datasette

An open source multi-tool for exploring and publishing data
https://datasette.io
Apache License 2.0
9.61k stars 690 forks source link

Ability to set a custom facet_size per table #1804

Closed simonw closed 2 years ago

simonw commented 2 years ago

Suggestion from Discord: https://discord.com/channels/823971286308356157/823971286941302908/1016725586351247430

Is it possible to limit the facet size per database or even per table?

This is a really good idea, it could be done in metadata.yml.

simonw commented 2 years ago

Relevant example from https://docs.datasette.io/en/stable/metadata.html#per-database-and-per-table-metadata

{
    "databases": {
        "database1": {
            "tables": {
                "example_table": {
                    "sortable_columns": [
                        "height",
                        "weight"
                    ]
                }
            }
        }
    }
}

I think this becomes "facet_size": "..." at the table level, to match the name of the ?_facet_size=100 querystring parameter.

simonw commented 2 years ago

I'm going to do this just at the table level - if you want it at the database level I would imagine usually you would be OK running --setting default_facet_size 10 instead, since most Datasette instances only expose a single database.

I may reconsider this in the future.

simonw commented 2 years ago

Relevant code: https://github.com/simonw/datasette/blob/c9d1943aede436fa3413fd49bc56335cbda4ad07/datasette/facets.py#L102-L110

simonw commented 2 years ago

In that code self.table is the name of the current table, but can also be None if the faceting is being run against a custom SQL query instead (a future feature). self.database is the name of the current database.

So reading metadata via self.ds should work fine.

simonw commented 2 years ago

Still needs documentation.

simonw commented 2 years ago

facet_size is now documented here: https://docs.datasette.io/en/latest/facets.html#facets-in-metadata-json