Implemented import shortcuts, closes #957

This commit is contained in:
Simon Willison 2022-02-05 22:34:33 -08:00
commit 8a25ea9bca
4 changed files with 29 additions and 1 deletions

View file

@ -935,3 +935,18 @@ This example uses the :ref:`register_routes() <plugin_register_routes>` plugin h
Adding ``?_trace=1`` will show that the trace covers both of those child tasks.
.. _internals_shortcuts:
Import shortcuts
================
The following commonly used symbols can be imported directly from the ``datasette`` module:
.. code-block:: python
from datasette import Response
from datasette import Forbidden
from datasette import NotFound
from datasette import hookimpl
from datasette import actor_matches_allow

View file

@ -542,7 +542,7 @@ Return a list of ``(regex, view_function)`` pairs, something like this:
.. code-block:: python
from datasette.utils.asgi import Response
from datasette import Response
import html
@ -582,6 +582,8 @@ The view function can be a regular function or an ``async def`` function, depend
The function can either return a :ref:`internals_response` or it can return nothing and instead respond directly to the request using the ASGI ``send`` function (for advanced uses only).
It can also rase the ``datasette.NotFound`` exception to return a 404 not found error, or the ``datasette.Forbidden`` exception for a 403 forbidden.
See :ref:`writing_plugins_designing_urls` for tips on designing the URL routes used by your plugin.
Examples: `datasette-auth-github <https://datasette.io/plugins/datasette-auth-github>`__, `datasette-psutil <https://datasette.io/plugins/datasette-psutil>`__