CLI 参考

datasette CLI 工具提供了许多命令。

运行 datasette 时不指定命令,将运行默认命令 datasette serve。有关该命令的完整选项列表,请参阅datasette serve

datasette --help

运行 datasette --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...

使用 register_commands(cli) 钩子添加的附加命令也将在此列出。

datasette serve

此命令启动在您的机器上运行的 Datasette Web 应用程序。

datasette serve mydatabase.db

或者,由于这是默认命令,您可以运行此命令代替:

datasette mydatabase.db

启动后,您可以通过 http://localhost:8001 访问它。

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.

datasette --get

datasette serve (或仅 datasette) 的 --get 选项指定 Datasette 中页面的路径,并使 Datasette 输出该路径的内容而不启动 Web 服务器。

这意味着 Datasette 的所有功能都可以直接从命令行访问。

例如:

$ 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"
    ]
  }
}

如果请求成功,退出码将为 0;如果请求产生了除 200 之外的 HTTP 状态码(例如 404 或 500 错误),退出码将为 1。

这允许您在持续集成环境(例如 GitHub Actions)中使用 datasette --get / 对 Datasette 应用程序运行测试。

datasette serve --help-settings

此命令输出所有可用的 Datasette 设置

这些设置可以使用 datasette serve --setting name value 传递给 datasette serve

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=/)

datasette plugins

输出 JSON,显示所有当前安装的插件、它们的版本、它们是否包含静态文件或模板以及它们使用了哪些插件钩子

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.

示例输出:

[
    {
        "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"
        ]
    }
]

datasette install

安装新的 Datasette 插件。此命令的作用类似于 pip install,但确保您的插件将安装到与 Datasette 相同的环境中。

此命令:

datasette install datasette-cluster-map

将安装 datasette-cluster-map 插件。

Usage: datasette install [OPTIONS] PACKAGES...

  Install plugins and packages from PyPI into the same environment as Datasette

Options:
  -U, --upgrade  Upgrade packages to latest version
  --help         Show this message and exit.

datasette uninstall

卸载一个或多个插件。

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.

datasette publish

显示使用 Datasette 发布数据的可用部署目标的列表。

使用 publish_subcommand(publish) 钩子添加的附加部署目标也可以在此处列出。

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

datasette publish cloudrun

请参阅 发布到 Google Cloud Run

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 <TEXT TEXT TEXT>...
                                  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.

datasette publish heroku

请参阅 发布到 Heroku

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 <TEXT TEXT TEXT>...
                                  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.

datasette package

将 SQLite 文件打包到 Datasette Docker 容器中,请参阅 datasette package

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.

datasette inspect

输出一个或多个 SQLite 数据库文件的内省数据(JSON 格式)。

如果您正在打开一个不可变数据库,您可以将此文件传递给 --inspect-data 选项,通过允许 Datasette 在启动时跳过对数据库进行行计数,来提高其性能。

datasette inspect mydatabase.db > inspect-data.json
datasette serve -i mydatabase.db --inspect-file inspect-data.json

此性能优化由一些 datasette publish 命令自动使用。您不太可能需要手动应用此优化。

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.