Skip to content

Conversation

fgregg
Copy link
Contributor

@fgregg fgregg commented Oct 22, 2021

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'})
    ]

datasette/app.py Outdated
assert len(extra_options) == 1
routes.append((regex, wrapped_view, extra_options[0]))
else:
routes.append((regex, wrapped_view))

def add_route(view, regex):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should gain a third optional options parameter. The code above can then call this method.

Also, I think the internal format of the routes property should change to a list of three-tuples, where tuples with no options have None as their third item.

@simonw
Copy link
Owner

simonw commented Oct 24, 2021

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
Copy link
Owner

simonw commented Oct 24, 2021

To land this change we'll need a unit test that demonstrates the new capability - I suggest putting that next to this test:

@pytest.mark.parametrize(
"path,body",
[
("/one/", "2"),
("/two/Ray?greeting=Hail", "Hail Ray"),
("/not-async/", "This was not async"),
],
)
def test_hook_register_routes(app_client, path, body):
response = app_client.get(path)
assert 200 == response.status
assert body == response.text

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

@fgregg fgregg changed the title [SPIKE] allow routes to have extra options Allow routes to have extra options Oct 29, 2021
@fgregg
Copy link
Contributor Author

fgregg commented Oct 29, 2021

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

@mroswell
Copy link
Contributor

mroswell commented Nov 4, 2021

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
Copy link
Contributor

A nudge on this.

@fgregg fgregg requested a review from simonw November 19, 2021 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants