Giter Site home page Giter Site logo

datasette-app-support's Introduction

datasette-app-support

PyPI Changelog Tests License

Part of https://github.com/simonw/datasette-app

Installation

Install this plugin in the same environment as Datasette.

$ datasette install datasette-app-support

Using this outside of the context of Datasette.app probably won't work.

API endpoints

This plugin exposes APIs that are called by the Electron wrapper.

All plugins are protected by authentication: they need to be called with a Authorization: Bearer xxx token here the xxx matches the value of the DATASETTE_API_TOKEN environment variable.

/-/auth-app-user

POST /-/auth-app-user
{"redirect": "/-/metadata"}

If a valid Authorization header is passed, sets a signed cookie identifying the user as {"id": "admin"} and redirects them to the specified page.

/-/open-database-file

POST /-/open-database-file
{"path": "/path/to/file.db"}

Attaches a new database file to the running Datasette instance - used by the "Open Database..." menu option.

Returns HTTP 200 status with {"ok": True, "path": "/file"} if it works, 400 with an "error" JSON string message if it fails.

/-/new-empty-database-file

POST /-/new-empty-database-file
{"path": "/path/to/file.db"}

Creates a brand new empty SQLite database file at the specified path and attaches it to the Datasette instance. Used by the "Create Empty Database..." menu option.

Returns HTTP 200 status with {"ok": True, "path": "/file"} if it works, 400 with an "error" JSON string message if it fails.

/-/open-csv-file

POST /-/open-csv-file
{"path": "/path/to/file.csv"}

Imports a CSV or TSV file into the default /temporary in-memory database. Used by the "Open CSV..." menu option.

Returns HTTP 200 status with {"ok": True, "path": "/temporary/table"} if it works, 400 or 500 with an "error" JSON string message if it fails.

/-/import-csv-file

POST /-/import-csv-file
{"path": "/path/to/file.csv", "database": "database_name"}

Permanently imports a CSV or TSV file into the specified database. Used by the "Import CSV..." menu option.

Returns HTTP 200 status with {"ok": True, "path": "/database_name/table"} if it works, 400 or 500 with an "error" JSON string message if it fails.

/-/open-csv-from-url

POST /-/open-csv-from-url
{"url": "https://example.com/file.csv", "table_name": "My_suggested_table_name"}

Imports a CSV file into the default /temporary in-memory database. Used by the "Open CSV from URL..." menu option. table_name is optional - if omitted the name to use will be derived from the URL.

Returns HTTP 200 status with {"ok": True, "path": "/temporary/table"} if it works, 400 or 500 with an "error" JSON string message if it fails.

/-/dump-temporary-to-file

POST /-/dump-temporary-to-file
{"path": "/path/to/backup.db"}

Dumps the contents of the temporary in-memory database to the specified file. This is intended to be used to take a temporary backup when the Datasette server is restarted after a plugin has been installed, see datasette-app/issues/42.

Returns HTTP 200 status with {"ok": True, "path": "/path/to/backup.db"} if it works, 400 or 500 with an "error" JSON string message if it fails.

/-/restore-temporary-to-file

POST /-/restore-temporary-from-file
{"path": "/path/to/backup.db"}

Restores the temporary in-memory database to the contents of the specified file.

Returns HTTP 200 status with {"ok": True, "path": "/path/to/backup.db"} if it works, 400 or 500 with an "error" JSON string message if it fails.

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-app-support
python3 -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

datasette-app-support's People

Contributors

simonw avatar spod avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mnckapilan spod

datasette-app-support's Issues

startup() hook firing twice

I'm not sure why this is happening, but in some circumstances the startup() hook fires twice, resulting in the following:

image

This also explains the previous issue:

Tests fail due to allow_redirects

https://github.com/simonw/datasette-app-support/actions/runs/2658851433

response = await datasette.client.post(
"/-/dump-temporary-to-file",
json={"path": backup_path},
headers={"Authorization": "Bearer fake-token"},
allow_redirects=False,
)

response = await datasette.client.post(
"/-/auth-app-user",
json={"redirect": "/-/metadata"},
headers={"Authorization": "Bearer fake-token"},
allow_redirects=False,
)

Survive `_memory` KeyError

Spotted this when the app failed to start:

{
  message: '  File "/Users/simon/.datasette-app/venv/lib/python3.9/site-packages/datasette/utils/__init__.py", line 111, in await_me_maybe',
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}
{
  message: '    value = await value',
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}
{
  message: '  File "/Users/simon/.datasette-app/venv/lib/python3.9/site-packages/datasette_app_support/__init__.py", line 51, in inner',
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}
{
  message: '    datasette.remove_database("_memory")',
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}
{
  message: '  File "/Users/simon/.datasette-app/venv/lib/python3.9/site-packages/datasette/app.py", line 456, in remove_database',
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}
{
  message: '    new_databases.pop(name)',
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}
{
  message: "KeyError: '_memory'",
  type: 'stderr',
  ts: 2023-02-17T02:02:36.938Z
}

I'm not sure under what circumstances that would happen, but this fixes the problem and getting out a fast fix is a good idea:

        try:
            datasette.remove_database("_memory")
        except KeyError:
            pass

/-/auth-app-user API endpoint

Setting cookies from Electron code looks too complicated - I'll go with the simpler mechanism where every new BrowserWindow hits /-/auth-app-user?redirect=/-/plugins which accepts the Authorization: Bearer xxx token from #53 and redirects with the newly set cookie.

Originally posted by @simonw in simonw/datasette-app#37 (comment)

Sign user in as root but hide the _internal database

This needs a better solution in the longer-term, but a hack will do for the moment.

Plugins such as datasette-edit-schema have a default permission check for the "root" user. To avoid confusion, the app doesn't sign users in as "root", it uses "admin" instead.

But... https://github.com/simonw/datasette-edit-schema/blob/9b7a139c541d8b44aca77fde9745edc82c527cd7/datasette_edit_schema/__init__.py#L8-L11 currently does this:

@hookimpl
def permission_allowed(actor, action):
    if action == "edit-schema" and actor and actor.get("id") == "root":
        return True

We want that permission check to pass for the "admin" user too.

More plugin directory facet values: upgradable, default

Plugins that are not at their most recent version should show a "upgrade" button and have a installation status of "upgradable"

Default plugins cannot be uninstalled, so should have a status of "default" and not offer the forthcoming uninstall button.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.