Wykres commitów

406 Commity (1d64c9a8dac45b9a3452acf8e76dfadea2b0bc49)

Autor SHA1 Wiadomość Data
Simon Willison 669fa21a71 Include request duration in traces 2019-05-01 16:27:14 -07:00
Russ Garrett cf406c0754 New plugin hook: register_output_renderer hook (#441)
Thanks @russss!

* Add register_output_renderer hook

This changeset refactors out the JSON renderer and then adds a hook and
dispatcher system to allow custom output renderers to be registered.

The CSV output renderer is untouched because supporting streaming
renderers through this system would be significantly more complex, and
probably not worthwhile.

We can't simply allow hooks to be called at request time because we need
a list of supported file extensions when the request is being routed in
order to resolve ambiguous database/table names. So, renderers need to
be registered at startup.

I've tried to make this API independent of Sanic's request/response
objects so that this can remain stable during the switch to ASGI. I'm
using dictionaries to keep it simple and to make adding additional
options in the future easy.

Fixes #440
2019-05-01 16:01:56 -07:00
Simon Willison 11b352b4d5 Fix for Python 3.5, refs #435 2019-04-21 11:20:20 -07:00
Simon Willison 58a862cee4 ?_trace=1 now adds SQL trace info to JSON/HTML response
Also added documentation for it. Refs #435
2019-04-21 10:41:16 -07:00
Simon Willison 7d01ca34a1 Added ?_trace=1 option to trace SQL
Currently just dumps all SQL statements out on the console.
2019-04-20 22:34:34 -07:00
Simon Willison 2b11948d7c New ConnectedDatabase.mtime_ns property
I plan to use this for some clever table count caching tricks
2019-04-20 10:50:45 -07:00
Simon Willison d1075b8259 Cleaned up pylint warnings 2019-04-13 12:20:10 -07:00
Simon Willison 13ee3c222f Moved BaseView.absolute_url() to Datasette 2019-04-13 12:16:05 -07:00
Simon Willison 274ef43bb7 Moved expand_foreign_keys() from TableView to Datasette 2019-04-13 11:48:00 -07:00
Simon Willison 53bf875483 expand_foreign_keys() no longer uses inspect, refs #420 2019-04-06 19:56:07 -07:00
Simon Willison 97331f3435 sortable_columns_for_table() no longer uses inspect()
Refs #420
2019-04-06 18:58:51 -07:00
Simon Willison d8851e0011 Removed rogue print() 2019-04-06 18:54:51 -07:00
Simon Willison 7d0f668556 .resolve_db_name() and .execute() work without inspect
Refs #420
2019-03-31 16:51:52 -07:00
Simon Willison 0209a0a344 table_exists() now uses async SQL, refs #420 2019-03-31 11:02:22 -07:00
Simon Willison 82fec60481 Fix for TypeError
File "../datasette/app.py", line 138, in __init__
      self.files = files + immutables
    TypeError: can only concatenate tuple (not "list") to tuple
2019-03-17 16:36:35 -07:00
Simon Willison 47032636b5 'datasette serve -i immutable.db' option, refs #419 2019-03-17 16:25:15 -07:00
Simon Willison 6f6d0ff2b4
URL hashing is now off by default - closes #418
Prior to this commit Datasette would calculate the content hash of every
database and redirect to a URL containing that hash, like so:

    https://v0-27.datasette.io/fixtures => https://v0-27.datasette.io/fixtures-dd88475

This assumed that all databases were opened in immutable mode and were not
expected to change.

This will be changing as a result of #419 - so this commit takes the first step
in implementing that change by changing this default behaviour. Datasette will
now only redirect hash-free URLs under two circumstances:

* The new `hash_urls` config option is set to true (it defaults to false).
* The user passes `?_hash=1` in the URL
2019-03-17 15:55:04 -07:00
Simon Willison 9743e1d91b Support for :memory: databases
If you start Datasette with no files, it will connect to :memory: instead.

When starting it with files you can add --memory to also get a :memory: database.
2019-03-14 20:54:42 -07:00
Simon Willison bf6b0f918d about and about_url metadata options 2019-03-14 20:54:42 -07:00
Simon Willison 4462a5ab28 Show size of database file next to download link, closes #172 2019-02-05 20:58:29 -08:00
Simon Willison 909cc8fbdf New 'datasette plugins' command to list installed plugins 2019-01-26 12:01:16 -08:00
Simon Willison 8ce7866312 compile_options output in /-/versions, closes #396 2019-01-10 16:44:50 -08:00
Simon Willison a2bfcfc1b1
Fix some regex DeprecationWarnings (#392) 2018-12-28 18:22:27 -08:00
Simon Willison b7c6a9f9bd
extra_css_urls(template, database, table, datasette)
The extra_css_urls and extra_js_urls hooks now take additional optional
parameters.

Also refactored them out of the Datasette class and into RenderMixin.

Plus improved plugin documentation to explicitly list parameters.
2018-08-28 11:56:57 +01:00
Simon Willison 0a14a4846b
Added plugin_config() method 2018-08-28 01:35:21 -07:00
Simon Willison 1905c03364
New ds.metadata() method 2018-08-28 00:45:39 -07:00
Simon Willison 0bd41d4cb0
Do not show default plugins on /-/plugins 2018-08-28 00:36:22 -07:00
Simon Willison aae49fef3b
Import pysqlite3 if available, closes #360 (#361) 2018-08-15 17:58:56 -07:00
Simon Willison 2189be1440
Refactor to use new datasatte.config(key) method 2018-08-11 13:09:07 -07:00
Simon Willison 4ac9132240
render_cell(value) plugin hook, closes #352
New plugin hook for customizing the way cells values are rendered in HTML.

The first full example of this hook in use is https://github.com/simonw/datasette-json-html
2018-08-04 17:14:56 -07:00
Simon Willison dbbe707841
publish_subcommand hook + default plugins mechanism, used for publish heroku/now (#349)
This change introduces a new plugin hook, publish_subcommand, which can be
used to implement new subcommands for the "datasette publish" command family.

I've used this new hook to refactor out the "publish now" and "publish heroku"
implementations into separate modules. I've also added unit tests for these
two publishers, mocking the subprocess.call and subprocess.check_output
functions.

As part of this, I introduced a mechanism for loading default plugins. These
are defined in the new "default_plugins" list inside datasette/app.py

Closes #217 (Plugin support for datasette publish)
Closes #348 (Unit tests for "datasette publish")
Refs #14, #59, #102, #103, #146, #236, #347
2018-07-25 22:15:59 -07:00
Simon Willison f24b49a1a8
New force_https_urls option, refs #333 2018-07-23 08:58:29 -07:00
Simon Willison 4e82d0e64d
Removed unused imports 2018-07-23 08:39:00 -07:00
Simon Willison 6e37f091ed
Support title/description for canned queries, closes #342
Demo here: https://latest.datasette.io/fixtures/neighborhood_search
2018-07-15 19:33:30 -07:00
Simon Willison fc9b1cc136
New truncate_cells_html config for truncating cells, closes #330 2018-07-10 09:20:41 -07:00
Simon Willison 2db2ae4f21
Show custom error message if SpatiaLite needed, closes #331 2018-07-10 08:13:27 -07:00
Simon Willison 8ac71a6127
Removed rogue print statement left over from #309 2018-07-07 20:09:45 -07:00
Simon Willison d08faa8987
Fix for weird nested exception in RequestTimeout
I saw this error:

    sanic.exceptions.RequestTimeout: Request Timeout

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/Users/simonw/Dropbox/Development/datasette/venv/lib/python3.6/site-packages/sanic/handlers.py", line 82, in response
        response = handler(request=request, exception=exception)
      File "/Users/simonw/Dropbox/Development/datasette/datasette/app.py", line 512, in on_exception
        if request.path.split("?")[0].endswith(".json"):
    AttributeError: 'NoneType' object has no attribute 'path'

Strangely "if request and request.path..." did not work here, because the
Sanic Request class extends builtins.dict and hence evaluates to False if it
has no headers.
2018-07-07 19:58:11 -07:00
Simon Willison 120f20cd97
Allow "." in database filenames, closes #302 2018-06-21 08:21:09 -07:00
Simon Willison 97ae66ccab
404s ending in slash redirect to remove that slash, closes #309 2018-06-21 08:13:07 -07:00
Simon Willison fc3660cfad
Streaming mode for downloading all rows as a CSV (#315)
* table.csv?_stream=1 to download all rows - refs #266

This option causes Datasette to serve ALL rows in the table, by internally
following the _next= pagination links and serving everything out as a stream.

Also added new config option, allow_csv_stream, which can be used to disable
this feature.

* New config option max_csv_mb limiting size of CSV export
2018-06-17 20:21:02 -07:00
Simon Willison db1e6bc182
--version-note for datasette, datasette publish and datasette package
This is a relatively obscure new command-line argument that helps solve the
problem of showing accurate version information in deployed instances of
Datasette even if they were deployed directly from source code.

You can pass --version-note to datasette publish and package and it will then
in turn be passed to datasette when it starts:

    datasette --version-note=hello fixtures.db

Now if you visit /-/versions.json you will see this:

    {
        "datasette": {
            "note": "hello",
            "version": "0+unknown"
        },
        "python": {
            "full": "3.6.5 (default, Jun  6 2018, 19:19:24) \n[GCC 6.3.0 20170516]",
            "version": "3.6.5"
        },
        ...
    }

I plan to use this in some Travis CI configuration, refs #313
2018-06-17 14:19:39 -07:00
Simon Willison 0c22fa8f09
Cleaned up view_definition/table_definition code in table view
Also moved those out of standard JSON into just the HTML template context
2018-06-16 10:33:17 -07:00
Simon Willison 3a79ad98ea
Basic CSV export, refs #266
Tables and custom SQL query results can now be exported as CSV.

The easiest way to do this is to use the .csv extension, e.g.

	/test_tables/facet_cities.csv

By default this is served as Content-Type: text/plain so you can see it in
your browser. If you want to download the file (using text/csv and with an
appropriate Content-Disposition: attachment header) you can do so like this:

	/test_tables/facet_cities.csv?_dl=1

We link to the CSV and downloadable CSV URLs from the table and query pages.

The links use ?_size=max and so by default will return 1,000 rows.

Also fixes #303 - table names ending in .json or .csv are now detected and
URLs are generated that look like this instead:

	/test_tables/table%2Fwith%2Fslashes.csv?_format=csv

The ?_format= option is available for everything else too, but we link to the
.csv / .json versions in most cases because they are aesthetically pleasing.
2018-06-14 23:51:23 -07:00
Simon Willison a246f476b4
Moved JsonDataView into views/special,py 2018-06-07 08:22:29 -07:00
Simon Willison 29edbe90ff
New cache_size_kb config for SQLite, closes #304 2018-06-04 09:02:07 -07:00
Simon Willison b0a95da963
Show more useful error message for SQL interrupted, closes #142 2018-05-28 14:24:19 -07:00
Simon Willison 02870e5731
Filter out duplicate JS/CSS URLs, refs #291 (testme) 2018-05-27 01:45:03 -07:00
Simon Willison 7944a8b0de
Added num_sql_threads config option, closes #285 2018-05-26 17:43:22 -07:00
Simon Willison b463f60158
?_ttl= parameter and default_cache_ttl config
Refs #285, Closes #289
2018-05-26 15:17:33 -07:00
Simon Willison f722b0a730
allow_sql config option to disable custom SQL, closes #284 2018-05-24 22:50:50 -07:00
Simon Willison 50920cfe3d
allow_facet, allow_download, suggest_facets boolean --config
Refs #284
2018-05-24 18:12:27 -07:00
Simon Willison 81df47e8d9
Moved .execute() method from BaseView to Datasette class
Also introduced new Results() class with results.truncated, results.description, results.rows
2018-05-24 17:15:53 -07:00
Ravi Kotecha bd30c696e1 Build Dockerfile with recent Sqlite + Spatialite (#280)
Closes #278

```bash
  $ docker run --rm -it datasette spatialite
  SpatiaLite version ..: 4.4.0-RC0	Supported Extensions:
    - 'VirtualShape'	[direct Shapefile access]
    - 'VirtualDbf'		[direct DBF access]
    - 'VirtualXL'		[direct XLS access]
    - 'VirtualText'		[direct CSV/TXT access]
    - 'VirtualNetwork'	[Dijkstra shortest path]
    - 'RTree'		[Spatial Index - R*Tree]
    - 'MbrCache'		[Spatial Index - MBR cache]
    - 'VirtualSpatialIndex'	[R*Tree metahandler]
    - 'VirtualElementary'	[ElemGeoms metahandler]
    - 'VirtualKNN'	[K-Nearest Neighbors metahandler]
    - 'VirtualXPath'	[XML Path Language - XPath]
    - 'VirtualFDO'		[FDO-OGR interoperability]
    - 'VirtualGPKG'	[OGC GeoPackage interoperability]
    - 'VirtualBBox'		[BoundingBox tables]
    - 'SpatiaLite'		[Spatial SQL - OGC]
  PROJ.4 version ......: Rel. 4.9.3, 15 August 2016
  GEOS version ........: 3.5.1-CAPI-1.9.1 r4246
  TARGET CPU ..........: x86_64-linux-gnu
  the SPATIAL_REF_SYS table already contains some row(s)
  SQLite version ......: 3.23.1
  Enter ".help" for instructions
  SQLite version 3.23.1 2018-04-10 17:39:29
  Enter ".help" for instructions
  Enter SQL statements terminated with a ";"
  spatialite>
```

```bash
$ docker run --rm -it datasette python -c "import sqlite3; print(sqlite3.sqlite_version)"
3.23.1
```

Also updates the query used to check for FTS5 as the old version wasn't
detecting FTS5 for some reason.
2018-05-23 10:43:34 -07:00
Russ Garrett 58b5a37dbb Refactor inspect logic 2018-05-22 07:03:06 -07:00
Simon Willison f6183ff5fa
Renamed --limit to --config, 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)
2018-05-20 10:01:49 -07:00
Simon Willison 39426ff0e4
Added /-/limits and /-/limits.json, closes #270 2018-05-17 23:16:28 -07:00
Simon Willison cef9a9a870
--limit= mechanism plus new limits for facets
Replaced the --max_returned_rows and --sql_time_limit_ms options to
"datasette serve" with a new --limit option, which supports a larger
list of limits.

Example usage:

	datasette serve --limit max_returned_rows:1000 \
		--limit sql_time_limit_ms:2500 \
		--limit default_facet_size:50 \
		--limit facet_time_limit_ms:1000 \
		--limit facet_suggest_time_limit_ms:500

New docs: https://datasette.readthedocs.io/en/latest/limits.html

Closes #270
Closes #264
2018-05-17 22:08:26 -07:00
Simon Willison eaf715a60a Extract /-/plugins view into a method 2018-05-14 00:04:23 -03:00
Simon Willison cf1fe693e5 Used isort to re-order my imports 2018-05-14 00:04:23 -03:00
Simon Willison 3686385551 Ran black source formatting tool against new views/ and app.py 2018-05-14 00:04:23 -03:00
Simon Willison 1f69269fe9 Refactored views into new views/ modules, refs #256 2018-05-14 00:04:23 -03:00
Simon Willison eaaa3ea149
Initial implementation of facets, plus tests and docs
Refs #255
2018-05-12 19:29:41 -03:00
Simon Willison 04d21ccd08
/-/versions now includes SQLite fts_versions, closes #252 2018-05-11 10:19:25 -03:00
Simon Willison e10f8e1274
Slight simplification of /-/inspect 2018-05-06 10:05:38 -03:00
Simon Willison 1259b8ac0b
Support _search_COLUMN=text searches, closes #237 2018-05-05 19:33:08 -03:00
Simon Willison ca290719ed
Show version on /-/plugins page, closes #248 2018-05-04 15:04:33 -03:00
Simon Willison d4da4c92c8
?_size=max option, closes #249 2018-05-04 15:03:40 -03:00
Simon Willison bb87cf8730
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"
        }
      }
    }
2018-05-03 11:09:27 -03:00
Simon Willison 349e262bb1 Renamed ?_sql_time_limit_ms= to ?_timelimit, closes #242 2018-05-01 17:20:39 -07:00
Simon Willison a4cc5dc813 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
2018-05-01 17:20:39 -07:00
Simon Willison 2565d623af ?_shape=array experimental feature 2018-05-01 17:20:39 -07:00
Simon Willison aa954382c3
FTS tables now detected by inspect(), closes #240 2018-04-28 17:04:32 -07:00
Simon Willison f188ceaa2a
New ?_size=XXX querystring parameter for table view, closes #229
Also added documentation for all of the _special arguments.

Plus deleted some duplicate logic implementing _group_count.
2018-04-25 21:47:12 -07:00
Simon Willison 4504d5160b
If max_returned_rows==page_size, increment max_returned_rows
Fixes #230, where if the two were equal pagination didn't work correctly.
2018-04-25 21:04:12 -07:00
Simon Willison 02ee31c8b4
New hidden: True option for table metadat, closes #239 2018-04-25 20:42:57 -07:00
Simon Willison d3a0069c54
Hide idx_* tables if spatialite detected, closes #228 2018-04-25 20:25:21 -07:00
Simon Willison f3f4295712
label_column now defined on the table-being-linked-to, fixes #234 2018-04-22 13:46:18 -07:00
Simon Willison f27cabbaf3
label_column option in metadata.json - closes #234 2018-04-22 10:53:16 -07:00
Simon Willison e2750c7cc0
Add col-X classes to HTML table on custom query page 2018-04-19 08:36:14 -07:00
Simon Willison b52171db1e
Plugins can now bundle custom templates, closes #224
Refs #14
2018-04-18 22:50:27 -07:00
Simon Willison b55809a1e2
Added /-/metadata /-/plugins /-/inspect, closes #225 2018-04-18 22:25:22 -07:00
Simon Willison 4be6deb947
Fix for plugins in Python 3.5 (#222)
ModuleNotFoundError is not a thing in Python 3.5, so catch KeyError/ImportError instead.
2018-04-17 20:24:20 -07:00
Simon Willison 1c36d07dd4
New plugin hooks: extra_css_urls() and extra_js_urls()
Closes #214
2018-04-17 20:12:21 -07:00
Simon Willison 0bb483ca5b
/-/static-plugins/PLUGIN_NAME/ now serves static/ from plugins
Refs #214
2018-04-17 19:32:48 -07:00
Simon Willison a5792a8c61
<th> now gets class="col-X" - plus added col-X documentation
Refs #209
2018-04-17 19:11:54 -07:00
Russ Garrett 4586aa506a Don't duplicate simple primary keys in the link column
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.
2018-04-17 18:13:02 -07:00
Russ Garrett b231d4243d Correct escaping for HTML display of row links 2018-04-17 18:13:02 -07:00
Simon Willison 5ebc7137d7
Use application/octet-stream for downloadable databses
I'd also like to send the Content-Length here but that's not currently
possible in Sanic - see bug report here:

https://github.com/channelcat/sanic/issues/1194
2018-04-17 17:31:54 -07:00
Simon Willison e7c769ef30
Working implementation of #216 which passes the tests
Reverted commit 5364fa7f33 (where I removed the
code that didn't work).

Added primary keys to order-by clause for sorting to get tests to pass
2018-04-16 18:41:55 -07:00
Simon Willison 5364fa7f33
Revert #216 until I can get tests to pass in Travis
Revert "Fix for _sort_desc=sortable_with_nulls test, refs #216"

This reverts commit 07fc2d113e.

Revert "Fixed #216 - paginate correctly when sorting by nullable column"

This reverts commit 2abe539a0f.
2018-04-16 17:53:37 -07:00
Simon Willison 2abe539a0f
Fixed #216 - paginate correctly when sorting by nullable column 2018-04-16 16:52:50 -07:00
Simon Willison 10a34f995c
Removed rogue print() call 2018-04-16 08:38:09 -07:00
Simon Willison b2955d9065
New --plugins-dir=plugins/ option (#212)
* New --plugins-dir=plugins/ option

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/

* Unit tests for --plugins-dir=plugins/

Closes #211
2018-04-15 22:22:01 -07:00
Simon Willison 33c7c53ff8
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
2018-04-15 17:56:15 -07:00
Russ Garrett efbb4e8337 Return HTTP 405 on InvalidUsage rather than 500
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.
2018-04-14 11:00:38 -07:00
Simon Willison dd4491dd81
Update number of expected tables 2018-04-14 08:16:54 -07:00
Russ Garrett f2b940d602 Link foreign keys which don't have labels
This renders unlabeled FKs as simple links. I can't see why this would
cause any major problems.

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.
2018-04-14 07:59:59 -07:00
Russ Garrett 1cc5161089 Fix sqlite error when loading rows with no incoming FKs
This fixes `ERROR: conn=<sqlite3.Connection object at 0x10bbb9f10>, sql
= 'select ', params = {'id': '1'}` caused by an invalid query when
loading incoming FKs.

The error was ignored due to async but it still got printed to the
console.
2018-04-14 07:24:24 -07:00
Russ Garrett 3c985ec271
Allow custom units to be registered with Pint 2018-04-14 12:27:06 +01:00
Russ Garrett ab85605c61
Support units in filters 2018-04-14 11:43:35 +01:00
Russ Garrett 8bfeb98478
Tidy up units support
* Add units to exported JSON
* Units key in metadata skeleton
* Docs
2018-04-14 11:43:34 +01:00
Russ Garrett ec6abc81e4 Initial units support
Add support for specifying units for a column in metadata.json and
rendering them on display using
[pint](https://pint.readthedocs.io/en/latest/).

ref #203
2018-04-13 20:32:53 -07:00
Simon Willison 9f28bbe43d
Better mechanism for handling errors; 404s for missing table/database
New error mechanism closes #193

404s for missing tables/databesse closes #184

Makes pull request #202 unnecessary.
2018-04-13 11:17:22 -07:00
Russ Garrett d08a133140 Hide Spatialite system tables
They were getting on my nerves.
2018-04-12 14:34:47 -07:00
Simon Willison 46b237c29a
datasette inspect now finds primary_keys
Closes #195
2018-04-09 17:54:12 -07:00
Simon Willison 57b19f09d1
Ability to sort using form fields (for mobile portrait mode)
We now display sort options as a select box plus a descending checkbox, which
means you can apply sort orders even in portrait mode on a mobile phone where
the column headers are hidden.

Closes #199
2018-04-09 17:34:32 -07:00
Simon Willison a290f28caa
table_rows => table_rows_count, filtered_table_rows => filtered_table_rows_count
Renamed properties. Closes #194
2018-04-08 22:24:24 -07:00
Simon Willison 23e0fdb0f3 Removed unnecessary enumerate template helper
I made this obsolete in d1756d7736

Refs #189
2018-04-08 22:10:22 -07:00
Simon Willison b13f0986f2 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
2018-04-08 22:10:22 -07:00
Simon Willison a87df963a0 Error handling for ?_sort and ?_sort_desc
Verifies that they match an existing column, and only one or the other option
is provided - refs #189

Eses a new DatasetteError exception that closes #193
2018-04-08 22:10:22 -07:00
Simon Willison bfb19e3a17 Correctly escape sort-by columns in SQL (refs #189) 2018-04-08 22:10:22 -07:00
Simon Willison 747a801b50 Column headers now link to sort/desc sort - refs #189 2018-04-08 22:10:22 -07:00
Simon Willison 9f2ec39fbc Current sort order now reflected in human filter description
Plus renamed human_description to human_description_en

Refs #189
2018-04-08 22:10:22 -07:00
Simon Willison f3a3820ff5 _sort and _sort_desc parameters for table views
Allows for paginated sorted results based on a specified column.

Refs #189
2018-04-08 22:10:22 -07:00
Simon Willison 29f9a29250 Total row count now correct even if _next= applied 2018-04-08 22:10:22 -07:00
Simon Willison b2188f0442
Use .custom_sql() for _group_count implementation (refs #150) 2018-04-08 08:43:45 -07:00
Simon Willison 0abd3abacb
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
2018-04-03 07:52:54 -07:00
Simon Willison 8f0d44d646
escape_sqlite_table_name => escape_sqlite, handles reserved words
It can be used for column names as well as table names.

Reserved word list from https://www.sqlite.org/lang_keywords.html
2018-04-03 06:40:49 -07:00
Simon Willison 7365c3f51c
Compound primary key _next= now plays well with extra filters
Closes #190
2018-03-29 23:26:22 -07:00
Simon Willison 31f63d1672
Fixed bug with keyset pagination over compound primary keys
Closes #190
2018-03-29 22:11:02 -07:00
Simon Willison 89d9fbb91b
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
2018-03-27 09:18:32 -07:00
Simon Willison 012fc7c5cd
Fix for FTS virtual table counting error 2018-03-20 18:26:04 -07:00
Simon Willison 1aad396c9e
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
2017-12-12 21:36:03 -08:00
Simon Willison ae94006809
No longer include database hash in hyperlinks
It was making the unit tests unreliable. Also we do not do that for foreign key links.
2017-12-09 17:31:08 -08:00
Simon Willison 794c3bfcfc
Cleaned up row/column display logic, fixed bug
Closes #167
2017-12-09 16:59:25 -08:00
Simon Willison 7a7e4b2ed8
Started unit tests for row/table HTML pages
Refs #167

Thanks to the new tests, spotted and fixed a bug where pages that were
supposed to have 100 things on them were actually displaying 101.
2017-12-09 15:32:54 -08:00
Simon Willison 7126d08f10
HTML comment showing which templates were considered for a page
Closes #171
2017-12-09 13:47:32 -08:00
Simon Willison 3c9f889715
Custom templates for canned queries
Closes #170
2017-12-09 13:34:46 -08:00
Simon Willison 1c0d93c39b
Fix Python 3.5 test failure 2017-12-09 10:39:19 -08:00
Simon Willison 16dfccb1c5
Include sha1 hash in /static/app.css URL
This means that when Datasette's CSS changes the new CSS will be loaded
even though browsers may have cached the previous version.

Closes #154
2017-12-08 19:10:09 -08:00
Simon Willison 80bf3afa43
metadata.json support for per-table/per-database metadata
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 <em>description with HTML</em>.",
        "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/"
                    }
                }
            }
        }
    }

Closes #165, Refs #164
2017-12-07 08:47:07 -08:00
Simon Willison 7e1ba161ec
Ability to easily customize _rows_and_columns.html per database table
Also added documentation for this.

Refs #158, Closes #159.
2017-12-06 22:11:22 -08:00
Simon Willison a2b954e828
Fixed #155 2017-12-06 21:39:47 -08:00
Simon Willison 198b8b2955
Fixed row page for tables with a primary key
Closes #152
2017-12-06 21:23:13 -08:00
Simon Willison 52a5e95d21
Fixed bug with filter columns
Closes #162
2017-12-06 21:05:53 -08:00
Simon Willison a743cdeafc
Canned query support + database/query template refactor
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.

As part of this, I refactored the logic for the database index page. It used
to combine the functionality for listing available tables and the
functionality for executing custom SQL queries in a single template and view.
I have split that template out into database.html and query.html and reworked
the view to more clearly separate the custom SQL executing code.

Refs #20
2017-12-05 08:17:02 -08:00
Simon Willison e981ac7d4d
--static option for datasette serve
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

Refs #160
2017-12-03 08:33:36 -08:00
Simon Willison 3cd06729f4
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 <body> CSS classes
introduced in 8ab3a169d4 - 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 %}
    <h1>EXTRA HTML AT THE TOP OF THE CONTENT BLOCK</h1>
    <p>This line renders the original block:</p>
    {{ super() }}
    {% endblock %}

Closes #12, refs #153
2017-11-30 08:38:16 -08:00
Simon Willison 7ff51598c4
git commit -m "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.

Refs #12, #153
2017-11-30 08:05:01 -08:00
Simon Willison ffa77f62b4
Removed dependency on sanic-jinja2
I wasn't using any of the functionality it adds on top of raw Jinja2.

Refs #12 and #153
2017-11-30 07:51:40 -08:00
Simon Willison 8ab3a169d4
CSS styling hooks as classes on the body
Refs #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:

    <body class="index">

The database template (/dbname/) gets this:

    <body class="db db-dbname">

The table template (/dbname/tablename) gets:

    <body class="table db-dbname table-tablename">

The row template (/dbname/tablename/rowid) gets:

    <body class="row db-dbname table-tablename">

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"
2017-11-29 23:09:54 -08:00
Simon Willison b67890d15d
Auto-link column values that look like URLs
Refs #153
2017-11-29 09:05:24 -08:00
Simon Willison 0120c24927
extra_css_urls/extra_js_urls in metadata, refs #153
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 <head> 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 stylsheet or JavaScript if the SRI hash
matches the content served. You can generate hashes using www.srihash.org
2017-11-28 18:38:15 -08:00
Simon Willison de6c62ed9a
Load SQLite extensions inside .inspect() too 2017-11-26 15:02:00 -08:00
Simon Willison e3dd2483bb
Fixed quoting on foreign-key links to tables with spaces in name 2017-11-24 16:05:28 -08:00
Simon Willison a802cbee74
Search now applies to current filters
Combined search into the same form as filters.

Closes #133
2017-11-24 14:29:54 -08:00
Simon Willison a53d09d919
Show count of matching rows on table view
Closes #127
2017-11-24 14:01:53 -08:00
Simon Willison ab32bd6427
Fixed bug with params other than p0
p1 and upwards were not correctly extracted.
2017-11-24 13:55:00 -08:00
Simon Willison 4b7596ee5c
Row page now resolves foreign keys
Closes #132
2017-11-23 13:51:16 -08:00
Simon Willison a30c5b220c
Fixed bug on rows page, added unit test
Rows page for rows that linked to the same table in more
than one columns were display incorrectly. Fixed that and added a test.

Also introduced /db/table/row-pk.json?_extras=foreign_key_tables

This is used by the new unit test, but is the first example of a new
?_extras=comma-separated-list pattern I am introducing.
2017-11-23 13:09:45 -08:00
Simon Willison ef3eacf622
Select option for removing filters 2017-11-23 12:32:54 -08:00
Simon Willison 2f97834d0e
NBSP before ID display 2017-11-22 20:45:04 -08:00
Simon Willison 38dc1ef73b
Show linked foreign key in table cells 2017-11-22 20:43:27 -08:00
Simon Willison 0071b5d6f5
Added UI for editing table filters
Refs #86
2017-11-22 20:33:55 -08:00
Simon Willison 53534b6e9d
Even more complicated redirect scheme
This:

    ?_filter_column_1=name&_filter_op_1=contains&_filter_value_1=hello
    &_filter_column_2=age&_filter_op_2=gte&_filter_value_2=12

Now redirects to this:

    ?name__contains=hello&age__gte=12

This is needed for the filter editing interface, refs #86
2017-11-22 20:03:46 -08:00
Simon Willison 1c8638c30a
Hide FTS-created tables on index pages
Closes #129
2017-11-22 12:18:57 -08:00
Simon Willison a9b9d42791
Mark FTS-related tables as 'hidden' in inspect()
Refs #129
2017-11-21 12:55:59 -08:00
Simon Willison 771b0ee347
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

Still todo: clean up code a bunch (it currently fakes being a 'view'), get
foreign key expansion working.
2017-11-19 22:04:13 -08:00
Simon Willison b4e6211a97
Refactored filter logic and added human descriptions - refs #86 2017-11-19 22:03:24 -08:00
Simon Willison 523c6f9e3a
Fixed bug with FTS against tables with hyphens in the name 2017-11-19 21:59:53 -08:00
Simon Willison a5881e105a
?_filter_column=col&_filter_op=isnull__1 redirect
if filter_op contains a __ the value is set to the right hand side.

e.g.

    ?_filter_column=col&_filter_op=isnull__1&_filter_value=x

Redirects to:

    ?col__isnull=1

Refs #86
2017-11-19 12:33:30 -08:00
Simon Willison 386fb11d42
?_filter_column=col&_filter_op=op&_filter_value=value redirect
Part of implementing the filters UI (refs #86) - the following:

    /trees/Trees?_filter_column=SiteOrder&_filter_op=gt&_filter_value=2

Now redirects to this;

    /trees/Trees?SiteOrder__gt=2
2017-11-19 12:25:29 -08:00
Simon Willison eed6a0fe36
Implemented ?_search=XXX + UI if a FTS table is detected
Closes #131
2017-11-19 08:59:26 -08:00
Simon Willison 1b04662585 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.
2017-11-17 19:15:49 -08:00
Simon Willison 6a007f6322 Row pages show incoming foreign key relationships 2017-11-17 19:15:49 -08:00
Simon Willison e16ca1169c Refactored table column/row display logic
Simplified the template and made the way for upcoming foreign key work.

Refs #85

Also fixed &nbsp; bug on database page - closes #113
2017-11-17 07:39:36 -08:00
Simon Willison b7c4165346 Added --load-extension argument to datasette serve
Allows loading of SQLite extensions. Refs #110.
2017-11-16 08:48:49 -08:00
Simon Willison 228bce83a3 Added __version__, bumped it to 0.12
Closes #108
2017-11-16 07:20:54 -08:00
Simon Willison 9199945a1b Fixed 500 on views page
Bug introduced in a4af532a31

Refs 85
2017-11-16 06:46:49 -08:00
Simon Willison 82261a638b Turn on auto-escaping in Jinja
We had XSS holes! Since we don't do cookies or authentication
they shouldn't cause any actual harm, but still really not good.

https://github.com/pallets/jinja/issues/528
2017-11-15 17:59:42 -08:00
Simon Willison a4af532a31 Include foreign key info in inspect() output
Refs #85
2017-11-15 17:34:32 -08:00
Simon Willison 5d8084a285 Added a UI for editing named parameters
Fixes #96
2017-11-15 17:32:48 -08:00
Simon Willison 9cb69cbd45 New ?_sql_time_limit_ms=10 argument to database and table page
Allows callers to opt for a lower time limit.

Closes #95
2017-11-14 18:55:10 -08:00
Simon Willison 0b8c1b0a6d Test for sql_time_limit_ms + sqlite_functions mechanism
Added a unit test for the sql_time_limit_ms option.

To test this, I needed to add a custom SQLite sleep() function. I've added a
simple mechanism to the Datasette class for registering custom functions.

I also had to modify the sqlite_timelimit() function. It makes use of a magic
value, N, which is the number of SQLite virtual machine instructions that
should execute in between calls to my termination decision function.

The value of N was not finely grained enough for my test to work - so I've
added logic that says that if the time limit is less than 50ms, N is set to 1.
This got the tests working.

Refs #95
2017-11-14 18:43:34 -08:00
Simon Willison 50e817801f Fixed #83
Turns out we had a redirect bug as well.
2017-11-13 16:44:43 -08:00
Simon Willison 1e698787a4 Added --sql_time_limit_ms and --extra-options
The serve command now accepts --sql_time_limit_ms for customizing the SQL time
limit.

The publish and package commands now accept --extra-options which can be used
to specify additional options to be passed to the datasite serve command when
it executes inside the rusulting Docker containers.
2017-11-13 14:00:53 -08:00
Simon Willison 847f3e0c92 Implemented offset/limit pagination for views
Closes #70
2017-11-13 13:10:55 -08:00
Simon Willison 7dac1c05cd Improved pagination
Closes #78
2017-11-13 12:34:56 -08:00
Simon Willison 8252e71da4 Limit on max rows returned, controlled by --max_returned_rows option
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.

Closes #69
2017-11-13 11:33:01 -08:00
Simon Willison 4fec50597a Include license/source in JSON output if provided 2017-11-13 10:39:25 -08:00
Simon Willison 97c4bf4271 Added --cors argument to enable CORS
Closes #75
2017-11-13 10:17:42 -08:00
Simon Willison 3ef35ca8b4 serve and publish commands now take a --metadata option
If provided, the --metadata option is the path to a JSON file containing
metadata that should be displayed alongside the dataset.

    datasette /tmp/fivethirtyeight.db --metadata /tmp/metadata.json

Currently that metadata format looks like this:

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

If provided, this will be used by the index template and to populate the
common footer.

The publish command also accepts this argument, and will package any provided
metadata up and include it with the resulting Docker container.

    datasette publish --metadata /tmp/metadata.json /tmp/fivethirtyeight.db

Closes #68
2017-11-13 07:20:02 -08:00
Simon Willison 2bbe9ca34a Support OPTIONS requests for CORS 2017-11-12 18:11:52 -08:00
Simon Willison 26370b14d8 Handle table names with slashes in them
e.g. https://datasette-wdlexdiaoz.now.sh/fivethirtyeight-75d605c/bob-ross%2Felements-by-episode.csv?CABIN=1&BUSHES=1&CLOUDS=1
2017-11-12 15:17:00 -08:00
Simon Willison efecae5a11 Full path to database now works
e.g. datasette /tmp/blah.db

Previously this failed because it did not open with full path.
2017-11-12 15:01:29 -08:00
Simon Willison 22851ed9f0 Table page now shows CREATE TABLE at bottom
Closes #66
2017-11-12 13:16:59 -08:00
Simon Willison 666aa03253 Improved error handling
Invalid SQL now shows a special error.html template, and is covered by tests.
2017-11-12 13:16:15 -08:00
Simon Willison ff2ab9dc7d Views now show their SQL, are handled a bit better
Refs #66
2017-11-12 12:32:25 -08:00
Simon Willison b51836f846 Fixed bug with tables with spaces in their name
The new database index page was erroring.
2017-11-12 12:02:45 -08:00
Simon Willison 59580d02da Implemented custom SQL via textarea
Closes #65
2017-11-11 18:35:35 -08:00
Simon Willison fd3a33989a Implemented new database view and template
Closes #53 - see comments there for screenshots.
2017-11-11 17:50:21 -08:00
Simon Willison 2366a016f2 Added links to .json and .jsono representations
Closes #62
2017-11-11 14:40:44 -08:00
Simon Willison e9e1def4c0 Revised JSON design a bit
Closes #63
2017-11-11 14:20:00 -08:00
Simon Willison e4bf66d9b0 Added header with breadcrumbs and footer
Also cleaned up titles on various pages.

Closes #61
2017-11-11 12:36:20 -08:00
Simon Willison 40a563ebac Reworked metadata building options
Building metadata is now optional. If you want to do it, do this:

    datasette build *.db --metadata=metadata.json

Then when you run the server you can tell it to read from metadata:

    datasette serve *.db --metadata=metadata.json

The Dockerfile generated by datasette publish now uses this mechanism.

Closes #60
2017-11-11 12:11:51 -08:00
Simon Willison ad8b5d3bd2 JSON version of our homepage
Now available at http://localhost:8006/.json

Tested by tests added in 407795b612
2017-11-11 09:49:47 -08:00
Simon Willison 407795b612 Initial unit tests against our Sanic app
Refs #50

I had to disable the build metadata function to get these tests to work
sensibly. I need to completely rethink how that mechanism works.
2017-11-11 09:47:59 -08:00
Simon Willison 7d81083d40 Implemented responsive tables, removed bootstrap
No need for all of bootstrap since we only need to style a few elements.

Implemented responsive table pattern from here:
https://css-tricks.com/responsive-data-tables/

Refs #16
2017-11-10 21:55:50 -08:00
Simon Willison 21c9c04310 Implemented cursor-based pagination for table view
Closes #5
2017-11-10 12:41:14 -08:00
Simon Willison e9fce44195 Don't serve cache headers in debug or refresh modes 2017-11-10 12:26:37 -08:00
Simon Willison a8a293cd71 Refactored util functions into new utils module 2017-11-10 11:25:54 -08:00
Simon Willison 1c57bd202f Replaced app_factory with new Datasette class
This should make it easier to add unit tests.
2017-11-10 11:05:57 -08:00
Simon Willison 03c58fb350 Show databases in alphabetical order on index page 2017-11-10 11:04:56 -08:00
Simon Willison e06b011771 Updated tests
Refs #23
2017-11-10 10:43:54 -08:00
Simon Willison e7e50875d3 Renamed to 'datasette' 2017-11-10 10:38:35 -08:00