跳转到主要内容

SQLite数据库的即时JSON API

项目描述

Datasette

PyPI Travis CI Documentation Status License

SQLite数据库的即时JSON API

Datasette为任何SQLite数据库提供即时、只读的JSON API。它还提供工具,可以将数据库打包成Docker容器,并将该容器部署到诸如Zeit Now等托管提供商。

有CSV数据?使用csvs-to-sqlite将其转换为SQLite,然后使用Datasette发布它们。或者尝试Datasette Publish,这是一个Web应用程序,允许您上传CSV数据并使用Datasette部署它,而无需安装任何软件。

文档:http://datasette.readthedocs.io/ 示例:https://github.com/simonw/datasette/wiki/Datasettes

新闻

  • 2018年5月23日:Datasette 0.22.1错误修复以及我们现在使用versioneer
  • 2018年5月20日:Datasette 0.22:Datasette Facets
  • 2018年5月5日:Datasette 0.21:新的_shape=,新的_size=,列内搜索
  • 2018年4月25日:[使用SQL和Datasette探索英国成员利益登记册](https://simonwillison.net/2018/Apr/25/register-members-interests/) - 一篇教程,介绍了如何构建register-of-members-interests.datasettes.com源代码在此
  • 2018年4月20日:[Datasette插件,以及构建集群地图可视化](https://simonwillison.net/2018/Apr/20/datasette-plugins/) - 介绍Datasette的新插件系统和datasette-cluster-map,一个用于在地图上可视化数据的插件
  • 2018年4月20日:[Datasette 0.20:插件静态资源和模板](https://github.com/simonw/datasette/releases/tag/0.20)
  • 2018年4月16日:[Datasette 0.19:插件预览](https://github.com/simonw/datasette/releases/tag/0.19)
  • 2018年4月14日:[Datasette 0.18:单位](https://github.com/simonw/datasette/releases/tag/0.18)
  • 2018年4月9日:[Datasette 0.15:按列排序](https://github.com/simonw/datasette/releases/tag/0.15)
  • 2018年3月28日:[巴尔的摩太阳报公共薪酬记录](https://simonwillison.net/2018/Mar/28/datasette-in-the-wild/) - 一个由Datasette提供动力的巴尔的摩太阳报数据新闻项目 - 源代码在此处
  • 2018年3月27日:[云优先:使用容器快速部署web应用](https://wwwf.imperial.ac.uk/blog/research-software-engineering/2018/03/27/cloud-first-rapid-webapp-deployment-using-containers/) - 伦敦帝国理工学院研究软件工程团队关于使用微软Azure部署Datasette的教程
  • 2018年1月28日:[使用Datasette分析我的Twitter粉丝](https://simonwillison.net/2018/Jan/28/analyzing-my-twitter-followers/) - 一篇教程,介绍如何使用Datasette分析从Twitter API抓取的粉丝数据
  • 2018年1月17日:[Datasette Publish:将CSV文件作为在线数据库发布的web应用](https://simonwillison.net/2018/Jan/17/datasette-publish/)
  • 2017年12月12日:[使用SpatiaLite、OpenStreetMap和Datasette构建位置到时区API](https://simonwillison.net/2017/Dec/12/building-a-location-time-zone-api/)
  • 2017年12月9日:[Datasette 0.14:定制版](https://github.com/simonw/datasette/releases/tag/0.14)
  • 2017年11月25日:[Datasette新功能:过滤器、外键和搜索](https://simonwillison.net/2017/Nov/25/new-in-datasette/)
  • 2017年11月13日:[Datasette:立即创建和发布SQLite数据库的API](https://simonwillison.net/2017/Nov/13/datasette/)

安装

pip3 install datasette

Datasette需要Python 3.5或更高版本。

基本使用

datasette serve path/to/database.db

这将启动一个在8001端口的web服务器 - 访问http://localhost:8001/来访问web界面。

serve是默认子命令,如果你愿意可以省略。

使用OS X上的Chrome?你可以像这样运行datasette对浏览器历史进行操作

 datasette ~/Library/Application\ Support/Google/Chrome/Default/History

现在访问http://localhost:8001/History/downloads将显示一个web界面来浏览你的下载数据

Downloads table rendered by datasette

http://localhost:8001/History/downloads.json将返回该数据作为JSON

{
    "database": "History",
    "columns": [
        "id",
        "current_path",
        "target_path",
        "start_time",
        "received_bytes",
        "total_bytes",
        ...
    ],
    "table_rows_count": 576,
    "rows": [
        [
            1,
            "/Users/simonw/Downloads/DropboxInstaller.dmg",
            "/Users/simonw/Downloads/DropboxInstaller.dmg",
            13097290269022132,
            626688,
            0,
            ...
        ]
    ]
}

http://localhost:8001/History/downloads.json?_shape=objects将以更方便但效率较低的方式返回该数据作为JSON

{
    ...
    "rows": [
        {
            "start_time": 13097290269022132,
            "interrupt_reason": 0,
            "hash": "",
            "id": 1,
            "site_url": "",
            "referrer": "https://www.dropbox.com/downloading?src=index",
            ...
        }
    ]
}

datasette serve选项

$ datasette serve --help
Usage: datasette serve [OPTIONS] [FILES]...

  Serve up specified SQLite database files with a web UI

Options:
  -h, --host TEXT              host for server, defaults to 127.0.0.1
  -p, --port INTEGER           port for server, defaults to 8001
  --debug                      Enable debug mode - useful for development
  --reload                     Automatically reload if code change detected -
                               useful for development
  --cors                       Enable CORS by serving Access-Control-Allow-
                               Origin: *
  --load-extension PATH        Path to a SQLite extension to load
  --inspect-file TEXT          Path to JSON file created using "datasette
                               inspect"
  -m, --metadata FILENAME      Path to JSON file containing license/source
                               metadata
  --template-dir DIRECTORY     Path to directory containing custom templates
  --plugins-dir DIRECTORY      Path to directory containing custom plugins
  --static STATIC MOUNT        mountpoint:path-to-directory for serving static
                               files
  --config CONFIG              Set config option using configname:value
                               datasette.readthedocs.io/en/latest/config.html
  --help-config                Show available config options
  --help                       Show this message and exit.

metadata.json

如果你想在生成的datasette网站中包含许可和源信息,可以使用类似于下面的JSON文件来实现

{
    "title": "Five Thirty Eight",
    "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"
}

许可和源信息将显示在首页和页脚中。它们还将包含在API生成的JSON中。

datasette publish

如果您已配置 Zeit NowHeroku,datasette 可以通过一条命令将一个或多个 SQLite 数据库部署到互联网上。

datasette publish now database.db

或者:

datasette publish heroku database.db

这将创建一个包含 datasette 应用程序和指定 SQLite 数据库文件的 Docker 镜像。然后,它会将该镜像部署到 Zeit Now 或 Heroku,并提供访问 API 的 URL。

$ datasette publish --help
Usage: datasette publish [OPTIONS] PUBLISHER [FILES]...

  Publish specified SQLite database files to the internet along with a
  datasette API.

  Options for PUBLISHER:     * 'now' - You must have Zeit Now installed:
  https://zeit.co/now     * 'heroku' - You must have Heroku installed:
  https://cli.heroku.com/

  Example usage: datasette publish now my-database.db

Options:
  -n, --name TEXT           Application name to use when deploying to Now
                            (ignored for Heroku)
  -m, --metadata FILENAME   Path to JSON file containing metadata to publish
  --extra-options TEXT      Extra options to pass to datasette serve
  --force                   Pass --force option to now
  --branch TEXT             Install datasette from a GitHub branch e.g. master
  --template-dir DIRECTORY  Path to directory containing custom templates
  --plugins-dir DIRECTORY   Path to directory containing custom plugins
  --static STATIC MOUNT     mountpoint:path-to-directory for serving static
                            files
  --install TEXT            Additional packages (e.g. plugins) to install
  --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
  --help                    Show this message and exit.

datasette 软件包

如果您已安装 docker,可以使用 datasette package 在本地仓库中创建一个新的 Docker 镜像,包含 datasette 应用程序和所选的 SQLite 数据库。

$ datasette package --help
Usage: datasette package [OPTIONS] FILES...

  Package specified SQLite files into a new 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 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. master
  --template-dir DIRECTORY  Path to directory containing custom templates
  --plugins-dir DIRECTORY   Path to directory containing custom plugins
  --static STATIC MOUNT     mountpoint:path-to-directory for serving static
                            files
  --install TEXT            Additional packages (e.g. plugins) to install
  --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
  --help                    Show this message and exit.

发布和打包都接受一个 extra_options 参数选项,该选项将影响结果应用程序的执行方式。例如,如果您想增加特定容器的 SQL 时间限制:

datasette package parlgov.db \
    --extra-options="--config sql_time_limit_ms:2500 --config default_page_size:10"

生成的容器将以这些选项运行应用程序。

以下是打包命令的示例输出:

$ datasette package parlgov.db --extra-options="--config sql_time_limit_ms:2500"
Sending build context to Docker daemon  4.459MB
Step 1/7 : FROM python:3
 ---> 79e1dc9af1c1
Step 2/7 : COPY . /app
 ---> Using cache
 ---> cd4ec67de656
Step 3/7 : WORKDIR /app
 ---> Using cache
 ---> 139699e91621
Step 4/7 : RUN pip install datasette
 ---> Using cache
 ---> 340efa82bfd7
Step 5/7 : RUN datasette inspect parlgov.db --inspect-file inspect-data.json
 ---> Using cache
 ---> 5fddbe990314
Step 6/7 : EXPOSE 8001
 ---> Using cache
 ---> 8e83844b0fed
Step 7/7 : CMD datasette serve parlgov.db --port 8001 --inspect-file inspect-data.json --config sql_time_limit_ms:2500
 ---> Using cache
 ---> 1bd380ea8af3
Successfully built 1bd380ea8af3

现在您可以像这样运行生成的容器:

docker run -p 8081:8001 1bd380ea8af3

这将在容器内部将端口 8001 映射到主机上的端口 8081,因此您可以在 http://localhost:8081/ 访问应用程序。

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于 安装软件包 的信息。

源分布

datasette-core-0.22.1.tar.gz (183.7 kB 查看哈希值)

上传时间 源代码

构建分布

datasette_core-0.22.1-py3-none-any.whl (180.0 kB 查看哈希值)

上传时间 Python 3

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面