id,page,ref,title,content,breadcrumbs,references
internals:datasette-sign,internals,datasette-sign,".sign(value, namespace=""default"")","value - any serializable type
The value to be signed.
namespace - string, optional
An alternative namespace, see the itsdangerous salt documentation .
Utility method for signing values, such that you can safely pass data to and from an untrusted environment. This is a wrapper around the itsdangerous library.
This method returns a signed string, which can be decoded and verified using .unsign(value, namespace=""default"") .","[""Internals for plugins"", ""Datasette class""]","[{""href"": ""https://itsdangerous.palletsprojects.com/en/1.1.x/serializer/#the-salt"", ""label"": ""itsdangerous salt documentation""}, {""href"": ""https://itsdangerous.palletsprojects.com/"", ""label"": ""itsdangerous""}]"
internals:datasette-render-template,internals,datasette-render-template,"await .render_template(template, context=None, request=None)","template - string, list of strings or jinja2.Template
The template file to be rendered, e.g. my_plugin.html . Datasette will search for this file first in the --template-dir= location, if it was specified - then in the plugin's bundled templates and finally in Datasette's set of default templates.
If this is a list of template file names then the first one that exists will be loaded and rendered.
If this is a Jinja Template object it will be used directly.
context - None or a Python dictionary
The context variables to pass to the template.
request - request object or None
If you pass a Datasette request object here it will be made available to the template.
Renders a Jinja template 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.","[""Internals for plugins"", ""Datasette class""]","[{""href"": ""https://jinja.palletsprojects.com/en/2.11.x/api/#jinja2.Template"", ""label"": ""Template object""}, {""href"": ""https://jinja.palletsprojects.com/en/2.11.x/"", ""label"": ""Jinja template""}]"
ecosystem:sqlite-utils,ecosystem,sqlite-utils,sqlite-utils,"sqlite-utils is a key building block for the wider Datasette ecosystem. It provides a collection of utilities for manipulating SQLite databases, both as a Python library and a command-line utility. Features include:
Insert data into a SQLite database from JSON, CSV or TSV, automatically creating tables with the correct schema or altering existing tables to add missing columns.
Configure tables for use with SQLite full-text search, including creating triggers needed to keep the search index up-to-date.
Modify tables in ways that are not supported by SQLite's default ALTER TABLE syntax - for example changing the types of columns or selecting a new primary key for a table.
Adding foreign keys to existing database tables.
Extracting columns of data into a separate lookup table.","[""The Datasette Ecosystem""]","[{""href"": ""https://sqlite-utils.datasette.io/"", ""label"": ""sqlite-utils""}]"
full_text_search:configuring-fts-using-sqlite-utils,full_text_search,configuring-fts-using-sqlite-utils,Configuring FTS using sqlite-utils,"sqlite-utils is a CLI utility and Python library for manipulating SQLite databases. You can use it from Python code to configure FTS search, or you can achieve the same goal using the accompanying command-line tool .
Here's how to use sqlite-utils to enable full-text search for an items table across the name and description columns:
$ sqlite-utils enable-fts mydatabase.db items name description","[""Full-text search"", ""Enabling full-text search for a SQLite table""]","[{""href"": ""https://sqlite-utils.datasette.io/"", ""label"": ""sqlite-utils""}, {""href"": ""https://sqlite-utils.datasette.io/en/latest/python-api.html#enabling-full-text-search"", ""label"": ""it from Python code""}, {""href"": ""https://sqlite-utils.datasette.io/en/latest/cli.html#configuring-full-text-search"", ""label"": ""using the accompanying command-line tool""}]"
internals:datasette-unsign,internals,datasette-unsign,".unsign(value, namespace=""default"")","signed - any serializable type
The signed string that was created using .sign(value, namespace=""default"") .
namespace - string, optional
The alternative namespace, if one was used.
Returns the original, decoded object that was passed to .sign(value, namespace=""default"") . If the signature is not valid this raises a itsdangerous.BadSignature exception.","[""Internals for plugins"", ""Datasette class""]",[]
changelog:id70,changelog,id70,0.33 (2019-12-22),"rowid is now included in dropdown menus for filtering tables ( #636 )
Columns are now only suggested for faceting if they have at least one value with more than one record ( #638 )
Queries with no results now display ""0 results"" ( #637 )
Improved documentation for the --static option ( #641 )
asyncio task information is now included on the /-/threads debug page
Bumped Uvicorn dependency 0.11
You can now use --port 0 to listen on an available port
New template_debug setting for debugging templates, e.g. https://latest.datasette.io/fixtures/roadside_attractions?_context=1 ( #654 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/636"", ""label"": ""#636""}, {""href"": ""https://github.com/simonw/datasette/issues/638"", ""label"": ""#638""}, {""href"": ""https://github.com/simonw/datasette/issues/637"", ""label"": ""#637""}, {""href"": ""https://github.com/simonw/datasette/issues/641"", ""label"": ""#641""}, {""href"": ""https://latest.datasette.io/fixtures/roadside_attractions?_context=1"", ""label"": ""https://latest.datasette.io/fixtures/roadside_attractions?_context=1""}, {""href"": ""https://github.com/simonw/datasette/issues/654"", ""label"": ""#654""}]"
internals:internals-multiparams,internals,internals-multiparams,The MultiParams class,"request.args is a MultiParams object - a dictionary-like object which provides access to query string parameters that may have multiple values.
Consider the query string ?foo=1&foo=2&bar=3 - with two values for foo and one value for bar .
request.args[key] - string
Returns the first value for that key, or raises a KeyError if the key is missing. For the above example request.args[""foo""] would return ""1"" .
request.args.get(key) - string or None
Returns the first value for that key, or None if the key is missing. Pass a second argument to specify a different default, e.g. q = request.args.get(""q"", """") .
request.args.getlist(key) - list of strings
Returns the list of strings for that key. request.args.getlist(""foo"") would return [""1"", ""2""] in the above example. request.args.getlist(""bar"") would return [""3""] . If the key is missing an empty list will be returned.
request.args.keys() - list of strings
Returns the list of available keys - for the example this would be [""foo"", ""bar""] .
key in request.args - True or False
You can use if key in request.args to check if a key is present.
for key in request.args - iterator
This lets you loop through every available key.
len(request.args) - integer
Returns the number of keys.","[""Internals for plugins""]",[]
internals:datasette-absolute-url,internals,datasette-absolute-url,".absolute_url(request, path)","request - Request
The current Request object
path - string
A path, for example /dbname/table.json
Returns the absolute URL for the given path, including the protocol and host. For example:
absolute_url = datasette.absolute_url(
request, ""/dbname/table.json""
)
# Would return ""http://localhost:8001/dbname/table.json""
The current request object is used to determine the hostname and protocol that should be used for the returned URL. The force_https_urls configuration setting is taken into account.","[""Internals for plugins"", ""Datasette class""]",[]
internals:datasette-add-message,internals,datasette-add-message,".add_message(request, message, type=datasette.INFO)","request - Request
The current Request object
message - string
The message string
type - constant, optional
The message type - datasette.INFO , datasette.WARNING or datasette.ERROR
Datasette's flash messaging mechanism allows you to add a message that will be displayed to the user on the next page that they visit. Messages are persisted in a ds_messages cookie. This method adds a message to that cookie.
You can try out these messages (including the different visual styling of the three message types) using the /-/messages debugging tool.","[""Internals for plugins"", ""Datasette class""]",[]
plugin_hooks:plugin-hook-filters-from-request,plugin_hooks,plugin-hook-filters-from-request,"filters_from_request(request, database, table, datasette)","request - Request object
The current HTTP request.
database - string
The name of the database.
table - string
The name of the table.
datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
This hook runs on the table page, and can influence the where clause of the SQL query used to populate that page, based on query string arguments on the incoming request.
The hook should return an instance of datasette.filters.FilterArguments which has one required and three optional arguments:
return FilterArguments(
where_clauses=[""id > :max_id""],
params={""max_id"": 5},
human_descriptions=[""max_id is greater than 5""],
extra_context={},
)
The arguments to the FilterArguments class constructor are as follows:
where_clauses - list of strings, required
A list of SQL fragments that will be inserted into the SQL query, joined by the and operator. These can include :named parameters which will be populated using data in params .
params - dictionary, optional
Additional keyword arguments to be used when the query is executed. These should match any :arguments in the where clauses.
human_descriptions - list of strings, optional
These strings will be included in the human-readable description at the top of the page and the page
.
extra_context - dictionary, optional
Additional context variables that should be made available to the table.html template when it is rendered.
This example plugin causes 0 results to be returned if ?_nothing=1 is added to the URL:
from datasette import hookimpl
from datasette.filters import FilterArguments
@hookimpl
def filters_from_request(self, request):
if request.args.get(""_nothing""):
return FilterArguments(
[""1 = 0""], human_descriptions=[""NOTHING""]
)
Example: datasette-leaflet-freedraw","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-leaflet-freedraw"", ""label"": ""datasette-leaflet-freedraw""}]"
changelog:new-plugin-hooks,changelog,new-plugin-hooks,New plugin hooks,"register_magic_parameters(datasette) can be used to define new types of magic canned query parameters.
startup(datasette) can run custom code when Datasette first starts up. datasette-init is a new plugin that uses this hook to create database tables and views on startup if they have not yet been created. ( #834 )
canned_queries(datasette, database, actor) lets plugins provide additional canned queries beyond those defined in Datasette's metadata. See datasette-saved-queries for an example of this hook in action. ( #852 )
forbidden(datasette, request, message) is a hook for customizing how Datasette responds to 403 forbidden errors. ( #812 )","[""Changelog"", ""0.45 (2020-07-01)""]","[{""href"": ""https://github.com/simonw/datasette-init"", ""label"": ""datasette-init""}, {""href"": ""https://github.com/simonw/datasette/issues/834"", ""label"": ""#834""}, {""href"": ""https://github.com/simonw/datasette-saved-queries"", ""label"": ""datasette-saved-queries""}, {""href"": ""https://github.com/simonw/datasette/issues/852"", ""label"": ""#852""}, {""href"": ""https://github.com/simonw/datasette/issues/812"", ""label"": ""#812""}]"
plugin_hooks:plugin-hook-publish-subcommand,plugin_hooks,plugin-hook-publish-subcommand,publish_subcommand(publish),"publish - Click publish command group
The Click command group for the datasette publish subcommand
This hook allows you to create new providers for the datasette publish
command. Datasette uses this hook internally to implement the default cloudrun
and heroku subcommands, so you can read
their source
to see examples of this hook in action.
Let's say you want to build a plugin that adds a datasette publish my_hosting_provider --api_key=xxx mydatabase.db publish command. Your implementation would start like this:
from datasette import hookimpl
from datasette.publish.common import (
add_common_publish_arguments_and_options,
)
import click
@hookimpl
def publish_subcommand(publish):
@publish.command()
@add_common_publish_arguments_and_options
@click.option(
""-k"",
""--api_key"",
help=""API key for talking to my hosting provider"",
)
def my_hosting_provider(
files,
metadata,
extra_options,
branch,
template_dir,
plugins_dir,
static,
install,
plugin_secret,
version_note,
secret,
title,
license,
license_url,
source,
source_url,
about,
about_url,
api_key,
):
...
Examples: datasette-publish-fly , datasette-publish-vercel","[""Plugin hooks""]","[{""href"": ""https://github.com/simonw/datasette/tree/main/datasette/publish"", ""label"": ""their source""}, {""href"": ""https://datasette.io/plugins/datasette-publish-fly"", ""label"": ""datasette-publish-fly""}, {""href"": ""https://datasette.io/plugins/datasette-publish-vercel"", ""label"": ""datasette-publish-vercel""}]"
internals:datasette-plugin-config,internals,datasette-plugin-config,".plugin_config(plugin_name, database=None, table=None)","plugin_name - string
The name of the plugin to look up configuration for. Usually this is something similar to datasette-cluster-map .
database - None or string
The database the user is interacting with.
table - None or string
The table the user is interacting with.
This method lets you read plugin configuration values that were set in metadata.json . See Writing plugins that accept configuration for full details of how this method should be used.
The return value will be the value from the configuration file - usually a dictionary.
If the plugin is not configured the return value will be None .","[""Internals for plugins"", ""Datasette class""]",[]
installation:installation-pipx,installation,installation-pipx,Using pipx,"pipx is a tool for installing Python software with all of its dependencies in an isolated environment, to ensure that they will not conflict with any other installed Python software.
If you use Homebrew on macOS you can install pipx like this:
brew install pipx
pipx ensurepath
Without Homebrew you can install it like so:
python3 -m pip install --user pipx
python3 -m pipx ensurepath
The pipx ensurepath command configures your shell to ensure it can find commands that have been installed by pipx - generally by making sure ~/.local/bin has been added to your PATH .
Once pipx is installed you can use it to install Datasette like this:
pipx install datasette
Then run datasette --version to confirm that it has been successfully installed.","[""Installation"", ""Advanced installation options""]","[{""href"": ""https://pipxproject.github.io/pipx/"", ""label"": ""pipx""}, {""href"": ""https://brew.sh/"", ""label"": ""Homebrew""}]"
internals:datasette-get-database,internals,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.","[""Internals for plugins"", ""Datasette class""]",[]
internals:datasette-remove-database,internals,datasette-remove-database,.remove_database(name),"name - string
The name of the database to be removed.
This removes a database that has been previously added. name= is the unique name of that database.","[""Internals for plugins"", ""Datasette class""]",[]
contributing:general-guidelines,contributing,general-guidelines,General guidelines,"main should always be releasable . Incomplete features should live in branches. This ensures that any small bug fixes can be quickly released.
The ideal commit should bundle together the implementation, unit tests and associated documentation updates. The commit message should link to an associated issue.
New plugin hooks should only be shipped if accompanied by a separate release of a non-demo plugin that uses them.","[""Contributing""]",[]
internals:datasette-setting,internals,datasette-setting,.setting(key),"key - string
The name of the setting, e.g. base_url .
Returns the configured value for the specified setting . This can be a string, boolean or integer depending on the requested setting.
For example:
downloads_are_allowed = datasette.setting(""allow_download"")","[""Internals for plugins"", ""Datasette class""]",[]
plugin_hooks:plugin-hook-prepare-jinja2-environment,plugin_hooks,plugin-hook-prepare-jinja2-environment,"prepare_jinja2_environment(env, datasette)","env - jinja2 Environment
The template environment that is being prepared
datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name)
This hook is called with the Jinja2 environment that is used to evaluate
Datasette HTML templates. You can use it to do things like register custom
template filters , for
example:
from datasette import hookimpl
@hookimpl
def prepare_jinja2_environment(env):
env.filters[""uppercase""] = lambda u: u.upper()
You can now use this filter in your custom templates like so:
Table name: {{ table|uppercase }}
This function can return an awaitable function if it needs to run any async code.
Examples: datasette-edit-templates","[""Plugin hooks""]","[{""href"": ""http://jinja.pocoo.org/docs/2.10/api/#custom-filters"", ""label"": ""register custom\n template filters""}, {""href"": ""https://datasette.io/plugins/datasette-edit-templates"", ""label"": ""datasette-edit-templates""}]"
authentication:authentication-ds-actor-expiry,authentication,authentication-ds-actor-expiry,Including an expiry time,"ds_actor cookies can optionally include a signed expiry timestamp, after which the cookies will no longer be valid. Authentication plugins may chose to use this mechanism to limit the lifetime of the cookie. For example, if a plugin implements single-sign-on against another source it may decide to set short-lived cookies so that if the user is removed from the SSO system their existing Datasette cookies will stop working shortly afterwards.
To include an expiry, add a ""e"" key to the cookie value containing a base62-encoded integer representing the timestamp when the cookie should expire. For example, here's how to set a cookie that expires after 24 hours:
import time
from datasette.utils import baseconv
expires_at = int(time.time()) + (24 * 60 * 60)
response = Response.redirect(""/"")
response.set_cookie(
""ds_actor"",
datasette.sign(
{
""a"": {""id"": ""cleopaws""},
""e"": baseconv.base62.encode(expires_at),
},
""actor"",
),
)
The resulting cookie will encode data that looks something like this:
{
""a"": {
""id"": ""cleopaws""
},
""e"": ""1jjSji""
}","[""Authentication and permissions"", ""The ds_actor cookie""]",[]
internals:datasette-add-database,internals,datasette-add-database,".add_database(db, name=None, route=None)","db - datasette.database.Database instance
The database to be attached.
name - string, optional
The name to be used for this database . If not specified Datasette will pick one based on the filename or memory name.
route - string, optional
This will be used in the URL path. If not specified, it will default to the same thing as the name .
The datasette.add_database(db) method lets you add a new database to the current Datasette instance.
The db parameter should be an instance of the datasette.database.Database class. For example:
from datasette.database import Database
datasette.add_database(
Database(
datasette,
path=""path/to/my-new-database.db"",
)
)
This will add a mutable database and serve it at /my-new-database .
Use is_mutable=False to add an immutable database.
.add_database() returns the Database instance, with its name set as the database.name attribute. Any time you are working with a newly added database you should use the return value of .add_database() , for example:
db = datasette.add_database(
Database(datasette, memory_name=""statistics"")
)
await db.execute_write(
""CREATE TABLE foo(id integer primary key)""
)","[""Internals for plugins"", ""Datasette class""]",[]
changelog:id87,changelog,id87,0.26 (2019-01-02),"datasette serve --reload now restarts Datasette if a database file changes on disk.
datasette publish now now takes an optional --alias mysite.now.sh argument. This will attempt to set an alias after the deploy completes.
Fixed a bug where the advanced CSV export form failed to include the currently selected filters ( #393 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/393"", ""label"": ""#393""}]"
changelog:id88,changelog,id88,0.25.2 (2018-12-16),"datasette publish heroku now uses the python-3.6.7 runtime
Added documentation on how to build the documentation
Added documentation covering our release process
Upgraded to pytest 4.0.2","[""Changelog""]",[]
publish:publish-custom-metadata-and-plugins,publish,publish-custom-metadata-and-plugins,Custom metadata and plugins,"datasette publish accepts a number of additional options which can be used to further customize your Datasette instance.
You can define your own Metadata and deploy that with your instance like so:
datasette publish cloudrun --service=my-service mydatabase.db -m metadata.json
If you just want to set the title, license or source information you can do that directly using extra options to datasette publish :
datasette publish cloudrun mydatabase.db --service=my-service \
--title=""Title of my database"" \
--source=""Where the data originated"" \
--source_url=""http://www.example.com/""
You can also specify plugins you would like to install. For example, if you want to include the datasette-vega visualization plugin you can use the following:
datasette publish cloudrun mydatabase.db --service=my-service --install=datasette-vega
If a plugin has any Secret configuration values you can use the --plugin-secret option to set those secrets at publish time. For example, using Heroku with datasette-auth-github you might run the following command:
$ datasette publish heroku my_database.db \
--name my-heroku-app-demo \
--install=datasette-auth-github \
--plugin-secret datasette-auth-github client_id your_client_id \
--plugin-secret datasette-auth-github client_secret your_client_secret","[""Publishing data"", ""datasette publish""]","[{""href"": ""https://github.com/simonw/datasette-vega"", ""label"": ""datasette-vega""}, {""href"": ""https://github.com/simonw/datasette-auth-github"", ""label"": ""datasette-auth-github""}]"
plugin_hooks:plugin-hook-get-metadata,plugin_hooks,plugin-hook-get-metadata,"get_metadata(datasette, key, database, table)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) .
actor - dictionary or None
The currently authenticated actor .
database - string or None
The name of the database metadata is being asked for.
table - string or None
The name of the table.
key - string or None
The name of the key for which data is being asked for.
This hook is responsible for returning a dictionary corresponding to Datasette Metadata . This function is passed the database , table and key which were passed to the upstream internal request for metadata. Regardless, it is important to return a global metadata object, where ""databases"": [] would be a top-level key. The dictionary returned here, will be merged with, and overwritten by, the contents of the physical metadata.yaml if one is present.
The design of this plugin hook does not currently provide a mechanism for interacting with async code, and may change in the future. See issue 1384 .
@hookimpl
def get_metadata(datasette, key, database, table):
metadata = {
""title"": ""This will be the Datasette landing page title!"",
""description"": get_instance_description(datasette),
""databases"": [],
}
for db_name, db_data_dict in get_my_database_meta(
datasette, database, table, key
):
metadata[""databases""][db_name] = db_data_dict
# whatever we return here will be merged with any other plugins using this hook and
# will be overwritten by a local metadata.yaml if one exists!
return metadata
Example: datasette-remote-metadata plugin","[""Plugin hooks""]","[{""href"": ""https://github.com/simonw/datasette/issues/1384"", ""label"": ""issue 1384""}, {""href"": ""https://datasette.io/plugins/datasette-remote-metadata"", ""label"": ""datasette-remote-metadata plugin""}]"
plugin_hooks:plugin-hook-register-magic-parameters,plugin_hooks,plugin-hook-register-magic-parameters,register_magic_parameters(datasette),"datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) .
Magic parameters can be used to add automatic parameters to canned queries . This plugin hook allows additional magic parameters to be defined by plugins.
Magic parameters all take this format: _prefix_rest_of_parameter . The prefix indicates which magic parameter function should be called - the rest of the parameter is passed as an argument to that function.
To register a new function, return it as a tuple of (string prefix, function) from this hook. The function you register should take two arguments: key and request , where key is the rest_of_parameter portion of the parameter and request is the current Request object .
This example registers two new magic parameters: :_request_http_version returning the HTTP version of the current request, and :_uuid_new which returns a new UUID:
from uuid import uuid4
def uuid(key, request):
if key == ""new"":
return str(uuid4())
else:
raise KeyError
def request(key, request):
if key == ""http_version"":
return request.scope[""http_version""]
else:
raise KeyError
@hookimpl
def register_magic_parameters(datasette):
return [
(""request"", request),
(""uuid"", uuid),
]","[""Plugin hooks""]",[]
plugin_hooks:plugin-hook-forbidden,plugin_hooks,plugin-hook-forbidden,"forbidden(datasette, request, message)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to render templates or execute SQL queries.
request - Request object
The current HTTP request.
message - string
A message hinting at why the request was forbidden.
Plugins can use this to customize how Datasette responds when a 403 Forbidden error occurs - usually because a page failed a permission check, see Permissions .
If a plugin hook wishes to react to the error, it should return a Response object .
This example returns a redirect to a /-/login page:
from datasette import hookimpl
from urllib.parse import urlencode
@hookimpl
def forbidden(request, message):
return Response.redirect(
""/-/login?="" + urlencode({""message"": message})
)
The function can alternatively return an awaitable function if it needs to make any asynchronous method calls. This example renders a template:
from datasette import hookimpl, Response
@hookimpl
def forbidden(datasette):
async def inner():
return Response.html(
await datasette.render_template(
""render_message.html"", request=request
)
)
return inner","[""Plugin hooks""]",[]
plugin_hooks:plugin-hook-handle-exception,plugin_hooks,plugin-hook-handle-exception,"handle_exception(datasette, request, exception)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to render templates or execute SQL queries.
request - Request object
The current HTTP request.
exception - Exception
The exception that was raised.
This hook is called any time an unexpected exception is raised. You can use it to record the exception.
If your handler returns a Response object it will be returned to the client in place of the default Datasette error page.
The handler can return a response directly, or it can return return an awaitable function that returns a response.
This example logs an error to Sentry and then renders a custom error page:
from datasette import hookimpl, Response
import sentry_sdk
@hookimpl
def handle_exception(datasette, exception):
sentry_sdk.capture_exception(exception)
async def inner():
return Response.html(
await datasette.render_template(
""custom_error.html"", request=request
)
)
return inner
Example: datasette-sentry","[""Plugin hooks""]","[{""href"": ""https://sentry.io/"", ""label"": ""Sentry""}, {""href"": ""https://datasette.io/plugins/datasette-sentry"", ""label"": ""datasette-sentry""}]"
plugin_hooks:plugin-hook-skip-csrf,plugin_hooks,plugin-hook-skip-csrf,"skip_csrf(datasette, scope)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
scope - dictionary
The ASGI scope for the incoming HTTP request.
This hook can be used to skip CSRF protection for a specific incoming request. For example, you might have a custom path at /submit-comment which is designed to accept comments from anywhere, whether or not the incoming request originated on the site and has an accompanying CSRF token.
This example will disable CSRF protection for that specific URL path:
from datasette import hookimpl
@hookimpl
def skip_csrf(scope):
return scope[""path""] == ""/submit-comment""
If any of the currently active skip_csrf() plugin hooks return True , CSRF protection will be skipped for the request.","[""Plugin hooks""]","[{""href"": ""https://asgi.readthedocs.io/en/latest/specs/www.html#http-connection-scope"", ""label"": ""ASGI scope""}]"
plugin_hooks:plugin-hook-actor-from-request,plugin_hooks,plugin-hook-actor-from-request,"actor_from_request(datasette, request)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
request - Request object
The current HTTP request.
This is part of Datasette's authentication and permissions system . The function should attempt to authenticate an actor (either a user or an API actor of some sort) based on information in the request.
If it cannot authenticate an actor, it should return None . Otherwise it should return a dictionary representing that actor.
Here's an example that authenticates the actor based on an incoming API key:
from datasette import hookimpl
import secrets
SECRET_KEY = ""this-is-a-secret""
@hookimpl
def actor_from_request(datasette, request):
authorization = (
request.headers.get(""authorization"") or """"
)
expected = ""Bearer {}"".format(SECRET_KEY)
if secrets.compare_digest(authorization, expected):
return {""id"": ""bot""}
If you install this in your plugins directory you can test it like this:
$ curl -H 'Authorization: Bearer this-is-a-secret' http://localhost:8003/-/actor.json
Instead of returning a dictionary, this function can return an awaitable function which itself returns either None or a dictionary. This is useful for authentication functions that need to make a database query - for example:
from datasette import hookimpl
@hookimpl
def actor_from_request(datasette, request):
async def inner():
token = request.args.get(""_token"")
if not token:
return None
# Look up ?_token=xxx in sessions table
result = await datasette.get_database().execute(
""select count(*) from sessions where token = ?"",
[token],
)
if result.first()[0]:
return {""token"": token}
else:
return None
return inner
Example: datasette-auth-tokens","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-auth-tokens"", ""label"": ""datasette-auth-tokens""}]"
plugin_hooks:plugin-hook-canned-queries,plugin_hooks,plugin-hook-canned-queries,"canned_queries(datasette, database, actor)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
database - string
The name of the database.
actor - dictionary or None
The currently authenticated actor .
Use this hook to return a dictionary of additional canned query definitions for the specified database. The return value should be the same shape as the JSON described in the canned query documentation.
from datasette import hookimpl
@hookimpl
def canned_queries(datasette, database):
if database == ""mydb"":
return {
""my_query"": {
""sql"": ""select * from my_table where id > :min_id""
}
}
The hook can alternatively return an awaitable function that returns a list. Here's an example that returns queries that have been stored in the saved_queries database table, if one exists:
from datasette import hookimpl
@hookimpl
def canned_queries(datasette, database):
async def inner():
db = datasette.get_database(database)
if await db.table_exists(""saved_queries""):
results = await db.execute(
""select name, sql from saved_queries""
)
return {
result[""name""]: {""sql"": result[""sql""]}
for result in results
}
return inner
The actor parameter can be used to include the currently authenticated actor in your decision. Here's an example that returns saved queries that were saved by that actor:
from datasette import hookimpl
@hookimpl
def canned_queries(datasette, database, actor):
async def inner():
db = datasette.get_database(database)
if actor is not None and await db.table_exists(
""saved_queries""
):
results = await db.execute(
""select name, sql from saved_queries where actor_id = :id"",
{""id"": actor[""id""]},
)
return {
result[""name""]: {""sql"": result[""sql""]}
for result in results
}
return inner
Example: datasette-saved-queries","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-saved-queries"", ""label"": ""datasette-saved-queries""}]"
plugin_hooks:plugin-hook-menu-links,plugin_hooks,plugin-hook-menu-links,"menu_links(datasette, actor, request)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
actor - dictionary or None
The currently authenticated actor .
request - Request object or None
The current HTTP request. This can be None if the request object is not available.
This hook allows additional items to be included in the menu displayed by Datasette's top right menu icon.
The hook should return a list of {""href"": ""..."", ""label"": ""...""} menu items. These will be added to the menu.
It can alternatively return an async def awaitable function which returns a list of menu items.
This example adds a new menu item but only if the signed in user is ""root"" :
from datasette import hookimpl
@hookimpl
def menu_links(datasette, actor):
if actor and actor.get(""id"") == ""root"":
return [
{
""href"": datasette.urls.path(
""/-/edit-schema""
),
""label"": ""Edit schema"",
},
]
Using datasette.urls here ensures that links in the menu will take the base_url setting into account.
Examples: datasette-search-all , datasette-graphql","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-search-all"", ""label"": ""datasette-search-all""}, {""href"": ""https://datasette.io/plugins/datasette-graphql"", ""label"": ""datasette-graphql""}]"
plugin_hooks:plugin-hook-table-actions,plugin_hooks,plugin-hook-table-actions,"table_actions(datasette, actor, database, table, request)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
actor - dictionary or None
The currently authenticated actor .
database - string
The name of the database.
table - string
The name of the table.
request - Request object or None
The current HTTP request. This can be None if the request object is not available.
This hook allows table actions to be displayed in a menu accessed via an action icon at the top of the table page. It should return a list of {""href"": ""..."", ""label"": ""...""} menu items.
It can alternatively return an async def awaitable function which returns a list of menu items.
This example adds a new table action if the signed in user is ""root"" :
from datasette import hookimpl
@hookimpl
def table_actions(datasette, actor, database, table):
if actor and actor.get(""id"") == ""root"":
return [
{
""href"": datasette.urls.path(
""/-/edit-schema/{}/{}"".format(
database, table
)
),
""label"": ""Edit schema for this table"",
}
]
Example: datasette-graphql","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-graphql"", ""label"": ""datasette-graphql""}]"
plugin_hooks:plugin-hook-database-actions,plugin_hooks,plugin-hook-database-actions,"database_actions(datasette, actor, database, request)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
actor - dictionary or None
The currently authenticated actor .
database - string
The name of the database.
request - Request object
The current HTTP request.
This hook is similar to table_actions(datasette, actor, database, table, request) but populates an actions menu on the database page.
Example: datasette-graphql","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-graphql"", ""label"": ""datasette-graphql""}]"
plugin_hooks:plugin-hook-permission-allowed,plugin_hooks,plugin-hook-permission-allowed,"permission_allowed(datasette, actor, action, resource)","datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
actor - dictionary
The current actor, as decided by actor_from_request(datasette, request) .
action - string
The action to be performed, e.g. ""edit-table"" .
resource - string or None
An identifier for the individual resource, e.g. the name of the table.
Called to check that an actor has permission to perform an action on a resource. Can return True if the action is allowed, False if the action is not allowed or None if the plugin does not have an opinion one way or the other.
Here's an example plugin which randomly selects if a permission should be allowed or denied, except for view-instance which always uses the default permission scheme instead.
from datasette import hookimpl
import random
@hookimpl
def permission_allowed(action):
if action != ""view-instance"":
# Return True or False at random
return random.random() > 0.5
# Returning None falls back to default permissions
This function can alternatively return an awaitable function which itself returns True , False or None . You can use this option if you need to execute additional database queries using await datasette.execute(...) .
Here's an example that allows users to view the admin_log table only if their actor id is present in the admin_users table. It aso disallows arbitrary SQL queries for the staff.db database for all users.
@hookimpl
def permission_allowed(datasette, actor, action, resource):
async def inner():
if action == ""execute-sql"" and resource == ""staff"":
return False
if action == ""view-table"" and resource == (
""staff"",
""admin_log"",
):
if not actor:
return False
user_id = actor[""id""]
return await datasette.get_database(
""staff""
).execute(
""select count(*) from admin_users where user_id = :user_id"",
{""user_id"": user_id},
)
return inner
See built-in permissions for a full list of permissions that are included in Datasette core.
Example: datasette-permissions-sql","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-permissions-sql"", ""label"": ""datasette-permissions-sql""}]"
plugin_hooks:plugin-register-output-renderer,plugin_hooks,plugin-register-output-renderer,register_output_renderer(datasette),"datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name)
Registers a new output renderer, to output data in a custom format. The hook function should return a dictionary, or a list of dictionaries, of the following shape:
@hookimpl
def register_output_renderer(datasette):
return {
""extension"": ""test"",
""render"": render_demo,
""can_render"": can_render_demo, # Optional
}
This will register render_demo to be called when paths with the extension .test (for example /database.test , /database/table.test , or /database/table/row.test ) are requested.
render_demo is a Python function. It can be a regular function or an async def render_demo() awaitable function, depending on if it needs to make any asynchronous calls.
can_render_demo is a Python function (or async def function) which accepts the same arguments as render_demo but just returns True or False . It lets Datasette know if the current SQL query can be represented by the plugin - and hence influnce if a link to this output format is displayed in the user interface. If you omit the ""can_render"" key from the dictionary every query will be treated as being supported by the plugin.
When a request is received, the ""render"" callback function is called with zero or more of the following arguments. Datasette will inspect your callback function and pass arguments that match its function signature.
datasette - Datasette class
For accessing plugin configuration and executing queries.
columns - list of strings
The names of the columns returned by this query.
rows - list of sqlite3.Row objects
The rows returned by the query.
sql - string
The SQL query that was executed.
query_name - string or None
If this was the execution of a canned query , the name of that query.
database - string
The name of the database.
table - string or None
The table or view, if one is being rendered.
request - Request object
The current HTTP request.
view_name - string
The name of the current view being called. index , database , table , and row are the most important ones.
The callback function can return None , if it is unable to render the data, or a Response class that will be returned to the caller.
It can also return a dictionary with the following keys. This format is deprecated as-of Datasette 0.49 and will be removed by Datasette 1.0.
body - string or bytes, optional
The response body, default empty
content_type - string, optional
The Content-Type header, default text/plain
status_code - integer, optional
The HTTP status code, default 200
headers - dictionary, optional
Extra HTTP headers to be returned in the response.
An example of an output renderer callback function:
def render_demo():
return Response.text(""Hello World"")
Here is a more complex example:
async def render_demo(datasette, columns, rows):
db = datasette.get_database()
result = await db.execute(""select sqlite_version()"")
first_row = "" | "".join(columns)
lines = [first_row]
lines.append(""="" * len(first_row))
for row in rows:
lines.append("" | "".join(row))
return Response(
""\n"".join(lines),
content_type=""text/plain; charset=utf-8"",
headers={""x-sqlite-version"": result.first()[0]},
)
And here is an example can_render function which returns True only if the query results contain the columns atom_id , atom_title and atom_updated :
def can_render_demo(columns):
return {
""atom_id"",
""atom_title"",
""atom_updated"",
}.issubset(columns)
Examples: datasette-atom , datasette-ics , datasette-geojson , datasette-copyable","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-atom"", ""label"": ""datasette-atom""}, {""href"": ""https://datasette.io/plugins/datasette-ics"", ""label"": ""datasette-ics""}, {""href"": ""https://datasette.io/plugins/datasette-geojson"", ""label"": ""datasette-geojson""}, {""href"": ""https://datasette.io/plugins/datasette-copyable"", ""label"": ""datasette-copyable""}]"
plugin_hooks:plugin-register-routes,plugin_hooks,plugin-register-routes,register_routes(datasette),"datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name)
Register additional view functions to execute for specified URL routes.
Return a list of (regex, view_function) pairs, something like this:
from datasette import hookimpl, Response
import html
async def hello_from(request):
name = request.url_vars[""name""]
return Response.html(
""Hello from {}"".format(html.escape(name))
)
@hookimpl
def register_routes():
return [(r""^/hello-from/(?P.*)$"", hello_from)]
The view functions can take a number of different optional arguments. The corresponding argument will be passed to your function depending on its named parameters - a form of dependency injection.
The optional view function arguments are as follows:
datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
request - Request object
The current HTTP request.
scope - dictionary
The incoming ASGI scope dictionary.
send - function
The ASGI send function.
receive - function
The ASGI receive function.
The view function can be a regular function or an async def function, depending on if it needs to use any await APIs.
The function can either return a Response class or it can return nothing and instead respond directly to the request using the ASGI send function (for advanced uses only).
It can also raise the datasette.NotFound exception to return a 404 not found error, or the datasette.Forbidden exception for a 403 forbidden.
See Designing URLs for your plugin for tips on designing the URL routes used by your plugin.
Examples: datasette-auth-github , datasette-psutil","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-auth-github"", ""label"": ""datasette-auth-github""}, {""href"": ""https://datasette.io/plugins/datasette-psutil"", ""label"": ""datasette-psutil""}]"
plugin_hooks:plugin-hook-prepare-connection,plugin_hooks,plugin-hook-prepare-connection,"prepare_connection(conn, database, datasette)","conn - sqlite3 connection object
The connection that is being opened
database - string
The name of the database
datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name)
This hook is called when a new SQLite database connection is created. You can
use it to register custom SQL functions ,
aggregates and collations. For example:
from datasette import hookimpl
import random
@hookimpl
def prepare_connection(conn):
conn.create_function(
""random_integer"", 2, random.randint
)
This registers a SQL function called random_integer which takes two
arguments and can be called like this:
select random_integer(1, 10);
Examples: datasette-jellyfish , datasette-jq , datasette-haversine , datasette-rure","[""Plugin hooks""]","[{""href"": ""https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.create_function"", ""label"": ""register custom SQL functions""}, {""href"": ""https://datasette.io/plugins/datasette-jellyfish"", ""label"": ""datasette-jellyfish""}, {""href"": ""https://datasette.io/plugins/datasette-jq"", ""label"": ""datasette-jq""}, {""href"": ""https://datasette.io/plugins/datasette-haversine"", ""label"": ""datasette-haversine""}, {""href"": ""https://datasette.io/plugins/datasette-rure"", ""label"": ""datasette-rure""}]"
plugin_hooks:plugin-hook-register-commands,plugin_hooks,plugin-hook-register-commands,register_commands(cli),"cli - the root Datasette Click command group
Use this to register additional CLI commands
Register additional CLI commands that can be run using datsette yourcommand ... . This provides a mechanism by which plugins can add new CLI commands to Datasette.
This example registers a new datasette verify file1.db file2.db command that checks if the provided file paths are valid SQLite databases:
from datasette import hookimpl
import click
import sqlite3
@hookimpl
def register_commands(cli):
@cli.command()
@click.argument(
""files"", type=click.Path(exists=True), nargs=-1
)
def verify(files):
""Verify that files can be opened by Datasette""
for file in files:
conn = sqlite3.connect(str(file))
try:
conn.execute(""select * from sqlite_master"")
except sqlite3.DatabaseError:
raise click.ClickException(
""Invalid database: {}"".format(file)
)
The new command can then be executed like so:
datasette verify fixtures.db
Help text (from the docstring for the function plus any defined Click arguments or options) will become available using:
datasette verify --help
Plugins can register multiple commands by making multiple calls to the @cli.command() decorator. Consult the Click documentation for full details on how to build a CLI command, including how to define arguments and options.
Note that register_commands() plugins cannot used with the --plugins-dir mechanism - they need to be installed into the same virtual environment as Datasette using pip install . Provided it has a setup.py file (see Packaging a plugin ) you can run pip install directly against the directory in which you are developing your plugin like so:
pip install -e path/to/my/datasette-plugin
Examples: datasette-auth-passwords , datasette-verify","[""Plugin hooks""]","[{""href"": ""https://click.palletsprojects.com/en/latest/commands/#callback-invocation"", ""label"": ""Click command group""}, {""href"": ""https://click.palletsprojects.com/"", ""label"": ""Click documentation""}, {""href"": ""https://datasette.io/plugins/datasette-auth-passwords"", ""label"": ""datasette-auth-passwords""}, {""href"": ""https://datasette.io/plugins/datasette-verify"", ""label"": ""datasette-verify""}]"
internals:datasette-ensure-permissions,internals,datasette-ensure-permissions,"await .ensure_permissions(actor, permissions)","actor - dictionary
The authenticated actor. This is usually request.actor .
permissions - list
A list of permissions to check. Each permission in that list can be a string action name or a 2-tuple of (action, resource) .
This method allows multiple permissions to be checked at once. It raises a datasette.Forbidden exception if any of the checks are denied before one of them is explicitly granted.
This is useful when you need to check multiple permissions at once. For example, an actor should be able to view a table if either one of the following checks returns True or not a single one of them returns False :
await self.ds.ensure_permissions(
request.actor,
[
(""view-table"", (database, table)),
(""view-database"", database),
""view-instance"",
],
)","[""Internals for plugins"", ""Datasette class""]",[]
internals:datasette-check-visibility,internals,datasette-check-visibility,"await .check_visibility(actor, action=None, resource=None, permissions=None)","actor - dictionary
The authenticated actor. This is usually request.actor .
action - string, optional
The name of the action that is being permission checked.
resource - string or tuple, optional
The resource, e.g. the name of the database, or a tuple of two strings containing the name of the database and the name of the table. Only some permissions apply to a resource.
permissions - list of action strings or (action, resource) tuples, optional
Provide this instead of action and resource to check multiple permissions at once.
This convenience method can be used to answer the question ""should this item be considered private, in that it is visible to me but it is not visible to anonymous users?""
It returns a tuple of two booleans, (visible, private) . visible indicates if the actor can see this resource. private will be True if an anonymous user would not be able to view the resource.
This example checks if the user can access a specific table, and sets private so that a padlock icon can later be displayed:
visible, private = await self.ds.check_visibility(
request.actor,
action=""view-table"",
resource=(database, table),
)
The following example runs three checks in a row, similar to await .ensure_permissions(actor, permissions) . If any of the checks are denied before one of them is explicitly granted then visible will be False . private will be True if an anonymous user would not be able to view the resource.
visible, private = await self.ds.check_visibility(
request.actor,
permissions=[
(""view-table"", (database, table)),
(""view-database"", database),
""view-instance"",
],
)","[""Internals for plugins"", ""Datasette class""]",[]
internals:datasette-permission-allowed,internals,datasette-permission-allowed,"await .permission_allowed(actor, action, resource=None, default=False)","actor - dictionary
The authenticated actor. This is usually request.actor .
action - string
The name of the action that is being permission checked.
resource - string or tuple, optional
The resource, e.g. the name of the database, or a tuple of two strings containing the name of the database and the name of the table. Only some permissions apply to a resource.
default - optional, True or False
Should this permission check be default allow or default deny.
Check if the given actor has permission to perform the given action on the given resource.
Some permission checks are carried out against rules defined in metadata.json , while other custom permissions may be decided by plugins that implement the permission_allowed(datasette, actor, action, resource) plugin hook.
If neither metadata.json nor any of the plugins provide an answer to the permission query the default argument will be returned.
See Built-in permissions for a full list of permission actions included in Datasette core.","[""Internals for plugins"", ""Datasette class""]",[]
changelog:id69,changelog,id69,0.34 (2020-01-29),"_search= queries are now correctly escaped using a new escape_fts() custom SQL function. This means you can now run searches for strings like park. without seeing errors. ( #651 )
Google Cloud Run is no longer in beta, so datasette publish cloudrun has been updated to work even if the user has not installed the gcloud beta components package. Thanks, Katie McLaughlin ( #660 )
datasette package now accepts a --port option for specifying which port the resulting Docker container should listen on. ( #661 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/651"", ""label"": ""#651""}, {""href"": ""https://cloud.google.com/run/"", ""label"": ""Google Cloud Run""}, {""href"": ""https://github.com/simonw/datasette/pull/660"", ""label"": ""#660""}, {""href"": ""https://github.com/simonw/datasette/issues/661"", ""label"": ""#661""}]"
deploying:deploying-proxy,deploying,deploying-proxy,Running Datasette behind a proxy,"You may wish to run Datasette behind an Apache or nginx proxy, using a path within your existing site.
You can use the base_url configuration setting to tell Datasette to serve traffic with a specific URL prefix. For example, you could run Datasette like this:
datasette my-database.db --setting base_url /my-datasette/ -p 8009
This will run Datasette with the following URLs:
http://127.0.0.1:8009/my-datasette/ - the Datasette homepage
http://127.0.0.1:8009/my-datasette/my-database - the page for the my-database.db database
http://127.0.0.1:8009/my-datasette/my-database/some_table - the page for the some_table table
You can now set your nginx or Apache server to proxy the /my-datasette/ path to this Datasette instance.","[""Deploying Datasette""]",[]
writing_plugins:id1,writing_plugins,id1,Writing plugins,"You can write one-off plugins that apply to just one Datasette instance, or you can write plugins which can be installed using pip and can be shipped to the Python Package Index ( PyPI ) for other people to install.
Want to start by looking at an example? The Datasette plugins directory lists more than 90 open source plugins with code you can explore. The plugin hooks page includes links to example plugins for each of the documented hooks.",[],"[{""href"": ""https://pypi.org/"", ""label"": ""PyPI""}, {""href"": ""https://datasette.io/plugins"", ""label"": ""Datasette plugins directory""}]"
custom_templates:custom-pages-redirects,custom_templates,custom-pages-redirects,Custom redirects,"You can use the custom_redirect(location) function to redirect users to another page, for example in a file called pages/datasette.html :
{{ custom_redirect(""https://github.com/simonw/datasette"") }}
Now requests to http://localhost:8001/datasette will result in a redirect.
These redirects are served with a 302 Found status code by default. You can send a 301 Moved Permanently code by passing 301 as the second argument to the function:
{{ custom_redirect(""https://github.com/simonw/datasette"", 301) }}","[""Custom pages and templates"", ""Custom pages""]",[]
installation:upgrading-packages-using-pipx,installation,upgrading-packages-using-pipx,Upgrading packages using pipx,"You can upgrade your pipx installation to the latest release of Datasette using pipx upgrade datasette :
$ pipx upgrade datasette
upgraded package datasette from 0.39 to 0.40 (location: /Users/simon/.local/pipx/venvs/datasette)
To upgrade a plugin within the pipx environment use pipx runpip datasette install -U name-of-plugin - like this:
% datasette plugins
[
{
""name"": ""datasette-vega"",
""static"": true,
""templates"": false,
""version"": ""0.6""
}
]
$ pipx runpip datasette install -U datasette-vega
Collecting datasette-vega
Downloading datasette_vega-0.6.2-py3-none-any.whl (1.8 MB)
|████████████████████████████████| 1.8 MB 2.0 MB/s
...
Installing collected packages: datasette-vega
Attempting uninstall: datasette-vega
Found existing installation: datasette-vega 0.6
Uninstalling datasette-vega-0.6:
Successfully uninstalled datasette-vega-0.6
Successfully installed datasette-vega-0.6.2
$ datasette plugins
[
{
""name"": ""datasette-vega"",
""static"": true,
""templates"": false,
""version"": ""0.6.2""
}
]","[""Installation"", ""Advanced installation options"", ""Using pipx""]",[]
facets:facets-metadata,facets,facets-metadata,Facets in metadata.json,"You can turn facets on by default for specific tables by adding them to a ""facets"" key in a Datasette Metadata file.
Here's an example that turns on faceting by default for the qLegalStatus column in the Street_Tree_List table in the sf-trees database:
{
""databases"": {
""sf-trees"": {
""tables"": {
""Street_Tree_List"": {
""facets"": [""qLegalStatus""]
}
}
}
}
}
Facets defined in this way will always be shown in the interface and returned in the API, regardless of the _facet arguments passed to the view.
You can specify array or date facets in metadata using JSON objects with a single key of array or date and a value specifying the column, like this:
{
""facets"": [
{""array"": ""tags""},
{""date"": ""created""}
]
}
You can change the default facet size (the number of results shown for each facet) for a table using facet_size :
{
""databases"": {
""sf-trees"": {
""tables"": {
""Street_Tree_List"": {
""facets"": [""qLegalStatus""],
""facet_size"": 10
}
}
}
}
}","[""Facets""]",[]
plugins:plugins-installed,plugins,plugins-installed,Seeing what plugins are installed,"You can see a list of installed plugins by navigating to the /-/plugins page of your Datasette instance - for example: https://fivethirtyeight.datasettes.com/-/plugins
You can also use the datasette plugins command:
$ datasette plugins
[
{
""name"": ""datasette_json_html"",
""static"": false,
""templates"": false,
""version"": ""0.4.0""
}
]
[[[cog
from datasette import cli
from click.testing import CliRunner
import textwrap, json
cog.out(""\n"")
result = CliRunner().invoke(cli.cli, [""plugins"", ""--all""])
# cog.out() with text containing newlines was unindenting for some reason
cog.outl(""If you run ``datasette plugins --all`` it will include default plugins that ship as part of Datasette::\n"")
plugins = [p for p in json.loads(result.output) if p[""name""].startswith(""datasette."")]
indented = textwrap.indent(json.dumps(plugins, indent=4), "" "")
for line in indented.split(""\n""):
cog.outl(line)
cog.out(""\n\n"")
]]]
If you run datasette plugins --all it will include default plugins that ship as part of Datasette:
[
{
""name"": ""datasette.actor_auth_cookie"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""actor_from_request""
]
},
{
""name"": ""datasette.blob_renderer"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""register_output_renderer""
]
},
{
""name"": ""datasette.default_magic_parameters"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""register_magic_parameters""
]
},
{
""name"": ""datasette.default_menu_links"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""menu_links""
]
},
{
""name"": ""datasette.default_permissions"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""permission_allowed""
]
},
{
""name"": ""datasette.facets"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""register_facet_classes""
]
},
{
""name"": ""datasette.filters"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""filters_from_request""
]
},
{
""name"": ""datasette.forbidden"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""forbidden""
]
},
{
""name"": ""datasette.handle_exception"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""handle_exception""
]
},
{
""name"": ""datasette.publish.cloudrun"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""publish_subcommand""
]
},
{
""name"": ""datasette.publish.heroku"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""publish_subcommand""
]
},
{
""name"": ""datasette.sql_functions"",
""static"": false,
""templates"": false,
""version"": null,
""hooks"": [
""prepare_connection""
]
}
]
[[[end]]]
You can add the --plugins-dir= option to include any plugins found in that directory.","[""Plugins""]","[{""href"": ""https://fivethirtyeight.datasettes.com/-/plugins"", ""label"": ""https://fivethirtyeight.datasettes.com/-/plugins""}]"
deploying:deploying-systemd,deploying,deploying-systemd,Running Datasette using systemd,"You can run Datasette on Ubuntu or Debian systems using systemd .
First, ensure you have Python 3 and pip installed. On Ubuntu you can use sudo apt-get install python3 python3-pip .
You can install Datasette into a virtual environment, or you can install it system-wide. To install system-wide, use sudo pip3 install datasette .
Now create a folder for your Datasette databases, for example using mkdir /home/ubuntu/datasette-root .
You can copy a test database into that folder like so:
cd /home/ubuntu/datasette-root
curl -O https://latest.datasette.io/fixtures.db
Create a file at /etc/systemd/system/datasette.service with the following contents:
[Unit]
Description=Datasette
After=network.target
[Service]
Type=simple
User=ubuntu
Environment=DATASETTE_SECRET=
WorkingDirectory=/home/ubuntu/datasette-root
ExecStart=datasette serve . -h 127.0.0.1 -p 8000
Restart=on-failure
[Install]
WantedBy=multi-user.target
Add a random value for the DATASETTE_SECRET - this will be used to sign Datasette cookies such as the CSRF token cookie. You can generate a suitable value like so:
$ python3 -c 'import secrets; print(secrets.token_hex(32))'
This configuration will run Datasette against all database files contained in the /home/ubuntu/datasette-root directory. If that directory contains a metadata.yml (or .json ) file or a templates/ or plugins/ sub-directory those will automatically be loaded by Datasette - see Configuration directory mode for details.
You can start the Datasette process running using the following:
sudo systemctl daemon-reload
sudo systemctl start datasette.service
You will need to restart the Datasette service after making changes to its metadata.json configuration or adding a new database file to that directory. You can do that using:
sudo systemctl restart datasette.service
Once the service has started you can confirm that Datasette is running on port 8000 like so:
curl 127.0.0.1:8000/-/versions.json
# Should output JSON showing the installed version
Datasette will not be accessible from outside the server because it is listening on 127.0.0.1 . You can expose it by instead listening on 0.0.0.0 , but a better way is to set up a proxy such as nginx - see Running Datasette behind a proxy .","[""Deploying Datasette""]",[]
writing_plugins:writing-plugins-designing-urls,writing_plugins,writing-plugins-designing-urls,Designing URLs for your plugin,"You can register new URL routes within Datasette using the register_routes(datasette) plugin hook.
Datasette's default URLs include these:
/dbname - database page
/dbname/tablename - table page
/dbname/tablename/pk - row page
See Pages and API endpoints and Introspection for more default URL routes.
To avoid accidentally conflicting with a database file that may be loaded into Datasette, plugins should register URLs using a /-/ prefix. For example, if your plugin adds a new interface for uploading Excel files you might register a URL route like this one:
/-/upload-excel
Try to avoid registering URLs that clash with other plugins that your users might have installed. There is no central repository of reserved URL paths (yet) but you can review existing plugins by browsing the plugins directory .
If your plugin includes functionality that relates to a specific database you could also register a URL route like this:
/dbname/-/upload-excel
Or for a specific table like this:
/dbname/tablename/-/modify-table-schema
Note that a row could have a primary key of - and this URL scheme will still work, because Datasette row pages do not ever have a trailing slash followed by additional path components.","[""Writing plugins""]",[]
changelog:control-http-caching-with-ttl,changelog,control-http-caching-with-ttl,Control HTTP caching with ?_ttl=,"You can now customize the HTTP max-age header that is sent on a per-URL basis, using the new ?_ttl= query string parameter.
You can set this to any value in seconds, or you can set it to 0 to disable HTTP caching entirely.
Consider for example this query which returns a randomly selected member of the Avengers:
select * from [avengers/avengers] order by random() limit 1
If you hit the following page repeatedly you will get the same result, due to HTTP caching:
/fivethirtyeight?sql=select+*+from+%5Bavengers%2Favengers%5D+order+by+random%28%29+limit+1
By adding ?_ttl=0 to the zero you can ensure the page will not be cached and get back a different super hero every time:
/fivethirtyeight?sql=select+*+from+%5Bavengers%2Favengers%5D+order+by+random%28%29+limit+1&_ttl=0","[""Changelog"", ""0.23 (2018-06-18)""]","[{""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight?sql=select+*+from+%5Bavengers%2Favengers%5D+order+by+random%28%29+limit+1"", ""label"": ""/fivethirtyeight?sql=select+*+from+%5Bavengers%2Favengers%5D+order+by+random%28%29+limit+1""}, {""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight?sql=select+*+from+%5Bavengers%2Favengers%5D+order+by+random%28%29+limit+1&_ttl=0"", ""label"": ""/fivethirtyeight?sql=select+*+from+%5Bavengers%2Favengers%5D+order+by+random%28%29+limit+1&_ttl=0""}]"
changelog:id61,changelog,id61,0.41 (2020-05-06),"You can now create custom pages within your Datasette instance using a custom template file. For example, adding a template file called templates/pages/about.html will result in a new page being served at /about on your instance. See the custom pages documentation for full details, including how to return custom HTTP headers, redirects and status codes. ( #648 )
Configuration directory mode ( #731 ) allows you to define a custom Datasette instance as a directory. So instead of running the following:
$ datasette one.db two.db \
--metadata=metadata.json \
--template-dir=templates/ \
--plugins-dir=plugins \
--static css:css
You can instead arrange your files in a single directory called my-project and run this:
$ datasette my-project/
Also in this release:
New NOT LIKE table filter: ?colname__notlike=expression . ( #750 )
Datasette now has a pattern portfolio at /-/patterns - e.g. https://latest.datasette.io/-/patterns . This is a page that shows every Datasette user interface component in one place, to aid core development and people building custom CSS themes. ( #151 )
SQLite PRAGMA functions such as pragma_table_info(tablename) are now allowed in Datasette SQL queries. ( #761 )
Datasette pages now consistently return a content-type of text/html; charset=utf-8"" . ( #752 )
Datasette now handles an ASGI raw_path value of None , which should allow compatibility with the Mangum adapter for running ASGI apps on AWS Lambda. Thanks, Colin Dellow. ( #719 )
Installation documentation now covers how to Using pipx . ( #756 )
Improved the documentation for Full-text search . ( #748 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/648"", ""label"": ""#648""}, {""href"": ""https://github.com/simonw/datasette/issues/731"", ""label"": ""#731""}, {""href"": ""https://github.com/simonw/datasette/issues/750"", ""label"": ""#750""}, {""href"": ""https://latest.datasette.io/-/patterns"", ""label"": ""https://latest.datasette.io/-/patterns""}, {""href"": ""https://github.com/simonw/datasette/issues/151"", ""label"": ""#151""}, {""href"": ""https://www.sqlite.org/pragma.html#pragfunc"", ""label"": ""PRAGMA functions""}, {""href"": ""https://github.com/simonw/datasette/issues/761"", ""label"": ""#761""}, {""href"": ""https://github.com/simonw/datasette/issues/752"", ""label"": ""#752""}, {""href"": ""https://github.com/erm/mangum"", ""label"": ""Mangum""}, {""href"": ""https://github.com/simonw/datasette/pull/719"", ""label"": ""#719""}, {""href"": ""https://github.com/simonw/datasette/issues/756"", ""label"": ""#756""}, {""href"": ""https://github.com/simonw/datasette/issues/748"", ""label"": ""#748""}]"
authentication:authentication-permissions-metadata,authentication,authentication-permissions-metadata,Configuring permissions in metadata.json,"You can limit who is allowed to view different parts of your Datasette instance using ""allow"" keys in your Metadata configuration.
You can control the following:
Access to the entire Datasette instance
Access to specific databases
Access to specific tables and views
Access to specific Canned queries
If a user cannot access a specific database, they will not be able to access tables, views or queries within that database. If a user cannot access the instance they will not be able to access any of the databases, tables, views or queries.","[""Authentication and permissions""]",[]
installation:installing-plugins-using-pipx,installation,installing-plugins-using-pipx,Installing plugins using pipx,"You can install additional datasette plugins with pipx inject like so:
$ pipx inject datasette datasette-json-html
injected package datasette-json-html into venv datasette
done! ✨ 🌟 ✨
$ datasette plugins
[
{
""name"": ""datasette-json-html"",
""static"": false,
""templates"": false,
""version"": ""0.6""
}
]","[""Installation"", ""Advanced installation options"", ""Using pipx""]",[]
full_text_search:full-text-search-custom-sql,full_text_search,full-text-search-custom-sql,Searches using custom SQL,"You can include full-text search results in custom SQL queries. The general pattern with SQLite search is to run the search as a sub-select that returns rowid values, then include those rowids in another part of the query.
You can see the syntax for a basic search by running that search on a table page and then clicking ""View and edit SQL"" to see the underlying SQL. For example, consider this search for manafort is the US FARA database :
/fara/FARA_All_ShortForms?_search=manafort
If you click View and edit SQL you'll see that the underlying SQL looks like this:
select
rowid,
Short_Form_Termination_Date,
Short_Form_Date,
Short_Form_Last_Name,
Short_Form_First_Name,
Registration_Number,
Registration_Date,
Registrant_Name,
Address_1,
Address_2,
City,
State,
Zip
from
FARA_All_ShortForms
where
rowid in (
select
rowid
from
FARA_All_ShortForms_fts
where
FARA_All_ShortForms_fts match escape_fts(:search)
)
order by
rowid
limit
101","[""Full-text search""]","[{""href"": ""https://fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort"", ""label"": ""manafort is the US FARA database""}, {""href"": ""https://fara.datasettes.com/fara?sql=select%0D%0A++rowid%2C%0D%0A++Short_Form_Termination_Date%2C%0D%0A++Short_Form_Date%2C%0D%0A++Short_Form_Last_Name%2C%0D%0A++Short_Form_First_Name%2C%0D%0A++Registration_Number%2C%0D%0A++Registration_Date%2C%0D%0A++Registrant_Name%2C%0D%0A++Address_1%2C%0D%0A++Address_2%2C%0D%0A++City%2C%0D%0A++State%2C%0D%0A++Zip%0D%0Afrom%0D%0A++FARA_All_ShortForms%0D%0Awhere%0D%0A++rowid+in+%28%0D%0A++++select%0D%0A++++++rowid%0D%0A++++from%0D%0A++++++FARA_All_ShortForms_fts%0D%0A++++where%0D%0A++++++FARA_All_ShortForms_fts+match+escape_fts%28%3Asearch%29%0D%0A++%29%0D%0Aorder+by%0D%0A++rowid%0D%0Alimit%0D%0A++101&search=manafort"", ""label"": ""View and edit SQL""}]"
metadata:metadata-column-descriptions,metadata,metadata-column-descriptions,Column descriptions,"You can include descriptions for your columns by adding a ""columns"": {""name-of-column"": ""description-of-column""} block to your table metadata:
{
""databases"": {
""database1"": {
""tables"": {
""example_table"": {
""columns"": {
""column1"": ""Description of column 1"",
""column2"": ""Description of column 2""
}
}
}
}
}
}
These will be displayed at the top of the table page, and will also show in the cog menu for each column.
You can see an example of how these look at latest.datasette.io/fixtures/roadside_attractions .","[""Metadata""]","[{""href"": ""https://latest.datasette.io/fixtures/roadside_attractions"", ""label"": ""latest.datasette.io/fixtures/roadside_attractions""}]"
metadata:metadata-hiding-tables,metadata,metadata-hiding-tables,Hiding tables,"You can hide tables from the database listing view (in the same way that FTS and
SpatiaLite tables are automatically hidden) using ""hidden"": true :
{
""databases"": {
""database1"": {
""tables"": {
""example_table"": {
""hidden"": true
}
}
}
}
}","[""Metadata""]",[]
json_api:column-filter-arguments,json_api,column-filter-arguments,Column filter arguments,"You can filter the data returned by the table based on column values using a query string argument.
?column__exact=value or ?_column=value
Returns rows where the specified column exactly matches the value.
?column__not=value
Returns rows where the column does not match the value.
?column__contains=value
Rows where the string column contains the specified value ( column like ""%value%"" in SQL).
?column__endswith=value
Rows where the string column ends with the specified value ( column like ""%value"" in SQL).
?column__startswith=value
Rows where the string column starts with the specified value ( column like ""value%"" in SQL).
?column__gt=value
Rows which are greater than the specified value.
?column__gte=value
Rows which are greater than or equal to the specified value.
?column__lt=value
Rows which are less than the specified value.
?column__lte=value
Rows which are less than or equal to the specified value.
?column__like=value
Match rows with a LIKE clause, case insensitive and with % as the wildcard character.
?column__notlike=value
Match rows that do not match the provided LIKE clause.
?column__glob=value
Similar to LIKE but uses Unix wildcard syntax and is case sensitive.
?column__in=value1,value2,value3
Rows where column matches any of the provided values.
You can use a comma separated string, or you can use a JSON array.
The JSON array option is useful if one of your matching values itself contains a comma:
?column__in=[""value"",""value,with,commas""]
?column__notin=value1,value2,value3
Rows where column does not match any of the provided values. The inverse of __in= . Also supports JSON arrays.
?column__arraycontains=value
Works against columns that contain JSON arrays - matches if any of the values in that array match the provided value.
This is only available if the json1 SQLite extension is enabled.
?column__arraynotcontains=value
Works against columns that contain JSON arrays - matches if none of the values in that array match the provided value.
This is only available if the json1 SQLite extension is enabled.
?column__date=value
Column is a datestamp occurring on the specified YYYY-MM-DD date, e.g. 2018-01-02 .
?column__isnull=1
Matches rows where the column is null.
?column__notnull=1
Matches rows where the column is not null.
?column__isblank=1
Matches rows where the column is blank, meaning null or the empty string.
?column__notblank=1
Matches rows where the column is not blank.","[""JSON API"", ""Table arguments""]",[]
custom_templates:custom-pages-parameters,custom_templates,custom-pages-parameters,Path parameters for pages,"You can define custom pages that match multiple paths by creating files with {variable} definitions in their filenames.
For example, to capture any request to a URL matching /about/* , you would create a template in the following location:
templates/pages/about/{slug}.html
A hit to /about/news would render that template and pass in a variable called slug with a value of ""news"" .
If you use this mechanism don't forget to return a 404 if the referenced content could not be found. You can do this using {{ raise_404() }} described below.
Templates defined using custom page routes work particularly well with the sql() template function from datasette-template-sql or the graphql() template function from datasette-graphql .","[""Custom pages and templates"", ""Custom pages""]","[{""href"": ""https://github.com/simonw/datasette-template-sql"", ""label"": ""datasette-template-sql""}, {""href"": ""https://github.com/simonw/datasette-graphql#the-graphql-template-function"", ""label"": ""datasette-graphql""}]"
plugins:one-off-plugins-using-plugins-dir,plugins,one-off-plugins-using-plugins-dir,One-off plugins using --plugins-dir,"You can also define one-off per-project plugins by saving them as plugin_name.py functions in a plugins/ folder and then passing that folder to datasette using the --plugins-dir option:
datasette mydb.db --plugins-dir=plugins/","[""Plugins"", ""Installing plugins""]",[]
custom_templates:id1,custom_templates,id1,Custom pages,"You can add templated pages to your Datasette instance by creating HTML files in a pages directory within your templates directory.
For example, to add a custom page that is served at http://localhost/about you would create a file in templates/pages/about.html , then start Datasette like this:
$ datasette mydb.db --template-dir=templates/
You can nest directories within pages to create a nested structure. To create a http://localhost:8001/about/map page you would create templates/pages/about/map.html .","[""Custom pages and templates""]",[]
changelog:flash-messages,changelog,flash-messages,Flash messages,"Writable canned queries needed a mechanism to let the user know that the query has been successfully executed. The new flash messaging system ( #790 ) allows messages to persist in signed cookies which are then displayed to the user on the next page that they visit. Plugins can use this mechanism to display their own messages, see .add_message(request, message, type=datasette.INFO) for details.
You can try out the new messages using the /-/messages debug tool, for example at https://latest.datasette.io/-/messages","[""Changelog"", ""0.44 (2020-06-11)""]","[{""href"": ""https://github.com/simonw/datasette/issues/790"", ""label"": ""#790""}, {""href"": ""https://latest.datasette.io/-/messages"", ""label"": ""https://latest.datasette.io/-/messages""}]"
sql_queries:canned-queries-json-api,sql_queries,canned-queries-json-api,JSON API for writable canned queries,"Writable canned queries can also be accessed using a JSON API. You can POST data to them using JSON, and you can request that their response is returned to you as JSON.
To submit JSON to a writable canned query, encode key/value parameters as a JSON document:
POST /mydatabase/add_message
{""message"": ""Message goes here""}
You can also continue to submit data using regular form encoding, like so:
POST /mydatabase/add_message
message=Message+goes+here
There are three options for specifying that you would like the response to your request to return JSON data, as opposed to an HTTP redirect to another page.
Set an Accept: application/json header on your request
Include ?_json=1 in the URL that you POST to
Include ""_json"": 1 in your JSON body, or &_json=1 in your form encoded body
The JSON response will look like this:
{
""ok"": true,
""message"": ""Query executed, 1 row affected"",
""redirect"": ""/data/add_name""
}
The ""message"" and ""redirect"" values here will take into account on_success_message , on_success_redirect , on_error_message and on_error_redirect , if they have been set.","[""Running SQL queries"", ""Canned queries""]",[]
changelog:smaller-changes,changelog,smaller-changes,Smaller changes,"Wide tables shown within Datasette now scroll horizontally ( #998 ). This is achieved using a new
element which may impact the implementation of some plugins (for example this change to datasette-cluster-map ).
New debug-menu permission. ( #1068 )
Removed --debug option, which didn't do anything. ( #814 )
Link: HTTP header pagination. ( #1014 )
x button for clearing filters. ( #1016 )
Edit SQL button on canned queries, ( #1019 )
--load-extension=spatialite shortcut. ( #1028 )
scale-in animation for column action menu. ( #1039 )
Option to pass a list of templates to .render_template() is now documented. ( #1045 )
New datasette.urls.static_plugins() method. ( #1033 )
datasette -o option now opens the most relevant page. ( #976 )
datasette --cors option now enables access to /database.db downloads. ( #1057 )
Database file downloads now implement cascading permissions, so you can download a database if you have view-database-download permission even if you do not have permission to access the Datasette instance. ( #1058 )
New documentation on Designing URLs for your plugin . ( #1053 )","[""Changelog"", ""0.51 (2020-10-31)""]","[{""href"": ""https://github.com/simonw/datasette/issues/998"", ""label"": ""#998""}, {""href"": ""https://github.com/simonw/datasette-cluster-map/commit/fcb4abbe7df9071c5ab57defd39147de7145b34e"", ""label"": ""this change to datasette-cluster-map""}, {""href"": ""https://github.com/simonw/datasette/issues/1068"", ""label"": ""#1068""}, {""href"": ""https://github.com/simonw/datasette/issues/814"", ""label"": ""#814""}, {""href"": ""https://github.com/simonw/datasette/issues/1014"", ""label"": ""#1014""}, {""href"": ""https://github.com/simonw/datasette/issues/1016"", ""label"": ""#1016""}, {""href"": ""https://github.com/simonw/datasette/issues/1019"", ""label"": ""#1019""}, {""href"": ""https://github.com/simonw/datasette/issues/1028"", ""label"": ""#1028""}, {""href"": ""https://github.com/simonw/datasette/issues/1039"", ""label"": ""#1039""}, {""href"": ""https://github.com/simonw/datasette/issues/1045"", ""label"": ""#1045""}, {""href"": ""https://github.com/simonw/datasette/issues/1033"", ""label"": ""#1033""}, {""href"": ""https://github.com/simonw/datasette/issues/976"", ""label"": ""#976""}, {""href"": ""https://github.com/simonw/datasette/issues/1057"", ""label"": ""#1057""}, {""href"": ""https://github.com/simonw/datasette/issues/1058"", ""label"": ""#1058""}, {""href"": ""https://github.com/simonw/datasette/issues/1053"", ""label"": ""#1053""}]"
custom_templates:customization-css-and-javascript,custom_templates,customization-css-and-javascript,Custom CSS and JavaScript,"When you launch Datasette, you can specify a custom metadata file like this:
datasette mydb.db --metadata metadata.json
Your metadata.json file can include links that look like this:
{
""extra_css_urls"": [
""https://simonwillison.net/static/css/all.bf8cd891642c.css""
],
""extra_js_urls"": [
""https://code.jquery.com/jquery-3.2.1.slim.min.js""
]
}
The extra CSS and JavaScript files will be linked in the of every page:
You can also specify a SRI (subresource integrity hash) for these assets:
{
""extra_css_urls"": [
{
""url"": ""https://simonwillison.net/static/css/all.bf8cd891642c.css"",
""sri"": ""sha384-9qIZekWUyjCyDIf2YK1FRoKiPJq4PHt6tp/ulnuuyRBvazd0hG7pWbE99zvwSznI""
}
],
""extra_js_urls"": [
{
""url"": ""https://code.jquery.com/jquery-3.2.1.slim.min.js"",
""sri"": ""sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=""
}
]
}
This will produce:
Modern browsers will only execute the stylesheet or JavaScript if the SRI hash
matches the content served. You can generate hashes using www.srihash.org
Items in ""extra_js_urls"" can specify ""module"": true if they reference JavaScript that uses JavaScript modules . This configuration:
{
""extra_js_urls"": [
{
""url"": ""https://example.datasette.io/module.js"",
""module"": true
}
]
}
Will produce this HTML:
","[""Custom pages and templates""]","[{""href"": ""https://www.srihash.org/"", ""label"": ""www.srihash.org""}, {""href"": ""https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules"", ""label"": ""JavaScript modules""}]"
writing_plugins:writing-plugins-configuration,writing_plugins,writing-plugins-configuration,Writing plugins that accept configuration,"When you are writing plugins, you can access plugin configuration like this using the datasette plugin_config() method. If you know you need plugin configuration for a specific table, you can access it like this:
plugin_config = datasette.plugin_config(
""datasette-cluster-map"", database=""sf-trees"", table=""Street_Tree_List""
)
This will return the {""latitude_column"": ""lat"", ""longitude_column"": ""lng""} in the above example.
If there is no configuration for that plugin, the method will return None .
If it cannot find the requested configuration at the table layer, it will fall back to the database layer and then the root layer. For example, a user may have set the plugin configuration option like so:
{
""databases: {
""sf-trees"": {
""plugins"": {
""datasette-cluster-map"": {
""latitude_column"": ""xlat"",
""longitude_column"": ""xlng""
}
}
}
}
}
In this case, the above code would return that configuration for ANY table within the sf-trees database.
The plugin configuration could also be set at the top level of metadata.json :
{
""title"": ""This is the top-level title in metadata.json"",
""plugins"": {
""datasette-cluster-map"": {
""latitude_column"": ""xlat"",
""longitude_column"": ""xlng""
}
}
}
Now that datasette-cluster-map plugin configuration will apply to every table in every database.","[""Writing plugins""]",[]
testing_plugins:testing-plugins-register-in-test,testing_plugins,testing-plugins-register-in-test,Registering a plugin for the duration of a test,"When writing tests for plugins you may find it useful to register a test plugin just for the duration of a single test. You can do this using pm.register() and pm.unregister() like this:
from datasette import hookimpl
from datasette.app import Datasette
from datasette.plugins import pm
import pytest
@pytest.mark.asyncio
async def test_using_test_plugin():
class TestPlugin:
__name__ = ""TestPlugin""
# Use hookimpl and method names to register hooks
@hookimpl
def register_routes(self):
return [
(r""^/error$"", lambda: 1 / 0),
]
pm.register(TestPlugin(), name=""undo"")
try:
# The test implementation goes here
datasette = Datasette()
response = await datasette.client.get(""/error"")
assert response.status_code == 500
finally:
pm.unregister(name=""undo"")","[""Testing plugins""]",[]
changelog:foreign-key-expansions,changelog,foreign-key-expansions,Foreign key expansions,"When Datasette detects a foreign key reference it attempts to resolve a label
for that reference (automatically or using the Specifying the label column for a table metadata
option) so it can display a link to the associated row.
This expansion is now also available for JSON and CSV representations of the
table, using the new _labels=on query string option. See
Expanding foreign key references for more details.","[""Changelog"", ""0.23 (2018-06-18)""]",[]
settings:setting-facet-suggest-time-limit-ms,settings,setting-facet-suggest-time-limit-ms,facet_suggest_time_limit_ms,"When Datasette calculates suggested facets it needs to run a SQL query for every column in your table. The default for this time limit is 50ms to account for the fact that it needs to run once for every column. If the time limit is exceeded the column will not be suggested as a facet.
You can increase this time limit like so:
datasette mydatabase.db --setting facet_suggest_time_limit_ms 500","[""Settings"", ""Settings""]",[]
full_text_search:configuring-fts-by-hand,full_text_search,configuring-fts-by-hand,Configuring FTS by hand,"We recommend using sqlite-utils , but if you want to hand-roll a SQLite full-text search table you can do so using the following SQL.
To enable full-text search for a table called items that works against the name and description columns, you would run this SQL to create a new items_fts FTS virtual table:
CREATE VIRTUAL TABLE ""items_fts"" USING FTS4 (
name,
description,
content=""items""
);
This creates a set of tables to power full-text search against items . The new items_fts table will be detected by Datasette as the fts_table for the items table.
Creating the table is not enough: you also need to populate it with a copy of the data that you wish to make searchable. You can do that using the following SQL:
INSERT INTO ""items_fts"" (rowid, name, description)
SELECT rowid, name, description FROM items;
If your table has columns that are foreign key references to other tables you can include that data in your full-text search index using a join. Imagine the items table has a foreign key column called category_id which refers to a categories table - you could create a full-text search table like this:
CREATE VIRTUAL TABLE ""items_fts"" USING FTS4 (
name,
description,
category_name,
content=""items""
);
And then populate it like this:
INSERT INTO ""items_fts"" (rowid, name, description, category_name)
SELECT items.rowid,
items.name,
items.description,
categories.name
FROM items JOIN categories ON items.category_id=categories.id;
You can use this technique to populate the full-text search index from any combination of tables and joins that makes sense for your project.","[""Full-text search"", ""Enabling full-text search for a SQLite table""]","[{""href"": ""https://sqlite-utils.datasette.io/"", ""label"": ""sqlite-utils""}]"
testing_plugins:id1,testing_plugins,id1,Testing plugins,"We recommend using pytest to write automated tests for your plugins.
If you use the template described in Starting an installable plugin using cookiecutter your plugin will start with a single test in your tests/ directory that looks like this:
from datasette.app import Datasette
import pytest
@pytest.mark.asyncio
async def test_plugin_is_installed():
datasette = Datasette(memory=True)
response = await datasette.client.get(""/-/plugins.json"")
assert response.status_code == 200
installed_plugins = {p[""name""] for p in response.json()}
assert (
""datasette-plugin-template-demo""
in installed_plugins
)
This test uses the datasette.client object to exercise a test instance of Datasette. datasette.client is a wrapper around the HTTPX Python library which can imitate HTTP requests using ASGI. This is the recommended way to write tests against a Datasette instance.
This test also uses the pytest-asyncio package to add support for async def test functions running under pytest.
You can install these packages like so:
pip install pytest pytest-asyncio
If you are building an installable package you can add them as test dependencies to your setup.py module like this:
setup(
name=""datasette-my-plugin"",
# ...
extras_require={""test"": [""pytest"", ""pytest-asyncio""]},
tests_require=[""datasette-my-plugin[test]""],
)
You can then install the test dependencies like so:
pip install -e '.[test]'
Then run the tests using pytest like so:
pytest",[],"[{""href"": ""https://docs.pytest.org/"", ""label"": ""pytest""}, {""href"": ""https://www.python-httpx.org/"", ""label"": ""HTTPX""}, {""href"": ""https://pypi.org/project/pytest-asyncio/"", ""label"": ""pytest-asyncio""}]"
changelog:id83,changelog,id83,Small changes,"We now show the size of the database file next to the download link ( #172 )
New /-/databases introspection page shows currently connected databases ( #470 )
Binary data is no longer displayed on the table and row pages ( #442 - thanks, Russ Garrett)
New show/hide SQL links on custom query pages ( #415 )
The extra_body_script plugin hook now accepts an optional view_name argument ( #443 - thanks, Russ Garrett)
Bumped Jinja2 dependency to 2.10.1 ( #426 )
All table filters are now documented, and documentation is enforced via unit tests ( 2c19a27 )
New project guideline: master should stay shippable at all times! ( 31f36e1 )
Fixed a bug where sqlite_timelimit() occasionally failed to clean up after itself ( bac4e01 )
We no longer load additional plugins when executing pytest ( #438 )
Homepage now links to database views if there are less than five tables in a database ( #373 )
The --cors option is now respected by error pages ( #453 )
datasette publish heroku now uses the --include-vcs-ignore option, which means it works under Travis CI ( #407 )
datasette publish heroku now publishes using Python 3.6.8 ( 666c374 )
Renamed datasette publish now to datasette publish nowv1 ( #472 )
datasette publish nowv1 now accepts multiple --alias parameters ( 09ef305 )
Removed the datasette skeleton command ( #476 )
The documentation on how to build the documentation now recommends sphinx-autobuild","[""Changelog"", ""0.28 (2019-05-19)""]","[{""href"": ""https://github.com/simonw/datasette/issues/172"", ""label"": ""#172""}, {""href"": ""https://github.com/simonw/datasette/issues/470"", ""label"": ""#470""}, {""href"": ""https://github.com/simonw/datasette/pull/442"", ""label"": ""#442""}, {""href"": ""https://github.com/simonw/datasette/issues/415"", ""label"": ""#415""}, {""href"": ""https://github.com/simonw/datasette/pull/443"", ""label"": ""#443""}, {""href"": ""https://github.com/simonw/datasette/pull/426"", ""label"": ""#426""}, {""href"": ""https://github.com/simonw/datasette/commit/2c19a27d15a913e5f3dd443f04067169a6f24634"", ""label"": ""2c19a27""}, {""href"": ""https://github.com/simonw/datasette/commit/31f36e1b97ccc3f4387c80698d018a69798b6228"", ""label"": ""31f36e1""}, {""href"": ""https://github.com/simonw/datasette/commit/bac4e01f40ae7bd19d1eab1fb9349452c18de8f5"", ""label"": ""bac4e01""}, {""href"": ""https://github.com/simonw/datasette/issues/438"", ""label"": ""#438""}, {""href"": ""https://github.com/simonw/datasette/issues/373"", ""label"": ""#373""}, {""href"": ""https://github.com/simonw/datasette/issues/453"", ""label"": ""#453""}, {""href"": ""https://github.com/simonw/datasette/pull/407"", ""label"": ""#407""}, {""href"": ""https://github.com/simonw/datasette/commit/666c37415a898949fae0437099d62a35b1e9c430"", ""label"": ""666c374""}, {""href"": ""https://github.com/simonw/datasette/issues/472"", ""label"": ""#472""}, {""href"": ""https://github.com/simonw/datasette/commit/09ef305c687399384fe38487c075e8669682deb4"", ""label"": ""09ef305""}, {""href"": ""https://github.com/simonw/datasette/issues/476"", ""label"": ""#476""}]"
publish:publish-vercel,publish,publish-vercel,Publishing to Vercel,"Vercel - previously known as Zeit Now - provides a layer over AWS Lambda to allow for quick, scale-to-zero deployment. You can deploy Datasette instances to Vercel using the datasette-publish-vercel plugin.
pip install datasette-publish-vercel
datasette publish vercel mydatabase.db --project my-database-project
Not every feature is supported: consult the datasette-publish-vercel README for more details.","[""Publishing data"", ""datasette publish""]","[{""href"": ""https://vercel.com/"", ""label"": ""Vercel""}, {""href"": ""https://github.com/simonw/datasette-publish-vercel"", ""label"": ""datasette-publish-vercel""}, {""href"": ""https://github.com/simonw/datasette-publish-vercel/blob/main/README.md"", ""label"": ""datasette-publish-vercel README""}]"
changelog:id209,changelog,id209,0.8 (2017-11-13),"V0.8 - added PyPI metadata, ready to ship.
Implemented offset/limit pagination for views ( #70 ).
Improved pagination. ( #78 )
Limit on max rows returned, controlled by --max_returned_rows option. ( #69 )
If someone executes 'select * from table' against a table with a million rows
in it, we could run into problems: just serializing that much data as JSON is
likely to lock up the server.
Solution: we now have a hard limit on the maximum number of rows that can be
returned by a query. If that limit is exceeded, the server will return a
""truncated"": true field in the JSON.
This limit can be optionally controlled by the new --max_returned_rows
option. Setting that option to 0 disables the limit entirely.","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/70"", ""label"": ""#70""}, {""href"": ""https://github.com/simonw/datasette/issues/78"", ""label"": ""#78""}, {""href"": ""https://github.com/simonw/datasette/issues/69"", ""label"": ""#69""}]"
internals:internals-utils-await-me-maybe,internals,internals-utils-await-me-maybe,await_me_maybe(value),"Utility function for calling await on a return value if it is awaitable, otherwise returning the value. This is used by Datasette to support plugin hooks that can optionally return awaitable functions. Read more about this function in The “await me maybe” pattern for Python asyncio .
async datasette.utils. await_me_maybe value : Any Any
If value is callable, call it. If awaitable, await it. Otherwise return it.","[""Internals for plugins"", ""The datasette.utils module""]","[{""href"": ""https://simonwillison.net/2020/Sep/2/await-me-maybe/"", ""label"": ""The “await me maybe” pattern for Python asyncio""}]"
cli-reference:cli-help-uninstall-help,cli-reference,cli-help-uninstall-help,datasette uninstall,"Uninstall one or more plugins.
[[[cog
help([""uninstall"", ""--help""])
]]]
Usage: datasette uninstall [OPTIONS] PACKAGES...
Uninstall plugins and Python packages from the Datasette environment
Options:
-y, --yes Don't ask for confirmation
--help Show this message and exit.
[[[end]]]","[""CLI reference""]",[]
authentication:permissions-view-instance,authentication,permissions-view-instance,view-instance,"Top level permission - Actor is allowed to view any pages within this instance, starting at https://latest.datasette.io/
Default allow .","[""Authentication and permissions"", ""Built-in permissions""]","[{""href"": ""https://latest.datasette.io/"", ""label"": ""https://latest.datasette.io/""}]"
facets:facets-in-query-strings,facets,facets-in-query-strings,Facets in query strings,"To turn on faceting for specific columns on a Datasette table view, add one or more _facet=COLUMN parameters to the URL.
For example, if you want to turn on facets for the city_id and state columns, construct a URL that looks like this:
/dbname/tablename?_facet=state&_facet=city_id
This works for both the HTML interface and the .json view.
When enabled, facets will cause a facet_results block to be added to the JSON output, looking something like this:
{
""state"": {
""name"": ""state"",
""results"": [
{
""value"": ""CA"",
""label"": ""CA"",
""count"": 10,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&state=CA"",
""selected"": false
},
{
""value"": ""MI"",
""label"": ""MI"",
""count"": 4,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&state=MI"",
""selected"": false
},
{
""value"": ""MC"",
""label"": ""MC"",
""count"": 1,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&state=MC"",
""selected"": false
}
],
""truncated"": false
}
""city_id"": {
""name"": ""city_id"",
""results"": [
{
""value"": 1,
""label"": ""San Francisco"",
""count"": 6,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&city_id=1"",
""selected"": false
},
{
""value"": 2,
""label"": ""Los Angeles"",
""count"": 4,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&city_id=2"",
""selected"": false
},
{
""value"": 3,
""label"": ""Detroit"",
""count"": 4,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&city_id=3"",
""selected"": false
},
{
""value"": 4,
""label"": ""Memnonia"",
""count"": 1,
""toggle_url"": ""http://...?_facet=city_id&_facet=state&city_id=4"",
""selected"": false
}
],
""truncated"": false
}
}
If Datasette detects that a column is a foreign key, the ""label"" property will be automatically derived from the detected label column on the referenced table.
The default number of facet results returned is 30, controlled by the default_facet_size setting.
You can increase this on an individual page by adding ?_facet_size=100 to the query string, up to a maximum of max_returned_rows (which defaults to 1000).","[""Facets""]",[]
internals:internals-response-set-cookie,internals,internals-response-set-cookie,Setting cookies with response.set_cookie(),"To set cookies on the response, use the response.set_cookie(...) method. The method signature looks like this:
def set_cookie(
self,
key,
value="""",
max_age=None,
expires=None,
path=""/"",
domain=None,
secure=False,
httponly=False,
samesite=""lax"",
):
...
You can use this with datasette.sign() to set signed cookies. Here's how you would set the ds_actor cookie for use with Datasette authentication :
response = Response.redirect(""/"")
response.set_cookie(
""ds_actor"",
datasette.sign({""a"": {""id"": ""cleopaws""}}, ""actor""),
)
return response","[""Internals for plugins"", ""Response class""]",[]
contributing:contributing-using-fixtures,contributing,contributing-using-fixtures,Using fixtures,"To run Datasette itself, type datasette .
You're going to need at least one SQLite database. A quick way to get started is to use the fixtures database that Datasette uses for its own tests.
You can create a copy of that database by running this command:
python tests/fixtures.py fixtures.db
Now you can run Datasette against the new fixtures database like so:
datasette fixtures.db
This will start a server at http://127.0.0.1:8001/ .
Any changes you make in the datasette/templates or datasette/static folder will be picked up immediately (though you may need to do a force-refresh in your browser to see changes to CSS or JavaScript).
If you want to change Datasette's Python code you can use the --reload option to cause Datasette to automatically reload any time the underlying code changes:
datasette --reload fixtures.db
You can also use the fixtures.py script to recreate the testing version of metadata.json used by the unit tests. To do that:
python tests/fixtures.py fixtures.db fixtures-metadata.json
Or to output the plugins used by the tests, run this:
python tests/fixtures.py fixtures.db fixtures-metadata.json fixtures-plugins
Test tables written to fixtures.db
- metadata written to fixtures-metadata.json
Wrote plugin: fixtures-plugins/register_output_renderer.py
Wrote plugin: fixtures-plugins/view_name.py
Wrote plugin: fixtures-plugins/my_plugin.py
Wrote plugin: fixtures-plugins/messages_output_renderer.py
Wrote plugin: fixtures-plugins/my_plugin_2.py
Then run Datasette like this:
datasette fixtures.db -m fixtures-metadata.json --plugins-dir=fixtures-plugins/","[""Contributing""]",[]
publish:publish-heroku,publish,publish-heroku,Publishing to Heroku,"To publish your data using Heroku , first create an account there and install and configure the Heroku CLI tool .
You can publish one or more databases to Heroku using the following command:
datasette publish heroku mydatabase.db
This will output some details about the new deployment, including a URL like this one:
https://limitless-reef-88278.herokuapp.com/ deployed to Heroku
You can specify a custom app name by passing -n my-app-name to the publish command. This will also allow you to overwrite an existing app.
Rather than deploying directly you can use the --generate-dir option to output the files that would be deployed to a directory:
datasette publish heroku mydatabase.db --generate-dir=/tmp/deploy-this-to-heroku
See datasette publish heroku for the full list of options for this command.","[""Publishing data"", ""datasette publish""]","[{""href"": ""https://www.heroku.com/"", ""label"": ""Heroku""}, {""href"": ""https://devcenter.heroku.com/articles/heroku-cli"", ""label"": ""Heroku CLI tool""}]"
authentication:authentication-permissions-table,authentication,authentication-permissions-table,Controlling access to specific tables and views,"To limit access to the users table in your bakery.db database:
{
""databases"": {
""bakery"": {
""tables"": {
""users"": {
""allow"": {
""id"": ""*""
}
}
}
}
}
}
This works for SQL views as well - you can list their names in the ""tables"" block above in the same way as regular tables.
Restricting access to tables and views in this way will NOT prevent users from querying them using arbitrary SQL queries, like this for example.
If you are restricting access to specific tables you should also use the ""allow_sql"" block to prevent users from bypassing the limit with their own SQL queries - see Controlling the ability to execute arbitrary SQL .","[""Authentication and permissions"", ""Configuring permissions in metadata.json""]","[{""href"": ""https://latest.datasette.io/fixtures?sql=select+*+from+facetable"", ""label"": ""like this""}]"
authentication:authentication-permissions-database,authentication,authentication-permissions-database,Controlling access to specific databases,"To limit access to a specific private.db database to just authenticated users, use the ""allow"" block like this:
{
""databases"": {
""private"": {
""allow"": {
""id"": ""*""
}
}
}
}","[""Authentication and permissions"", ""Configuring permissions in metadata.json""]",[]
contributing:contributing-formatting-prettier,contributing,contributing-formatting-prettier,Prettier,"To install Prettier, install Node.js and then run the following in the root of your datasette repository checkout:
$ npm install
This will install Prettier in a node_modules directory. You can then check that your code matches the coding style like so:
$ npm run prettier -- --check
> prettier
> prettier 'datasette/static/*[!.min].js' ""--check""
Checking formatting...
[warn] datasette/static/plugins.js
[warn] Code style issues found in the above file(s). Forgot to run Prettier?
You can fix any problems by running:
$ npm run fix","[""Contributing"", ""Code formatting""]","[{""href"": ""https://nodejs.org/en/download/package-manager/"", ""label"": ""install Node.js""}]"
custom_templates:custom-pages-404,custom_templates,custom-pages-404,Returning 404s,"To indicate that content could not be found and display the default 404 page you can use the raise_404(message) function:
{% if not rows %}
{{ raise_404(""Content not found"") }}
{% endif %}
If you call raise_404() the other content in your template will be ignored.","[""Custom pages and templates"", ""Custom pages""]",[]
changelog:id84,changelog,id84,0.27.1 (2019-05-09),"Tiny bugfix release: don't install tests/ in the wrong place. Thanks, Veit Heller.","[""Changelog""]",[]
authentication:authentication-actor,authentication,authentication-actor,Actors,"Through plugins, Datasette can support both authenticated users (with cookies) and authenticated API agents (via authentication tokens). The word ""actor"" is used to cover both of these cases.
Every request to Datasette has an associated actor value, available in the code as request.actor . This can be None for unauthenticated requests, or a JSON compatible Python dictionary for authenticated users or API agents.
The actor dictionary can be any shape - the design of that data structure is left up to the plugins. A useful convention is to include an ""id"" string, as demonstrated by the ""root"" actor below.
Plugins can use the actor_from_request(datasette, request) hook to implement custom logic for authenticating an actor based on the incoming HTTP request.","[""Authentication and permissions""]",[]
changelog:id74,changelog,id74,0.31 (2019-11-11),"This version adds compatibility with Python 3.8 and breaks compatibility with Python 3.5.
If you are still running Python 3.5 you should stick with 0.30.2 , which you can install like this:
pip install datasette==0.30.2
Format SQL button now works with read-only SQL queries - thanks, Tobias Kunze ( #602 )
New ?column__notin=x,y,z filter for table views ( #614 )
Table view now uses select col1, col2, col3 instead of select *
Database filenames can now contain spaces - thanks, Tobias Kunze ( #590 )
Removed obsolete ?_group_count=col feature ( #504 )
Improved user interface and documentation for datasette publish cloudrun ( #608 )
Tables with indexes now show the CREATE INDEX statements on the table page ( #618 )
Current version of uvicorn is now shown on /-/versions
Python 3.8 is now supported! ( #622 )
Python 3.5 is no longer supported.","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/pull/602"", ""label"": ""#602""}, {""href"": ""https://github.com/simonw/datasette/issues/614"", ""label"": ""#614""}, {""href"": ""https://github.com/simonw/datasette/pull/590"", ""label"": ""#590""}, {""href"": ""https://github.com/simonw/datasette/issues/504"", ""label"": ""#504""}, {""href"": ""https://github.com/simonw/datasette/issues/608"", ""label"": ""#608""}, {""href"": ""https://github.com/simonw/datasette/issues/618"", ""label"": ""#618""}, {""href"": ""https://www.uvicorn.org/"", ""label"": ""uvicorn""}, {""href"": ""https://github.com/simonw/datasette/issues/622"", ""label"": ""#622""}]"
plugin_hooks:plugin-hook-extra-js-urls,plugin_hooks,plugin-hook-extra-js-urls,"extra_js_urls(template, database, table, columns, view_name, request, datasette)","This takes the same arguments as extra_template_vars(...)
This works in the same way as extra_css_urls() but for JavaScript. You can
return a list of URLs, a list of dictionaries or an awaitable function that returns those things:
from datasette import hookimpl
@hookimpl
def extra_js_urls():
return [
{
""url"": ""https://code.jquery.com/jquery-3.3.1.slim.min.js"",
""sri"": ""sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"",
}
]
You can also return URLs to files from your plugin's static/ directory, if
you have one:
@hookimpl
def extra_js_urls():
return [""/-/static-plugins/your-plugin/app.js""]
Note that your-plugin here should be the hyphenated plugin name - the name that is displayed in the list on the /-/plugins debug page.
If your code uses JavaScript modules you should include the ""module"": True key. See Custom CSS and JavaScript for more details.
@hookimpl
def extra_js_urls():
return [
{
""url"": ""/-/static-plugins/your-plugin/app.js"",
""module"": True,
}
]
Examples: datasette-cluster-map , datasette-vega","[""Plugin hooks""]","[{""href"": ""https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules"", ""label"": ""JavaScript modules""}, {""href"": ""https://datasette.io/plugins/datasette-cluster-map"", ""label"": ""datasette-cluster-map""}, {""href"": ""https://datasette.io/plugins/datasette-vega"", ""label"": ""datasette-vega""}]"
plugin_hooks:plugin-hook-extra-css-urls,plugin_hooks,plugin-hook-extra-css-urls,"extra_css_urls(template, database, table, columns, view_name, request, datasette)","This takes the same arguments as extra_template_vars(...)
Return a list of extra CSS URLs that should be included on the page. These can
take advantage of the CSS class hooks described in Custom pages and templates .
This can be a list of URLs:
from datasette import hookimpl
@hookimpl
def extra_css_urls():
return [
""https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css""
]
Or a list of dictionaries defining both a URL and an
SRI hash :
@hookimpl
def extra_css_urls():
return [
{
""url"": ""https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"",
""sri"": ""sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4"",
}
]
This function can also return an awaitable function, useful if it needs to run any async code:
@hookimpl
def extra_css_urls(datasette):
async def inner():
db = datasette.get_database()
results = await db.execute(
""select url from css_files""
)
return [r[0] for r in results]
return inner
Examples: datasette-cluster-map , datasette-vega","[""Plugin hooks""]","[{""href"": ""https://www.srihash.org/"", ""label"": ""SRI hash""}, {""href"": ""https://datasette.io/plugins/datasette-cluster-map"", ""label"": ""datasette-cluster-map""}, {""href"": ""https://datasette.io/plugins/datasette-vega"", ""label"": ""datasette-vega""}]"
settings:setting-template-debug,settings,setting-template-debug,template_debug,"This setting enables template context debug mode, which is useful to help understand what variables are available to custom templates when you are writing them.
Enable it like this:
datasette mydatabase.db --setting template_debug 1
Now you can add ?_context=1 or &_context=1 to any Datasette page to see the context that was passed to that template.
Some examples:
https://latest.datasette.io/?_context=1
https://latest.datasette.io/fixtures?_context=1
https://latest.datasette.io/fixtures/roadside_attractions?_context=1","[""Settings"", ""Settings""]","[{""href"": ""https://latest.datasette.io/?_context=1"", ""label"": ""https://latest.datasette.io/?_context=1""}, {""href"": ""https://latest.datasette.io/fixtures?_context=1"", ""label"": ""https://latest.datasette.io/fixtures?_context=1""}, {""href"": ""https://latest.datasette.io/fixtures/roadside_attractions?_context=1"", ""label"": ""https://latest.datasette.io/fixtures/roadside_attractions?_context=1""}]"
settings:setting-trace-debug,settings,setting-trace-debug,trace_debug,"This setting enables appending ?_trace=1 to any page in order to see the SQL queries and other trace information that was used to generate that page.
Enable it like this:
datasette mydatabase.db --setting trace_debug 1
Some examples:
https://latest.datasette.io/?_trace=1
https://latest.datasette.io/fixtures/roadside_attractions?_trace=1
See datasette.tracer for details on how to hook into this mechanism as a plugin author.","[""Settings"", ""Settings""]","[{""href"": ""https://latest.datasette.io/?_trace=1"", ""label"": ""https://latest.datasette.io/?_trace=1""}, {""href"": ""https://latest.datasette.io/fixtures/roadside_attractions?_trace=1"", ""label"": ""https://latest.datasette.io/fixtures/roadside_attractions?_trace=1""}]"
authentication:id1,authentication,id1,Built-in permissions,"This section lists all of the permission checks that are carried out by Datasette core, along with the resource if it was passed.","[""Authentication and permissions""]",[]
changelog:id149,changelog,id149,0.18 (2018-04-14),"This release introduces support for units ,
contributed by Russ Garrett ( #203 ).
You can now optionally specify the units for specific columns using metadata.json .
Once specified, units will be displayed in the HTML view of your table. They also become
available for use in filters - if a column is configured with a unit of distance, you can
request all rows where that column is less than 50 meters or more than 20 feet for example.
Link foreign keys which don't have labels. [Russ Garrett]
This renders unlabeled FKs as simple links.
Also includes bonus fixes for two minor issues:
In foreign key link hrefs the primary key was escaped using HTML
escaping rather than URL escaping. This broke some non-integer PKs.
Print tracebacks to console when handling 500 errors.
Fix SQLite error when loading rows with no incoming FKs. [Russ
Garrett]
This fixes an error caused by an invalid query when loading incoming FKs.
The error was ignored due to async but it still got printed to the
console.
Allow custom units to be registered with Pint. [Russ Garrett]
Support units in filters. [Russ Garrett]
Tidy up units support. [Russ Garrett]
Add units to exported JSON
Units key in metadata skeleton
Docs
Initial units support. [Russ Garrett]
Add support for specifying units for a column in metadata.json and
rendering them on display using
pint","[""Changelog""]","[{""href"": ""https://docs.datasette.io/en/stable/metadata.html#specifying-units-for-a-column"", ""label"": ""support for units""}, {""href"": ""https://github.com/simonw/datasette/issues/203"", ""label"": ""#203""}, {""href"": ""https://pint.readthedocs.io/en/latest/"", ""label"": ""pint""}]"
changelog:id41,changelog,id41,0.52 (2020-11-28),"This release includes a number of changes relating to an internal rebranding effort: Datasette's configuration mechanism (things like datasette --config default_page_size:10 ) has been renamed to settings .
New --setting default_page_size 10 option as a replacement for --config default_page_size:10 (note the lack of a colon). The --config option is deprecated but will continue working until Datasette 1.0. ( #992 )
The /-/config introspection page is now /-/settings , and the previous page redirects to the new one. ( #1103 )
The config.json file in Configuration directory mode is now called settings.json . ( #1104 )
The undocumented datasette.config() internal method has been replaced by a documented .setting(key) method. ( #1107 )
Also in this release:
New plugin hook: database_actions(datasette, actor, database, request) , which adds menu items to a new cog menu shown at the top of the database page. ( #1077 )
datasette publish cloudrun has a new --apt-get-install option that can be used to install additional Ubuntu packages as part of the deployment. This is useful for deploying the new datasette-ripgrep plugin . ( #1110 )
Swept the documentation to remove words that minimize involved difficulty. ( #1089 )
And some bug fixes:
Foreign keys linking to rows with blank label columns now display as a hyphen, allowing those links to be clicked. ( #1086 )
Fixed bug where row pages could sometimes 500 if the underlying queries exceeded a time limit. ( #1088 )
Fixed a bug where the table action menu could appear partially obscured by the edge of the page. ( #1084 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/992"", ""label"": ""#992""}, {""href"": ""https://github.com/simonw/datasette/issues/1103"", ""label"": ""#1103""}, {""href"": ""https://github.com/simonw/datasette/issues/1104"", ""label"": ""#1104""}, {""href"": ""https://github.com/simonw/datasette/issues/1107"", ""label"": ""#1107""}, {""href"": ""https://github.com/simonw/datasette/issues/1077"", ""label"": ""#1077""}, {""href"": ""https://github.com/simonw/datasette-ripgrep"", ""label"": ""datasette-ripgrep plugin""}, {""href"": ""https://github.com/simonw/datasette/issues/1110"", ""label"": ""#1110""}, {""href"": ""https://github.com/simonw/datasette/issues/1089"", ""label"": ""#1089""}, {""href"": ""https://github.com/simonw/datasette/issues/1086"", ""label"": ""#1086""}, {""href"": ""https://github.com/simonw/datasette/issues/1088"", ""label"": ""#1088""}, {""href"": ""https://github.com/simonw/datasette/issues/1084"", ""label"": ""#1084""}]"
changelog:id30,changelog,id30,0.56.1 (2021-06-05),"This release fixes a reflected cross-site scripting security hole with the ?_trace=1 feature. You should upgrade to this version, or to Datasette 0.57, as soon as possible. ( #1360 )","[""Changelog""]","[{""href"": ""https://owasp.org/www-community/attacks/xss/#reflected-xss-attacks"", ""label"": ""reflected cross-site scripting""}, {""href"": ""https://github.com/simonw/datasette/issues/1360"", ""label"": ""#1360""}]"
changelog:id29,changelog,id29,0.57 (2021-06-05),"This release fixes a reflected cross-site scripting security hole with the ?_trace=1 feature. You should upgrade to this version, or to Datasette 0.56.1, as soon as possible. ( #1360 )
In addition to the security fix, this release includes ?_col= and ?_nocol= options for controlling which columns are displayed for a table, ?_facet_size= for increasing the number of facet results returned, re-display of your SQL query should an error occur and numerous bug fixes.","[""Changelog""]","[{""href"": ""https://owasp.org/www-community/attacks/xss/#reflected-xss-attacks"", ""label"": ""reflected cross-site scripting""}, {""href"": ""https://github.com/simonw/datasette/issues/1360"", ""label"": ""#1360""}]"
changelog:id111,changelog,id111,0.23 (2018-06-18),"This release features CSV export, improved options for foreign key expansions,
new configuration settings and improved support for SpatiaLite.
See datasette/compare/0.22.1...0.23 for a full list of
commits added since the last release.","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/compare/0.22.1...0.23"", ""label"": ""datasette/compare/0.22.1...0.23""}]"
changelog:id54,changelog,id54,0.46 (2020-08-09),"This release contains a security fix related to authenticated writable canned queries. If you are using this feature you should upgrade as soon as possible.
Security fix: CSRF tokens were incorrectly included in read-only canned query forms, which could allow them to be leaked to a sophisticated attacker. See issue 918 for details.
Datasette now supports GraphQL via the new datasette-graphql plugin - see GraphQL in Datasette with the new datasette-graphql plugin .
Principle git branch has been renamed from master to main . ( #849 )
New debugging tool: /-/allow-debug tool ( demo here ) helps test allow blocks against actors, as described in Defining permissions with ""allow"" blocks . ( #908 )
New logo for the documentation, and a new project tagline: ""An open source multi-tool for exploring and publishing data"".
Whitespace in column values is now respected on display, using white-space: pre-wrap . ( #896 )
New await request.post_body() method for accessing the raw POST body, see Request object . ( #897 )
Database file downloads now include a content-length HTTP header, enabling download progress bars. ( #905 )
File downloads now also correctly set the suggested file name using a content-disposition HTTP header. ( #909 )
tests are now excluded from the Datasette package properly - thanks, abeyerpath. ( #456 )
The Datasette package published to PyPI now includes sdist as well as bdist_wheel .
Better titles for canned query pages. ( #887 )
Now only loads Python files from a directory passed using the --plugins-dir option - thanks, Amjith Ramanujam. ( #890 )
New documentation section on Publishing to Vercel .","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/918"", ""label"": ""issue 918""}, {""href"": ""https://github.com/simonw/datasette-graphql"", ""label"": ""datasette-graphql""}, {""href"": ""https://simonwillison.net/2020/Aug/7/datasette-graphql/"", ""label"": ""GraphQL in Datasette with the new datasette-graphql plugin""}, {""href"": ""https://github.com/simonw/datasette/issues/849"", ""label"": ""#849""}, {""href"": ""https://latest.datasette.io/-/allow-debug"", ""label"": ""demo here""}, {""href"": ""https://github.com/simonw/datasette/issues/908"", ""label"": ""#908""}, {""href"": ""https://github.com/simonw/datasette/issues/896"", ""label"": ""#896""}, {""href"": ""https://github.com/simonw/datasette/issues/897"", ""label"": ""#897""}, {""href"": ""https://github.com/simonw/datasette/issues/905"", ""label"": ""#905""}, {""href"": ""https://github.com/simonw/datasette/issues/909"", ""label"": ""#909""}, {""href"": ""https://github.com/simonw/datasette/issues/456"", ""label"": ""#456""}, {""href"": ""https://github.com/simonw/datasette/issues/887"", ""label"": ""#887""}, {""href"": ""https://github.com/simonw/datasette/pull/890"", ""label"": ""#890""}]"
internals:internals-datasette,internals,internals-datasette,Datasette class,"This object is an instance of the Datasette class, passed to many plugin hooks as an argument called datasette .
You can create your own instance of this - for example to help write tests for a plugin - like so:
from datasette.app import Datasette
# With no arguments a single in-memory database will be attached
datasette = Datasette()
# The files= argument can load files from disk
datasette = Datasette(files=[""/path/to/my-database.db""])
# Pass metadata as a JSON dictionary like this
datasette = Datasette(
files=[""/path/to/my-database.db""],
metadata={
""databases"": {
""my-database"": {
""description"": ""This is my database""
}
}
},
)
Constructor parameters include:
files=[...] - a list of database files to open
immutables=[...] - a list of database files to open in immutable mode
metadata={...} - a dictionary of Metadata
config_dir=... - the configuration directory to use, stored in datasette.config_dir","[""Internals for plugins""]",[]
internals:database-execute-write-fn,internals,database-execute-write-fn,"await db.execute_write_fn(fn, block=True)","This method works like .execute_write() , but instead of a SQL statement you give it a callable Python function. Your function will be queued up and then called when the write connection is available, passing that connection as the argument to the function.
The function can then perform multiple actions, safe in the knowledge that it has exclusive access to the single writable connection for as long as it is executing.
fn needs to be a regular function, not an async def function.
For example:
def delete_and_return_count(conn):
conn.execute(""delete from some_table where id > 5"")
return conn.execute(
""select count(*) from some_table""
).fetchone()[0]
try:
num_rows_left = await database.execute_write_fn(
delete_and_return_count
)
except Exception as e:
print(""An error occurred:"", e)
The value returned from await database.execute_write_fn(...) will be the return value from your function.
If your function raises an exception that exception will be propagated up to the await line.
If you specify block=False the method becomes fire-and-forget, queueing your function to be executed and then allowing your code after the call to .execute_write_fn() to continue running while the underlying thread waits for an opportunity to run your function. A UUID representing the queued task will be returned. Any exceptions in your code will be silently swallowed.","[""Internals for plugins"", ""Database class""]",[]
settings:setting-facet-time-limit-ms,settings,setting-facet-time-limit-ms,facet_time_limit_ms,"This is the time limit Datasette allows for calculating a facet, which defaults to 200ms:
datasette mydatabase.db --setting facet_time_limit_ms 1000","[""Settings"", ""Settings""]",[]
changelog:id142,changelog,id142,0.19 (2018-04-16),"This is the first preview of the new Datasette plugins mechanism. Only two
plugin hooks are available so far - for custom SQL functions and custom template
filters. There's plenty more to come - read the documentation and get involved in
the tracking ticket if you
have feedback on the direction so far.
Fix for _sort_desc=sortable_with_nulls test, refs #216
Fixed #216 - paginate correctly when sorting by nullable column
Initial documentation for plugins, closes #213
https://docs.datasette.io/en/stable/plugins.html
New --plugins-dir=plugins/ option ( #212 )
New option causing Datasette to load and evaluate all of the Python files in
the specified directory and register any plugins that are defined in those
files.
This new option is available for the following commands:
datasette serve mydb.db --plugins-dir=plugins/
datasette publish now/heroku mydb.db --plugins-dir=plugins/
datasette package mydb.db --plugins-dir=plugins/
Start of the plugin system, based on pluggy ( #210 )
Uses https://pluggy.readthedocs.io/ originally created for the py.test project
We're starting with two plugin hooks:
prepare_connection(conn)
This is called when a new SQLite connection is created. It can be used to register custom SQL functions.
prepare_jinja2_environment(env)
This is called with the Jinja2 environment. It can be used to register custom template tags and filters.
An example plugin which uses these two hooks can be found at https://github.com/simonw/datasette-plugin-demos or installed using pip install datasette-plugin-demos
Refs #14
Return HTTP 405 on InvalidUsage rather than 500. [Russ Garrett]
This also stops it filling up the logs. This happens for HEAD requests
at the moment - which perhaps should be handled better, but that's a
different issue.","[""Changelog""]","[{""href"": ""https://docs.datasette.io/en/stable/plugins.html"", ""label"": ""the documentation""}, {""href"": ""https://github.com/simonw/datasette/issues/14"", ""label"": ""the tracking ticket""}, {""href"": ""https://github.com/simonw/datasette/issues/216"", ""label"": ""#216""}, {""href"": ""https://github.com/simonw/datasette/issues/216"", ""label"": ""#216""}, {""href"": ""https://github.com/simonw/datasette/issues/213"", ""label"": ""#213""}, {""href"": ""https://docs.datasette.io/en/stable/plugins.html"", ""label"": ""https://docs.datasette.io/en/stable/plugins.html""}, {""href"": ""https://github.com/simonw/datasette/issues/212"", ""label"": ""#212""}, {""href"": ""https://github.com/simonw/datasette/issues/14"", ""label"": ""#210""}, {""href"": ""https://pluggy.readthedocs.io/"", ""label"": ""https://pluggy.readthedocs.io/""}, {""href"": ""https://github.com/simonw/datasette-plugin-demos"", ""label"": ""https://github.com/simonw/datasette-plugin-demos""}, {""href"": ""https://github.com/simonw/datasette/issues/14"", ""label"": ""#14""}]"
plugin_hooks:plugin-hook-startup,plugin_hooks,plugin-hook-startup,startup(datasette),"This hook fires when the Datasette application server first starts up. You can implement a regular function, for example to validate required plugin configuration:
@hookimpl
def startup(datasette):
config = datasette.plugin_config(""my-plugin"") or {}
assert (
""required-setting"" in config
), ""my-plugin requires setting required-setting""
Or you can return an async function which will be awaited on startup. Use this option if you need to make any database queries:
@hookimpl
def startup(datasette):
async def inner():
db = datasette.get_database()
if ""my_table"" not in await db.table_names():
await db.execute_write(
""""""
create table my_table (mycol text)
""""""
)
return inner
Potential use-cases:
Run some initialization code for the plugin
Create database tables that a plugin needs on startup
Validate the metadata configuration for a plugin on startup, and raise an error if it is invalid
If you are writing unit tests for a plugin that uses this hook and doesn't exercise Datasette by sending
any simulated requests through it you will need to explicitly call await ds.invoke_startup() in your tests. An example:
@pytest.mark.asyncio
async def test_my_plugin():
ds = Datasette()
await ds.invoke_startup()
# Rest of test goes here
Examples: datasette-saved-queries , datasette-init","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-saved-queries"", ""label"": ""datasette-saved-queries""}, {""href"": ""https://datasette.io/plugins/datasette-init"", ""label"": ""datasette-init""}]"
internals:internals-utils-parse-metadata,internals,internals-utils-parse-metadata,parse_metadata(content),"This function accepts a string containing either JSON or YAML, expected to be of the format described in Metadata . It returns a nested Python dictionary representing the parsed data from that string.
If the metadata cannot be parsed as either JSON or YAML the function will raise a utils.BadMetadataError exception.
datasette.utils. parse_metadata content : str dict
Detects if content is JSON or YAML and parses it appropriately.","[""Internals for plugins"", ""The datasette.utils module""]",[]
cli-reference:cli-help-serve-help,cli-reference,cli-help-serve-help,datasette serve,"This command starts the Datasette web application running on your machine:
datasette serve mydatabase.db
Or since this is the default command you can run this instead:
datasette mydatabase.db
Once started you can access it at http://localhost:8001
[[[cog
help([""serve"", ""--help""])
]]]
Usage: datasette serve [OPTIONS] [FILES]...
Serve up specified SQLite database files with a web UI
Options:
-i, --immutable PATH Database files to open in immutable mode
-h, --host TEXT Host for server. Defaults to 127.0.0.1 which
means only connections from the local machine
will be allowed. Use 0.0.0.0 to listen to all
IPs and allow access from other machines.
-p, --port INTEGER RANGE Port for server, defaults to 8001. Use -p 0 to
automatically assign an available port.
[0<=x<=65535]
--uds TEXT Bind to a Unix domain socket
--reload Automatically reload if code or metadata
change detected - useful for development
--cors Enable CORS by serving Access-Control-Allow-
Origin: *
--load-extension PATH:ENTRYPOINT?
Path to a SQLite extension to load, and
optional entrypoint
--inspect-file TEXT Path to JSON file created using ""datasette
inspect""
-m, --metadata FILENAME Path to JSON/YAML file containing
license/source metadata
--template-dir DIRECTORY Path to directory containing custom templates
--plugins-dir DIRECTORY Path to directory containing custom plugins
--static MOUNT:DIRECTORY Serve static files from this directory at
/MOUNT/...
--memory Make /_memory database available
--config CONFIG Deprecated: set config option using
configname:value. Use --setting instead.
--setting SETTING... Setting, see
docs.datasette.io/en/stable/settings.html
--secret TEXT Secret used for signing secure values, such as
signed cookies
--root Output URL that sets a cookie authenticating
the root user
--get TEXT Run an HTTP GET request against this path,
print results and exit
--version-note TEXT Additional note to show on /-/versions
--help-settings Show available settings
--pdb Launch debugger on any errors
-o, --open Open Datasette in your web browser
--create Create database files if they do not exist
--crossdb Enable cross-database joins using the /_memory
database
--nolock Ignore locking, open locked files in read-only
mode
--ssl-keyfile TEXT SSL key file
--ssl-certfile TEXT SSL certificate file
--help Show this message and exit.
[[[end]]]","[""CLI reference""]",[]
cli-reference:cli-help-serve-help-settings,cli-reference,cli-help-serve-help-settings,datasette serve --help-settings,"This command outputs all of the available Datasette settings .
These can be passed to datasette serve using datasette serve --setting name value .
[[[cog
help([""--help-settings""])
]]]
Settings:
default_page_size Default page size for the table view
(default=100)
max_returned_rows Maximum rows that can be returned from a table or
custom query (default=1000)
num_sql_threads Number of threads in the thread pool for
executing SQLite queries (default=3)
sql_time_limit_ms Time limit for a SQL query in milliseconds
(default=1000)
default_facet_size Number of values to return for requested facets
(default=30)
facet_time_limit_ms Time limit for calculating a requested facet
(default=200)
facet_suggest_time_limit_ms Time limit for calculating a suggested facet
(default=50)
allow_facet Allow users to specify columns to facet using
?_facet= parameter (default=True)
default_allow_sql Allow anyone to run arbitrary SQL queries
(default=True)
allow_download Allow users to download the original SQLite
database files (default=True)
suggest_facets Calculate and display suggested facets
(default=True)
default_cache_ttl Default HTTP cache TTL (used in Cache-Control:
max-age= header) (default=5)
cache_size_kb SQLite cache size in KB (0 == use SQLite default)
(default=0)
allow_csv_stream Allow .csv?_stream=1 to download all rows
(ignoring max_returned_rows) (default=True)
max_csv_mb Maximum size allowed for CSV export in MB - set 0
to disable this limit (default=100)
truncate_cells_html Truncate cells longer than this in HTML table
view - set 0 to disable (default=2048)
force_https_urls Force URLs in API output to always use https://
protocol (default=False)
template_debug Allow display of template debug information with
?_context=1 (default=False)
trace_debug Allow display of SQL trace debug information with
?_trace=1 (default=False)
base_url Datasette URLs should use this base path
(default=/)
[[[end]]]","[""CLI reference"", ""datasette serve""]",[]
internals:internals-internal,internals,internals-internal,The _internal database,"This API should be considered unstable - the structure of these tables may change prior to the release of Datasette 1.0.
Datasette maintains an in-memory SQLite database with details of the the databases, tables and columns for all of the attached databases.
By default all actors are denied access to the view-database permission for the _internal database, so the database is not visible to anyone unless they sign in as root .
Plugins can access this database by calling db = datasette.get_database(""_internal"") and then executing queries using the Database API .
You can explore an example of this database by signing in as root to the latest.datasette.io demo instance and then navigating to latest.datasette.io/_internal .","[""Internals for plugins""]","[{""href"": ""https://latest.datasette.io/login-as-root"", ""label"": ""signing in as root""}, {""href"": ""https://latest.datasette.io/_internal"", ""label"": ""latest.datasette.io/_internal""}]"
full_text_search:full-text-search-fts-versions,full_text_search,full-text-search-fts-versions,FTS versions,"There are three different versions of the SQLite FTS module: FTS3, FTS4 and FTS5. You can tell which versions are supported by your instance of Datasette by checking the /-/versions page.
FTS5 is the most advanced module but may not be available in the SQLite version that is bundled with your Python installation. Most importantly, FTS5 is the only version that has the ability to order by search relevance without needing extra code.
If you can't be sure that FTS5 will be available, you should use FTS4.","[""Full-text search""]",[]
changelog:id34,changelog,id34,0.54 (2021-01-25),"The two big new features in this release are the _internal SQLite in-memory database storing details of all connected databases and tables, and support for JavaScript modules in plugins and additional scripts.
For additional commentary on this release, see Datasette 0.54, the annotated release notes .","[""Changelog""]","[{""href"": ""https://simonwillison.net/2021/Jan/25/datasette/"", ""label"": ""Datasette 0.54, the annotated release notes""}]"
metadata:metadata-source-license-about,metadata,metadata-source-license-about,"Source, license and about","The three visible metadata fields you can apply to everything, specific databases or specific tables are source, license and about. All three are optional.
source and source_url should be used to indicate where the underlying data came from.
license and license_url should be used to indicate the license under which the data can be used.
about and about_url can be used to link to further information about the project - an accompanying blog entry for example.
For each of these you can provide just the *_url field and Datasette will treat that as the default link label text and display the URL directly on the page.","[""Metadata""]",[]
changelog:id171,changelog,id171,0.14 (2017-12-09),"The theme of this release is customization: Datasette now allows every aspect
of its presentation to be customized
either using additional CSS or by providing entirely new templates.
Datasette's metadata.json format
has also been expanded, to allow per-database and per-table metadata. A new
datasette skeleton command can be used to generate a skeleton JSON file
ready to be filled in with per-database and per-table details.
The metadata.json file can also be used to define
canned queries ,
as a more powerful alternative to SQL views.
extra_css_urls / extra_js_urls in metadata
A mechanism in the metadata.json format for adding custom CSS and JS urls.
Create a metadata.json file that looks like this:
{
""extra_css_urls"": [
""https://simonwillison.net/static/css/all.bf8cd891642c.css""
],
""extra_js_urls"": [
""https://code.jquery.com/jquery-3.2.1.slim.min.js""
]
}
Then start datasette like this:
datasette mydb.db --metadata=metadata.json
The CSS and JavaScript files will be linked in the of every page.
You can also specify a SRI (subresource integrity hash) for these assets:
{
""extra_css_urls"": [
{
""url"": ""https://simonwillison.net/static/css/all.bf8cd891642c.css"",
""sri"": ""sha384-9qIZekWUyjCyDIf2YK1FRoKiPJq4PHt6tp/ulnuuyRBvazd0hG7pWbE99zvwSznI""
}
],
""extra_js_urls"": [
{
""url"": ""https://code.jquery.com/jquery-3.2.1.slim.min.js"",
""sri"": ""sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=""
}
]
}
Modern browsers will only execute the stylesheet or JavaScript if the SRI hash
matches the content served. You can generate hashes using https://www.srihash.org/
Auto-link column values that look like URLs ( #153 )
CSS styling hooks as classes on the body ( #153 )
Every template now gets CSS classes in the body designed to support custom
styling.
The index template (the top level page at / ) gets this:
The database template ( /dbname/ ) gets this:
The table template ( /dbname/tablename ) gets:
The row template ( /dbname/tablename/rowid ) gets:
The db-x and table-x classes use the database or table names themselves IF
they are valid CSS identifiers. If they aren't, we strip any invalid
characters out and append a 6 character md5 digest of the original name, in
order to ensure that multiple tables which resolve to the same stripped
character version still have different CSS classes.
Some examples (extracted from the unit tests):
""simple"" => ""simple""
""MixedCase"" => ""MixedCase""
""-no-leading-hyphens"" => ""no-leading-hyphens-65bea6""
""_no-leading-underscores"" => ""no-leading-underscores-b921bc""
""no spaces"" => ""no-spaces-7088d7""
""-"" => ""336d5e""
""no $ characters"" => ""no--characters-59e024""
datasette --template-dir=mytemplates/ argument
You can now pass an additional argument specifying a directory to look for
custom templates in.
Datasette will fall back on the default templates if a template is not
found in that directory.
Ability to over-ride templates for individual tables/databases.
It is now possible to over-ride templates on a per-database / per-row or per-
table basis.
When you access e.g. /mydatabase/mytable Datasette will look for the following:
- table-mydatabase-mytable.html
- table.html
If you provided a --template-dir argument to datasette serve it will look in
that directory first.
The lookup rules are as follows:
Index page (/):
index.html
Database page (/mydatabase):
database-mydatabase.html
database.html
Table page (/mydatabase/mytable):
table-mydatabase-mytable.html
table.html
Row page (/mydatabase/mytable/id):
row-mydatabase-mytable.html
row.html
If a table name has spaces or other unexpected characters in it, the template
filename will follow the same rules as our custom CSS classes
- for example, a table called ""Food Trucks""
will attempt to load the following templates:
table-mydatabase-Food-Trucks-399138.html
table.html
It is possible to extend the default templates using Jinja template
inheritance. If you want to customize EVERY row template with some additional
content you can do so by creating a row.html template like this:
{% extends ""default:row.html"" %}
{% block content %}
EXTRA HTML AT THE TOP OF THE CONTENT BLOCK
This line renders the original block:
{{ super() }}
{% endblock %}
--static option for datasette serve ( #160 )
You can now tell Datasette to serve static files from a specific location at a
specific mountpoint.
For example:
datasette serve mydb.db --static extra-css:/tmp/static/css
Now if you visit this URL:
http://localhost:8001/extra-css/blah.css
The following file will be served:
/tmp/static/css/blah.css
Canned query support.
Named canned queries can now be defined in metadata.json like this:
{
""databases"": {
""timezones"": {
""queries"": {
""timezone_for_point"": ""select tzid from timezones ...""
}
}
}
}
These will be shown in a new ""Queries"" section beneath ""Views"" on the database page.
New datasette skeleton command for generating metadata.json ( #164 )
metadata.json support for per-table/per-database metadata ( #165 )
Also added support for descriptions and HTML descriptions.
Here's an example metadata.json file illustrating custom per-database and per-
table metadata:
{
""title"": ""Overall datasette title"",
""description_html"": ""This is a description with HTML."",
""databases"": {
""db1"": {
""title"": ""First database"",
""description"": ""This is a string description & has no HTML"",
""license_url"": ""http://example.com/"",
""license"": ""The example license"",
""queries"": {
""canned_query"": ""select * from table1 limit 3;""
},
""tables"": {
""table1"": {
""title"": ""Custom title for table1"",
""description"": ""Tables can have descriptions too"",
""source"": ""This has a custom source"",
""source_url"": ""http://example.com/""
}
}
}
}
}
Renamed datasette build command to datasette inspect ( #130 )
Upgrade to Sanic 0.7.0 ( #168 )
https://github.com/channelcat/sanic/releases/tag/0.7.0
Package and publish commands now accept --static and --template-dir
Example usage:
datasette package --static css:extra-css/ --static js:extra-js/ \
sf-trees.db --template-dir templates/ --tag sf-trees --branch master
This creates a local Docker image that includes copies of the templates/,
extra-css/ and extra-js/ directories. You can then run it like this:
docker run -p 8001:8001 sf-trees
For publishing to Zeit now:
datasette publish now --static css:extra-css/ --static js:extra-js/ \
sf-trees.db --template-dir templates/ --name sf-trees --branch master
HTML comment showing which templates were considered for a page ( #171 )","[""Changelog""]","[{""href"": ""https://docs.datasette.io/en/stable/custom_templates.html"", ""label"": ""to be customized""}, {""href"": ""https://docs.datasette.io/en/stable/metadata.html"", ""label"": ""metadata.json format""}, {""href"": ""https://docs.datasette.io/en/stable/sql_queries.html#canned-queries"", ""label"": ""canned queries""}, {""href"": ""https://www.srihash.org/"", ""label"": ""https://www.srihash.org/""}, {""href"": ""https://github.com/simonw/datasette/issues/153"", ""label"": ""#153""}, {""href"": ""https://github.com/simonw/datasette/issues/153"", ""label"": ""#153""}, {""href"": ""https://github.com/simonw/datasette/issues/160"", ""label"": ""#160""}, {""href"": ""https://github.com/simonw/datasette/issues/164"", ""label"": ""#164""}, {""href"": ""https://github.com/simonw/datasette/issues/165"", ""label"": ""#165""}, {""href"": ""https://github.com/simonw/datasette/issues/130"", ""label"": ""#130""}, {""href"": ""https://github.com/simonw/datasette/issues/168"", ""label"": ""#168""}, {""href"": ""https://github.com/channelcat/sanic/releases/tag/0.7.0"", ""label"": ""https://github.com/channelcat/sanic/releases/tag/0.7.0""}, {""href"": ""https://github.com/simonw/datasette/issues/171"", ""label"": ""#171""}]"
pages:tableview,pages,tableview,Table,"The table page is the heart of Datasette: it allows users to interactively explore the contents of a database table, including sorting, filtering, Full-text search and applying Facets .
The HTML interface is worth spending some time exploring. As with other pages, you can return the JSON data by appending .json to the URL path, before any ? query string arguments.
The query string arguments are described in more detail here: Table arguments
You can also use the table page to interactively construct a SQL query - by applying different filters and a sort order for example - and then click the ""View and edit SQL"" link to see the SQL query that was used for the page and edit and re-submit it.
Some examples:
../items lists all of the line-items registered by UK MPs as potential conflicts of interest. It demonstrates Datasette's support for Full-text search .
../antiquities-act%2Factions_under_antiquities_act is an interface for exploring the ""actions under the antiquities act"" data table published by FiveThirtyEight.
../global-power-plants?country_long=United+Kingdom&primary_fuel=Gas is a filtered table page showing every Gas power plant in the United Kingdom. It includes some default facets (configured using its metadata.json ) and uses the datasette-cluster-map plugin to show a map of the results.","[""Pages and API endpoints""]","[{""href"": ""https://register-of-members-interests.datasettes.com/regmem/items"", ""label"": ""../items""}, {""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight/antiquities-act%2Factions_under_antiquities_act"", ""label"": ""../antiquities-act%2Factions_under_antiquities_act""}, {""href"": ""https://global-power-plants.datasettes.com/global-power-plants/global-power-plants?_facet=primary_fuel&_facet=owner&_facet=country_long&country_long__exact=United+Kingdom&primary_fuel=Gas"", ""label"": ""../global-power-plants?country_long=United+Kingdom&primary_fuel=Gas""}, {""href"": ""https://global-power-plants.datasettes.com/-/metadata"", ""label"": ""its metadata.json""}, {""href"": ""https://github.com/simonw/datasette-cluster-map"", ""label"": ""datasette-cluster-map""}]"
authentication:authentication-permissions-allow,authentication,authentication-permissions-allow,"Defining permissions with ""allow"" blocks","The standard way to define permissions in Datasette is to use an ""allow"" block. This is a JSON document describing which actors are allowed to perform a permission.
The most basic form of allow block is this ( allow demo , deny demo ):
{
""allow"": {
""id"": ""root""
}
}
This will match any actors with an ""id"" property of ""root"" - for example, an actor that looks like this:
{
""id"": ""root"",
""name"": ""Root User""
}
An allow block can specify ""deny all"" using false ( demo ):
{
""allow"": false
}
An ""allow"" of true allows all access ( demo ):
{
""allow"": true
}
Allow keys can provide a list of values. These will match any actor that has any of those values ( allow demo , deny demo ):
{
""allow"": {
""id"": [""simon"", ""cleopaws""]
}
}
This will match any actor with an ""id"" of either ""simon"" or ""cleopaws"" .
Actors can have properties that feature a list of values. These will be matched against the list of values in an allow block. Consider the following actor:
{
""id"": ""simon"",
""roles"": [""staff"", ""developer""]
}
This allow block will provide access to any actor that has ""developer"" as one of their roles ( allow demo , deny demo ):
{
""allow"": {
""roles"": [""developer""]
}
}
Note that ""roles"" is not a concept that is baked into Datasette - it's a convention that plugins can choose to implement and act on.
If you want to provide access to any actor with a value for a specific key, use ""*"" . For example, to match any logged-in user specify the following ( allow demo , deny demo ):
{
""allow"": {
""id"": ""*""
}
}
You can specify that only unauthenticated actors (from anynomous HTTP requests) should be allowed access using the special ""unauthenticated"": true key in an allow block ( allow demo , deny demo ):
{
""allow"": {
""unauthenticated"": true
}
}
Allow keys act as an ""or"" mechanism. An actor will be able to execute the query if any of their JSON properties match any of the values in the corresponding lists in the allow block. The following block will allow users with either a role of ""ops"" OR users who have an id of ""simon"" or ""cleopaws"" :
{
""allow"": {
""id"": [""simon"", ""cleopaws""],
""role"": ""ops""
}
}
Demo for cleopaws , demo for ops role , demo for an actor matching neither rule .","[""Authentication and permissions"", ""Permissions""]","[{""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%22id%22%3A+%22root%22%7D&allow=%7B%0D%0A++++++++%22id%22%3A+%22root%22%0D%0A++++%7D"", ""label"": ""allow demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%22id%22%3A+%22trevor%22%7D&allow=%7B%0D%0A++++++++%22id%22%3A+%22root%22%0D%0A++++%7D"", ""label"": ""deny demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22root%22%0D%0A%7D&allow=false"", ""label"": ""demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22root%22%0D%0A%7D&allow=true"", ""label"": ""demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22cleopaws%22%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%5B%0D%0A++++++++%22simon%22%2C%0D%0A++++++++%22cleopaws%22%0D%0A++++%5D%0D%0A%7D"", ""label"": ""allow demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22pancakes%22%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%5B%0D%0A++++++++%22simon%22%2C%0D%0A++++++++%22cleopaws%22%0D%0A++++%5D%0D%0A%7D"", ""label"": ""deny demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22simon%22%2C%0D%0A++++%22roles%22%3A+%5B%0D%0A++++++++%22staff%22%2C%0D%0A++++++++%22developer%22%0D%0A++++%5D%0D%0A%7D&allow=%7B%0D%0A++++%22roles%22%3A+%5B%0D%0A++++++++%22developer%22%0D%0A++++%5D%0D%0A%7D"", ""label"": ""allow demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22cleopaws%22%2C%0D%0A++++%22roles%22%3A+%5B%22dog%22%5D%0D%0A%7D&allow=%7B%0D%0A++++%22roles%22%3A+%5B%0D%0A++++++++%22developer%22%0D%0A++++%5D%0D%0A%7D"", ""label"": ""deny demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22simon%22%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%22*%22%0D%0A%7D"", ""label"": ""allow demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22bot%22%3A+%22readme-bot%22%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%22*%22%0D%0A%7D"", ""label"": ""deny demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=null&allow=%7B%0D%0A++++%22unauthenticated%22%3A+true%0D%0A%7D"", ""label"": ""allow demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22hello%22%0D%0A%7D&allow=%7B%0D%0A++++%22unauthenticated%22%3A+true%0D%0A%7D"", ""label"": ""deny demo""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22cleopaws%22%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%5B%0D%0A++++++++%22simon%22%2C%0D%0A++++++++%22cleopaws%22%0D%0A++++%5D%2C%0D%0A++++%22role%22%3A+%22ops%22%0D%0A%7D"", ""label"": ""Demo for cleopaws""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22trevor%22%2C%0D%0A++++%22role%22%3A+%5B%0D%0A++++++++%22ops%22%2C%0D%0A++++++++%22staff%22%0D%0A++++%5D%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%5B%0D%0A++++++++%22simon%22%2C%0D%0A++++++++%22cleopaws%22%0D%0A++++%5D%2C%0D%0A++++%22role%22%3A+%22ops%22%0D%0A%7D"", ""label"": ""demo for ops role""}, {""href"": ""https://latest.datasette.io/-/allow-debug?actor=%7B%0D%0A++++%22id%22%3A+%22percy%22%2C%0D%0A++++%22role%22%3A+%5B%0D%0A++++++++%22staff%22%0D%0A++++%5D%0D%0A%7D&allow=%7B%0D%0A++++%22id%22%3A+%5B%0D%0A++++++++%22simon%22%2C%0D%0A++++++++%22cleopaws%22%0D%0A++++%5D%2C%0D%0A++++%22role%22%3A+%22ops%22%0D%0A%7D"", ""label"": ""demo for an actor matching neither rule""}]"
pages:indexview,pages,indexview,Top-level index,"The root page of any Datasette installation is an index page that lists all of the currently attached databases. Some examples:
fivethirtyeight.datasettes.com
global-power-plants.datasettes.com
register-of-members-interests.datasettes.com
Add /.json to the end of the URL for the JSON version of the underlying data:
fivethirtyeight.datasettes.com/.json
global-power-plants.datasettes.com/.json
register-of-members-interests.datasettes.com/.json","[""Pages and API endpoints""]","[{""href"": ""https://fivethirtyeight.datasettes.com/"", ""label"": ""fivethirtyeight.datasettes.com""}, {""href"": ""https://global-power-plants.datasettes.com/"", ""label"": ""global-power-plants.datasettes.com""}, {""href"": ""https://register-of-members-interests.datasettes.com/"", ""label"": ""register-of-members-interests.datasettes.com""}, {""href"": ""https://fivethirtyeight.datasettes.com/.json"", ""label"": ""fivethirtyeight.datasettes.com/.json""}, {""href"": ""https://global-power-plants.datasettes.com/.json"", ""label"": ""global-power-plants.datasettes.com/.json""}, {""href"": ""https://register-of-members-interests.datasettes.com/.json"", ""label"": ""register-of-members-interests.datasettes.com/.json""}]"
internals:internals-request,internals,internals-request,Request object,"The request object is passed to various plugin hooks. It represents an incoming HTTP request. It has the following properties:
.scope - dictionary
The ASGI scope that was used to construct this request, described in the ASGI HTTP connection scope specification.
.method - string
The HTTP method for this request, usually GET or POST .
.url - string
The full URL for this request, e.g. https://latest.datasette.io/fixtures .
.scheme - string
The request scheme - usually https or http .
.headers - dictionary (str -> str)
A dictionary of incoming HTTP request headers. Header names have been converted to lowercase.
.cookies - dictionary (str -> str)
A dictionary of incoming cookies
.host - string
The host header from the incoming request, e.g. latest.datasette.io or localhost .
.path - string
The path of the request excluding the query string, e.g. /fixtures .
.full_path - string
The path of the request including the query string if one is present, e.g. /fixtures?sql=select+sqlite_version() .
.query_string - string
The query string component of the request, without the ? - e.g. name__contains=sam&age__gt=10 .
.args - MultiParams
An object representing the parsed query string parameters, see below.
.url_vars - dictionary (str -> str)
Variables extracted from the URL path, if that path was defined using a regular expression. See register_routes(datasette) .
.actor - dictionary (str -> Any) or None
The currently authenticated actor (see actors ), or None if the request is unauthenticated.
The object also has two awaitable methods:
await request.post_vars() - dictionary
Returns a dictionary of form variables that were submitted in the request body via POST . Don't forget to read about CSRF protection !
await request.post_body() - bytes
Returns the un-parsed body of a request submitted by POST - useful for things like incoming JSON data.
And a class method that can be used to create fake request objects for use in tests:
fake(path_with_query_string, method=""GET"", scheme=""http"", url_vars=None)
Returns a Request instance for the specified path and method. For example:
from datasette import Request
from pprint import pprint
request = Request.fake(
""/fixtures/facetable/"",
url_vars={""database"": ""fixtures"", ""table"": ""facetable""},
)
pprint(request.scope)
This outputs:
{'http_version': '1.1',
'method': 'GET',
'path': '/fixtures/facetable/',
'query_string': b'',
'raw_path': b'/fixtures/facetable/',
'scheme': 'http',
'type': 'http',
'url_route': {'kwargs': {'database': 'fixtures', 'table': 'facetable'}}}","[""Internals for plugins""]","[{""href"": ""https://asgi.readthedocs.io/en/latest/specs/www.html#connection-scope"", ""label"": ""ASGI HTTP connection scope""}]"
writing_plugins:writing-plugins-one-off,writing_plugins,writing-plugins-one-off,Writing one-off plugins,"The quickest way to start writing a plugin is to create a my_plugin.py file and drop it into your plugins/ directory. Here is an example plugin, which adds a new custom SQL function called hello_world() which takes no arguments and returns the string Hello world! .
from datasette import hookimpl
@hookimpl
def prepare_connection(conn):
conn.create_function(
""hello_world"", 0, lambda: ""Hello world!""
)
If you save this in plugins/my_plugin.py you can then start Datasette like this:
datasette serve mydb.db --plugins-dir=plugins/
Now you can navigate to http://localhost:8001/mydb and run this SQL:
select hello_world();
To see the output of your plugin.","[""Writing plugins""]","[{""href"": ""http://localhost:8001/mydb"", ""label"": ""http://localhost:8001/mydb""}]"
deploying:deploying,deploying,deploying,Deploying Datasette,"The quickest way to deploy a Datasette instance on the internet is to use the datasette publish command, described in Publishing data . This can be used to quickly deploy Datasette to a number of hosting providers including Heroku, Google Cloud Run and Vercel.
You can deploy Datasette to other hosting providers using the instructions on this page.",[],[]
changelog:better-plugin-documentation,changelog,better-plugin-documentation,Better plugin documentation,"The plugin documentation has been re-arranged into four sections, including a brand new section on testing plugins. ( #687 )
Plugins introduces Datasette's plugin system and describes how to install and configure plugins.
Writing plugins describes how to author plugins, from one-off single file plugins to packaged plugins that can be published to PyPI. It also describes how to start a plugin using the new datasette-plugin cookiecutter template.
Plugin hooks is a full list of detailed documentation for every Datasette plugin hook.
Testing plugins describes how to write tests for Datasette plugins, using pytest and HTTPX .","[""Changelog"", ""0.45 (2020-07-01)""]","[{""href"": ""https://github.com/simonw/datasette/issues/687"", ""label"": ""#687""}, {""href"": ""https://github.com/simonw/datasette-plugin"", ""label"": ""datasette-plugin""}, {""href"": ""https://docs.pytest.org/"", ""label"": ""pytest""}, {""href"": ""https://www.python-httpx.org/"", ""label"": ""HTTPX""}]"
facets:speeding-up-facets-with-indexes,facets,speeding-up-facets-with-indexes,Speeding up facets with indexes,"The performance of facets can be greatly improved by adding indexes on the columns you wish to facet by.
Adding indexes can be performed using the sqlite3 command-line utility. Here's how to add an index on the state column in a table called Food_Trucks :
$ sqlite3 mydatabase.db
SQLite version 3.19.3 2017-06-27 16:48:08
Enter "".help"" for usage hints.
sqlite> CREATE INDEX Food_Trucks_state ON Food_Trucks(""state"");
Or using the sqlite-utils command-line utility:
$ sqlite-utils create-index mydatabase.db Food_Trucks state","[""Facets""]","[{""href"": ""https://sqlite-utils.datasette.io/en/stable/cli.html#creating-indexes"", ""label"": ""sqlite-utils""}]"
authentication:logoutview,authentication,logoutview,The /-/logout page,The page at /-/logout provides the ability to log out of a ds_actor cookie authentication session.,"[""Authentication and permissions"", ""The ds_actor cookie""]",[]
changelog:faceting,changelog,faceting,Faceting,"The number of unique values in a facet is now always displayed. Previously it was only displayed if the user specified ?_facet_size=max . ( #1556 )
Facets of type date or array can now be configured in metadata.json , see Facets in metadata.json . Thanks, David Larlet. ( #1552 )
New ?_nosuggest=1 parameter for table views, which disables facet suggestion. ( #1557 )
Fixed bug where ?_facet_array=tags&_facet=tags would only display one of the two selected facets. ( #625 )","[""Changelog"", ""0.60 (2022-01-13)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1556"", ""label"": ""#1556""}, {""href"": ""https://github.com/simonw/datasette/issues/1552"", ""label"": ""#1552""}, {""href"": ""https://github.com/simonw/datasette/issues/1557"", ""label"": ""#1557""}, {""href"": ""https://github.com/simonw/datasette/issues/625"", ""label"": ""#625""}]"
changelog:url-building,changelog,url-building,URL building,"The new datasette.urls family of methods can be used to generate URLs to key pages within the Datasette interface, both within custom templates and Datasette plugins. See Building URLs within plugins for more details. ( #904 )","[""Changelog"", ""0.51 (2020-10-31)""]","[{""href"": ""https://github.com/simonw/datasette/issues/904"", ""label"": ""#904""}]"
changelog:through-for-joins-through-many-to-many-tables,changelog,through-for-joins-through-many-to-many-tables,?_through= for joins through many-to-many tables,"The new ?_through={json} argument to the Table view allows records to be filtered based on a many-to-many relationship. See Special table arguments for full documentation - here's an example . ( #355 )
This feature was added to help support facet by many-to-many , which isn't quite ready yet but will be coming in the next Datasette release.","[""Changelog"", ""0.29 (2019-07-07)""]","[{""href"": ""https://latest.datasette.io/fixtures/roadside_attractions?_through={%22table%22:%22roadside_attraction_characteristics%22,%22column%22:%22characteristic_id%22,%22value%22:%221%22}"", ""label"": ""an example""}, {""href"": ""https://github.com/simonw/datasette/issues/355"", ""label"": ""#355""}, {""href"": ""https://github.com/simonw/datasette/issues/551"", ""label"": ""facet by many-to-many""}]"
settings:setting-max-csv-mb,settings,setting-max-csv-mb,max_csv_mb,"The maximum size of CSV that can be exported, in megabytes. Defaults to 100MB.
You can disable the limit entirely by settings this to 0:
datasette mydatabase.db --setting max_csv_mb 0","[""Settings"", ""Settings""]",[]
changelog:id59,changelog,id59,0.43 (2020-05-28),"The main focus of this release is a major upgrade to the register_output_renderer(datasette) plugin hook, which allows plugins to provide new output formats for Datasette such as datasette-atom and datasette-ics .
Redesign of register_output_renderer(datasette) to provide more context to the render callback and support an optional ""can_render"" callback that controls if a suggested link to the output format is provided. ( #581 , #770 )
Visually distinguish float and integer columns - useful for figuring out why order-by-column might be returning unexpected results. ( #729 )
The Request object , which is passed to several plugin hooks, is now documented. ( #706 )
New metadata.json option for setting a custom default page size for specific tables and views, see Setting a custom page size . ( #751 )
Canned queries can now be configured with a default URL fragment hash, useful when working with plugins such as datasette-vega , see Additional canned query options . ( #706 )
Fixed a bug in datasette publish when running on operating systems where the /tmp directory lives in a different volume, using a backport of the Python 3.8 shutil.copytree() function. ( #744 )
Every plugin hook is now covered by the unit tests, and a new unit test checks that each plugin hook has at least one corresponding test. ( #771 , #773 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette-atom"", ""label"": ""datasette-atom""}, {""href"": ""https://github.com/simonw/datasette-ics"", ""label"": ""datasette-ics""}, {""href"": ""https://github.com/simonw/datasette/issues/581"", ""label"": ""#581""}, {""href"": ""https://github.com/simonw/datasette/issues/770"", ""label"": ""#770""}, {""href"": ""https://github.com/simonw/datasette/issues/729"", ""label"": ""#729""}, {""href"": ""https://github.com/simonw/datasette/issues/706"", ""label"": ""#706""}, {""href"": ""https://github.com/simonw/datasette/issues/751"", ""label"": ""#751""}, {""href"": ""https://github.com/simonw/datasette-vega"", ""label"": ""datasette-vega""}, {""href"": ""https://github.com/simonw/datasette/issues/706"", ""label"": ""#706""}, {""href"": ""https://github.com/simonw/datasette/issues/744"", ""label"": ""#744""}, {""href"": ""https://github.com/simonw/datasette/issues/771"", ""label"": ""#771""}, {""href"": ""https://github.com/simonw/datasette/issues/773"", ""label"": ""#773""}]"
changelog:id46,changelog,id46,0.50 (2020-10-09),"The key new feature in this release is the column actions menu on the table page ( #891 ). This can be used to sort a column in ascending or descending order, facet data by that column or filter the table to just rows that have a value for that column.
Plugin authors can use the new datasette.client object to make internal HTTP requests from their plugins, allowing them to make use of Datasette's JSON API. ( #943 )
New Deploying Datasette documentation with guides for deploying Datasette on a Linux server using systemd or to hosting providers that support buildpacks . ( #514 , #997 )
Other improvements in this release:
Publishing to Google Cloud Run documentation now covers Google Cloud SDK options. Thanks, Geoffrey Hing. ( #995 )
New datasette -o option which opens your browser as soon as Datasette starts up. ( #970 )
Datasette now sets sqlite3.enable_callback_tracebacks(True) so that errors in custom SQL functions will display tracebacks. ( #891 )
Fixed two rendering bugs with column headers in portrait mobile view. ( #978 , #980 )
New db.table_column_details(table) introspection method for retrieving full details of the columns in a specific table, see Database introspection .
Fixed a routing bug with custom page wildcard templates. ( #996 )
datasette publish heroku now deploys using Python 3.8.6.
New datasette publish heroku --tar= option. ( #969 )
OPTIONS requests against HTML pages no longer return a 500 error. ( #1001 )
Datasette now supports Python 3.9.
See also Datasette 0.50: The annotated release notes .","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/891"", ""label"": ""#891""}, {""href"": ""https://github.com/simonw/datasette/issues/943"", ""label"": ""#943""}, {""href"": ""https://github.com/simonw/datasette/issues/514"", ""label"": ""#514""}, {""href"": ""https://github.com/simonw/datasette/issues/997"", ""label"": ""#997""}, {""href"": ""https://github.com/simonw/datasette/pull/995"", ""label"": ""#995""}, {""href"": ""https://github.com/simonw/datasette/issues/970"", ""label"": ""#970""}, {""href"": ""https://github.com/simonw/datasette/issues/891"", ""label"": ""#891""}, {""href"": ""https://github.com/simonw/datasette/issues/978"", ""label"": ""#978""}, {""href"": ""https://github.com/simonw/datasette/issues/980"", ""label"": ""#980""}, {""href"": ""https://github.com/simonw/datasette/issues/996"", ""label"": ""#996""}, {""href"": ""https://github.com/simonw/datasette/issues/969"", ""label"": ""#969""}, {""href"": ""https://github.com/simonw/datasette/issues/1001"", ""label"": ""#1001""}, {""href"": ""https://simonwillison.net/2020/Oct/9/datasette-0-50/"", ""label"": ""Datasette 0.50: The annotated release notes""}]"
csv_export:csv-export-url-parameters,csv_export,csv-export-url-parameters,URL parameters,"The following options can be used to customize the CSVs returned by Datasette.
?_header=off
This removes the first row of the CSV file specifying the headings - only the row data will be returned.
?_stream=on
Stream all matching records, not just the first page of results. See below.
?_dl=on
Causes Datasette to return a content-disposition: attachment; filename=""filename.csv"" header.","[""CSV export""]",[]
settings:id2,settings,id2,Settings,"The following options can be set using --setting name value , or by storing them in the settings.json file for use with Configuration directory mode .","[""Settings""]",[]
internals:internals-shortcuts,internals,internals-shortcuts,Import shortcuts,"The following commonly used symbols can be imported directly from the datasette module:
from datasette import Response
from datasette import Forbidden
from datasette import NotFound
from datasette import hookimpl
from datasette import actor_matches_allow","[""Internals for plugins""]",[]
spatialite:installing-spatialite-on-os-x,spatialite,installing-spatialite-on-os-x,Installing SpatiaLite on OS X,"The easiest way to install SpatiaLite on OS X is to use Homebrew .
brew update
brew install spatialite-tools
This will install the spatialite command-line tool and the mod_spatialite dynamic library.
You can now run Datasette like so:
datasette --load-extension=spatialite","[""SpatiaLite"", ""Installation""]","[{""href"": ""https://brew.sh/"", ""label"": ""Homebrew""}]"
contributing:contributing-continuous-deployment,contributing,contributing-continuous-deployment,Continuously deployed demo instances,"The demo instance at latest.datasette.io is re-deployed automatically to Google Cloud Run for every push to main that passes the test suite. This is implemented by the GitHub Actions workflow at .github/workflows/deploy-latest.yml .
Specific branches can also be set to automatically deploy by adding them to the on: push: branches block at the top of the workflow YAML file. Branches configured in this way will be deployed to a new Cloud Run service whether or not their tests pass.
The Cloud Run URL for a branch demo can be found in the GitHub Actions logs.","[""Contributing""]","[{""href"": ""https://latest.datasette.io/"", ""label"": ""latest.datasette.io""}, {""href"": ""https://github.com/simonw/datasette/blob/main/.github/workflows/deploy-latest.yml"", ""label"": "".github/workflows/deploy-latest.yml""}]"
settings:setting-default-facet-size,settings,setting-default-facet-size,default_facet_size,"The default number of unique rows returned by Facets is 30. You can customize it like this:
datasette mydatabase.db --setting default_facet_size 50","[""Settings"", ""Settings""]",[]
settings:setting-default-page-size,settings,setting-default-page-size,default_page_size,"The default number of rows returned by the table page. You can over-ride this on a per-page basis using the ?_size=80 query string parameter, provided you do not specify a value higher than the max_returned_rows setting. You can set this default using --setting like so:
datasette mydatabase.db --setting default_page_size 50","[""Settings"", ""Settings""]",[]
json_api:json-api-shapes,json_api,json-api-shapes,Different shapes,"The default JSON representation of data from a SQLite table or custom query
looks like this:
{
""database"": ""sf-trees"",
""table"": ""qSpecies"",
""columns"": [
""id"",
""value""
],
""rows"": [
[
1,
""Myoporum laetum :: Myoporum""
],
[
2,
""Metrosideros excelsa :: New Zealand Xmas Tree""
],
[
3,
""Pinus radiata :: Monterey Pine""
]
],
""truncated"": false,
""next"": ""100"",
""next_url"": ""http://127.0.0.1:8001/sf-trees-02c8ef1/qSpecies.json?_next=100"",
""query_ms"": 1.9571781158447266
}
The columns key lists the columns that are being returned, and the rows
key then returns a list of lists, each one representing a row. The order of the
values in each row corresponds to the columns.
The _shape parameter can be used to access alternative formats for the
rows key which may be more convenient for your application. There are three
options:
?_shape=arrays - ""rows"" is the default option, shown above
?_shape=objects - ""rows"" is a list of JSON key/value objects
?_shape=array - an JSON array of objects
?_shape=array&_nl=on - a newline-separated list of JSON objects
?_shape=arrayfirst - a flat JSON array containing just the first value from each row
?_shape=object - a JSON object keyed using the primary keys of the rows
_shape=objects looks like this:
{
""database"": ""sf-trees"",
...
""rows"": [
{
""id"": 1,
""value"": ""Myoporum laetum :: Myoporum""
},
{
""id"": 2,
""value"": ""Metrosideros excelsa :: New Zealand Xmas Tree""
},
{
""id"": 3,
""value"": ""Pinus radiata :: Monterey Pine""
}
]
}
_shape=array looks like this:
[
{
""id"": 1,
""value"": ""Myoporum laetum :: Myoporum""
},
{
""id"": 2,
""value"": ""Metrosideros excelsa :: New Zealand Xmas Tree""
},
{
""id"": 3,
""value"": ""Pinus radiata :: Monterey Pine""
}
]
_shape=array&_nl=on looks like this:
{""id"": 1, ""value"": ""Myoporum laetum :: Myoporum""}
{""id"": 2, ""value"": ""Metrosideros excelsa :: New Zealand Xmas Tree""}
{""id"": 3, ""value"": ""Pinus radiata :: Monterey Pine""}
_shape=arrayfirst looks like this:
[1, 2, 3]
_shape=object looks like this:
{
""1"": {
""id"": 1,
""value"": ""Myoporum laetum :: Myoporum""
},
""2"": {
""id"": 2,
""value"": ""Metrosideros excelsa :: New Zealand Xmas Tree""
},
""3"": {
""id"": 3,
""value"": ""Pinus radiata :: Monterey Pine""
}
]
The object shape is only available for queries against tables - custom SQL
queries and views do not have an obvious primary key so cannot be returned using
this format.
The object keys are always strings. If your table has a compound primary
key, the object keys will be a comma-separated string.","[""JSON API""]",[]
json_api:json-api-pagination,json_api,json-api-pagination,Pagination,"The default JSON representation includes a ""next_url"" key which can be used to access the next page of results. If that key is null or missing then it means you have reached the final page of results.
Other representations include pagination information in the link HTTP header. That header will look something like this:
link: ; rel=""next""
Here is an example Python function built using requests that returns a list of all of the paginated items from one of these API endpoints:
def paginate(url):
items = []
while url:
response = requests.get(url)
try:
url = response.links.get(""next"").get(""url"")
except AttributeError:
url = None
items.extend(response.json())
return items","[""JSON API""]","[{""href"": ""https://requests.readthedocs.io/"", ""label"": ""requests""}]"
authentication:permissionsdebugview,authentication,permissionsdebugview,The permissions debug tool,"The debug tool at /-/permissions is only available to the authenticated root user (or any actor granted the permissions-debug action according to a plugin).
It shows the thirty most recent permission checks that have been carried out by the Datasette instance.
This is designed to help administrators and plugin authors understand exactly how permission checks are being carried out, in order to effectively configure Datasette's permission system.","[""Authentication and permissions""]",[]
introspection:messagesdebugview,introspection,messagesdebugview,/-/messages,"The debug tool at /-/messages can be used to set flash messages to try out that feature. See .add_message(request, message, type=datasette.INFO) for details of this feature.","[""Introspection""]",[]
changelog:id158,changelog,id158,0.15 (2018-04-09),"The biggest new feature in this release is the ability to sort by column. On the
table page the column headers can now be clicked to apply sort (or descending
sort), or you can specify ?_sort=column or ?_sort_desc=column directly
in the URL.
table_rows => table_rows_count , filtered_table_rows =>
filtered_table_rows_count
Renamed properties. Closes #194
New sortable_columns option in metadata.json to control sort options.
You can now explicitly set which columns in a table can be used for sorting
using the _sort and _sort_desc arguments using metadata.json :
{
""databases"": {
""database1"": {
""tables"": {
""example_table"": {
""sortable_columns"": [
""height"",
""weight""
]
}
}
}
}
}
Refs #189
Column headers now link to sort/desc sort - refs #189
_sort and _sort_desc parameters for table views
Allows for paginated sorted results based on a specified column.
Refs #189
Total row count now correct even if _next applied
Use .custom_sql() for _group_count implementation (refs #150 )
Make HTML title more readable in query template ( #180 ) [Ryan Pitts]
New ?_shape=objects/object/lists param for JSON API ( #192 )
New _shape= parameter replacing old .jsono extension
Now instead of this:
/database/table.jsono
We use the _shape parameter like this:
/database/table.json?_shape=objects
Also introduced a new _shape called object which looks like this:
/database/table.json?_shape=object
Returning an object for the rows key:
...
""rows"": {
""pk1"": {
...
},
""pk2"": {
...
}
}
Refs #122
Utility for writing test database fixtures to a .db file
python tests/fixtures.py /tmp/hello.db
This is useful for making a SQLite database of the test fixtures for
interactive exploration.
Compound primary key _next= now plays well with extra filters
Closes #190
Fixed bug with keyset pagination over compound primary keys
Refs #190
Database/Table views inherit source/license/source_url/license_url
metadata
If you set the source_url/license_url/source/license fields in your root
metadata those values will now be inherited all the way down to the database
and table templates.
The title/description are NOT inherited.
Also added unit tests for the HTML generated by the metadata.
Refs #185
Add metadata, if it exists, to heroku temp dir ( #178 ) [Tony Hirst]
Initial documentation for pagination
Broke up test_app into test_api and test_html
Fixed bug with .json path regular expression
I had a table called geojson and it caused an exception because the regex
was matching .json and not \.json
Deploy to Heroku with Python 3.6.3","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/194"", ""label"": ""#194""}, {""href"": ""https://github.com/simonw/datasette/issues/189"", ""label"": ""#189""}, {""href"": ""https://github.com/simonw/datasette/issues/189"", ""label"": ""#189""}, {""href"": ""https://github.com/simonw/datasette/issues/189"", ""label"": ""#189""}, {""href"": ""https://github.com/simonw/datasette/issues/150"", ""label"": ""#150""}, {""href"": ""https://github.com/simonw/datasette/issues/180"", ""label"": ""#180""}, {""href"": ""https://github.com/simonw/datasette/issues/192"", ""label"": ""#192""}, {""href"": ""https://github.com/simonw/datasette/issues/122"", ""label"": ""#122""}, {""href"": ""https://github.com/simonw/datasette/issues/190"", ""label"": ""#190""}, {""href"": ""https://github.com/simonw/datasette/issues/190"", ""label"": ""#190""}, {""href"": ""https://github.com/simonw/datasette/issues/185"", ""label"": ""#185""}, {""href"": ""https://github.com/simonw/datasette/issues/178"", ""label"": ""#178""}]"
changelog:id115,changelog,id115,0.22 (2018-05-20),"The big new feature in this release is Facets . Datasette can now apply faceted browse to any column in any table. It will also suggest possible facets. See the Datasette Facets announcement post for more details.
In addition to the work on facets:
Added docs for introspection endpoints
New --config option, added --help-config , closes #274
Removed the --page_size= argument to datasette serve in favour of:
datasette serve --config default_page_size:50 mydb.db
Added new help section:
$ datasette --help-config
Config options:
default_page_size Default page size for the table view
(default=100)
max_returned_rows Maximum rows that can be returned from a table
or custom query (default=1000)
sql_time_limit_ms Time limit for a SQL query in milliseconds
(default=1000)
default_facet_size Number of values to return for requested facets
(default=30)
facet_time_limit_ms Time limit for calculating a requested facet
(default=200)
facet_suggest_time_limit_ms Time limit for calculating a suggested facet
(default=50)
Only apply responsive table styles to .rows-and-column
Otherwise they interfere with tables in the description, e.g. on
https://fivethirtyeight.datasettes.com/fivethirtyeight/nba-elo%2Fnbaallelo
Refactored views into new views/ modules, refs #256
Documentation for SQLite full-text search support, closes #253
/-/versions now includes SQLite fts_versions , closes #252","[""Changelog""]","[{""href"": ""https://simonwillison.net/2018/May/20/datasette-facets/"", ""label"": ""Datasette Facets""}, {""href"": ""https://docs.datasette.io/en/stable/introspection.html"", ""label"": ""docs for introspection endpoints""}, {""href"": ""https://github.com/simonw/datasette/issues/274"", ""label"": ""#274""}, {""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight/nba-elo%2Fnbaallelo"", ""label"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight/nba-elo%2Fnbaallelo""}, {""href"": ""https://github.com/simonw/datasette/issues/256"", ""label"": ""#256""}, {""href"": ""https://docs.datasette.io/en/stable/full_text_search.html"", ""label"": ""Documentation for SQLite full-text search""}, {""href"": ""https://github.com/simonw/datasette/issues/253"", ""label"": ""#253""}, {""href"": ""https://github.com/simonw/datasette/issues/252"", ""label"": ""#252""}]"
getting_started:getting-started-demo,getting_started,getting-started-demo,Play with a live demo,"The best way to experience Datasette for the first time is with a demo:
global-power-plants.datasettes.com provides a searchable database of power plants around the world, using data from the World Resources Institude rendered using the datasette-cluster-map plugin.
fivethirtyeight.datasettes.com shows Datasette running against over 400 datasets imported from the FiveThirtyEight GitHub repository .","[""Getting started""]","[{""href"": ""https://global-power-plants.datasettes.com/global-power-plants/global-power-plants"", ""label"": ""global-power-plants.datasettes.com""}, {""href"": ""https://www.wri.org/publication/global-power-plant-database"", ""label"": ""World Resources Institude""}, {""href"": ""https://github.com/simonw/datasette-cluster-map"", ""label"": ""datasette-cluster-map""}, {""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight"", ""label"": ""fivethirtyeight.datasettes.com""}, {""href"": ""https://github.com/fivethirtyeight/data"", ""label"": ""FiveThirtyEight GitHub repository""}]"
testing_plugins:testing-plugins-datasette-test-instance,testing_plugins,testing-plugins-datasette-test-instance,Setting up a Datasette test instance,"The above example shows the easiest way to start writing tests against a Datasette instance:
from datasette.app import Datasette
import pytest
@pytest.mark.asyncio
async def test_plugin_is_installed():
datasette = Datasette(memory=True)
response = await datasette.client.get(""/-/plugins.json"")
assert response.status_code == 200
Creating a Datasette() instance like this as useful shortcut in tests, but there is one detail you need to be aware of. It's important to ensure that the async method .invoke_startup() is called on that instance. You can do that like this:
datasette = Datasette(memory=True)
await datasette.invoke_startup()
This method registers any startup(datasette) or prepare_jinja2_environment(env, datasette) plugins that might themselves need to make async calls.
If you are using await datasette.client.get() and similar methods then you don't need to worry about this - Datasette automatically calls invoke_startup() the first time it handles a request.","[""Testing plugins""]",[]
spatialite:spatialite-warning,spatialite,spatialite-warning,Warning,"The SpatiaLite extension adds a large number of additional SQL functions , some of which are not be safe for untrusted users to execute: they may cause the Datasette server to crash.
You should not expose a SpatiaLite-enabled Datasette instance to the public internet without taking extra measures to secure it against potentially harmful SQL queries.
The following steps are recommended:
Disable arbitrary SQL queries by untrusted users. See Controlling the ability to execute arbitrary SQL for ways to do this. The easiest is to start Datasette with the datasette --setting default_allow_sql off option.
Define Canned queries with the SQL queries that use SpatiaLite functions that you want people to be able to execute.
The Datasette SpatiaLite tutorial includes detailed instructions for running SpatiaLite safely using these techniques","[""SpatiaLite""]","[{""href"": ""https://www.gaia-gis.it/gaia-sins/spatialite-sql-5.0.1.html"", ""label"": ""a large number of additional SQL functions""}, {""href"": ""https://datasette.io/tutorials/spatialite"", ""label"": ""Datasette SpatiaLite tutorial""}]"
pages:pages,pages,pages,Pages and API endpoints,"The Datasette web application offers a number of different pages that can be accessed to explore the data in question, each of which is accompanied by an equivalent JSON API.",[],[]
json_api:id2,json_api,id2,Table arguments,The Datasette table view takes a number of special query string arguments.,"[""JSON API""]",[]
spatialite:querying-polygons-using-within,spatialite,querying-polygons-using-within,Querying polygons using within(),"The within() SQL function can be used to check if a point is within a geometry:
select
name
from
places
where
within(GeomFromText('POINT(-3.1724366 51.4704448)'), places.geom);
The GeomFromText() function takes a string of well-known text. Note that the order used here is longitude then latitude .
To run that same within() query in a way that benefits from the spatial index, use the following:
select
name
from
places
where
within(GeomFromText('POINT(-3.1724366 51.4704448)'), places.geom)
and rowid in (
SELECT pkid FROM idx_places_geom
where xmin < -3.1724366
and xmax > -3.1724366
and ymin < 51.4704448
and ymax > 51.4704448
);","[""SpatiaLite""]",[]
csv_export:streaming-all-records,csv_export,streaming-all-records,Streaming all records,"The stream all rows option is designed to be as efficient as possible -
under the hood it takes advantage of Python 3 asyncio capabilities and
Datasette's efficient pagination to stream back the full
CSV file.
Since databases can get pretty large, by default this option is capped at 100MB -
if a table returns more than 100MB of data the last line of the CSV will be a
truncation error message.
You can increase or remove this limit using the max_csv_mb config
setting. You can also disable the CSV export feature entirely using
allow_csv_stream .","[""CSV export""]",[]
spatialite:importing-shapefiles-into-spatialite,spatialite,importing-shapefiles-into-spatialite,Importing shapefiles into SpatiaLite,"The shapefile format is a common format for distributing geospatial data. You can use the spatialite command-line tool to create a new database table from a shapefile.
Try it now with the North America shapefile available from the University of North Carolina Global River Database project. Download the file and unzip it (this will create files called narivs.dbf , narivs.prj , narivs.shp and narivs.shx in the current directory), then run the following:
$ spatialite rivers-database.db
SpatiaLite version ..: 4.3.0a Supported Extensions:
...
spatialite> .loadshp narivs rivers CP1252 23032
========
Loading shapefile at 'narivs' into SQLite table 'rivers'
...
Inserted 467973 rows into 'rivers' from SHAPEFILE
This will load the data from the narivs shapefile into a new database table called rivers .
Exit out of spatialite (using Ctrl+D ) and run Datasette against your new database like this:
datasette rivers-database.db \
--load-extension=/usr/local/lib/mod_spatialite.dylib
If you browse to http://localhost:8001/rivers-database/rivers you will see the new table... but the Geometry column will contain unreadable binary data (SpatiaLite uses a custom format based on WKB ).
The easiest way to turn this into semi-readable data is to use the SpatiaLite AsGeoJSON function. Try the following using the SQL query interface at http://localhost:8001/rivers-database :
select *, AsGeoJSON(Geometry) from rivers limit 10;
This will give you back an additional column of GeoJSON. You can copy and paste GeoJSON from this column into the debugging tool at geojson.io to visualize it on a map.
To see a more interesting example, try ordering the records with the longest geometry first. Since there are 467,000 rows in the table you will first need to increase the SQL time limit imposed by Datasette:
datasette rivers-database.db \
--load-extension=/usr/local/lib/mod_spatialite.dylib \
--setting sql_time_limit_ms 10000
Now try the following query:
select *, AsGeoJSON(Geometry) from rivers
order by length(Geometry) desc limit 10;","[""SpatiaLite""]","[{""href"": ""https://en.wikipedia.org/wiki/Shapefile"", ""label"": ""shapefile format""}, {""href"": ""http://gaia.geosci.unc.edu/rivers/"", ""label"": ""Global River Database""}, {""href"": ""https://www.gaia-gis.it/gaia-sins/BLOB-Geometry.html"", ""label"": ""a custom format based on WKB""}, {""href"": ""https://geojson.io/"", ""label"": ""geojson.io""}]"
changelog:plugin-hooks-and-internals,changelog,plugin-hooks-and-internals,Plugin hooks and internals,"The prepare_jinja2_environment(env, datasette) plugin hook now accepts an optional datasette argument. Hook implementations can also now return an async function which will be awaited automatically. ( #1809 )
Database(is_mutable=) now defaults to True . ( #1808 )
The datasette.check_visibility() method now accepts an optional permissions= list, allowing it to take multiple permissions into account at once when deciding if something should be shown as public or private. This has been used to correctly display padlock icons in more places in the Datasette interface. ( #1829 )
Datasette no longer enforces upper bounds on its dependencies. ( #1800 )","[""Changelog"", ""0.63 (2022-10-27)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1809"", ""label"": ""#1809""}, {""href"": ""https://github.com/simonw/datasette/issues/1808"", ""label"": ""#1808""}, {""href"": ""https://github.com/simonw/datasette/issues/1829"", ""label"": ""#1829""}, {""href"": ""https://github.com/simonw/datasette/issues/1800"", ""label"": ""#1800""}]"
changelog:new-plugin-hook-extra-template-vars,changelog,new-plugin-hook-extra-template-vars,New plugin hook: extra_template_vars,"The extra_template_vars(template, database, table, columns, view_name, request, datasette) plugin hook allows plugins to inject their own additional variables into the Datasette template context. This can be used in conjunction with custom templates to customize the Datasette interface. datasette-auth-github uses this hook to add custom HTML to the new top navigation bar (which is designed to be modified by plugins, see #540 ).","[""Changelog"", ""0.29 (2019-07-07)""]","[{""href"": ""https://github.com/simonw/datasette-auth-github"", ""label"": ""datasette-auth-github""}, {""href"": ""https://github.com/simonw/datasette/issues/540"", ""label"": ""#540""}]"
changelog:log-out,changelog,log-out,Log out,"The ds_actor cookie can be used by plugins (or by Datasette's --root mechanism ) to authenticate users. The new /-/logout page provides a way to clear that cookie.
A ""Log out"" button now shows in the global navigation provided the user is authenticated using the ds_actor cookie. ( #840 )","[""Changelog"", ""0.45 (2020-07-01)""]","[{""href"": ""https://github.com/simonw/datasette/issues/840"", ""label"": ""#840""}]"
internals:database-results,internals,database-results,Results,"The db.execute() method returns a single Results object. This can be used to access the rows returned by the query.
Iterating over a Results object will yield SQLite Row objects . Each of these can be treated as a tuple or can be accessed using row[""column""] syntax:
info = []
results = await db.execute(""select name from sqlite_master"")
for row in results:
info.append(row[""name""])
The Results object also has the following properties and methods:
.truncated - boolean
Indicates if this query was truncated - if it returned more results than the specified page_size . If this is true then the results object will only provide access to the first page_size rows in the query result. You can disable truncation by passing truncate=False to the db.query() method.
.columns - list of strings
A list of column names returned by the query.
.rows - list of sqlite3.Row
This property provides direct access to the list of rows returned by the database. You can access specific rows by index using results.rows[0] .
.first() - row or None
Returns the first row in the results, or None if no rows were returned.
.single_value()
Returns the value of the first column of the first row of results - but only if the query returned a single row with a single column. Raises a datasette.database.MultipleValues exception otherwise.
.__len__()
Calling len(results) returns the (truncated) number of returned results.","[""Internals for plugins"", ""Database class""]","[{""href"": ""https://docs.python.org/3/library/sqlite3.html#row-objects"", ""label"": ""Row objects""}]"
installation:loading-spatialite,installation,loading-spatialite,Loading SpatiaLite,"The datasetteproject/datasette image includes a recent version of the
SpatiaLite extension for SQLite. To load and enable that
module, use the following command:
docker run -p 8001:8001 -v `pwd`:/mnt \
datasetteproject/datasette \
datasette -p 8001 -h 0.0.0.0 /mnt/fixtures.db \
--load-extension=spatialite
You can confirm that SpatiaLite is successfully loaded by visiting
http://127.0.0.1:8001/-/versions","[""Installation"", ""Advanced installation options"", ""Using Docker""]","[{""href"": ""http://127.0.0.1:8001/-/versions"", ""label"": ""http://127.0.0.1:8001/-/versions""}]"
internals:internals-utils,internals,internals-utils,The datasette.utils module,"The datasette.utils module contains various utility functions used by Datasette. As a general rule you should consider anything in this module to be unstable - functions and classes here could change without warning or be removed entirely between Datasette releases, without being mentioned in the release notes.
The exception to this rule is anythang that is documented here. If you find a need for an undocumented utility function in your own work, consider opening an issue requesting that the function you are using be upgraded to documented and supported status.","[""Internals for plugins""]","[{""href"": ""https://github.com/simonw/datasette/issues/new"", ""label"": ""opening an issue""}]"
internals:internals-datasette-urls,internals,internals-datasette-urls,datasette.urls,"The datasette.urls object contains methods for building URLs to pages within Datasette. Plugins should use this to link to pages, since these methods take into account any base_url configuration setting that might be in effect.
datasette.urls.instance(format=None)
Returns the URL to the Datasette instance root page. This is usually ""/"" .
datasette.urls.path(path, format=None)
Takes a path and returns the full path, taking base_url into account.
For example, datasette.urls.path(""-/logout"") will return the path to the logout page, which will be ""/-/logout"" by default or /prefix-path/-/logout if base_url is set to /prefix-path/
datasette.urls.logout()
Returns the URL to the logout page, usually ""/-/logout""
datasette.urls.static(path)
Returns the URL of one of Datasette's default static assets, for example ""/-/static/app.css""
datasette.urls.static_plugins(plugin_name, path)
Returns the URL of one of the static assets belonging to a plugin.
datasette.urls.static_plugins(""datasette_cluster_map"", ""datasette-cluster-map.js"") would return ""/-/static-plugins/datasette_cluster_map/datasette-cluster-map.js""
datasette.urls.static(path)
Returns the URL of one of Datasette's default static assets, for example ""/-/static/app.css""
datasette.urls.database(database_name, format=None)
Returns the URL to a database page, for example ""/fixtures""
datasette.urls.table(database_name, table_name, format=None)
Returns the URL to a table page, for example ""/fixtures/facetable""
datasette.urls.query(database_name, query_name, format=None)
Returns the URL to a query page, for example ""/fixtures/pragma_cache_size""
These functions can be accessed via the {{ urls }} object in Datasette templates, for example:
HomepageFixtures databasefacetable tablepragma_cache_size query
Use the format=""json"" (or ""csv"" or other formats supported by plugins) arguments to get back URLs to the JSON representation. This is the path with .json added on the end.
These methods each return a datasette.utils.PrefixedUrlString object, which is a subclass of the Python str type. This allows the logic that considers the base_url setting to detect if that prefix has already been applied to the path.","[""Internals for plugins"", ""Datasette class""]",[]
custom_templates:publishing-static-assets,custom_templates,publishing-static-assets,Publishing static assets,"The datasette publish command can be used to publish your static assets,
using the same syntax as above:
$ datasette publish cloudrun mydb.db --static assets:static-files/
This will upload the contents of the static-files/ directory as part of the
deployment, and configure Datasette to correctly serve the assets from /assets/ .","[""Custom pages and templates"", ""Custom CSS and JavaScript""]",[]
plugins:deploying-plugins-using-datasette-publish,plugins,deploying-plugins-using-datasette-publish,Deploying plugins using datasette publish,"The datasette publish and datasette package commands both take an optional --install argument. You can use this one or more times to tell Datasette to pip install specific plugins as part of the process:
datasette publish cloudrun mydb.db --install=datasette-vega
You can use the name of a package on PyPI or any of the other valid arguments to pip install such as a URL to a .zip file:
datasette publish cloudrun mydb.db \
--install=https://url-to-my-package.zip","[""Plugins"", ""Installing plugins""]",[]
settings:setting-publish-secrets,settings,setting-publish-secrets,Using secrets with datasette publish,"The datasette publish and datasette package commands both generate a secret for you automatically when Datasette is deployed.
This means that every time you deploy a new version of a Datasette project, a new secret will be generated. This will cause signed cookies to become invalid on every fresh deploy.
You can fix this by creating a secret that will be used for multiple deploys and passing it using the --secret option:
datasette publish cloudrun mydb.db --service=my-service --secret=cdb19e94283a20f9d42cca5","[""Settings""]",[]
changelog:id50,changelog,id50,0.47.3 (2020-08-15),The datasette --get command-line mechanism now ensures any plugins using the startup() hook are correctly executed. ( #934 ),"[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/934"", ""label"": ""#934""}]"
changelog:id67,changelog,id67,0.36 (2020-02-21),"The datasette object passed to plugins now has API documentation: Datasette class . ( #576 )
New methods on datasette : .add_database() and .remove_database() - documentation . ( #671 )
prepare_connection() plugin hook now takes optional datasette and database arguments - prepare_connection(conn, database, datasette) . ( #678 )
Added three new plugins and one new conversion tool to the The Datasette Ecosystem .","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/576"", ""label"": ""#576""}, {""href"": ""https://github.com/simonw/datasette/issues/671"", ""label"": ""#671""}, {""href"": ""https://github.com/simonw/datasette/issues/678"", ""label"": ""#678""}]"
cli-reference:id1,cli-reference,id1,CLI reference,"The datasette CLI tool provides a number of commands.
Running datasette without specifying a command runs the default command, datasette serve . See datasette serve for the full list of options for that command.
[[[cog
from datasette import cli
from click.testing import CliRunner
import textwrap
def help(args):
title = ""datasette "" + "" "".join(args)
cog.out(""\n::\n\n"")
result = CliRunner().invoke(cli.cli, args)
output = result.output.replace(""Usage: cli "", ""Usage: datasette "")
cog.out(textwrap.indent(output, ' '))
cog.out(""\n\n"")
]]]
[[[end]]]",[],[]
contributing:contributing-formatting-blacken-docs,contributing,contributing-formatting-blacken-docs,blacken-docs,"The blacken-docs command applies Black formatting rules to code examples in the documentation. Run it like this:
blacken-docs -l 60 docs/*.rst","[""Contributing"", ""Code formatting""]","[{""href"": ""https://pypi.org/project/blacken-docs/"", ""label"": ""blacken-docs""}]"
changelog:running-datasette-behind-a-proxy,changelog,running-datasette-behind-a-proxy,Running Datasette behind a proxy,"The base_url configuration option is designed to help run Datasette on a specific path behind a proxy - for example if you want to run an instance of Datasette at /my-datasette/ within your existing site's URL hierarchy, proxied behind nginx or Apache.
Support for this configuration option has been greatly improved ( #1023 ), and guidelines for using it are now available in a new documentation section on Running Datasette behind a proxy . ( #1027 )","[""Changelog"", ""0.51 (2020-10-31)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1023"", ""label"": ""#1023""}, {""href"": ""https://github.com/simonw/datasette/issues/1027"", ""label"": ""#1027""}]"
changelog:new-plugin-hook-asgi-wrapper,changelog,new-plugin-hook-asgi-wrapper,New plugin hook: asgi_wrapper,"The asgi_wrapper(datasette) plugin hook allows plugins to entirely wrap the Datasette ASGI application in their own ASGI middleware. ( #520 )
Two new plugins take advantage of this hook:
datasette-auth-github adds a authentication layer: users will have to sign in using their GitHub account before they can view data or interact with Datasette. You can also use it to restrict access to specific GitHub users, or to members of specified GitHub organizations or teams .
datasette-cors allows you to configure CORS headers for your Datasette instance. You can use this to enable JavaScript running on a whitelisted set of domains to make fetch() calls to the JSON API provided by your Datasette instance.","[""Changelog"", ""0.29 (2019-07-07)""]","[{""href"": ""https://github.com/simonw/datasette/issues/520"", ""label"": ""#520""}, {""href"": ""https://github.com/simonw/datasette-auth-github"", ""label"": ""datasette-auth-github""}, {""href"": ""https://help.github.com/en/articles/about-organizations"", ""label"": ""organizations""}, {""href"": ""https://help.github.com/en/articles/organizing-members-into-teams"", ""label"": ""teams""}, {""href"": ""https://github.com/simonw/datasette-cors"", ""label"": ""datasette-cors""}, {""href"": ""https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"", ""label"": ""CORS headers""}]"
spatialite:id1,spatialite,id1,SpatiaLite,"The SpatiaLite module for SQLite adds features for handling geographic and spatial data. For an example of what you can do with it, see the tutorial Building a location to time zone API with SpatiaLite .
To use it with Datasette, you need to install the mod_spatialite dynamic library. This can then be loaded into Datasette using the --load-extension command-line option.
Datasette can look for SpatiaLite in common installation locations if you run it like this:
datasette --load-extension=spatialite --setting default_allow_sql off
If SpatiaLite is in another location, use the full path to the extension instead:
datasette --setting default_allow_sql off \
--load-extension=/usr/local/lib/mod_spatialite.dylib",[],"[{""href"": ""https://www.gaia-gis.it/fossil/libspatialite/index"", ""label"": ""SpatiaLite module""}, {""href"": ""https://datasette.io/tutorials/spatialite"", ""label"": ""Building a location to time zone API with SpatiaLite""}]"
changelog:improved-support-for-spatialite,changelog,improved-support-for-spatialite,Improved support for SpatiaLite,"The SpatiaLite module
for SQLite adds robust geospatial features to the database.
Getting SpatiaLite working can be tricky, especially if you want to use the most
recent alpha version (with support for K-nearest neighbor).
Datasette now includes extensive documentation on SpatiaLite , and thanks to Ravi Kotecha our GitHub
repo includes a Dockerfile that can build
the latest SpatiaLite and configure it for use with Datasette.
The datasette publish and datasette package commands now accept a new
--spatialite argument which causes them to install and configure SpatiaLite
as part of the container they deploy.","[""Changelog"", ""0.23 (2018-06-18)""]","[{""href"": ""https://www.gaia-gis.it/fossil/libspatialite/index"", ""label"": ""SpatiaLite module""}, {""href"": ""https://github.com/r4vi"", ""label"": ""Ravi Kotecha""}, {""href"": ""https://github.com/simonw/datasette/blob/master/Dockerfile"", ""label"": ""Dockerfile""}]"
internals:internals-response,internals,internals-response,Response class,"The Response class can be returned from view functions that have been registered using the register_routes(datasette) hook.
The Response() constructor takes the following arguments:
body - string
The body of the response.
status - integer (optional)
The HTTP status - defaults to 200.
headers - dictionary (optional)
A dictionary of extra HTTP headers, e.g. {""x-hello"": ""world""} .
content_type - string (optional)
The content-type for the response. Defaults to text/plain .
For example:
from datasette.utils.asgi import Response
response = Response(
""This is XML"",
content_type=""application/xml; charset=utf-8"",
)
The quickest way to create responses is using the Response.text(...) , Response.html(...) , Response.json(...) or Response.redirect(...) helper methods:
from datasette.utils.asgi import Response
html_response = Response.html(""This is HTML"")
json_response = Response.json({""this_is"": ""json""})
text_response = Response.text(
""This will become utf-8 encoded text""
)
# Redirects are served as 302, unless you pass status=301:
redirect_response = Response.redirect(
""https://latest.datasette.io/""
)
Each of these responses will use the correct corresponding content-type - text/html; charset=utf-8 , application/json; charset=utf-8 or text/plain; charset=utf-8 respectively.
Each of the helper methods take optional status= and headers= arguments, documented above.","[""Internals for plugins""]",[]
changelog:id64,changelog,id64,0.38 (2020-03-08),"The Docker build of Datasette now uses SQLite 3.31.1, upgraded from 3.26. ( #695 )
datasette publish cloudrun now accepts an optional --memory=2Gi flag for setting the Cloud Run allocated memory to a value other than the default (256Mi). ( #694 )
Fixed bug where templates that shipped with plugins were sometimes not being correctly loaded. ( #697 )","[""Changelog""]","[{""href"": ""https://hub.docker.com/r/datasetteproject/datasette"", ""label"": ""Docker build""}, {""href"": ""https://github.com/simonw/datasette/issues/695"", ""label"": ""#695""}, {""href"": ""https://github.com/simonw/datasette/issues/694"", ""label"": ""#694""}, {""href"": ""https://github.com/simonw/datasette/issues/697"", ""label"": ""#697""}]"
internals:database-constructor,internals,database-constructor,"Database(ds, path=None, is_mutable=True, is_memory=False, memory_name=None)","The Database() constructor can be used by plugins, in conjunction with .add_database(db, name=None, route=None) , to create and register new databases.
The arguments are as follows:
ds - Datasette class (required)
The Datasette instance you are attaching this database to.
path - string
Path to a SQLite database file on disk.
is_mutable - boolean
Set this to False to cause Datasette to open the file in immutable mode.
is_memory - boolean
Use this to create non-shared memory connections.
memory_name - string or None
Use this to create a named in-memory database. Unlike regular memory databases these can be accessed by multiple threads and will persist an changes made to them for the lifetime of the Datasette server process.
The first argument is the datasette instance you are attaching to, the second is a path= , then is_mutable and is_memory are both optional arguments.","[""Internals for plugins"", ""Database class""]",[]
internals:internals-database-introspection,internals,internals-database-introspection,Database introspection,"The Database class also provides properties and methods for introspecting the database.
db.name - string
The name of the database - usually the filename without the .db prefix.
db.size - integer
The size of the database file in bytes. 0 for :memory: databases.
db.mtime_ns - integer or None
The last modification time of the database file in nanoseconds since the epoch. None for :memory: databases.
db.is_mutable - boolean
Is this database mutable, and allowed to accept writes?
db.is_memory - boolean
Is this database an in-memory database?
await db.attached_databases() - list of named tuples
Returns a list of additional databases that have been connected to this database using the SQLite ATTACH command. Each named tuple has fields seq , name and file .
await db.table_exists(table) - boolean
Check if a table called table exists.
await db.table_names() - list of strings
List of names of tables in the database.
await db.view_names() - list of strings
List of names of views in the database.
await db.table_columns(table) - list of strings
Names of columns in a specific table.
await db.table_column_details(table) - list of named tuples
Full details of the columns in a specific table. Each column is represented by a Column named tuple with fields cid (integer representing the column position), name (string), type (string, e.g. REAL or VARCHAR(30) ), notnull (integer 1 or 0), default_value (string or None), is_pk (integer 1 or 0).
await db.primary_keys(table) - list of strings
Names of the columns that are part of the primary key for this table.
await db.fts_table(table) - string or None
The name of the FTS table associated with this table, if one exists.
await db.label_column_for_table(table) - string or None
The label column that is associated with this table - either automatically detected or using the ""label_column"" key from Metadata , see Specifying the label column for a table .
await db.foreign_keys_for_table(table) - list of dictionaries
Details of columns in this table which are foreign keys to other tables. A list of dictionaries where each dictionary is shaped like this: {""column"": string, ""other_table"": string, ""other_column"": string} .
await db.hidden_table_names() - list of strings
List of tables which Datasette ""hides"" by default - usually these are tables associated with SQLite's full-text search feature, the SpatiaLite extension or tables hidden using the Hiding tables feature.
await db.get_table_definition(table) - string
Returns the SQL definition for the table - the CREATE TABLE statement and any associated CREATE INDEX statements.
await db.get_view_definition(view) - string
Returns the SQL definition of the named view.
await db.get_all_foreign_keys() - dictionary
Dictionary representing both incoming and outgoing foreign keys for this table. It has two keys, ""incoming"" and ""outgoing"" , each of which is a list of dictionaries with keys ""column"" , ""other_table"" and ""other_column"" . For example:
{
""incoming"": [],
""outgoing"": [
{
""other_table"": ""attraction_characteristic"",
""column"": ""characteristic_id"",
""other_column"": ""pk"",
},
{
""other_table"": ""roadside_attractions"",
""column"": ""attraction_id"",
""other_column"": ""pk"",
}
]
}","[""Internals for plugins"", ""Database class""]",[]
authentication:allowdebugview,authentication,allowdebugview,The /-/allow-debug tool,"The /-/allow-debug tool lets you try out different ""action"" blocks against different ""actor"" JSON objects. You can try that out here: https://latest.datasette.io/-/allow-debug","[""Authentication and permissions"", ""Permissions""]","[{""href"": ""https://latest.datasette.io/-/allow-debug"", ""label"": ""https://latest.datasette.io/-/allow-debug""}]"
binary_data:binary-linking,binary_data,binary-linking,Linking to binary downloads,"The .blob output format is used to return binary data. It requires a _blob_column= query string argument specifying which BLOB column should be downloaded, for example:
https://latest.datasette.io/fixtures/binary_data/1.blob?_blob_column=data
This output format can also be used to return binary data from an arbitrary SQL query. Since such queries do not specify an exact row, an additional ?_blob_hash= parameter can be used to specify the SHA-256 hash of the value that is being linked to.
Consider the query select data from binary_data - demonstrated here .
That page links to the binary value downloads. Those links look like this:
https://latest.datasette.io/fixtures.blob?sql=select+data+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d
These .blob links are also returned in the .csv exports Datasette provides for binary tables and queries, since the CSV format does not have a mechanism for representing binary data.","[""Binary data""]","[{""href"": ""https://latest.datasette.io/fixtures/binary_data/1.blob?_blob_column=data"", ""label"": ""https://latest.datasette.io/fixtures/binary_data/1.blob?_blob_column=data""}, {""href"": ""https://latest.datasette.io/fixtures?sql=select+data+from+binary_data"", ""label"": ""demonstrated here""}, {""href"": ""https://latest.datasette.io/fixtures.blob?sql=select+data+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d"", ""label"": ""https://latest.datasette.io/fixtures.blob?sql=select+data+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d""}]"
cli-reference:cli-datasette-get,cli-reference,cli-datasette-get,datasette --get,"The --get option to datasette serve (or just datasette ) specifies the path to a page within Datasette and causes Datasette to output the content from that path without starting the web server.
This means that all of Datasette's functionality can be accessed directly from the command-line.
For example:
$ datasette --get '/-/versions.json' | jq .
{
""python"": {
""version"": ""3.8.5"",
""full"": ""3.8.5 (default, Jul 21 2020, 10:48:26) \n[Clang 11.0.3 (clang-1103.0.32.62)]""
},
""datasette"": {
""version"": ""0.46+15.g222a84a.dirty""
},
""asgi"": ""3.0"",
""uvicorn"": ""0.11.8"",
""sqlite"": {
""version"": ""3.32.3"",
""fts_versions"": [
""FTS5"",
""FTS4"",
""FTS3""
],
""extensions"": {
""json1"": null
},
""compile_options"": [
""COMPILER=clang-11.0.3"",
""ENABLE_COLUMN_METADATA"",
""ENABLE_FTS3"",
""ENABLE_FTS3_PARENTHESIS"",
""ENABLE_FTS4"",
""ENABLE_FTS5"",
""ENABLE_GEOPOLY"",
""ENABLE_JSON1"",
""ENABLE_PREUPDATE_HOOK"",
""ENABLE_RTREE"",
""ENABLE_SESSION"",
""MAX_VARIABLE_NUMBER=250000"",
""THREADSAFE=1""
]
}
}
The exit code will be 0 if the request succeeds and 1 if the request produced an HTTP status code other than 200 - e.g. a 404 or 500 error.
This lets you use datasette --get / to run tests against a Datasette application in a continuous integration environment such as GitHub Actions.","[""CLI reference"", ""datasette serve""]",[]
full_text_search:full-text-search-table-view-api,full_text_search,full-text-search-table-view-api,The table page and table view API,"Table views that support full-text search can be queried using the ?_search=TERMS query string parameter. This will run the search against content from all of the columns that have been included in the index.
Try this example: fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort
SQLite full-text search supports wildcards. This means you can easily implement prefix auto-complete by including an asterisk at the end of the search term - for example:
/dbname/tablename/?_search=rob*
This will return all records containing at least one word that starts with the letters rob .
You can also run searches against just the content of a specific named column by using _search_COLNAME=TERMS - for example, this would search for just rows where the name column in the FTS index mentions Sarah :
/dbname/tablename/?_search_name=Sarah","[""Full-text search""]","[{""href"": ""https://fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort"", ""label"": ""fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort""}]"
changelog:id32,changelog,id32,0.55 (2021-02-18),"Support for cross-database SQL queries and built-in support for serving via HTTPS.
The new --crossdb command-line option causes Datasette to attach up to ten database files to the same /_memory database connection. This enables cross-database SQL queries, including the ability to use joins and unions to combine data from tables that exist in different database files. See Cross-database queries for details. ( #283 )
--ssl-keyfile and --ssl-certfile options can be used to specify a TLS certificate, allowing Datasette to serve traffic over https:// without needing to run it behind a separate proxy. ( #1221 )
The /:memory: page has been renamed (and redirected) to /_memory for consistency with the new /_internal database introduced in Datasette 0.54. ( #1205 )
Added plugin testing documentation on Using pdb for errors thrown inside Datasette . ( #1207 )
The official Datasette Docker image now uses Python 3.7.10, applying the latest security fix for that Python version. ( #1235 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/283"", ""label"": ""#283""}, {""href"": ""https://github.com/simonw/datasette/issues/1221"", ""label"": ""#1221""}, {""href"": ""https://github.com/simonw/datasette/issues/1205"", ""label"": ""#1205""}, {""href"": ""https://github.com/simonw/datasette/issues/1207"", ""label"": ""#1207""}, {""href"": ""https://hub.docker.com/r/datasetteproject/datasette"", ""label"": ""official Datasette Docker image""}, {""href"": ""https://www.python.org/downloads/release/python-3710/"", ""label"": ""the latest security fix""}, {""href"": ""https://github.com/simonw/datasette/issues/1235"", ""label"": ""#1235""}]"
spatialite:making-use-of-a-spatial-index,spatialite,making-use-of-a-spatial-index,Making use of a spatial index,"SpatiaLite spatial indexes are R*Trees. They allow you to run efficient bounding box queries using a sub-select, with a similar pattern to that used for Searches using custom SQL .
In the above example, the resulting index will be called idx_museums_point_geom . This takes the form of a SQLite virtual table. You can inspect its contents using the following query:
select * from idx_museums_point_geom limit 10;
Here's a live example: timezones-api.datasette.io/timezones/idx_timezones_Geometry
pkid
xmin
xmax
ymin
ymax
1
-8.601725578308105
-2.4930307865142822
4.162120819091797
10.74019718170166
2
-3.2607860565185547
1.27329421043396
4.539252281188965
11.174856185913086
3
32.997581481933594
47.98238754272461
3.3974475860595703
14.894054412841797
4
-8.66890811920166
11.997337341308594
18.9681453704834
37.296207427978516
5
36.43336486816406
43.300174713134766
12.354820251464844
18.070993423461914
You can now construct efficient bounding box queries that will make use of the index like this:
select * from museums where museums.rowid in (
SELECT pkid FROM idx_museums_point_geom
-- left-hand-edge of point > left-hand-edge of bbox (minx)
where xmin > :bbox_minx
-- right-hand-edge of point < right-hand-edge of bbox (maxx)
and xmax < :bbox_maxx
-- bottom-edge of point > bottom-edge of bbox (miny)
and ymin > :bbox_miny
-- top-edge of point < top-edge of bbox (maxy)
and ymax < :bbox_maxy
);
Spatial indexes can be created against polygon columns as well as point columns, in which case they will represent the minimum bounding rectangle of that polygon. This is useful for accelerating within queries, as seen in the Timezones API example.","[""SpatiaLite""]","[{""href"": ""https://timezones-api.datasette.io/timezones/idx_timezones_Geometry"", ""label"": ""timezones-api.datasette.io/timezones/idx_timezones_Geometry""}]"
spatialite:installing-spatialite-on-linux,spatialite,installing-spatialite-on-linux,Installing SpatiaLite on Linux,"SpatiaLite is packaged for most Linux distributions.
apt install spatialite-bin libsqlite3-mod-spatialite
Depending on your distribution, you should be able to run Datasette something like this:
datasette --load-extension=/usr/lib/x86_64-linux-gnu/mod_spatialite.so
If you are unsure of the location of the module, try running locate mod_spatialite and see what comes back.","[""SpatiaLite"", ""Installation""]",[]
sql_queries:fragment,sql_queries,fragment,fragment,"Some plugins, such as datasette-vega , can be configured by including additional data in the fragment hash of the URL - the bit that comes after a # symbol.
You can set a default fragment hash that will be included in the link to the canned query from the database index page using the ""fragment"" key.
This example demonstrates both fragment and hide_sql :
{
""databases"": {
""fixtures"": {
""queries"": {
""neighborhood_search"": {
""sql"": ""select neighborhood, facet_cities.name, state\nfrom facetable join facet_cities on facetable.city_id = facet_cities.id\nwhere neighborhood like '%' || :text || '%' order by neighborhood;"",
""fragment"": ""fragment-goes-here"",
""hide_sql"": true
}
}
}
}
}
See here for a demo of this in action.","[""Running SQL queries"", ""Canned queries"", ""Additional canned query options""]","[{""href"": ""https://github.com/simonw/datasette-vega"", ""label"": ""datasette-vega""}, {""href"": ""https://latest.datasette.io/fixtures#queries"", ""label"": ""See here""}]"
contributing:contributing-documentation-cog,contributing,contributing-documentation-cog,Running Cog,"Some pages of documentation (in particular the CLI reference ) are automatically updated using Cog .
To update these pages, run the following command:
cog -r docs/*.rst","[""Contributing"", ""Editing and building the documentation""]","[{""href"": ""https://github.com/nedbat/cog"", ""label"": ""Cog""}]"
deploying:deploying-buildpacks,deploying,deploying-buildpacks,Deploying using buildpacks,"Some hosting providers such as Heroku , DigitalOcean App Platform and Scalingo support the Buildpacks standard for deploying Python web applications.
Deploying Datasette on these platforms requires two files: requirements.txt and Procfile .
The requirements.txt file lets the platform know which Python packages should be installed. It should contain datasette at a minimum, but can also list any Datasette plugins you wish to install - for example:
datasette
datasette-vega
The Procfile lets the hosting platform know how to run the command that serves web traffic. It should look like this:
web: datasette . -h 0.0.0.0 -p $PORT --cors
The $PORT environment variable is provided by the hosting platform. --cors enables CORS requests from JavaScript running on other websites to your domain - omit this if you don't want to allow CORS. You can add additional Datasette Settings options here too.
These two files should be enough to deploy Datasette on any host that supports buildpacks. Datasette will serve any SQLite files that are included in the root directory of the application.
If you want to build SQLite files or download them as part of the deployment process you can do so using a bin/post_compile file. For example, the following bin/post_compile will download an example database that will then be served by Datasette:
wget https://fivethirtyeight.datasettes.com/fivethirtyeight.db
simonw/buildpack-datasette-demo is an example GitHub repository showing a Datasette configuration that can be deployed to a buildpack-supporting host.","[""Deploying Datasette""]","[{""href"": ""https://www.heroku.com/"", ""label"": ""Heroku""}, {""href"": ""https://www.digitalocean.com/docs/app-platform/"", ""label"": ""DigitalOcean App Platform""}, {""href"": ""https://scalingo.com/"", ""label"": ""Scalingo""}, {""href"": ""https://buildpacks.io/"", ""label"": ""Buildpacks standard""}, {""href"": ""https://github.com/simonw/buildpack-datasette-demo"", ""label"": ""simonw/buildpack-datasette-demo""}]"
changelog:csrf-protection,changelog,csrf-protection,CSRF protection,"Since writable canned queries are built using POST forms, Datasette now ships with CSRF protection ( #798 ). This applies automatically to any POST request, which means plugins need to include a csrftoken in any POST forms that they render. They can do that like so:
","[""Changelog"", ""0.44 (2020-06-11)""]","[{""href"": ""https://github.com/simonw/datasette/issues/798"", ""label"": ""#798""}]"
introspection:jsondataview-versions,introspection,jsondataview-versions,/-/versions,"Shows the version of Datasette, Python and SQLite. Versions example :
{
""datasette"": {
""version"": ""0.60""
},
""python"": {
""full"": ""3.8.12 (default, Dec 21 2021, 10:45:09) \n[GCC 10.2.1 20210110]"",
""version"": ""3.8.12""
},
""sqlite"": {
""extensions"": {
""json1"": null
},
""fts_versions"": [
""FTS5"",
""FTS4"",
""FTS3""
],
""compile_options"": [
""COMPILER=gcc-6.3.0 20170516"",
""ENABLE_FTS3"",
""ENABLE_FTS4"",
""ENABLE_FTS5"",
""ENABLE_JSON1"",
""ENABLE_RTREE"",
""THREADSAFE=1""
],
""version"": ""3.37.0""
}
}","[""Introspection""]","[{""href"": ""https://latest.datasette.io/-/versions"", ""label"": ""Versions example""}]"
introspection:jsondataview-actor,introspection,jsondataview-actor,/-/actor,"Shows the currently authenticated actor. Useful for debugging Datasette authentication plugins.
{
""actor"": {
""id"": 1,
""username"": ""some-user""
}
}","[""Introspection""]",[]
introspection:jsondataview-metadata,introspection,jsondataview-metadata,/-/metadata,"Shows the contents of the metadata.json file that was passed to datasette serve , if any. Metadata example :
{
""license"": ""CC Attribution 4.0 License"",
""license_url"": ""http://creativecommons.org/licenses/by/4.0/"",
""source"": ""fivethirtyeight/data on GitHub"",
""source_url"": ""https://github.com/fivethirtyeight/data"",
""title"": ""Five Thirty Eight"",
""databases"": {
}
}","[""Introspection""]","[{""href"": ""https://fivethirtyeight.datasettes.com/-/metadata"", ""label"": ""Metadata example""}]"
introspection:jsondataview-config,introspection,jsondataview-config,/-/settings,"Shows the Settings for this instance of Datasette. Settings example :
{
""default_facet_size"": 30,
""default_page_size"": 100,
""facet_suggest_time_limit_ms"": 50,
""facet_time_limit_ms"": 1000,
""max_returned_rows"": 1000,
""sql_time_limit_ms"": 1000
}","[""Introspection""]","[{""href"": ""https://fivethirtyeight.datasettes.com/-/settings"", ""label"": ""Settings example""}]"
introspection:jsondataview-threads,introspection,jsondataview-threads,/-/threads,"Shows details of threads and asyncio tasks. Threads example :
{
""num_threads"": 2,
""threads"": [
{
""daemon"": false,
""ident"": 4759197120,
""name"": ""MainThread""
},
{
""daemon"": true,
""ident"": 123145319682048,
""name"": ""Thread-1""
},
],
""num_tasks"": 3,
""tasks"": [
"" cb=[set.discard()]>"",
"" wait_for=()]> cb=[run_until_complete..()]>"",
"" wait_for=()]>>""
]
}","[""Introspection""]","[{""href"": ""https://latest.datasette.io/-/threads"", ""label"": ""Threads example""}]"
introspection:jsondataview-databases,introspection,jsondataview-databases,/-/databases,"Shows currently attached databases. Databases example :
[
{
""hash"": null,
""is_memory"": false,
""is_mutable"": true,
""name"": ""fixtures"",
""path"": ""fixtures.db"",
""size"": 225280
}
]","[""Introspection""]","[{""href"": ""https://latest.datasette.io/-/databases"", ""label"": ""Databases example""}]"
introspection:jsondataview-plugins,introspection,jsondataview-plugins,/-/plugins,"Shows a list of currently installed plugins and their versions. Plugins example :
[
{
""name"": ""datasette_cluster_map"",
""static"": true,
""templates"": false,
""version"": ""0.10"",
""hooks"": [""extra_css_urls"", ""extra_js_urls"", ""extra_body_script""]
}
]
Add ?all=1 to include details of the default plugins baked into Datasette.","[""Introspection""]","[{""href"": ""https://san-francisco.datasettes.com/-/plugins"", ""label"": ""Plugins example""}]"
cli-reference:cli-help-publish-help,cli-reference,cli-help-publish-help,datasette publish,"Shows a list of available deployment targets for publishing data with Datasette.
Additional deployment targets can be added by plugins that use the publish_subcommand(publish) hook.
[[[cog
help([""publish"", ""--help""])
]]]
Usage: datasette publish [OPTIONS] COMMAND [ARGS]...
Publish specified SQLite database files to the internet along with a
Datasette-powered interface and API
Options:
--help Show this message and exit.
Commands:
cloudrun Publish databases to Datasette running on Cloud Run
heroku Publish databases to Datasette running on Heroku
[[[end]]]","[""CLI reference""]",[]
changelog:id37,changelog,id37,0.52.4 (2020-12-05),"Show pysqlite3 version on /-/versions , if installed. ( #1125 )
Errors output by Datasette (e.g. for invalid SQL queries) now go to stderr , not stdout . ( #1131 )
Fix for a startup error on windows caused by unnecessary from os import EX_CANTCREAT - thanks, Abdussamet Koçak. ( #1094 )","[""Changelog""]","[{""href"": ""https://github.com/coleifer/pysqlite3"", ""label"": ""pysqlite3""}, {""href"": ""https://github.com/simonw/datasette/issues/1125"", ""label"": ""#1125""}, {""href"": ""https://github.com/simonw/datasette/issues/1131"", ""label"": ""#1131""}, {""href"": ""https://github.com/simonw/datasette/issues/1094"", ""label"": ""#1094""}]"
settings:setting-default-allow-sql,settings,setting-default-allow-sql,default_allow_sql,"Should users be able to execute arbitrary SQL queries by default?
Setting this to off causes permission checks for execute-sql to fail by default.
datasette mydatabase.db --setting default_allow_sql off
There are two ways to achieve this: the other is to add ""allow_sql"": false to your metadata.json file, as described in Controlling the ability to execute arbitrary SQL . This setting offers a more convenient way to do this.","[""Settings"", ""Settings""]",[]
settings:setting-allow-download,settings,setting-allow-download,allow_download,"Should users be able to download the original SQLite database using a link on the database index page? This is turned on by default. However, databases can only be downloaded if they are served in immutable mode and not in-memory. If downloading is unavailable for either of these reasons, the download link is hidden even if allow_download is on. To disable database downloads, use the following:
datasette mydatabase.db --setting allow_download off","[""Settings"", ""Settings""]",[]
settings:setting-suggest-facets,settings,setting-suggest-facets,suggest_facets,"Should Datasette calculate suggested facets? On by default, turn this off like so:
datasette mydatabase.db --setting suggest_facets off","[""Settings"", ""Settings""]",[]
binary_data:binary-plugins,binary_data,binary-plugins,Binary plugins,"Several Datasette plugins are available that change the way Datasette treats binary data.
datasette-render-binary modifies Datasette's default interface to show an automatic guess at what type of binary data is being stored, along with a visual representation of the binary value that displays ASCII strings directly in the interface.
datasette-render-images detects common image formats and renders them as images directly in the Datasette interface.
datasette-media allows Datasette interfaces to be configured to serve binary files from configured SQL queries, and includes the ability to resize images directly before serving them.","[""Binary data""]","[{""href"": ""https://github.com/simonw/datasette-render-binary"", ""label"": ""datasette-render-binary""}, {""href"": ""https://github.com/simonw/datasette-render-images"", ""label"": ""datasette-render-images""}, {""href"": ""https://github.com/simonw/datasette-media"", ""label"": ""datasette-media""}]"
settings:setting-cache-size-kb,settings,setting-cache-size-kb,cache_size_kb,"Sets the amount of memory SQLite uses for its per-connection cache , in KB.
datasette mydatabase.db --setting cache_size_kb 5000","[""Settings"", ""Settings""]","[{""href"": ""https://www.sqlite.org/pragma.html#pragma_cache_size"", ""label"": ""per-connection cache""}]"
changelog:id48,changelog,id48,0.49 (2020-09-14),"See also Datasette 0.49: The annotated release notes .
Writable canned queries now expose a JSON API, see JSON API for writable canned queries . ( #880 )
New mechanism for defining page templates with custom path parameters - a template file called pages/about/{slug}.html will be used to render any requests to /about/something . See Path parameters for pages . ( #944 )
register_output_renderer() render functions can now return a Response . ( #953 )
New --upgrade option for datasette install . ( #945 )
New datasette --pdb option. ( #962 )
datasette --get exit code now reflects the internal HTTP status code. ( #947 )
New raise_404() template function for returning 404 errors. ( #964 )
datasette publish heroku now deploys using Python 3.8.5
Upgraded CodeMirror to 5.57.0. ( #948 )
Upgraded code style to Black 20.8b1. ( #958 )
Fixed bug where selected facets were not correctly persisted in hidden form fields on the table page. ( #963 )
Renamed the default error template from 500.html to error.html .
Custom error pages are now documented, see Custom error pages . ( #965 )","[""Changelog""]","[{""href"": ""https://simonwillison.net/2020/Sep/15/datasette-0-49/"", ""label"": ""Datasette 0.49: The annotated release notes""}, {""href"": ""https://github.com/simonw/datasette/issues/880"", ""label"": ""#880""}, {""href"": ""https://github.com/simonw/datasette/issues/944"", ""label"": ""#944""}, {""href"": ""https://github.com/simonw/datasette/issues/953"", ""label"": ""#953""}, {""href"": ""https://github.com/simonw/datasette/issues/945"", ""label"": ""#945""}, {""href"": ""https://github.com/simonw/datasette/issues/962"", ""label"": ""#962""}, {""href"": ""https://github.com/simonw/datasette/issues/947"", ""label"": ""#947""}, {""href"": ""https://github.com/simonw/datasette/issues/964"", ""label"": ""#964""}, {""href"": ""https://codemirror.net/"", ""label"": ""CodeMirror""}, {""href"": ""https://github.com/simonw/datasette/issues/948"", ""label"": ""#948""}, {""href"": ""https://github.com/simonw/datasette/issues/958"", ""label"": ""#958""}, {""href"": ""https://github.com/simonw/datasette/issues/963"", ""label"": ""#963""}, {""href"": ""https://github.com/simonw/datasette/issues/965"", ""label"": ""#965""}]"
changelog:id55,changelog,id55,0.45 (2020-07-01),"See also Datasette 0.45: The annotated release notes .
Magic parameters for canned queries, a log out feature, improved plugin documentation and four new plugin hooks.","[""Changelog""]","[{""href"": ""https://simonwillison.net/2020/Jul/1/datasette-045/"", ""label"": ""Datasette 0.45: The annotated release notes""}]"
changelog:id57,changelog,id57,0.44 (2020-06-11),"See also Datasette 0.44: The annotated release notes .
Authentication and permissions, writable canned queries, flash messages, new plugin hooks and more.","[""Changelog""]","[{""href"": ""https://simonwillison.net/2020/Jun/12/annotated-release-notes/"", ""label"": ""Datasette 0.44: The annotated release notes""}]"
cli-reference:cli-help-publish-heroku-help,cli-reference,cli-help-publish-heroku-help,datasette publish heroku,"See Publishing to Heroku .
[[[cog
help([""publish"", ""heroku"", ""--help""])
]]]
Usage: datasette publish heroku [OPTIONS] [FILES]...
Publish databases to Datasette running on Heroku
Options:
-m, --metadata FILENAME Path to JSON/YAML file containing metadata to
publish
--extra-options TEXT Extra options to pass to datasette serve
--branch TEXT Install datasette from a GitHub branch e.g.
main
--template-dir DIRECTORY Path to directory containing custom templates
--plugins-dir DIRECTORY Path to directory containing custom plugins
--static MOUNT:DIRECTORY Serve static files from this directory at
/MOUNT/...
--install TEXT Additional packages (e.g. plugins) to install
--plugin-secret ...
Secrets to pass to plugins, e.g. --plugin-
secret datasette-auth-github client_id xxx
--version-note TEXT Additional note to show on /-/versions
--secret TEXT Secret used for signing secure values, such as
signed cookies
--title TEXT Title for metadata
--license TEXT License label for metadata
--license_url TEXT License URL for metadata
--source TEXT Source label for metadata
--source_url TEXT Source URL for metadata
--about TEXT About label for metadata
--about_url TEXT About URL for metadata
-n, --name TEXT Application name to use when deploying
--tar TEXT --tar option to pass to Heroku, e.g.
--tar=/usr/local/bin/gtar
--generate-dir DIRECTORY Output generated application files and stop
without deploying
--help Show this message and exit.
[[[end]]]","[""CLI reference""]",[]
cli-reference:cli-help-publish-cloudrun-help,cli-reference,cli-help-publish-cloudrun-help,datasette publish cloudrun,"See Publishing to Google Cloud Run .
[[[cog
help([""publish"", ""cloudrun"", ""--help""])
]]]
Usage: datasette publish cloudrun [OPTIONS] [FILES]...
Publish databases to Datasette running on Cloud Run
Options:
-m, --metadata FILENAME Path to JSON/YAML file containing metadata to
publish
--extra-options TEXT Extra options to pass to datasette serve
--branch TEXT Install datasette from a GitHub branch e.g.
main
--template-dir DIRECTORY Path to directory containing custom templates
--plugins-dir DIRECTORY Path to directory containing custom plugins
--static MOUNT:DIRECTORY Serve static files from this directory at
/MOUNT/...
--install TEXT Additional packages (e.g. plugins) to install
--plugin-secret ...
Secrets to pass to plugins, e.g. --plugin-
secret datasette-auth-github client_id xxx
--version-note TEXT Additional note to show on /-/versions
--secret TEXT Secret used for signing secure values, such as
signed cookies
--title TEXT Title for metadata
--license TEXT License label for metadata
--license_url TEXT License URL for metadata
--source TEXT Source label for metadata
--source_url TEXT Source URL for metadata
--about TEXT About label for metadata
--about_url TEXT About URL for metadata
-n, --name TEXT Application name to use when building
--service TEXT Cloud Run service to deploy (or over-write)
--spatialite Enable SpatialLite extension
--show-files Output the generated Dockerfile and
metadata.json
--memory TEXT Memory to allocate in Cloud Run, e.g. 1Gi
--cpu [1|2|4] Number of vCPUs to allocate in Cloud Run
--timeout INTEGER Build timeout in seconds
--apt-get-install TEXT Additional packages to apt-get install
--max-instances INTEGER Maximum Cloud Run instances
--min-instances INTEGER Minimum Cloud Run instances
--help Show this message and exit.
[[[end]]]","[""CLI reference""]",[]
changelog:id12,changelog,id12,0.63 (2022-10-27),See Datasette 0.63: The annotated release notes for more background on the changes in this release.,"[""Changelog""]","[{""href"": ""https://simonwillison.net/2022/Oct/27/datasette-0-63/"", ""label"": ""Datasette 0.63: The annotated release notes""}]"
changelog:id180,changelog,id180,0.13 (2017-11-24),"Search now applies to current filters.
Combined search into the same form as filters.
Closes #133
Much tidier design for table view header.
Closes #147
Added ?column__not=blah filter.
Closes #148
Row page now resolves foreign keys.
Closes #132
Further tweaks to select/input filter styling.
Refs #86 - thanks for the help, @natbat!
Show linked foreign key in table cells.
Added UI for editing table filters.
Refs #86
Hide FTS-created tables on index pages.
Closes #129
Add publish to heroku support [Jacob Kaplan-Moss]
datasette publish heroku mydb.db
Pull request #104
Initial implementation of ?_group_count=column .
URL shortcut for counting rows grouped by one or more columns.
?_group_count=column1&_group_count=column2 works as well.
SQL generated looks like this:
select ""qSpecies"", count(*) as ""count""
from Street_Tree_List
group by ""qSpecies""
order by ""count"" desc limit 100
Or for two columns like this:
select ""qSpecies"", ""qSiteInfo"", count(*) as ""count""
from Street_Tree_List
group by ""qSpecies"", ""qSiteInfo""
order by ""count"" desc limit 100
Refs #44
Added --build=master option to datasette publish and package.
The datasette publish and datasette package commands both now accept an
optional --build argument. If provided, this can be used to specify a branch
published to GitHub that should be built into the container.
This makes it easier to test code that has not yet been officially released to
PyPI, e.g.:
datasette publish now mydb.db --branch=master
Implemented ?_search=XXX + UI if a FTS table is detected.
Closes #131
Added datasette --version support.
Table views now show expanded foreign key references, if possible.
If a table has foreign key columns, and those foreign key tables have
label_columns , the TableView will now query those other tables for the
corresponding values and display those values as links in the corresponding
table cells.
label_columns are currently detected by the inspect() function, which looks
for any table that has just two columns - an ID column and one other - and
sets the label_column to be that second non-ID column.
Don't prevent tabbing to ""Run SQL"" button ( #117 ) [Robert Gieseke]
See comment in #115
Add keyboard shortcut to execute SQL query ( #115 ) [Robert Gieseke]
Allow --load-extension to be set via environment variable.
Add support for ?field__isnull=1 ( #107 ) [Ray N]
Add spatialite, switch to debian and local build ( #114 ) [Ariel Núñez]
Added --load-extension argument to datasette serve.
Allows loading of SQLite extensions. Refs #110 .","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/133"", ""label"": ""#133""}, {""href"": ""https://github.com/simonw/datasette/issues/147"", ""label"": ""#147""}, {""href"": ""https://github.com/simonw/datasette/issues/148"", ""label"": ""#148""}, {""href"": ""https://github.com/simonw/datasette/issues/132"", ""label"": ""#132""}, {""href"": ""https://github.com/simonw/datasette/issues/86"", ""label"": ""#86""}, {""href"": ""https://github.com/simonw/datasette/issues/86"", ""label"": ""#86""}, {""href"": ""https://github.com/simonw/datasette/issues/129"", ""label"": ""#129""}, {""href"": ""https://github.com/simonw/datasette/issues/104"", ""label"": ""#104""}, {""href"": ""https://github.com/simonw/datasette/issues/44"", ""label"": ""#44""}, {""href"": ""https://github.com/simonw/datasette/issues/131"", ""label"": ""#131""}, {""href"": ""https://github.com/simonw/datasette/issues/117"", ""label"": ""#117""}, {""href"": ""https://github.com/simonw/datasette/issues/115"", ""label"": ""#115""}, {""href"": ""https://github.com/simonw/datasette/issues/115"", ""label"": ""#115""}, {""href"": ""https://github.com/simonw/datasette/issues/107"", ""label"": ""#107""}, {""href"": ""https://github.com/simonw/datasette/issues/114"", ""label"": ""#114""}, {""href"": ""https://github.com/simonw/datasette/issues/110"", ""label"": ""#110""}]"
changelog:binary-data,changelog,binary-data,Binary data,"SQLite tables can contain binary data in BLOB columns. Datasette now provides links for users to download this data directly from Datasette, and uses those links to make binary data available from CSV exports. See Binary data for more details. ( #1036 and #1034 ).","[""Changelog"", ""0.51 (2020-10-31)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1036"", ""label"": ""#1036""}, {""href"": ""https://github.com/simonw/datasette/issues/1034"", ""label"": ""#1034""}]"
binary_data:binary,binary_data,binary,Binary data,"SQLite tables can contain binary data in BLOB columns.
Datasette includes special handling for these binary values. The Datasette interface detects binary values and provides a link to download their content, for example on https://latest.datasette.io/fixtures/binary_data
Binary data is represented in .json exports using Base64 encoding.
https://latest.datasette.io/fixtures/binary_data.json?_shape=array
[
{
""rowid"": 1,
""data"": {
""$base64"": true,
""encoded"": ""FRwCx60F/g==""
}
},
{
""rowid"": 2,
""data"": {
""$base64"": true,
""encoded"": ""FRwDx60F/g==""
}
},
{
""rowid"": 3,
""data"": null
}
]",[],"[{""href"": ""https://latest.datasette.io/fixtures/binary_data"", ""label"": ""https://latest.datasette.io/fixtures/binary_data""}, {""href"": ""https://latest.datasette.io/fixtures/binary_data.json?_shape=array"", ""label"": ""https://latest.datasette.io/fixtures/binary_data.json?_shape=array""}]"
installation:installation-extensions,installation,installation-extensions,A note about extensions,"SQLite supports extensions, such as SpatiaLite for geospatial operations.
These can be loaded using the --load-extension argument, like so:
datasette --load-extension=/usr/local/lib/mod_spatialite.dylib
Some Python installations do not include support for SQLite extensions. If this is the case you will see the following error when you attempt to load an extension:
Your Python installation does not have the ability to load SQLite extensions.
In some cases you may see the following error message instead:
AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'
On macOS the easiest fix for this is to install Datasette using Homebrew:
brew install datasette
Use which datasette to confirm that datasette will run that version. The output should look something like this:
/usr/local/opt/datasette/bin/datasette
If you get a different location here such as /Library/Frameworks/Python.framework/Versions/3.10/bin/datasette you can run the following command to cause datasette to execute the Homebrew version instead:
alias datasette=$(echo $(brew --prefix datasette)/bin/datasette)
You can undo this operation using:
unalias datasette
If you need to run SQLite with extension support for other Python code, you can do so by install Python itself using Homebrew:
brew install python
Then executing Python using:
/usr/local/opt/python@3/libexec/bin/python
A more convenient way to work with this version of Python may be to use it to create a virtual environment:
/usr/local/opt/python@3/libexec/bin/python -m venv datasette-venv
Then activate it like this:
source datasette-venv/bin/activate
Now running python and pip will work against a version of Python 3 that includes support for SQLite extensions:
pip install datasette
which datasette
datasette --version","[""Installation""]",[]
internals:database-execute-write,internals,database-execute-write,"await db.execute_write(sql, params=None, block=True)","SQLite only allows one database connection to write at a time. Datasette handles this for you by maintaining a queue of writes to be executed against a given database. Plugins can submit write operations to this queue and they will be executed in the order in which they are received.
This method can be used to queue up a non-SELECT SQL query to be executed against a single write connection to the database.
You can pass additional SQL parameters as a tuple or dictionary.
The method will block until the operation is completed, and the return value will be the return from calling conn.execute(...) using the underlying sqlite3 Python library.
If you pass block=False this behaviour changes to ""fire and forget"" - queries will be added to the write queue and executed in a separate thread while your code can continue to do other things. The method will return a UUID representing the queued task.","[""Internals for plugins"", ""Database class""]",[]
full_text_search:id1,full_text_search,id1,Full-text search,"SQLite includes a powerful mechanism for enabling full-text search against SQLite records. Datasette can detect if a table has had full-text search configured for it in the underlying database and display a search interface for filtering that table.
Here's an example search :
Datasette automatically detects which tables have been configured for full-text search.",[],"[{""href"": ""https://www.sqlite.org/fts3.html"", ""label"": ""a powerful mechanism for enabling full-text search""}, {""href"": ""https://register-of-members-interests.datasettes.com/regmem/items?_search=hamper&_sort_desc=date"", ""label"": ""an example search""}]"
sql_queries:id3,sql_queries,id3,Cross-database queries,"SQLite has the ability to run queries that join across multiple databases. Up to ten databases can be attached to a single SQLite connection and queried together.
Datasette can execute joins across multiple databases if it is started with the --crossdb option:
datasette fixtures.db extra_database.db --crossdb
If it is started in this way, the /_memory page can be used to execute queries that join across multiple databases.
References to tables in attached databases should be preceded by the database name and a period.
For example, this query will show a list of tables across both of the above databases:
select
'fixtures' as database, *
from
[fixtures].sqlite_master
union
select
'extra_database' as database, *
from
[extra_database].sqlite_master
Try that out here .","[""Running SQL queries""]","[{""href"": ""https://latest.datasette.io/_memory?sql=select%0D%0A++%27fixtures%27+as+database%2C+*%0D%0Afrom%0D%0A++%5Bfixtures%5D.sqlite_master%0D%0Aunion%0D%0Aselect%0D%0A++%27extra_database%27+as+database%2C+*%0D%0Afrom%0D%0A++%5Bextra_database%5D.sqlite_master"", ""label"": ""Try that out here""}]"
full_text_search:full-text-search-advanced-queries,full_text_search,full-text-search-advanced-queries,Advanced SQLite search queries,"SQLite full-text search includes support for a variety of advanced queries , including AND , OR , NOT and NEAR .
By default Datasette disables these features to ensure they do not cause errors or confusion for users who are not aware of them. You can disable this escaping and use the advanced queries by adding &_searchmode=raw to the table page query string.
If you want to enable these operators by default for a specific table, you can do so by adding ""searchmode"": ""raw"" to the metadata configuration for that table, see Configuring full-text search for a table or view .
If that option has been specified in the table metadata but you want to over-ride it and return to the default behavior you can append &_searchmode=escaped to the query string.","[""Full-text search""]","[{""href"": ""https://www.sqlite.org/fts5.html#full_text_query_syntax"", ""label"": ""a variety of advanced queries""}]"
changelog:v0-28-register-output-renderer,changelog,v0-28-register-output-renderer,register_output_renderer plugins,"Russ Garrett implemented a new Datasette plugin hook called register_output_renderer ( #441 ) which allows plugins to create additional output renderers in addition to Datasette's default .json and .csv .
Russ's in-development datasette-geo plugin includes an example of this hook being used to output .geojson automatically converted from SpatiaLite.","[""Changelog"", ""0.28 (2019-05-19)""]","[{""href"": ""https://github.com/simonw/datasette/pull/441"", ""label"": ""#441""}, {""href"": ""https://github.com/russss/datasette-geo"", ""label"": ""datasette-geo""}, {""href"": ""https://github.com/russss/datasette-geo/blob/d4cecc020848bbde91e9e17bf352f7c70bc3dccf/datasette_plugin_geo/geojson.py"", ""label"": ""an example""}]"
internals:internals-tracer,internals,internals-tracer,datasette.tracer,"Running Datasette with --setting trace_debug 1 enables trace debug output, which can then be viewed by adding ?_trace=1 to the query string for any page.
You can see an example of this at the bottom of latest.datasette.io/fixtures/facetable?_trace=1 . The JSON output shows full details of every SQL query that was executed to generate the page.
The datasette-pretty-traces plugin can be installed to provide a more readable display of this information. You can see a demo of that here .
You can add your own custom traces to the JSON output using the trace() context manager. This takes a string that identifies the type of trace being recorded, and records any keyword arguments as additional JSON keys on the resulting trace object.
The start and end time, duration and a traceback of where the trace was executed will be automatically attached to the JSON object.
This example uses trace to record the start, end and duration of any HTTP GET requests made using the function:
from datasette.tracer import trace
import httpx
async def fetch_url(url):
with trace(""fetch-url"", url=url):
async with httpx.AsyncClient() as client:
return await client.get(url)","[""Internals for plugins""]","[{""href"": ""https://latest.datasette.io/fixtures/facetable?_trace=1"", ""label"": ""latest.datasette.io/fixtures/facetable?_trace=1""}, {""href"": ""https://datasette.io/plugins/datasette-pretty-traces"", ""label"": ""datasette-pretty-traces""}, {""href"": ""https://latest-with-plugins.datasette.io/github/commits?_trace=1"", ""label"": ""a demo of that here""}]"
cli-reference:cli-help-help,cli-reference,cli-help-help,datasette --help,"Running datasette --help shows a list of all of the available commands.
[[[cog
help([""--help""])
]]]
Usage: datasette [OPTIONS] COMMAND [ARGS]...
Datasette is an open source multi-tool for exploring and publishing data
About Datasette: https://datasette.io/
Full documentation: https://docs.datasette.io/
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
serve* Serve up specified SQLite database files with a web UI
inspect Generate JSON summary of provided database files
install Install plugins and packages from PyPI into the same...
package Package SQLite files into a Datasette Docker container
plugins List currently installed plugins
publish Publish specified SQLite database files to the internet along...
uninstall Uninstall plugins and Python packages from the Datasette...
[[[end]]]
Additional commands added by plugins that use the register_commands(cli) hook will be listed here as well.","[""CLI reference""]",[]
plugin_hooks:plugin-asgi-wrapper,plugin_hooks,plugin-asgi-wrapper,asgi_wrapper(datasette),"Return an ASGI middleware wrapper function that will be applied to the Datasette ASGI application.
This is a very powerful hook. You can use it to manipulate the entire Datasette response, or even to configure new URL routes that will be handled by your own custom code.
You can write your ASGI code directly against the low-level specification, or you can use the middleware utilities provided by an ASGI framework such as Starlette .
This example plugin adds a x-databases HTTP header listing the currently attached databases:
from datasette import hookimpl
from functools import wraps
@hookimpl
def asgi_wrapper(datasette):
def wrap_with_databases_header(app):
@wraps(app)
async def add_x_databases_header(
scope, receive, send
):
async def wrapped_send(event):
if event[""type""] == ""http.response.start"":
original_headers = (
event.get(""headers"") or []
)
event = {
""type"": event[""type""],
""status"": event[""status""],
""headers"": original_headers
+ [
[
b""x-databases"",
"", "".join(
datasette.databases.keys()
).encode(""utf-8""),
]
],
}
await send(event)
await app(scope, receive, wrapped_send)
return add_x_databases_header
return wrap_with_databases_header
Examples: datasette-cors , datasette-pyinstrument , datasette-total-page-time","[""Plugin hooks""]","[{""href"": ""https://asgi.readthedocs.io/"", ""label"": ""ASGI""}, {""href"": ""https://www.starlette.io/middleware/"", ""label"": ""Starlette""}, {""href"": ""https://datasette.io/plugins/datasette-cors"", ""label"": ""datasette-cors""}, {""href"": ""https://datasette.io/plugins/datasette-pyinstrument"", ""label"": ""datasette-pyinstrument""}, {""href"": ""https://datasette.io/plugins/datasette-total-page-time"", ""label"": ""datasette-total-page-time""}]"
plugin_hooks:plugin-register-facet-classes,plugin_hooks,plugin-register-facet-classes,register_facet_classes(),"Return a list of additional Facet subclasses to be registered.
The design of this plugin hook is unstable and may change. See issue 830 .
Each Facet subclass implements a new type of facet operation. The class should look like this:
class SpecialFacet(Facet):
# This key must be unique across all facet classes:
type = ""special""
async def suggest(self):
# Use self.sql and self.params to suggest some facets
suggested_facets = []
suggested_facets.append(
{
""name"": column, # Or other unique name
# Construct the URL that will enable this facet:
""toggle_url"": self.ds.absolute_url(
self.request,
path_with_added_args(
self.request, {""_facet"": column}
),
),
}
)
return suggested_facets
async def facet_results(self):
# This should execute the facet operation and return results, again
# using self.sql and self.params as the starting point
facet_results = []
facets_timed_out = []
facet_size = self.get_facet_size()
# Do some calculations here...
for column in columns_selected_for_facet:
try:
facet_results_values = []
# More calculations...
facet_results_values.append(
{
""value"": value,
""label"": label,
""count"": count,
""toggle_url"": self.ds.absolute_url(
self.request, toggle_path
),
""selected"": selected,
}
)
facet_results.append(
{
""name"": column,
""results"": facet_results_values,
""truncated"": len(facet_rows_results)
> facet_size,
}
)
except QueryInterrupted:
facets_timed_out.append(column)
return facet_results, facets_timed_out
See datasette/facets.py for examples of how these classes can work.
The plugin hook can then be used to register the new facet class like this:
@hookimpl
def register_facet_classes():
return [SpecialFacet]","[""Plugin hooks""]","[{""href"": ""https://github.com/simonw/datasette/issues/830"", ""label"": ""issue 830""}, {""href"": ""https://github.com/simonw/datasette/blob/main/datasette/facets.py"", ""label"": ""datasette/facets.py""}]"
changelog:id151,changelog,id151,0.17 (2018-04-13),Release 0.17 to fix issues with PyPI,"[""Changelog""]",[]
testing_plugins:testing-plugins-fixtures,testing_plugins,testing-plugins-fixtures,Using pytest fixtures,"Pytest fixtures can be used to create initial testable objects which can then be used by multiple tests.
A common pattern for Datasette plugins is to create a fixture which sets up a temporary test database and wraps it in a Datasette instance.
Here's an example that uses the sqlite-utils library to populate a temporary test database. It also sets the title of that table using a simulated metadata.json configuration:
from datasette.app import Datasette
import pytest
import sqlite_utils
@pytest.fixture(scope=""session"")
def datasette(tmp_path_factory):
db_directory = tmp_path_factory.mktemp(""dbs"")
db_path = db_directory / ""test.db""
db = sqlite_utils.Database(db_path)
db[""dogs""].insert_all(
[
{""id"": 1, ""name"": ""Cleo"", ""age"": 5},
{""id"": 2, ""name"": ""Pancakes"", ""age"": 4},
],
pk=""id"",
)
datasette = Datasette(
[db_path],
metadata={
""databases"": {
""test"": {
""tables"": {
""dogs"": {""title"": ""Some dogs""}
}
}
}
},
)
return datasette
@pytest.mark.asyncio
async def test_example_table_json(datasette):
response = await datasette.client.get(
""/test/dogs.json?_shape=array""
)
assert response.status_code == 200
assert response.json() == [
{""id"": 1, ""name"": ""Cleo"", ""age"": 5},
{""id"": 2, ""name"": ""Pancakes"", ""age"": 4},
]
@pytest.mark.asyncio
async def test_example_table_html(datasette):
response = await datasette.client.get(""/test/dogs"")
assert "">Some dogs"" in response.text
Here the datasette() function defines the fixture, which is than automatically passed to the two test functions based on pytest automatically matching their datasette function parameters.
The @pytest.fixture(scope=""session"") line here ensures the fixture is reused for the full pytest execution session. This means that the temporary database file will be created once and reused for each test.
If you want to create that test database repeatedly for every individual test function, write the fixture function like this instead. You may want to do this if your plugin modifies the database contents in some way:
@pytest.fixture
def datasette(tmp_path_factory):
# This fixture will be executed repeatedly for every test
...","[""Testing plugins""]","[{""href"": ""https://docs.pytest.org/en/stable/fixture.html"", ""label"": ""Pytest fixtures""}, {""href"": ""https://sqlite-utils.datasette.io/en/stable/python-api.html"", ""label"": ""sqlite-utils library""}]"
internals:datasette-databases,internals,datasette-databases,.databases,"Property exposing a collections.OrderedDict of databases currently connected to Datasette.
The dictionary keys are the name of the database that is used in the URL - e.g. /fixtures would have a key of ""fixtures"" . The values are Database class instances.
All databases are listed, irrespective of user permissions. This means that the _internal database will always be listed here.","[""Internals for plugins"", ""Datasette class""]",[]
changelog:authentication,changelog,authentication,Authentication,"Prior to this release the Datasette ecosystem has treated authentication as exclusively the realm of plugins, most notably through datasette-auth-github .
0.44 introduces Authentication and permissions as core Datasette concepts ( #699 ). This enables different plugins to share responsibility for authenticating requests - you might have one plugin that handles user accounts and another one that allows automated access via API keys, for example.
You'll need to install plugins if you want full user accounts, but default Datasette can now authenticate a single root user with the new --root command-line option, which outputs a one-time use URL to authenticate as a root actor ( #784 ):
$ datasette fixtures.db --root
http://127.0.0.1:8001/-/auth-token?token=5b632f8cd44b868df625f5a6e2185d88eea5b22237fd3cc8773f107cc4fd6477
INFO: Started server process [14973]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
Plugins can implement new ways of authenticating users using the new actor_from_request(datasette, request) hook.","[""Changelog"", ""0.44 (2020-06-11)""]","[{""href"": ""https://github.com/simonw/datasette-auth-github"", ""label"": ""datasette-auth-github""}, {""href"": ""https://github.com/simonw/datasette/issues/699"", ""label"": ""#699""}, {""href"": ""https://github.com/simonw/datasette/issues/784"", ""label"": ""#784""}]"
authentication:authentication-actor-matches-allow,authentication,authentication-actor-matches-allow,actor_matches_allow(),"Plugins that wish to implement this same ""allow"" block permissions scheme can take advantage of the datasette.utils.actor_matches_allow(actor, allow) function:
from datasette.utils import actor_matches_allow
actor_matches_allow({""id"": ""root""}, {""id"": ""*""})
# returns True
The currently authenticated actor is made available to plugins as request.actor .","[""Authentication and permissions""]",[]
writing_plugins:writing-plugins-building-urls,writing_plugins,writing-plugins-building-urls,Building URLs within plugins,"Plugins that define their own custom user interface elements may need to link to other pages within Datasette.
This can be a bit tricky if the Datasette instance is using the base_url configuration setting to run behind a proxy, since that can cause Datasette's URLs to include an additional prefix.
The datasette.urls object provides internal methods for correctly generating URLs to different pages within Datasette, taking any base_url configuration into account.
This object is exposed in templates as the urls variable, which can be used like this:
Back to the Homepage
See datasette.urls for full details on this object.","[""Writing plugins""]",[]
writing_plugins:writing-plugins-cookiecutter,writing_plugins,writing-plugins-cookiecutter,Starting an installable plugin using cookiecutter,"Plugins that can be installed should be written as Python packages using a setup.py file.
The quickest way to start writing one an installable plugin is to use the datasette-plugin cookiecutter template. This creates a new plugin structure for you complete with an example test and GitHub Actions workflows for testing and publishing your plugin.
Install cookiecutter and then run this command to start building a plugin using the template:
cookiecutter gh:simonw/datasette-plugin
Read a cookiecutter template for writing Datasette plugins for more information about this template.","[""Writing plugins""]","[{""href"": ""https://github.com/simonw/datasette-plugin"", ""label"": ""datasette-plugin""}, {""href"": ""https://cookiecutter.readthedocs.io/en/stable/installation.html"", ""label"": ""Install cookiecutter""}, {""href"": ""https://simonwillison.net/2020/Jun/20/cookiecutter-plugins/"", ""label"": ""a cookiecutter template for writing Datasette plugins""}]"
changelog:id66,changelog,id66,0.37 (2020-02-25),"Plugins now have a supported mechanism for writing to a database, using the new .execute_write() and .execute_write_fn() methods. Documentation . ( #682 )
Immutable databases that have had their rows counted using the inspect command now use the calculated count more effectively - thanks, Kevin Keogh. ( #666 )
--reload no longer restarts the server if a database file is modified, unless that database was opened immutable mode with -i . ( #494 )
New ?_searchmode=raw option turns off escaping for FTS queries in ?_search= allowing full use of SQLite's FTS5 query syntax . ( #676 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/682"", ""label"": ""#682""}, {""href"": ""https://github.com/simonw/datasette/pull/666"", ""label"": ""#666""}, {""href"": ""https://github.com/simonw/datasette/issues/494"", ""label"": ""#494""}, {""href"": ""https://www.sqlite.org/fts5.html#full_text_query_syntax"", ""label"": ""FTS5 query syntax""}, {""href"": ""https://github.com/simonw/datasette/issues/676"", ""label"": ""#676""}]"
changelog:secret-plugin-configuration-options,changelog,secret-plugin-configuration-options,Secret plugin configuration options,"Plugins like datasette-auth-github need a safe way to set secret configuration options. Since the default mechanism for configuring plugins exposes those settings in /-/metadata a new mechanism was needed. Secret configuration values describes how plugins can now specify that their settings should be read from a file or an environment variable:
{
""plugins"": {
""datasette-auth-github"": {
""client_secret"": {
""$env"": ""GITHUB_CLIENT_SECRET""
}
}
}
}
These plugin secrets can be set directly using datasette publish . See Custom metadata and plugins for details. ( #538 and #543 )","[""Changelog"", ""0.29 (2019-07-07)""]","[{""href"": ""https://github.com/simonw/datasette-auth-github"", ""label"": ""datasette-auth-github""}, {""href"": ""https://github.com/simonw/datasette/issues/538"", ""label"": ""#538""}, {""href"": ""https://github.com/simonw/datasette/issues/543"", ""label"": ""#543""}]"
changelog:cookie-methods,changelog,cookie-methods,Cookie methods,"Plugins can now use the new response.set_cookie() method to set cookies.
A new request.cookies method on the :ref:internals_request` can be used to read incoming cookies.","[""Changelog"", ""0.44 (2020-06-11)""]",[]
changelog:register-routes-plugin-hooks,changelog,register-routes-plugin-hooks,register_routes() plugin hooks,"Plugins can now register new views and routes via the register_routes(datasette) plugin hook ( #819 ). View functions can be defined that accept any of the current datasette object, the current request , or the ASGI scope , send and receive objects.","[""Changelog"", ""0.44 (2020-06-11)""]","[{""href"": ""https://github.com/simonw/datasette/issues/819"", ""label"": ""#819""}]"
internals:internals-datasette-client,internals,internals-datasette-client,datasette.client,"Plugins can make internal simulated HTTP requests to the Datasette instance within which they are running. This ensures that all of Datasette's external JSON APIs are also available to plugins, while avoiding the overhead of making an external HTTP call to access those APIs.
The datasette.client object is a wrapper around the HTTPX Python library , providing an async-friendly API that is similar to the widely used Requests library .
It offers the following methods:
await datasette.client.get(path, **kwargs) - returns HTTPX Response
Execute an internal GET request against that path.
await datasette.client.post(path, **kwargs) - returns HTTPX Response
Execute an internal POST request. Use data={""name"": ""value""} to pass form parameters.
await datasette.client.options(path, **kwargs) - returns HTTPX Response
Execute an internal OPTIONS request.
await datasette.client.head(path, **kwargs) - returns HTTPX Response
Execute an internal HEAD request.
await datasette.client.put(path, **kwargs) - returns HTTPX Response
Execute an internal PUT request.
await datasette.client.patch(path, **kwargs) - returns HTTPX Response
Execute an internal PATCH request.
await datasette.client.delete(path, **kwargs) - returns HTTPX Response
Execute an internal DELETE request.
await datasette.client.request(method, path, **kwargs) - returns HTTPX Response
Execute an internal request with the given HTTP method against that path.
These methods can be used with datasette.urls - for example:
table_json = (
await datasette.client.get(
datasette.urls.table(
""fixtures"", ""facetable"", format=""json""
)
)
).json()
datasette.client methods automatically take the current base_url setting into account, whether or not you use the datasette.urls family of methods to construct the path.
For documentation on available **kwargs options and the shape of the HTTPX Response object refer to the HTTPX Async documentation .","[""Internals for plugins"", ""Datasette class""]","[{""href"": ""https://www.python-httpx.org/"", ""label"": ""HTTPX Python library""}, {""href"": ""https://requests.readthedocs.io/"", ""label"": ""Requests library""}, {""href"": ""https://www.python-httpx.org/async/"", ""label"": ""HTTPX Async documentation""}]"
plugins:plugins-configuration,plugins,plugins-configuration,Plugin configuration,"Plugins can have their own configuration, embedded in a Metadata file. Configuration options for plugins live within a ""plugins"" key in that file, which can be included at the root, database or table level.
Here is an example of some plugin configuration for a specific table:
{
""databases"": {
""sf-trees"": {
""tables"": {
""Street_Tree_List"": {
""plugins"": {
""datasette-cluster-map"": {
""latitude_column"": ""lat"",
""longitude_column"": ""lng""
}
}
}
}
}
}
}
This tells the datasette-cluster-map column which latitude and longitude columns should be used for a table called Street_Tree_List inside a database file called sf-trees.db .","[""Plugins""]",[]
writing_plugins:writing-plugins-packaging,writing_plugins,writing-plugins-packaging,Packaging a plugin,"Plugins can be packaged using Python setuptools. You can see an example of a packaged plugin at https://github.com/simonw/datasette-plugin-demos
The example consists of two files: a setup.py file that defines the plugin:
from setuptools import setup
VERSION = ""0.1""
setup(
name=""datasette-plugin-demos"",
description=""Examples of plugins for Datasette"",
author=""Simon Willison"",
url=""https://github.com/simonw/datasette-plugin-demos"",
license=""Apache License, Version 2.0"",
version=VERSION,
py_modules=[""datasette_plugin_demos""],
entry_points={
""datasette"": [
""plugin_demos = datasette_plugin_demos""
]
},
install_requires=[""datasette""],
)
And a Python module file, datasette_plugin_demos.py , that implements the plugin:
from datasette import hookimpl
import random
@hookimpl
def prepare_jinja2_environment(env):
env.filters[""uppercase""] = lambda u: u.upper()
@hookimpl
def prepare_connection(conn):
conn.create_function(
""random_integer"", 2, random.randint
)
Having built a plugin in this way you can turn it into an installable package using the following command:
python3 setup.py sdist
This will create a .tar.gz file in the dist/ directory.
You can then install your new plugin into a Datasette virtual environment or Docker container using pip :
pip install datasette-plugin-demos-0.1.tar.gz
To learn how to upload your plugin to PyPI for use by other people, read the PyPA guide to Packaging and distributing projects .","[""Writing plugins""]","[{""href"": ""https://github.com/simonw/datasette-plugin-demos"", ""label"": ""https://github.com/simonw/datasette-plugin-demos""}, {""href"": ""https://pypi.org/"", ""label"": ""PyPI""}, {""href"": ""https://packaging.python.org/tutorials/distributing-packages/"", ""label"": ""Packaging and distributing projects""}]"
cli-reference:cli-help-package-help,cli-reference,cli-help-package-help,datasette package,"Package SQLite files into a Datasette Docker container, see datasette package .
[[[cog
help([""package"", ""--help""])
]]]
Usage: datasette package [OPTIONS] FILES...
Package SQLite files into a Datasette Docker container
Options:
-t, --tag TEXT Name for the resulting Docker container, can
optionally use name:tag format
-m, --metadata FILENAME Path to JSON/YAML file containing metadata to
publish
--extra-options TEXT Extra options to pass to datasette serve
--branch TEXT Install datasette from a GitHub branch e.g. main
--template-dir DIRECTORY Path to directory containing custom templates
--plugins-dir DIRECTORY Path to directory containing custom plugins
--static MOUNT:DIRECTORY Serve static files from this directory at /MOUNT/...
--install TEXT Additional packages (e.g. plugins) to install
--spatialite Enable SpatialLite extension
--version-note TEXT Additional note to show on /-/versions
--secret TEXT Secret used for signing secure values, such as
signed cookies
-p, --port INTEGER RANGE Port to run the server on, defaults to 8001
[1<=x<=65535]
--title TEXT Title for metadata
--license TEXT License label for metadata
--license_url TEXT License URL for metadata
--source TEXT Source label for metadata
--source_url TEXT Source URL for metadata
--about TEXT About label for metadata
--about_url TEXT About URL for metadata
--help Show this message and exit.
[[[end]]]","[""CLI reference""]",[]
cli-reference:cli-help-inspect-help,cli-reference,cli-help-inspect-help,datasette inspect,"Outputs JSON representing introspected data about one or more SQLite database files.
If you are opening an immutable database, you can pass this file to the --inspect-data option to improve Datasette's performance by allowing it to skip running row counts against the database when it first starts running:
datasette inspect mydatabase.db > inspect-data.json
datasette serve -i mydatabase.db --inspect-file inspect-data.json
This performance optimization is used automatically by some of the datasette publish commands. You are unlikely to need to apply this optimization manually.
[[[cog
help([""inspect"", ""--help""])
]]]
Usage: datasette inspect [OPTIONS] [FILES]...
Generate JSON summary of provided database files
This can then be passed to ""datasette --inspect-file"" to speed up count
operations against immutable database files.
Options:
--inspect-file TEXT
--load-extension PATH:ENTRYPOINT?
Path to a SQLite extension to load, and
optional entrypoint
--help Show this message and exit.
[[[end]]]","[""CLI reference""]",[]
cli-reference:cli-help-plugins-help,cli-reference,cli-help-plugins-help,datasette plugins,"Output JSON showing all currently installed plugins, their versions, whether they include static files or templates and which Plugin hooks they use.
[[[cog
help([""plugins"", ""--help""])
]]]
Usage: datasette plugins [OPTIONS]
List currently installed plugins
Options:
--all Include built-in default plugins
--plugins-dir DIRECTORY Path to directory containing custom plugins
--help Show this message and exit.
[[[end]]]
Example output:
[
{
""name"": ""datasette-geojson"",
""static"": false,
""templates"": false,
""version"": ""0.3.1"",
""hooks"": [
""register_output_renderer""
]
},
{
""name"": ""datasette-geojson-map"",
""static"": true,
""templates"": false,
""version"": ""0.4.0"",
""hooks"": [
""extra_body_script"",
""extra_css_urls"",
""extra_js_urls""
]
},
{
""name"": ""datasette-leaflet"",
""static"": true,
""templates"": false,
""version"": ""0.2.2"",
""hooks"": [
""extra_body_script"",
""extra_template_vars""
]
}
]","[""CLI reference""]",[]
deploying:deploying-openrc,deploying,deploying-openrc,Running Datasette using OpenRC,"OpenRC is the service manager on non-systemd Linux distributions like Alpine Linux and Gentoo .
Create an init script at /etc/init.d/datasette with the following contents:
#!/sbin/openrc-run
name=""datasette""
command=""datasette""
command_args=""serve -h 0.0.0.0 /path/to/db.db""
command_background=true
pidfile=""/run/${RC_SVCNAME}.pid""
You then need to configure the service to run at boot and start it:
rc-update add datasette
rc-service datasette start","[""Deploying Datasette""]","[{""href"": ""https://www.alpinelinux.org/"", ""label"": ""Alpine Linux""}, {""href"": ""https://www.gentoo.org/"", ""label"": ""Gentoo""}]"
contributing:contributing-running-tests,contributing,contributing-running-tests,Running the tests,"Once you have done this, you can run the Datasette unit tests from inside your datasette/ directory using pytest like so:
pytest
You can run the tests faster using multiple CPU cores with pytest-xdist like this:
pytest -n auto -m ""not serial""
-n auto detects the number of available cores automatically. The -m ""not serial"" skips tests that don't work well in a parallel test environment. You can run those tests separately like so:
pytest -m ""serial""","[""Contributing""]","[{""href"": ""https://docs.pytest.org/"", ""label"": ""pytest""}, {""href"": ""https://pypi.org/project/pytest-xdist/"", ""label"": ""pytest-xdist""}]"
publish:cli-publish,publish,cli-publish,datasette publish,"Once you have created a SQLite database (e.g. using csvs-to-sqlite ) you can deploy it to a hosting account using a single command.
You will need a hosting account with Heroku or Google Cloud . Once you have created your account you will need to install and configure the heroku or gcloud command-line tools.","[""Publishing data""]","[{""href"": ""https://github.com/simonw/csvs-to-sqlite/"", ""label"": ""csvs-to-sqlite""}, {""href"": ""https://www.heroku.com/"", ""label"": ""Heroku""}, {""href"": ""https://cloud.google.com/"", ""label"": ""Google Cloud""}]"
changelog:features,changelog,features,Features,"Now tested against Python 3.11. Docker containers used by datasette publish and datasette package both now use that version of Python. ( #1853 )
--load-extension option now supports entrypoints. Thanks, Alex Garcia. ( #1789 )
Facet size can now be set per-table with the new facet_size table metadata option. ( #1804 )
The truncate_cells_html setting now also affects long URLs in columns. ( #1805 )
The non-JavaScript SQL editor textarea now increases height to fit the SQL query. ( #1786 )
Facets are now displayed with better line-breaks in long values. Thanks, Daniel Rech. ( #1794 )
The settings.json file used in Configuration directory mode is now validated on startup. ( #1816 )
SQL queries can now include leading SQL comments, using /* ... */ or -- ... syntax. Thanks, Charles Nepote. ( #1860 )
SQL query is now re-displayed when terminated with a time limit error. ( #1819 )
The inspect data mechanism is now used to speed up server startup - thanks, Forest Gregg. ( #1834 )
In Configuration directory mode databases with filenames ending in .sqlite or .sqlite3 are now automatically added to the Datasette instance. ( #1646 )
Breadcrumb navigation display now respects the current user's permissions. ( #1831 )","[""Changelog"", ""0.63 (2022-10-27)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1853"", ""label"": ""#1853""}, {""href"": ""https://github.com/simonw/datasette/pull/1789"", ""label"": ""#1789""}, {""href"": ""https://github.com/simonw/datasette/issues/1804"", ""label"": ""#1804""}, {""href"": ""https://github.com/simonw/datasette/issues/1805"", ""label"": ""#1805""}, {""href"": ""https://github.com/simonw/datasette/issues/1786"", ""label"": ""#1786""}, {""href"": ""https://github.com/simonw/datasette/pull/1794"", ""label"": ""#1794""}, {""href"": ""https://github.com/simonw/datasette/issues/1816"", ""label"": ""#1816""}, {""href"": ""https://github.com/simonw/datasette/issues/1860"", ""label"": ""#1860""}, {""href"": ""https://github.com/simonw/datasette/issues/1819"", ""label"": ""#1819""}, {""href"": ""https://github.com/simonw/datasette/issues/1834"", ""label"": ""#1834""}, {""href"": ""https://github.com/simonw/datasette/issues/1646"", ""label"": ""#1646""}, {""href"": ""https://github.com/simonw/datasette/issues/1831"", ""label"": ""#1831""}]"
settings:config-dir,settings,config-dir,Configuration directory mode,"Normally you configure Datasette using command-line options. For a Datasette instance with custom templates, custom plugins, a static directory and several databases this can get quite verbose:
$ datasette one.db two.db \
--metadata=metadata.json \
--template-dir=templates/ \
--plugins-dir=plugins \
--static css:css
As an alternative to this, you can run Datasette in configuration directory mode. Create a directory with the following structure:
# In a directory called my-app:
my-app/one.db
my-app/two.db
my-app/metadata.json
my-app/templates/index.html
my-app/plugins/my_plugin.py
my-app/static/my.css
Now start Datasette by providing the path to that directory:
$ datasette my-app/
Datasette will detect the files in that directory and automatically configure itself using them. It will serve all *.db files that it finds, will load metadata.json if it exists, and will load the templates , plugins and static folders if they are present.
The files that can be included in this directory are as follows. All are optional.
*.db (or *.sqlite3 or *.sqlite ) - SQLite database files that will be served by Datasette
metadata.json - Metadata for those databases - metadata.yaml or metadata.yml can be used as well
inspect-data.json - the result of running datasette inspect *.db --inspect-file=inspect-data.json from the configuration directory - any database files listed here will be treated as immutable, so they should not be changed while Datasette is running
settings.json - settings that would normally be passed using --setting - here they should be stored as a JSON object of key/value pairs
templates/ - a directory containing Custom templates
plugins/ - a directory containing plugins, see Writing one-off plugins
static/ - a directory containing static files - these will be served from /static/filename.txt , see Serving static files","[""Settings""]",[]
changelog:documentation,changelog,documentation,Documentation,"New tutorial: Cleaning data with sqlite-utils and Datasette .
Screenshots in the documentation are now maintained using shot-scraper , as described in Automating screenshots for the Datasette documentation using shot-scraper . ( #1844 )
More detailed command descriptions on the CLI reference page. ( #1787 )
New documentation on Running Datasette using OpenRC - thanks, Adam Simpson. ( #1825 )","[""Changelog"", ""0.63 (2022-10-27)""]","[{""href"": ""https://datasette.io/tutorials/clean-data"", ""label"": ""Cleaning data with sqlite-utils and Datasette""}, {""href"": ""https://shot-scraper.datasette.io/"", ""label"": ""shot-scraper""}, {""href"": ""https://simonwillison.net/2022/Oct/14/automating-screenshots/"", ""label"": ""Automating screenshots for the Datasette documentation using shot-scraper""}, {""href"": ""https://github.com/simonw/datasette/issues/1844"", ""label"": ""#1844""}, {""href"": ""https://github.com/simonw/datasette/issues/1787"", ""label"": ""#1787""}, {""href"": ""https://github.com/simonw/datasette/pull/1825"", ""label"": ""#1825""}]"
changelog:id90,changelog,id90,0.25 (2018-09-19),"New plugin hooks, improved database view support and an easier way to use more recent versions of SQLite.
New publish_subcommand plugin hook. A plugin can now add additional datasette publish publishers in addition to the default now and heroku , both of which have been refactored into default plugins. publish_subcommand documentation . Closes #349
New render_cell plugin hook. Plugins can now customize how values are displayed in the HTML tables produced by Datasette's browsable interface. datasette-json-html and datasette-render-images are two new plugins that use this hook. render_cell documentation . Closes #352
New extra_body_script plugin hook, enabling plugins to provide additional JavaScript that should be added to the page footer. extra_body_script documentation .
extra_css_urls and extra_js_urls hooks now take additional optional parameters, allowing them to be more selective about which pages they apply to. Documentation .
You can now use the sortable_columns metadata setting to explicitly enable sort-by-column in the interface for database views, as well as for specific tables.
The new fts_table and fts_pk metadata settings can now be used to explicitly configure full-text search for a table or a view , even if that table is not directly coupled to the SQLite FTS feature in the database schema itself.
Datasette will now use pysqlite3 in place of the standard library sqlite3 module if it has been installed in the current environment. This makes it much easier to run Datasette against a more recent version of SQLite, including the just-released SQLite 3.25.0 which adds window function support. More details on how to use this in #360
New mechanism that allows plugin configuration options to be set using metadata.json .","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/349"", ""label"": ""#349""}, {""href"": ""https://github.com/simonw/datasette-json-html"", ""label"": ""datasette-json-html""}, {""href"": ""https://github.com/simonw/datasette-render-images"", ""label"": ""datasette-render-images""}, {""href"": ""https://github.com/simonw/datasette/issues/352"", ""label"": ""#352""}, {""href"": ""https://github.com/coleifer/pysqlite3"", ""label"": ""pysqlite3""}, {""href"": ""https://www.sqlite.org/releaselog/3_25_0.html"", ""label"": ""SQLite 3.25.0""}, {""href"": ""https://github.com/simonw/datasette/issues/360"", ""label"": ""#360""}]"
changelog:plugin-hooks,changelog,plugin-hooks,Plugin hooks,"New plugin hook: handle_exception() , for custom handling of exceptions caught by Datasette. ( #1770 )
The render_cell() plugin hook is now also passed a row argument, representing the sqlite3.Row object that is being rendered. ( #1300 )
The configuration directory is now stored in datasette.config_dir , making it available to plugins. Thanks, Chris Amico. ( #1766 )","[""Changelog"", ""0.62 (2022-08-14)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1770"", ""label"": ""#1770""}, {""href"": ""https://github.com/simonw/datasette/issues/1300"", ""label"": ""#1300""}, {""href"": ""https://github.com/simonw/datasette/pull/1766"", ""label"": ""#1766""}]"
changelog:plugins-and-internals,changelog,plugins-and-internals,Plugins and internals,"New plugin hook: filters_from_request(request, database, table, datasette) , which runs on the table page and can be used to support new custom query string parameters that modify the SQL query. ( #473 )
Added two additional methods for writing to the database: await db.execute_write_script(sql, block=True) and await db.execute_write_many(sql, params_seq, block=True) . ( #1570 )
The db.execute_write() internal method now defaults to blocking until the write operation has completed. Previously it defaulted to queuing the write and then continuing to run code while the write was in the queue. ( #1579 )
Database write connections now execute the prepare_connection(conn, database, datasette) plugin hook. ( #1564 )
The Datasette() constructor no longer requires the files= argument, and is now documented at Datasette class . ( #1563 )
The tracing feature now traces write queries, not just read queries. ( #1568 )
The query string variables exposed by request.args will now include blank strings for arguments such as foo in ?foo=&bar=1 rather than ignoring those parameters entirely. ( #1551 )","[""Changelog"", ""0.60 (2022-01-13)""]","[{""href"": ""https://github.com/simonw/datasette/issues/473"", ""label"": ""#473""}, {""href"": ""https://github.com/simonw/datasette/issues/1570"", ""label"": ""#1570""}, {""href"": ""https://github.com/simonw/datasette/issues/1579"", ""label"": ""#1579""}, {""href"": ""https://github.com/simonw/datasette/issues/1564"", ""label"": ""#1564""}, {""href"": ""https://github.com/simonw/datasette/issues/1563"", ""label"": ""#1563""}, {""href"": ""https://github.com/simonw/datasette/issues/1568"", ""label"": ""#1568""}, {""href"": ""https://github.com/simonw/datasette/issues/1551"", ""label"": ""#1551""}]"
changelog:id58,changelog,id58,Smaller changes,"New internals documentation for Request object and Response class . ( #706 )
request.url now respects the force_https_urls config setting. closes ( #781 )
request.args.getlist() returns [] if missing. Removed request.raw_args entirely. ( #774 )
New datasette.get_database() method.
Added _ prefix to many private, undocumented methods of the Datasette class. ( #576 )
Removed the db.get_outbound_foreign_keys() method which duplicated the behaviour of db.foreign_keys_for_table() .
New await datasette.permission_allowed() method.
/-/actor debugging endpoint for viewing the currently authenticated actor.
New request.cookies property.
/-/plugins endpoint now shows a list of hooks implemented by each plugin, e.g. https://latest.datasette.io/-/plugins?all=1
request.post_vars() method no longer discards empty values.
New ""params"" canned query key for explicitly setting named parameters, see Canned query parameters . ( #797 )
request.args is now a MultiParams object.
Fixed a bug with the datasette plugins command. ( #802 )
Nicer pattern for using make_app_client() in tests. ( #395 )
New request.actor property.
Fixed broken CSS on nested 404 pages. ( #777 )
New request.url_vars property. ( #822 )
Fixed a bug with the python tests/fixtures.py command for outputting Datasette's testing fixtures database and plugins. ( #804 )
datasette publish heroku now deploys using Python 3.8.3.
Added a warning that the register_facet_classes() hook is unstable and may change in the future. ( #830 )
The {""$env"": ""ENVIRONMENT_VARIBALE""} mechanism (see Secret configuration values ) now works with variables inside nested lists. ( #837 )","[""Changelog"", ""0.44 (2020-06-11)""]","[{""href"": ""https://github.com/simonw/datasette/issues/706"", ""label"": ""#706""}, {""href"": ""https://github.com/simonw/datasette/issues/781"", ""label"": ""#781""}, {""href"": ""https://github.com/simonw/datasette/issues/774"", ""label"": ""#774""}, {""href"": ""https://github.com/simonw/datasette/issues/576"", ""label"": ""#576""}, {""href"": ""https://latest.datasette.io/-/plugins?all=1"", ""label"": ""https://latest.datasette.io/-/plugins?all=1""}, {""href"": ""https://github.com/simonw/datasette/issues/797"", ""label"": ""#797""}, {""href"": ""https://github.com/simonw/datasette/issues/802"", ""label"": ""#802""}, {""href"": ""https://github.com/simonw/datasette/issues/395"", ""label"": ""#395""}, {""href"": ""https://github.com/simonw/datasette/issues/777"", ""label"": ""#777""}, {""href"": ""https://github.com/simonw/datasette/issues/822"", ""label"": ""#822""}, {""href"": ""https://github.com/simonw/datasette/issues/804"", ""label"": ""#804""}, {""href"": ""https://github.com/simonw/datasette/issues/830"", ""label"": ""#830""}, {""href"": ""https://github.com/simonw/datasette/issues/837"", ""label"": ""#837""}]"
changelog:id85,changelog,id85,0.27 (2019-01-31),"New command: datasette plugins ( documentation ) shows you the currently installed list of plugins.
Datasette can now output newline-delimited JSON using the new ?_shape=array&_nl=on query string option.
Added documentation on The Datasette Ecosystem .
Now using Python 3.7.2 as the base for the official Datasette Docker image .","[""Changelog""]","[{""href"": ""http://ndjson.org/"", ""label"": ""newline-delimited JSON""}, {""href"": ""https://hub.docker.com/r/datasetteproject/datasette/"", ""label"": ""Datasette Docker image""}]"
changelog:id120,changelog,id120,0.21 (2018-05-05),"New JSON _shape= options, the ability to set table _size= and a mechanism for searching within specific columns.
Default tests to using a longer timelimit
Every now and then a test will fail in Travis CI on Python 3.5 because it hit
the default 20ms SQL time limit.
Test fixtures now default to a 200ms time limit, and we only use the 20ms time
limit for the specific test that tests query interruption. This should make
our tests on Python 3.5 in Travis much more stable.
Support _search_COLUMN=text searches, closes #237
Show version on /-/plugins page, closes #248
?_size=max option, closes #249
Added /-/versions and /-/versions.json , closes #244
Sample output:
{
""python"": {
""version"": ""3.6.3"",
""full"": ""3.6.3 (default, Oct 4 2017, 06:09:38) \n[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]""
},
""datasette"": {
""version"": ""0.20""
},
""sqlite"": {
""version"": ""3.23.1"",
""extensions"": {
""json1"": null,
""spatialite"": ""4.3.0a""
}
}
}
Renamed ?_sql_time_limit_ms= to ?_timelimit , closes #242
New ?_shape=array option + tweaks to _shape , closes #245
Default is now ?_shape=arrays (renamed from lists )
New ?_shape=array returns an array of objects as the root object
Changed ?_shape=object to return the object as the root
Updated docs
FTS tables now detected by inspect() , closes #240
New ?_size=XXX query string parameter for table view, closes #229
Also added documentation for all of the _special arguments.
Plus deleted some duplicate logic implementing _group_count .
If max_returned_rows==page_size , increment max_returned_rows - fixes #230
New hidden: True option for table metadata, closes #239
Hide idx_* tables if spatialite detected, closes #228
Added class=rows-and-columns to custom query results table
Added CSS class rows-and-columns to main table
label_column option in metadata.json - closes #234","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/237"", ""label"": ""#237""}, {""href"": ""https://github.com/simonw/datasette/issues/248"", ""label"": ""#248""}, {""href"": ""https://github.com/simonw/datasette/issues/249"", ""label"": ""#249""}, {""href"": ""https://github.com/simonw/datasette/issues/244"", ""label"": ""#244""}, {""href"": ""https://github.com/simonw/datasette/issues/242"", ""label"": ""#242""}, {""href"": ""https://github.com/simonw/datasette/issues/245"", ""label"": ""#245""}, {""href"": ""https://github.com/simonw/datasette/issues/240"", ""label"": ""#240""}, {""href"": ""https://github.com/simonw/datasette/issues/229"", ""label"": ""#229""}, {""href"": ""https://github.com/simonw/datasette/issues/230"", ""label"": ""#230""}, {""href"": ""https://github.com/simonw/datasette/issues/239"", ""label"": ""#239""}, {""href"": ""https://github.com/simonw/datasette/issues/228"", ""label"": ""#228""}, {""href"": ""https://github.com/simonw/datasette/issues/234"", ""label"": ""#234""}]"
changelog:id27,changelog,id27,0.58 (2021-07-14),"New datasette --uds /tmp/datasette.sock option for binding Datasette to a Unix domain socket, see proxy documentation ( #1388 )
""searchmode"": ""raw"" table metadata option for defaulting a table to executing SQLite full-text search syntax without first escaping it, see Advanced SQLite search queries . ( #1389 )
New plugin hook: get_metadata(datasette, key, database, table) , for returning custom metadata for an instance, database or table. Thanks, Brandon Roberts! ( #1384 )
New plugin hook: skip_csrf(datasette, scope) , for opting out of CSRF protection based on the incoming request. ( #1377 )
The menu_links() , table_actions() and database_actions() plugin hooks all gained a new optional request argument providing access to the current request. ( #1371 )
Major performance improvement for Datasette faceting. ( #1394 )
Improved documentation for Running Datasette behind a proxy to recommend using ProxyPreservehost On with Apache. ( #1387 )
POST requests to endpoints that do not support that HTTP verb now return a 405 error.
db.path can now be provided as a pathlib.Path object, useful when writing unit tests for plugins. Thanks, Chris Amico. ( #1365 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/1388"", ""label"": ""#1388""}, {""href"": ""https://github.com/simonw/datasette/issues/1389"", ""label"": ""#1389""}, {""href"": ""https://github.com/simonw/datasette/issues/1384"", ""label"": ""#1384""}, {""href"": ""https://github.com/simonw/datasette/issues/1377"", ""label"": ""#1377""}, {""href"": ""https://github.com/simonw/datasette/issues/1371"", ""label"": ""#1371""}, {""href"": ""https://github.com/simonw/datasette/issues/1394"", ""label"": ""#1394""}, {""href"": ""https://github.com/simonw/datasette/issues/1387"", ""label"": ""#1387""}, {""href"": ""https://github.com/simonw/datasette/issues/1365"", ""label"": ""#1365""}]"
changelog:id63,changelog,id63,0.39 (2020-03-24),"New base_url configuration setting for serving up the correct links while running Datasette under a different URL prefix. ( #394 )
New metadata settings ""sort"" and ""sort_desc"" for setting the default sort order for a table. See Setting a default sort order . ( #702 )
Sort direction arrow now displays by default on the primary key. This means you only have to click once (not twice) to sort in reverse order. ( #677 )
New await Request(scope, receive).post_vars() method for accessing POST form variables. ( #700 )
Plugin hooks documentation now links to example uses of each plugin. ( #709 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/394"", ""label"": ""#394""}, {""href"": ""https://github.com/simonw/datasette/issues/702"", ""label"": ""#702""}, {""href"": ""https://github.com/simonw/datasette/issues/677"", ""label"": ""#677""}, {""href"": ""https://github.com/simonw/datasette/issues/700"", ""label"": ""#700""}, {""href"": ""https://github.com/simonw/datasette/issues/709"", ""label"": ""#709""}]"
sql_queries:canned-queries-magic-parameters,sql_queries,canned-queries-magic-parameters,Magic parameters,"Named parameters that start with an underscore are special: they can be used to automatically add values created by Datasette that are not contained in the incoming form fields or query string.
These magic parameters are only supported for canned queries: to avoid security issues (such as queries that extract the user's private cookies) they are not available to SQL that is executed by the user as a custom SQL query.
Available magic parameters are:
_actor_* - e.g. _actor_id , _actor_name
Fields from the currently authenticated Actors .
_header_* - e.g. _header_user_agent
Header from the incoming HTTP request. The key should be in lower case and with hyphens converted to underscores e.g. _header_user_agent or _header_accept_language .
_cookie_* - e.g. _cookie_lang
The value of the incoming cookie of that name.
_now_epoch
The number of seconds since the Unix epoch.
_now_date_utc
The date in UTC, e.g. 2020-06-01
_now_datetime_utc
The ISO 8601 datetime in UTC, e.g. 2020-06-24T18:01:07Z
_random_chars_* - e.g. _random_chars_128
A random string of characters of the specified length.
Here's an example configuration (this time using metadata.yaml since that provides better support for multi-line SQL queries) that adds a message from the authenticated user, storing various pieces of additional metadata using magic parameters:
databases:
mydatabase:
queries:
add_message:
allow:
id: ""*""
sql: |-
INSERT INTO messages (
user_id, message, datetime
) VALUES (
:_actor_id, :message, :_now_datetime_utc
)
write: true
The form presented at /mydatabase/add_message will have just a field for message - the other parameters will be populated by the magic parameter mechanism.
Additional custom magic parameters can be added by plugins using the register_magic_parameters(datasette) hook.","[""Running SQL queries"", ""Canned queries""]",[]
changelog:id133,changelog,id133,0.20 (2018-04-20),"Mostly new work on the Plugins mechanism: plugins can now bundle static assets and custom templates, and datasette publish has a new --install=name-of-plugin option.
Add col-X classes to HTML table on custom query page
Fixed out-dated template in documentation
Plugins can now bundle custom templates, #224
Added /-/metadata /-/plugins /-/inspect, #225
Documentation for --install option, refs #223
Datasette publish/package --install option, #223
Fix for plugins in Python 3.5, #222
New plugin hooks: extra_css_urls() and extra_js_urls(), #214
/-/static-plugins/PLUGIN_NAME/ now serves static/ from plugins
now gets class=""col-X"" - plus added col-X documentation
Use to_css_class for table cell column classes
This ensures that columns with spaces in the name will still
generate usable CSS class names. Refs #209
Add column name classes to
s, make PK bold [Russ Garrett]
Don't duplicate simple primary keys in the link column [Russ Garrett]
When there's a simple (single-column) primary key, it looks weird to
duplicate it in the link column.
This change removes the second PK column and treats the link column as
if it were the PK column from a header/sorting perspective.
Correct escaping for HTML display of row links [Russ Garrett]
Longer time limit for test_paginate_compound_keys
It was failing intermittently in Travis - see #209
Use application/octet-stream for downloadable databases
Updated PyPI classifiers
Updated PyPI link to pypi.org","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/224"", ""label"": ""#224""}, {""href"": ""https://github.com/simonw/datasette/issues/225"", ""label"": ""#225""}, {""href"": ""https://github.com/simonw/datasette/issues/223"", ""label"": ""#223""}, {""href"": ""https://github.com/simonw/datasette/issues/223"", ""label"": ""#223""}, {""href"": ""https://github.com/simonw/datasette/issues/222"", ""label"": ""#222""}, {""href"": ""https://github.com/simonw/datasette/issues/214"", ""label"": ""#214""}, {""href"": ""https://github.com/simonw/datasette/issues/209"", ""label"": ""#209""}, {""href"": ""https://github.com/simonw/datasette/issues/209"", ""label"": ""#209""}]"
json_api:json-api-discover-alternate,json_api,json-api-discover-alternate,Discovering the JSON for a page,"Most of the HTML pages served by Datasette provide a mechanism for discovering their JSON equivalents using the HTML link mechanism.
You can find this near the top of the source code of those pages, looking like this:
The JSON URL is also made available in a Link HTTP header for the page:
Link: https://latest.datasette.io/fixtures/sortable.json; rel=""alternate""; type=""application/json+datasette""","[""JSON API""]",[]
changelog:id104,changelog,id104,0.23.1 (2018-06-21),"Minor bugfix release.
Correctly display empty strings in HTML table, closes #314
Allow ""."" in database filenames, closes #302
404s ending in slash redirect to remove that slash, closes #309
Fixed incorrect display of compound primary keys with foreign key
references. Closes #319
Docs + example of canned SQL query using || concatenation. Closes #321
Correctly display facets with value of 0 - closes #318
Default 'expand labels' to checked in CSV advanced export","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/314"", ""label"": ""#314""}, {""href"": ""https://github.com/simonw/datasette/issues/302"", ""label"": ""#302""}, {""href"": ""https://github.com/simonw/datasette/issues/309"", ""label"": ""#309""}, {""href"": ""https://github.com/simonw/datasette/issues/319"", ""label"": ""#319""}, {""href"": ""https://github.com/simonw/datasette/issues/321"", ""label"": ""#321""}, {""href"": ""https://github.com/simonw/datasette/issues/318"", ""label"": ""#318""}]"
changelog:id100,changelog,id100,0.23.2 (2018-07-07),"Minor bugfix and documentation release.
CSV export now respects --cors , fixes #326
Installation instructions , including docker image - closes #328
Fix for row pages for tables with / in, closes #325","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/326"", ""label"": ""#326""}, {""href"": ""https://github.com/simonw/datasette/issues/328"", ""label"": ""#328""}, {""href"": ""https://github.com/simonw/datasette/issues/325"", ""label"": ""#325""}]"
metadata:per-database-and-per-table-metadata,metadata,per-database-and-per-table-metadata,Per-database and per-table metadata,"Metadata at the top level of the JSON will be shown on the index page and in the
footer on every page of the site. The license and source is expected to apply to
all of your data.
You can also provide metadata at the per-database or per-table level, like this:
{
""databases"": {
""database1"": {
""source"": ""Alternative source"",
""source_url"": ""http://example.com/"",
""tables"": {
""example_table"": {
""description_html"": ""Custom table description"",
""license"": ""CC BY 3.0 US"",
""license_url"": ""https://creativecommons.org/licenses/by/3.0/us/""
}
}
}
}
}
Each of the top-level metadata fields can be used at the database and table level.","[""Metadata""]",[]
settings:setting-num-sql-threads,settings,setting-num-sql-threads,num_sql_threads,"Maximum number of threads in the thread pool Datasette uses to execute SQLite queries. Defaults to 3.
datasette mydatabase.db --setting num_sql_threads 10
Setting this to 0 turns off threaded SQL queries entirely - useful for environments that do not support threading such as Pyodide .","[""Settings"", ""Settings""]","[{""href"": ""https://pyodide.org/"", ""label"": ""Pyodide""}]"
internals:internals,internals,internals,Internals for plugins,Many Plugin hooks are passed objects that provide access to internal Datasette functionality. The interface to these objects should not be considered stable with the exception of methods that are documented here.,[],[]
changelog:other-small-fixes,changelog,other-small-fixes,Other small fixes,"Made several performance improvements to the database schema introspection code that runs when Datasette first starts up. ( #1555 )
Label columns detected for foreign keys are now case-insensitive, so Name or TITLE will be detected in the same way as name or title . ( #1544 )
Upgraded Pluggy dependency to 1.0. ( #1575 )
Now using Plausible analytics for the Datasette documentation.
explain query plan is now allowed with varying amounts of whitespace in the query. ( #1588 )
New CLI reference page showing the output of --help for each of the datasette sub-commands. This lead to several small improvements to the help copy. ( #1594 )
Fixed bug where writable canned queries could not be used with custom templates. ( #1547 )
Improved fix for a bug where columns with a underscore prefix could result in unnecessary hidden form fields. ( #1527 )","[""Changelog"", ""0.60 (2022-01-13)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1555"", ""label"": ""#1555""}, {""href"": ""https://github.com/simonw/datasette/issues/1544"", ""label"": ""#1544""}, {""href"": ""https://github.com/simonw/datasette/issues/1575"", ""label"": ""#1575""}, {""href"": ""https://plausible.io/"", ""label"": ""Plausible analytics""}, {""href"": ""https://github.com/simonw/datasette/issues/1588"", ""label"": ""#1588""}, {""href"": ""https://github.com/simonw/datasette/issues/1594"", ""label"": ""#1594""}, {""href"": ""https://github.com/simonw/datasette/issues/1547"", ""label"": ""#1547""}, {""href"": ""https://github.com/simonw/datasette/issues/1527"", ""label"": ""#1527""}]"
internals:database-execute-write-many,internals,database-execute-write-many,"await db.execute_write_many(sql, params_seq, block=True)","Like execute_write() but uses the sqlite3 conn.executemany() method. This will efficiently execute the same SQL statement against each of the parameters in the params_seq iterator, for example:
await db.execute_write_many(
""insert into characters (id, name) values (?, ?)"",
[(1, ""Melanie""), (2, ""Selma""), (2, ""Viktor"")],
)","[""Internals for plugins"", ""Database class""]","[{""href"": ""https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.executemany"", ""label"": ""conn.executemany()""}]"
internals:database-execute-write-script,internals,database-execute-write-script,"await db.execute_write_script(sql, block=True)","Like execute_write() but can be used to send multiple SQL statements in a single string separated by semicolons, using the sqlite3 conn.executescript() method.","[""Internals for plugins"", ""Database class""]","[{""href"": ""https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.executescript"", ""label"": ""conn.executescript()""}]"
plugin_hooks:plugin-hook-render-cell,plugin_hooks,plugin-hook-render-cell,"render_cell(row, value, column, table, database, datasette)","Lets you customize the display of values within table cells in the HTML table view.
row - sqlite.Row
The SQLite row object that the value being rendered is part of
value - string, integer, float, bytes or None
The value that was loaded from the database
column - string
The name of the column being rendered
table - string or None
The name of the table - or None if this is a custom SQL query
database - string
The name of the database
datasette - Datasette class
You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) , or to execute SQL queries.
If your hook returns None , it will be ignored. Use this to indicate that your hook is not able to custom render this particular value.
If the hook returns a string, that string will be rendered in the table cell.
If you want to return HTML markup you can do so by returning a jinja2.Markup object.
You can also return an awaitable function which returns a value.
Datasette will loop through all available render_cell hooks and display the value returned by the first one that does not return None .
Here is an example of a custom render_cell() plugin which looks for values that are a JSON string matching the following format:
{""href"": ""https://www.example.com/"", ""label"": ""Name""}
If the value matches that pattern, the plugin returns an HTML link element:
from datasette import hookimpl
import markupsafe
import json
@hookimpl
def render_cell(value):
# Render {""href"": ""..."", ""label"": ""...""} as link
if not isinstance(value, str):
return None
stripped = value.strip()
if not (
stripped.startswith(""{"") and stripped.endswith(""}"")
):
return None
try:
data = json.loads(value)
except ValueError:
return None
if not isinstance(data, dict):
return None
if set(data.keys()) != {""href"", ""label""}:
return None
href = data[""href""]
if not (
href.startswith(""/"")
or href.startswith(""http://"")
or href.startswith(""https://"")
):
return None
return markupsafe.Markup(
'{label}'.format(
href=markupsafe.escape(data[""href""]),
label=markupsafe.escape(data[""label""] or """")
or "" "",
)
)
Examples: datasette-render-binary , datasette-render-markdown , datasette-json-html","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-render-binary"", ""label"": ""datasette-render-binary""}, {""href"": ""https://datasette.io/plugins/datasette-render-markdown"", ""label"": ""datasette-render-markdown""}, {""href"": ""https://datasette.io/plugins/datasette-json-html"", ""label"": ""datasette-json-html""}]"
changelog:javascript-modules,changelog,javascript-modules,JavaScript modules,"JavaScript modules were introduced in ECMAScript 2015 and provide native browser support for the import and export keywords.
To use modules, JavaScript needs to be included in element:
@hookimpl
def extra_body_script():
return {
""module"": True,
""script"": ""console.log('Your JavaScript goes here...')"",
}
This will add the following to the end of your page:
Example: datasette-cluster-map","[""Plugin hooks""]","[{""href"": ""https://datasette.io/plugins/datasette-cluster-map"", ""label"": ""datasette-cluster-map""}]"
internals:database-execute-fn,internals,database-execute-fn,await db.execute_fn(fn),"Executes a given callback function against a read-only database connection running in a thread. The function will be passed a SQLite connection, and the return value from the function will be returned by the await .
Example usage:
def get_version(conn):
return conn.execute(
""select sqlite_version()""
).fetchall()[0][0]
version = await db.execute_fn(get_version)","[""Internals for plugins"", ""Database class""]",[]
internals:database-execute,internals,database-execute,"await db.execute(sql, ...)","Executes a SQL query against the database and returns the resulting rows (see Results ).
sql - string (required)
The SQL query to execute. This can include ? or :named parameters.
params - list or dict
A list or dictionary of values to use for the parameters. List for ? , dictionary for :named .
truncate - boolean
Should the rows returned by the query be truncated at the maximum page size? Defaults to True , set this to False to disable truncation.
custom_time_limit - integer ms
A custom time limit for this query. This can be set to a lower value than the Datasette configured default. If a query takes longer than this it will be terminated early and raise a dataette.database.QueryInterrupted exception.
page_size - integer
Set a custom page size for truncation, over-riding the configured Datasette default.
log_sql_errors - boolean
Should any SQL errors be logged to the console in addition to being raised as an error? Defaults to True .","[""Internals for plugins"", ""Database class""]",[]
changelog:id15,changelog,id15,Documentation,"Examples in the documentation now include a copy-to-clipboard button. ( #1748 )
Documentation now uses the Furo Sphinx theme. ( #1746 )
Code examples in the documentation are now all formatted using Black. ( #1718 )
Request.fake() method is now documented, see Request object .
New documentation for plugin authors: Registering a plugin for the duration of a test . ( #903 )","[""Changelog"", ""0.62 (2022-08-14)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1748"", ""label"": ""#1748""}, {""href"": ""https://github.com/pradyunsg/furo"", ""label"": ""Furo""}, {""href"": ""https://github.com/simonw/datasette/issues/1746"", ""label"": ""#1746""}, {""href"": ""https://github.com/simonw/datasette/issues/1718"", ""label"": ""#1718""}, {""href"": ""https://github.com/simonw/datasette/issues/903"", ""label"": ""#903""}]"
pages:rowview,pages,rowview,Row,"Every row in every Datasette table has its own URL. This means individual records can be linked to directly.
Table cells with extremely long text contents are truncated on the table view according to the truncate_cells_html setting. If a cell has been truncated the full length version of that cell will be available on the row page.
Rows which are the targets of foreign key references from other tables will show a link to a filtered search for all records that reference that row. Here's an example from the Registers of Members Interests database:
../people/uk.org.publicwhip%2Fperson%2F10001
Note that this URL includes the encoded primary key of the record.
Here's that same page as JSON:
../people/uk.org.publicwhip%2Fperson%2F10001.json","[""Pages and API endpoints""]","[{""href"": ""https://register-of-members-interests.datasettes.com/regmem/people/uk.org.publicwhip%2Fperson%2F10001"", ""label"": ""../people/uk.org.publicwhip%2Fperson%2F10001""}, {""href"": ""https://register-of-members-interests.datasettes.com/regmem/people/uk.org.publicwhip%2Fperson%2F10001.json"", ""label"": ""../people/uk.org.publicwhip%2Fperson%2F10001.json""}]"
custom_templates:css-classes-on-the-body,custom_templates,css-classes-on-the-body,CSS classes on the ,"Every default template includes CSS classes in the body designed to support
custom styling.
The index template (the top level page at / ) gets this:
The database template ( /dbname ) gets this:
The custom SQL template ( /dbname?sql=... ) gets this:
A canned query template ( /dbname/queryname ) gets this:
The table template ( /dbname/tablename ) gets:
The row template ( /dbname/tablename/rowid ) gets:
The db-x and table-x classes use the database or table names themselves if
they are valid CSS identifiers. If they aren't, we strip any invalid
characters out and append a 6 character md5 digest of the original name, in
order to ensure that multiple tables which resolve to the same stripped
character version still have different CSS classes.
Some examples:
""simple"" => ""simple""
""MixedCase"" => ""MixedCase""
""-no-leading-hyphens"" => ""no-leading-hyphens-65bea6""
""_no-leading-underscores"" => ""no-leading-underscores-b921bc""
""no spaces"" => ""no-spaces-7088d7""
""-"" => ""336d5e""
""no $ characters"" => ""no--characters-59e024""
and
elements also get custom CSS classes reflecting the
database column they are representing, for example:
","[""Custom pages and templates"", ""Custom CSS and JavaScript""]",[]
changelog:latest-datasette-io,changelog,latest-datasette-io,latest.datasette.io,"Every commit to Datasette master is now automatically deployed by Travis CI to
https://latest.datasette.io/ - ensuring there is always a live demo of the
latest version of the software.
The demo uses the fixtures from our
unit tests, ensuring it demonstrates the same range of functionality that is
covered by the tests.
You can see how the deployment mechanism works in our .travis.yml file.","[""Changelog"", ""0.23 (2018-06-18)""]","[{""href"": ""https://latest.datasette.io/"", ""label"": ""https://latest.datasette.io/""}, {""href"": ""https://github.com/simonw/datasette/blob/master/tests/fixtures.py"", ""label"": ""the fixtures""}, {""href"": ""https://github.com/simonw/datasette/blob/master/.travis.yml"", ""label"": "".travis.yml""}]"
json_api:json-api-special,json_api,json-api-special,Special JSON arguments,"Every Datasette endpoint that can return JSON also accepts the following
query string arguments:
?_shape=SHAPE
The shape of the JSON to return, documented above.
?_nl=on
When used with ?_shape=array produces newline-delimited JSON objects.
?_json=COLUMN1&_json=COLUMN2
If any of your SQLite columns contain JSON values, you can use one or more
_json= parameters to request that those columns be returned as regular
JSON. Without this argument those columns will be returned as JSON objects
that have been double-encoded into a JSON string value.
Compare this query without the argument to this query using the argument
?_json_infinity=on
If your data contains infinity or -infinity values, Datasette will replace
them with None when returning them as JSON. If you pass _json_infinity=1
Datasette will instead return them as Infinity or -Infinity which is
invalid JSON but can be processed by some custom JSON parsers.
?_timelimit=MS
Sets a custom time limit for the query in ms. You can use this for optimistic
queries where you would like Datasette to give up if the query takes too
long, for example if you want to implement autocomplete search but only if
it can be executed in less than 10ms.
?_ttl=SECONDS
For how many seconds should this response be cached by HTTP proxies? Use
?_ttl=0 to disable HTTP caching entirely for this request.
?_trace=1
Turns on tracing for this page: SQL queries executed during the request will
be gathered and included in the response, either in a new ""_traces"" key
for JSON responses or at the bottom of the page if the response is in HTML.
The structure of the data returned here should be considered highly unstable
and very likely to change.
Only available if the trace_debug setting is enabled.","[""JSON API""]","[{""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight.json?sql=select+%27{%22this+is%22%3A+%22a+json+object%22}%27+as+d&_shape=array"", ""label"": ""this query without the argument""}, {""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight.json?sql=select+%27{%22this+is%22%3A+%22a+json+object%22}%27+as+d&_shape=array&_json=d"", ""label"": ""this query using the argument""}]"
settings:setting-allow-csv-stream,settings,setting-allow-csv-stream,allow_csv_stream,"Enables the CSV export feature where an entire table
(potentially hundreds of thousands of rows) can be exported as a single CSV
file. This is turned on by default - you can turn it off like this:
datasette mydatabase.db --setting allow_csv_stream off","[""Settings"", ""Settings""]",[]
pages:databaseview,pages,databaseview,Database,"Each database has a page listing the tables, views and canned queries available for that database. If the execute-sql permission is enabled (it's on by default) there will also be an interface for executing arbitrary SQL select queries against the data.
Examples:
fivethirtyeight.datasettes.com/fivethirtyeight
global-power-plants.datasettes.com/global-power-plants
The JSON version of this page provides programmatic access to the underlying data:
fivethirtyeight.datasettes.com/fivethirtyeight.json
global-power-plants.datasettes.com/global-power-plants.json","[""Pages and API endpoints""]","[{""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight"", ""label"": ""fivethirtyeight.datasettes.com/fivethirtyeight""}, {""href"": ""https://global-power-plants.datasettes.com/global-power-plants"", ""label"": ""global-power-plants.datasettes.com/global-power-plants""}, {""href"": ""https://fivethirtyeight.datasettes.com/fivethirtyeight.json"", ""label"": ""fivethirtyeight.datasettes.com/fivethirtyeight.json""}, {""href"": ""https://global-power-plants.datasettes.com/global-power-plants.json"", ""label"": ""global-power-plants.datasettes.com/global-power-plants.json""}]"
changelog:id3,changelog,id3,0.64.5 (2023-10-08),"Dropped dependency on click-default-group-wheel , which could cause a dependency conflict. ( #2197 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/2197"", ""label"": ""#2197""}]"
changelog:bug-fixes,changelog,bug-fixes,Bug fixes,"Don't show the facet option in the cog menu if faceting is not allowed. ( #1683 )
?_sort and ?_sort_desc now work if the column that is being sorted has been excluded from the query using ?_col= or ?_nocol= . ( #1773 )
Fixed bug where ?_sort_desc was duplicated in the URL every time the Apply button was clicked. ( #1738 )","[""Changelog"", ""0.62 (2022-08-14)""]","[{""href"": ""https://github.com/simonw/datasette/issues/1683"", ""label"": ""#1683""}, {""href"": ""https://github.com/simonw/datasette/issues/1773"", ""label"": ""#1773""}, {""href"": ""https://github.com/simonw/datasette/issues/1738"", ""label"": ""#1738""}]"
changelog:id65,changelog,id65,0.37.1 (2020-03-02),"Don't attempt to count table rows to display on the index page for databases > 100MB. ( #688 )
Print exceptions if they occur in the write thread rather than silently swallowing them.
Handle the possibility of scope[""path""] being a string rather than bytes
Better documentation for the extra_template_vars(template, database, table, columns, view_name, request, datasette) plugin hook.","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/688"", ""label"": ""#688""}]"
ecosystem:dogsheep,ecosystem,dogsheep,Dogsheep,Dogsheep is a collection of tools for personal analytics using SQLite and Datasette. The project provides tools like github-to-sqlite and twitter-to-sqlite that can import data from different sources in order to create a personal data warehouse. Personal Data Warehouses: Reclaiming Your Data is a talk that explains Dogsheep and demonstrates it in action.,"[""The Datasette Ecosystem""]","[{""href"": ""https://dogsheep.github.io/"", ""label"": ""Dogsheep""}, {""href"": ""https://datasette.io/tools/github-to-sqlite"", ""label"": ""github-to-sqlite""}, {""href"": ""https://datasette.io/tools/twitter-to-sqlite"", ""label"": ""twitter-to-sqlite""}, {""href"": ""https://simonwillison.net/2020/Nov/14/personal-data-warehouses/"", ""label"": ""Personal Data Warehouses: Reclaiming Your Data""}]"
changelog:id40,changelog,id40,0.52.1 (2020-11-29),"Documentation on Testing plugins now recommends using datasette.client . ( #1102 )
Fix bug where compound foreign keys produced broken links. ( #1098 )
datasette --load-module=spatialite now also checks for /usr/local/lib/mod_spatialite.so . Thanks, Dan Peterson. ( #1114 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/1102"", ""label"": ""#1102""}, {""href"": ""https://github.com/simonw/datasette/issues/1098"", ""label"": ""#1098""}, {""href"": ""https://github.com/simonw/datasette/issues/1114"", ""label"": ""#1114""}]"
changelog:id7,changelog,id7,0.64.1 (2023-01-11),"Documentation now links to a current source of information for installing Python 3. ( #1987 )
Incorrectly calling the Datasette constructor using Datasette(""path/to/data.db"") instead of Datasette([""path/to/data.db""]) now returns a useful error message. ( #1985 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/1987"", ""label"": ""#1987""}, {""href"": ""https://github.com/simonw/datasette/issues/1985"", ""label"": ""#1985""}]"
changelog:id31,changelog,id31,0.56 (2021-03-28),"Documentation improvements, bug fixes and support for SpatiaLite 5.
The SQL editor can now be resized by dragging a handle. ( #1236 )
Fixed a bug with JSON faceting and the __arraycontains filter caused by tables with spaces in their names. ( #1239 )
Upgraded httpx dependency. ( #1005 )
JSON faceting is now suggested even if a column contains blank strings. ( #1246 )
New datasette.add_memory_database() method. ( #1247 )
The Response.asgi_send() method is now documented. ( #1266 )
The official Datasette Docker image now bundles SpatiaLite version 5. ( #1278 )
Fixed a no such table: pragma_database_list bug when running Datasette against SQLite versions prior to SQLite 3.16.0. ( #1276 )
HTML lists displayed in table cells are now styled correctly. Thanks, Bob Whitelock. ( #1141 , #1252 )
Configuration directory mode now correctly serves immutable databases that are listed in inspect-data.json . Thanks Campbell Allen and Frankie Robertson. ( #1031 , #1229 )","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/1236"", ""label"": ""#1236""}, {""href"": ""https://github.com/simonw/datasette/issues/1239"", ""label"": ""#1239""}, {""href"": ""https://github.com/simonw/datasette/issues/1005"", ""label"": ""#1005""}, {""href"": ""https://github.com/simonw/datasette/issues/1246"", ""label"": ""#1246""}, {""href"": ""https://github.com/simonw/datasette/issues/1247"", ""label"": ""#1247""}, {""href"": ""https://github.com/simonw/datasette/issues/1266"", ""label"": ""#1266""}, {""href"": ""https://github.com/simonw/datasette/issues/1278"", ""label"": ""#1278""}, {""href"": ""https://github.com/simonw/datasette/issues/1276"", ""label"": ""#1276""}, {""href"": ""https://github.com/simonw/datasette/issues/1141"", ""label"": ""#1141""}, {""href"": ""https://github.com/simonw/datasette/pull/1252"", ""label"": ""#1252""}, {""href"": ""https://github.com/simonw/datasette/pull/1031"", ""label"": ""#1031""}, {""href"": ""https://github.com/simonw/datasette/pull/1229"", ""label"": ""#1229""}]"
changelog:id89,changelog,id89,0.25.1 (2018-11-04),"Documentation improvements plus a fix for publishing to Zeit Now.
datasette publish now now uses Zeit's v1 platform, to work around the new 100MB image limit. Thanks, @slygent - closes #366 .","[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/issues/366"", ""label"": ""#366""}]"
changelog:id73,changelog,id73,0.31.1 (2019-11-12),Deployments created using datasette publish now use python:3.8 base Docker image ( #629 ),"[""Changelog""]","[{""href"": ""https://github.com/simonw/datasette/pull/629"", ""label"": ""#629""}]"
settings:setting-default-cache-ttl,settings,setting-default-cache-ttl,default_cache_ttl,"Default HTTP caching max-age header in seconds, used for Cache-Control: max-age=X . Can be over-ridden on a per-request basis using the ?_ttl= query string parameter. Set this to 0 to disable HTTP caching entirely. Defaults to 5 seconds.
datasette mydatabase.db --setting default_cache_ttl 60","[""Settings"", ""Settings""]",[]
facets:suggested-facets,facets,suggested-facets,Suggested facets,"Datasette's table UI will suggest facets for the user to apply, based on the following criteria:
For the currently filtered data are there any columns which, if applied as a facet...
Will return 30 or less unique options
Will return more than one unique option
Will return less unique options than the total number of filtered rows
And the query used to evaluate this criteria can be completed in under 50ms
That last point is particularly important: Datasette runs a query for every column that is displayed on a page, which could get expensive - so to avoid slow load times it sets a time limit of just 50ms for each of those queries.
This means suggested facets are unlikely to appear for tables with millions of records in them.","[""Facets""]",[]
plugins:id1,plugins,id1,Plugins,"Datasette's plugin system allows additional features to be implemented as Python
code (or front-end JavaScript) which can be wrapped up in a separate Python
package. The underlying mechanism uses pluggy .
See the Datasette plugins directory for a list of existing plugins, or take a look at the
datasette-plugin topic on GitHub.
Things you can do with plugins include:
Add visualizations to Datasette, for example
datasette-cluster-map and
datasette-vega .
Make new custom SQL functions available for use within Datasette, for example
datasette-haversine and
datasette-jellyfish .
Define custom output formats with custom extensions, for example datasette-atom and
datasette-ics .
Add template functions that can be called within your Jinja custom templates,
for example datasette-render-markdown .
Customize how database values are rendered in the Datasette interface, for example
datasette-render-binary and
datasette-pretty-json .
Customize how Datasette's authentication and permissions systems work, for example datasette-auth-tokens and
datasette-permissions-sql .",[],"[{""href"": ""https://pluggy.readthedocs.io/"", ""label"": ""pluggy""}, {""href"": ""https://datasette.io/plugins"", ""label"": ""Datasette plugins directory""}, {""href"": ""https://github.com/topics/datasette-plugin"", ""label"": ""datasette-plugin""}, {""href"": ""https://github.com/simonw/datasette-cluster-map"", ""label"": ""datasette-cluster-map""}, {""href"": ""https://github.com/simonw/datasette-vega"", ""label"": ""datasette-vega""}, {""href"": ""https://github.com/simonw/datasette-haversine"", ""label"": ""datasette-haversine""}, {""href"": ""https://github.com/simonw/datasette-jellyfish"", ""label"": ""datasette-jellyfish""}, {""href"": ""https://github.com/simonw/datasette-atom"", ""label"": ""datasette-atom""}, {""href"": ""https://github.com/simonw/datasette-ics"", ""label"": ""datasette-ics""}, {""href"": ""https://github.com/simonw/datasette-render-markdown#markdown-in-templates"", ""label"": ""datasette-render-markdown""}, {""href"": ""https://github.com/simonw/datasette-render-binary"", ""label"": ""datasette-render-binary""}, {""href"": ""https://github.com/simonw/datasette-pretty-json"", ""label"": ""datasette-pretty-json""}, {""href"": ""https://github.com/simonw/datasette-auth-tokens"", ""label"": ""datasette-auth-tokens""}, {""href"": ""https://github.com/simonw/datasette-permissions-sql"", ""label"": ""datasette-permissions-sql""}]"
contributing:contributing-documentation,contributing,contributing-documentation,Editing and building the documentation,"Datasette's documentation lives in the docs/ directory and is deployed automatically using Read The Docs .
The documentation is written using reStructuredText. You may find this article on The subset of reStructuredText worth committing to memory useful.
You can build it locally by installing sphinx and sphinx_rtd_theme in your Datasette development environment and then running make html directly in the docs/ directory:
# You may first need to activate your virtual environment:
source venv/bin/activate
# Install the dependencies needed to build the docs
pip install -e .[docs]
# Now build the docs
cd docs/
make html
This will create the HTML version of the documentation in docs/_build/html . You can open it in your browser like so:
open _build/html/index.html
Any time you make changes to a .rst file you can re-run make html to update the built documents, then refresh them in your browser.
For added productivity, you can use use sphinx-autobuild to run Sphinx in auto-build mode. This will run a local webserver serving the docs that automatically rebuilds them and refreshes the page any time you hit save in your editor.
sphinx-autobuild will have been installed when you ran pip install -e .[docs] . In your docs/ directory you can start the server by running the following:
make livehtml
Now browse to http://localhost:8000/ to view the documentation. Any edits you make should be instantly reflected in your browser.","[""Contributing""]","[{""href"": ""https://readthedocs.org/"", ""label"": ""Read The Docs""}, {""href"": ""https://simonwillison.net/2018/Aug/25/restructuredtext/"", ""label"": ""The subset of reStructuredText worth committing to memory""}, {""href"": ""https://pypi.org/project/sphinx-autobuild/"", ""label"": ""sphinx-autobuild""}]"
sql_queries:id2,sql_queries,id2,Pagination,"Datasette's default table pagination is designed to be extremely efficient. SQL OFFSET/LIMIT pagination can have a significant performance penalty once you get into multiple thousands of rows, as each page still requires the database to scan through every preceding row to find the correct offset.
When paginating through tables, Datasette instead orders the rows in the table by their primary key and performs a WHERE clause against the last seen primary key for the previous page. For example:
select rowid, * from Tree_List where rowid > 200 order by rowid limit 101
This represents page three for this particular table, with a page size of 100.
Note that we request 101 items in the limit clause rather than 100. This allows us to detect if we are on the last page of the results: if the query returns less than 101 rows we know we have reached the end of the pagination set. Datasette will only return the first 100 rows - the 101st is used purely to detect if there should be another page.
Since the where clause acts against the index on the primary key, the query is extremely fast even for records that are a long way into the overall pagination set.","[""Running SQL queries""]",[]
metadata:label-columns,metadata,label-columns,Specifying the label column for a table,"Datasette's HTML interface attempts to display foreign key references as
labelled hyperlinks. By default, it looks for referenced tables that only have
two columns: a primary key column and one other. It assumes that the second
column should be used as the link label.
If your table has more than two columns you can specify which column should be
used for the link label with the label_column property:
{
""databases"": {
""database1"": {
""tables"": {
""example_table"": {
""label_column"": ""title""
}
}
}
}
}","[""Metadata""]",[]
changelog:new-configuration-settings,changelog,new-configuration-settings,New configuration settings,"Datasette's Settings now also supports boolean settings. A number of new
configuration options have been added:
num_sql_threads - the number of threads used to execute SQLite queries. Defaults to 3.
allow_facet - enable or disable custom Facets using the _facet= parameter. Defaults to on.
suggest_facets - should Datasette suggest facets? Defaults to on.
allow_download - should users be allowed to download the entire SQLite database? Defaults to on.
allow_sql - should users be allowed to execute custom SQL queries? Defaults to on.
default_cache_ttl - Default HTTP caching max-age header in seconds. Defaults to 365 days - caching can be disabled entirely by settings this to 0.
cache_size_kb - Set the amount of memory SQLite uses for its per-connection cache , in KB.
allow_csv_stream - allow users to stream entire result sets as a single CSV file. Defaults to on.
max_csv_mb - maximum size of a returned CSV file in MB. Defaults to 100MB, set to 0 to disable this limit.","[""Changelog"", ""0.23 (2018-06-18)""]","[{""href"": ""https://www.sqlite.org/pragma.html#pragma_cache_size"", ""label"": ""per-connection cache""}]"
changelog:writable-canned-queries,changelog,writable-canned-queries,Writable canned queries,"Datasette's Canned queries feature lets you define SQL queries in metadata.json which can then be executed by users visiting a specific URL. https://latest.datasette.io/fixtures/neighborhood_search for example.
Canned queries were previously restricted to SELECT , but Datasette 0.44 introduces the ability for canned queries to execute INSERT or UPDATE queries as well, using the new ""write"": true property ( #800 ):
{
""databases"": {
""dogs"": {
""queries"": {
""add_name"": {
""sql"": ""INSERT INTO names (name) VALUES (:name)"",
""write"": true
}
}
}
}
}
See Writable canned queries for more details.","[""Changelog"", ""0.44 (2020-06-11)""]","[{""href"": ""https://latest.datasette.io/fixtures/neighborhood_search"", ""label"": ""https://latest.datasette.io/fixtures/neighborhood_search""}, {""href"": ""https://github.com/simonw/datasette/issues/800"", ""label"": ""#800""}]"
contributing:contributing-formatting,contributing,contributing-formatting,Code formatting,"Datasette uses opinionated code formatters: Black for Python and Prettier for JavaScript.
These formatters are enforced by Datasette's continuous integration: if a commit includes Python or JavaScript code that does not match the style enforced by those tools, the tests will fail.
When developing locally, you can verify and correct the formatting of your code using these tools.","[""Contributing""]","[{""href"": ""https://github.com/psf/black"", ""label"": ""Black""}, {""href"": ""https://prettier.io/"", ""label"": ""Prettier""}]"
settings:setting-secret,settings,setting-secret,Configuring the secret,"Datasette uses a secret string to sign secure values such as cookies.
If you do not provide a secret, Datasette will create one when it starts up. This secret will reset every time the Datasette server restarts though, so things like authentication cookies will not stay valid between restarts.
You can pass a secret to Datasette in two ways: with the --secret command-line option or by setting a DATASETTE_SECRET environment variable.
$ datasette mydb.db --secret=SECRET_VALUE_HERE
Or:
$ export DATASETTE_SECRET=SECRET_VALUE_HERE
$ datasette mydb.db
One way to generate a secure random secret is to use Python like this:
$ python3 -c 'import secrets; print(secrets.token_hex(32))'
cdb19e94283a20f9d42cca50c5a4871c0aa07392db308755d60a1a5b9bb0fa52
Plugin authors make use of this signing mechanism in their plugins using .sign(value, namespace=""default"") and .unsign(value, namespace=""default"") .","[""Settings""]",[]
internals:internals-tilde-encoding,internals,internals-tilde-encoding,Tilde encoding,"Datasette uses a custom encoding scheme in some places, called tilde encoding . This is primarily used for table names and row primary keys, to avoid any confusion between / characters in those values and the Datasette URLs that reference them.
Tilde encoding uses the same algorithm as URL percent-encoding , but with the ~ tilde character used in place of % .
Any character other than ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789_- will be replaced by the numeric equivalent preceded by a tilde. For example:
/ becomes ~2F
. becomes ~2E
% becomes ~25
~ becomes ~7E
Space becomes +
polls/2022.primary becomes polls~2F2022~2Eprimary
Note that the space character is a special case: it will be replaced with a + symbol.
datasette.utils. tilde_encode s : str str
Returns tilde-encoded string - for example /foo/bar -> ~2Ffoo~2Fbar
datasette.utils. tilde_decode s : str str
Decodes a tilde-encoded string, so ~2Ffoo~2Fbar -> /foo/bar","[""Internals for plugins"", ""The datasette.utils module""]","[{""href"": ""https://developer.mozilla.org/en-US/docs/Glossary/percent-encoding"", ""label"": ""URL percent-encoding""}]"
internals:internals-csrf,internals,internals-csrf,CSRF protection,"Datasette uses asgi-csrf to guard against CSRF attacks on form POST submissions. Users receive a ds_csrftoken cookie which is compared against the csrftoken form field (or x-csrftoken HTTP header) for every incoming request.
If your plugin implements a