Added datasette.get_database() method

Refs #576
This commit is contained in:
Simon Willison 2020-05-30 07:28:29 -07:00
commit 31fb006a9b
5 changed files with 42 additions and 1 deletions

View file

@ -44,6 +44,16 @@ This method lets you read plugin configuration values that were set in ``metadat
Renders a `Jinja template <https://jinja.palletsprojects.com/en/2.11.x/>`__ using Datasette's preconfigured instance of Jinja and returns the resulting string. The template will have access to Datasette's default template functions and any functions that have been made available by other plugins.
.. _datasette_get_database:
.get_database(name)
-------------------
``name`` - string, optional
The name of the database - optional.
Returns the specified database object. Raises a ``KeyError`` if the database does not exist. Call this method without an argument to return the first connected database.
.. _datasette_add_database:
.add_database(name, db)

View file

@ -811,7 +811,7 @@ Here is a more complex example:
.. code-block:: python
async def render_demo(datasette, columns, rows):
db = next(iter(datasette.databases.values()))
db = datasette.get_database()
result = await db.execute("select sqlite_version()")
first_row = " | ".join(columns)
lines = [first_row]