datasette icon indicating copy to clipboard operation
datasette copied to clipboard

Allow routes to have extra options

Open fgregg opened this issue 4 years ago • 5 comments

Right now, datasette routes can only be a 2-tuple of (regex, view_fn).

If it was possible for datasette to handle extra options, like standard Django does, it would add flexibility for plugin authors.

For example, if extra options were enabled, then it would be easy to make a single table the home page (#1284). This plugin would accomplish it.

from datasette import hookimpl
from datasette.views.table import TableView

@hookimpl
def register_routes(datasette):
        return [
        (r"^/$", TableView.as_view(datasette), {'db_name': 'DB_NAME',
                                                'table': 'TABLE_NAME'})
    ]

fgregg avatar Oct 22 '21 15:10 fgregg

This is a great idea - I've wanted this myself before, but never spent any time thinking about how to achieve it.

I think your design here is exactly right - an optional third item in the tuple consisting of a dictionary of options to pass to the view function.

simonw avatar Oct 24 '21 22:10 simonw

To land this change we'll need a unit test that demonstrates the new capability - I suggest putting that next to this test: https://github.com/simonw/datasette/blob/15a9d4abfff0c45dee2a9f851326e1d61b1c678c/tests/test_plugins.py#L648-L659

It will also need documentation, which should be added here: https://github.com/simonw/datasette/blob/15a9d4abfff0c45dee2a9f851326e1d61b1c678c/docs/plugin_hooks.rst#register-routes-datasette

simonw avatar Oct 24 '21 22:10 simonw

okay @simonw, made the requested changes. tests are running locally. i think this is ready for you to look at again.

fgregg avatar Oct 29 '21 03:10 fgregg

This all looks promising! I will need detailed documentation on how to upgrade datasette once it's available, and how to implement. (@fgregg example looks very straightforward on the plugin front.) I'll be so excited if I can get: https://list.saferdisinfectants.org/ instead of https://list.saferdisinfectants.org/disinfectants/listN

mroswell avatar Nov 04 '21 03:11 mroswell

A nudge on this.

mroswell avatar Nov 19 '21 14:11 mroswell