home / github / issue_comments

Menu
  • GraphQL API

issue_comments: 590606825

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions issue performed_via_github_app
https://github.com/simonw/datasette/pull/683#issuecomment-590606825 https://api.github.com/repos/simonw/datasette/issues/683 590606825 MDEyOklzc3VlQ29tbWVudDU5MDYwNjgyNQ== 9599 2020-02-24T23:47:38Z 2020-02-24T23:47:38Z OWNER Another demo plugin: `delete_table.py` ```python from datasette import hookimpl from datasette.utils import escape_sqlite from starlette.responses import HTMLResponse from starlette.endpoints import HTTPEndpoint class DeleteTableApp(HTTPEndpoint): def __init__(self, scope, receive, send, datasette): self.datasette = datasette super().__init__(scope, receive, send) async def post(self, request): formdata = await request.form() database = formdata["database"] db = self.datasette.databases[database] await db.execute_write("drop table {}".format(escape_sqlite(formdata["table"]))) return HTMLResponse("Table has been deleted.") @hookimpl def asgi_wrapper(datasette): def wrap_with_asgi_auth(app): async def wrapped_app(scope, recieve, send): if scope["path"] == "/-/delete-table": await DeleteTableApp(scope, recieve, send, datasette) else: await app(scope, recieve, send) return wrapped_app return wrap_with_asgi_auth ``` Then I saved this as `table.html` in the `write-templates/` directory: ```html+django {% extends "default:table.html" %} {% block content %} <form action="/-/delete-table" method="POST"> <p> <input type="hidden" name="database" value="{{ database }}"> <input type="hidden" name="table" value="{{ table }}"> <input type="submit" value="Delete this table"> </p> </form> {{ super() }} {% endblock %} ``` (Needs CSRF protection added) I ran Datasette like this: $ datasette --plugins-dir=write-plugins/ data.db --template-dir=write-templates/ Result: I can delete tables! <img width="596" alt="data__everything__30_132_rows_-_Mozilla_Firefox" src="https://user-images.githubusercontent.com/9599/75201302-f9cec580-571c-11ea-9c55-67a49e68ec0c.png"> {"total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0} 570101428  
Powered by Datasette · Queries took 1.167ms