Commit graph

66 commits

Author SHA1 Message Date
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