simonw / datasette

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

Make original path available to render hooks #1179

Open simonw opened 3 years ago

simonw commented 3 years ago

https://github.com/simonw/datasette-export-notebook/blob/0.1/datasette_export_notebook/__init__.py

async def render_notebook(datasette, request):
    return Response.html(
        await datasette.render_template(
            "export_notebook.html",
            {
                "csv_stream_url": datasette.absolute_url(
                    request,
                    path_with_format(
                        request=request, format="csv", extra_qs={"_stream": "on"}
                    ),
                ),
                "json_url": datasette.absolute_url(
                    request,
                    path_with_format(
                        request=request, format="json", extra_qs={"_shape": "array"}
                    ),
                ),
                "json": json,
            },
        )
    )

This results in https://latest-with-plugins.datasette.io/github/issue_comments.Notebook showing http://latest-with-plugins.datasette.io/github/issue_comments.Notebook?_format=json&_shape=array

simonw commented 3 years ago

An optional path argument to https://docs.datasette.io/en/stable/plugin_hooks.html#register-output-renderer-datasette which shows the path WITHOUT the .Notebook extension would be useful here.

simonw commented 3 years ago

This parameter will return the URL path, with querystring arguments, to the HTML version of the page - e.g. /github/issue_comments or /github/issue_comments?_sort_desc=created_at

Open questions:

simonw commented 3 years ago

Django calls this HttpRequest.get_full_path() - for the path plus the querystring.

simonw commented 3 years ago

I think I'll call this full_path for consistency with Django.

simonw commented 3 years ago

In that case maybe there are three new arguments: path, full_path and url.

I'll also add request.full_path for consistency with these: https://github.com/simonw/datasette/blob/97fb10c17dd007a275ab743742e93e932335ad67/datasette/utils/asgi.py#L77-L90

simonw commented 3 years ago

I'm just going to do path and full_path (which includes the querystring). Thedatasette.absolute_url()` method can be used by plugins that need the full URL.

simonw commented 3 years ago

The challenge here is figuring out what the original path, without the .format, actually was - while taking into account that Datasette has a special case for tables that themselves end in a .something.

The path_with_format() function nearly does what we need here:

https://github.com/simonw/datasette/blob/b6a7b58fa01af0cd5a5e94bd17d686d283a46819/datasette/utils/__init__.py#L710-L729

It can be called with replace_format="csv" to REMOVE the .csv format and replace it with something else.

Problem is, we want to use it to get rid of the format entirely.

We could update path_with_format() to accept format='' to mean "remove the format entirely", but it's a bit messy. It may be better to reconsider the design of path_with_format() and related utility functions entirely.

simonw commented 3 years ago

Relevant existing tests: https://github.com/simonw/datasette/blob/461670a0b87efa953141b449a9a261919864ceb3/tests/test_utils.py#L365-L398