datasette-charts 0.1a0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datasette_charts-0.1a0/LICENSE +201 -0
- datasette_charts-0.1a0/PKG-INFO +226 -0
- datasette_charts-0.1a0/README.md +209 -0
- datasette_charts-0.1a0/datasette_charts/__init__.py +230 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/__init__.py +26 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/bar.py +195 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/base.py +255 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/dimensions.py +57 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/histogram.py +172 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/line.py +257 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/map.py +156 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/scatter.py +130 -0
- datasette_charts-0.1a0/datasette_charts/chart_types/stacked_bar.py +229 -0
- datasette_charts-0.1a0/datasette_charts/cli.py +187 -0
- datasette_charts-0.1a0/datasette_charts/config.py +68 -0
- datasette_charts-0.1a0/datasette_charts/dsl.py +188 -0
- datasette_charts-0.1a0/datasette_charts/engine.py +214 -0
- datasette_charts-0.1a0/datasette_charts/filters.py +96 -0
- datasette_charts-0.1a0/datasette_charts/hookspecs.py +8 -0
- datasette_charts-0.1a0/datasette_charts/profiler/__init__.py +576 -0
- datasette_charts-0.1a0/datasette_charts/profiler/model.py +140 -0
- datasette_charts-0.1a0/datasette_charts/profiler/roles.py +469 -0
- datasette_charts-0.1a0/datasette_charts/profiler/sql.py +217 -0
- datasette_charts-0.1a0/datasette_charts/sources.py +111 -0
- datasette_charts-0.1a0/datasette_charts/sqlgen.py +442 -0
- datasette_charts-0.1a0/datasette_charts/static/charts.css +302 -0
- datasette_charts-0.1a0/datasette_charts/static/charts.js +824 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/bar.js +73 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/common.js +217 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/histogram.js +75 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/line.js +131 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/map.js +96 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/scatter.js +72 -0
- datasette_charts-0.1a0/datasette_charts/static/renderers/stacked_bar.js +123 -0
- datasette_charts-0.1a0/datasette_charts/static/vendor/README.md +83 -0
- datasette_charts-0.1a0/datasette_charts/static/vendor/d3.min.js +2 -0
- datasette_charts-0.1a0/datasette_charts/static/vendor/land-110m.json +1 -0
- datasette_charts-0.1a0/datasette_charts/static/vendor/plot.umd.min.js +2 -0
- datasette_charts-0.1a0/datasette_charts/static/vendor/topojson-client.min.js +2 -0
- datasette_charts-0.1a0/datasette_charts/store.py +81 -0
- datasette_charts-0.1a0/datasette_charts/views.py +404 -0
- datasette_charts-0.1a0/datasette_charts.egg-info/PKG-INFO +226 -0
- datasette_charts-0.1a0/datasette_charts.egg-info/SOURCES.txt +53 -0
- datasette_charts-0.1a0/datasette_charts.egg-info/dependency_links.txt +1 -0
- datasette_charts-0.1a0/datasette_charts.egg-info/entry_points.txt +2 -0
- datasette_charts-0.1a0/datasette_charts.egg-info/requires.txt +1 -0
- datasette_charts-0.1a0/datasette_charts.egg-info/top_level.txt +1 -0
- datasette_charts-0.1a0/pyproject.toml +44 -0
- datasette_charts-0.1a0/setup.cfg +4 -0
- datasette_charts-0.1a0/tests/test_api.py +637 -0
- datasette_charts-0.1a0/tests/test_chart_types.py +368 -0
- datasette_charts-0.1a0/tests/test_charts.py +11 -0
- datasette_charts-0.1a0/tests/test_cli.py +45 -0
- datasette_charts-0.1a0/tests/test_dsl.py +138 -0
- datasette_charts-0.1a0/tests/test_profiler.py +247 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: datasette-charts
|
|
3
|
+
Version: 0.1a0
|
|
4
|
+
Summary: A charting plugin for Datasette
|
|
5
|
+
Author: Datasette
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/datasette/datasette-charts
|
|
8
|
+
Project-URL: Changelog, https://github.com/datasette/datasette-charts/releases
|
|
9
|
+
Project-URL: Issues, https://github.com/datasette/datasette-charts/issues
|
|
10
|
+
Project-URL: CI, https://github.com/datasette/datasette-charts/actions
|
|
11
|
+
Classifier: Framework :: Datasette
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: datasette>=1.0a44
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# datasette-charts
|
|
19
|
+
|
|
20
|
+
[](https://pypi.org/project/datasette-charts/)
|
|
21
|
+
[](https://github.com/datasette/datasette-charts/releases)
|
|
22
|
+
[](https://github.com/datasette/datasette-charts/actions/workflows/test.yml)
|
|
23
|
+
[](https://github.com/datasette/datasette-charts/blob/main/LICENSE)
|
|
24
|
+
|
|
25
|
+
Suggests charts for any Datasette table, view or SQL query, and lets you build your own.
|
|
26
|
+
|
|
27
|
+
This is an early version of the design in [spec.md](spec.md): enough to try the idea out against small tables and queries. See [What is not built yet](#what-is-not-built-yet).
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
This needs a Datasette newer than 1.0a43: commit `56e02c1` or later, which is not yet in a release. Until it is, run against a checkout of Datasette's `main` branch. From a checkout of this repository:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
uv run --no-project --with-editable ~/dev/datasette --with-editable . datasette content.db
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`--no-project` matters here. Without it, uv also installs the latest stable Datasette into this project's own environment, and that copy is the one that gets imported.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
Install this plugin in the same environment as Datasette.
|
|
42
|
+
```bash
|
|
43
|
+
datasette install datasette-charts
|
|
44
|
+
```
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
Every table, view and SQL query page gets a **Charts** panel at the top. It shows a row of suggested charts, best first, each with a thumbnail, a title and the reason it was suggested. Select one to see it at full size.
|
|
48
|
+
|
|
49
|
+
Beside the full-size chart is a builder for changing the chart type, columns, aggregates, time units and bins. "Build a chart" at the end of the row starts from scratch. Anything the plugin can suggest can be built by hand.
|
|
50
|
+
|
|
51
|
+
The chart you are looking at is kept in the page's URL fragment, so the address bar always holds a link to it.
|
|
52
|
+
|
|
53
|
+
When nothing is suggested for a table, the panel starts collapsed. Opening it still offers "Build a chart".
|
|
54
|
+
|
|
55
|
+
Charts never fetch every row. Each one compiles to a SQL query that returns a small, aggregated result, and that query is what "View and edit this chart's SQL" links to.
|
|
56
|
+
|
|
57
|
+
### Filters and facets
|
|
58
|
+
|
|
59
|
+
On a filtered table page the charts cover the rows the page shows. Column filters such as `?state=CA` and `?price__gt=5`, `?_search=`, `?_where=` and `?_through=` all apply, and so does anything you click in a facet. The suggestions are worked out again for the filtered rows, so a column that the filter has narrowed to a single value is no longer suggested.
|
|
60
|
+
|
|
61
|
+
The chart you select stays selected as you filter, facet, sort and page through a table: each new page opens with the same chart, now drawn from that page's rows. Close the chart to stop it coming back. This is remembered per table, in the browser tab.
|
|
62
|
+
|
|
63
|
+
### Chart types
|
|
64
|
+
|
|
65
|
+
| Type | Shows | Suggested when |
|
|
66
|
+
|------|-------|----------------|
|
|
67
|
+
| `histogram` | The distribution of a numeric column, on a log scale when it has a long tail | A measure has spread |
|
|
68
|
+
| `bar` | A count or an aggregate for each value of a category: the top N plus "Other" | A category has a workable number of values |
|
|
69
|
+
| `line` | A count or an aggregate over time, optionally split into series | A date or timestamp column spans several buckets |
|
|
70
|
+
| `scatter` | Two numeric columns: points for small sources, a binned heatmap for large ones | There are two measures |
|
|
71
|
+
| `map` | Latitude and longitude: points for small sources, grid cells for large ones | A latitude/longitude pair exists |
|
|
72
|
+
| `stacked_bar` | Stacked or grouped bars for two categories, or a category over time | There are two categories |
|
|
73
|
+
|
|
74
|
+
### How large sources are handled
|
|
75
|
+
|
|
76
|
+
Suggestions come from a profile of the source: row count, types, distinct counts, top values, ranges and detected dates. Profiling runs inline, under Datasette's SQL time limit.
|
|
77
|
+
|
|
78
|
+
- A source of up to 50,000 rows (and 1,000,000 cells) is profiled in full.
|
|
79
|
+
- A larger one is profiled from a sample of up to 10,000 rows. The panel says so when this happens. Charts still query every row.
|
|
80
|
+
- For a table, the sample is spread evenly across its rows. For a view, a query or a filtered table it is the first rows the source returns, which can be less representative.
|
|
81
|
+
|
|
82
|
+
To let more work happen inline, raise Datasette's time limit and the plugin's limits together:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
datasette data.db --setting sql_time_limit_ms 10000 -c datasette.yaml
|
|
86
|
+
```
|
|
87
|
+
```yaml
|
|
88
|
+
plugins:
|
|
89
|
+
datasette-charts:
|
|
90
|
+
inline_row_limit: 5000000
|
|
91
|
+
inline_cell_limit: 100000000
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Configuration
|
|
95
|
+
|
|
96
|
+
All settings are optional. These are the defaults:
|
|
97
|
+
|
|
98
|
+
```yaml
|
|
99
|
+
plugins:
|
|
100
|
+
datasette-charts:
|
|
101
|
+
max_suggestions: 8
|
|
102
|
+
inline_row_limit: 50000 # profile the whole source up to this many rows...
|
|
103
|
+
inline_cell_limit: 1000000 # ...and this many rows x columns
|
|
104
|
+
sample_rows: 10000 # sample size for larger sources
|
|
105
|
+
temporal_min_match_rate: 0.95 # share of values that must look like dates
|
|
106
|
+
thumbnail_row_limit: 100000 # above this, thumbnails load on hover
|
|
107
|
+
thumbnail_concurrency: 2
|
|
108
|
+
hard_max_rows: 5000 # cap on the rows any chart query returns
|
|
109
|
+
distinct_cap: 1000
|
|
110
|
+
top_values: 20
|
|
111
|
+
profile_budget_ms: null # default: twice sql_time_limit_ms, at least 5000
|
|
112
|
+
chart_time_limit_ms: null # default: sql_time_limit_ms
|
|
113
|
+
profile_cache_seconds: 300 # profiles are kept in memory this long
|
|
114
|
+
auto_suggest: true # false: wait until the panel is opened
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Dates are detected from values: ISO 8601 dates and datetimes always, and Unix timestamps and years when the column name or declared type also suggests a time. To correct a column, set its kind in table-level configuration, or `false` to say it is not a time:
|
|
118
|
+
|
|
119
|
+
```yaml
|
|
120
|
+
databases:
|
|
121
|
+
shop:
|
|
122
|
+
tables:
|
|
123
|
+
orders:
|
|
124
|
+
plugins:
|
|
125
|
+
datasette-charts:
|
|
126
|
+
temporal:
|
|
127
|
+
shipped: unix_seconds
|
|
128
|
+
build_number: false
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The kinds are `iso_date`, `iso_datetime`, `unix_seconds`, `unix_ms` and `year`.
|
|
132
|
+
|
|
133
|
+
## Permissions
|
|
134
|
+
|
|
135
|
+
- Charts for a table or view need `view-table`. They do not need `execute-sql`.
|
|
136
|
+
- Charts for a SQL query need `execute-sql`.
|
|
137
|
+
- Charts for a stored query need `view-query`.
|
|
138
|
+
- The compiled SQL is only shown to actors with `execute-sql`.
|
|
139
|
+
|
|
140
|
+
## Command line
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
datasette charts suggest data.db orders
|
|
144
|
+
datasette charts suggest data.db --sql "select * from orders where state = :s" -p s CA
|
|
145
|
+
datasette charts profile data.db orders
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`suggest` prints the ranked suggestions with the signals behind each score. `profile` prints the facts gathered about each column. Both take `--json`, and `--time-limit-ms 10000` to run with a longer SQL time limit.
|
|
149
|
+
|
|
150
|
+
## HTTP API
|
|
151
|
+
|
|
152
|
+
| Endpoint | Returns |
|
|
153
|
+
|----------|---------|
|
|
154
|
+
| `/-/charts/types.json` | The registered chart types |
|
|
155
|
+
| `/<db>/<table>/-/charts/suggest.json` | Suggestions for a table or view |
|
|
156
|
+
| `/<db>/-/charts/suggest.json?sql=...` | Suggestions for a SQL query |
|
|
157
|
+
| `/<db>/-/charts/data.json?source=...&spec=...` | The rows for one chart |
|
|
158
|
+
| `/<db>/-/charts/profile.json?source=...` | The profile of a source |
|
|
159
|
+
|
|
160
|
+
`source` is a JSON object such as `{"kind": "table", "table": "orders"}` or `{"kind": "query", "sql": "...", "params": {}}`.
|
|
161
|
+
|
|
162
|
+
A filtered table is a table source with the filters from the table page's query string as pairs: `{"kind": "table", "table": "orders", "filters": [["state", "CA"], ["price__gt", "5"]]}`. The table's own suggestions endpoint takes that query string directly, so `/<db>/<table>/-/charts/suggest.json?state=CA&price__gt=5` means the same rows as the table page with those parameters, and its response includes the `source` to pass to `data.json`.
|
|
163
|
+
|
|
164
|
+
`spec` is a chart in the JSON format the suggestions use:
|
|
165
|
+
|
|
166
|
+
```json
|
|
167
|
+
{
|
|
168
|
+
"v": 1,
|
|
169
|
+
"type": "line",
|
|
170
|
+
"encoding": {
|
|
171
|
+
"x": {"column": "created_at", "timeUnit": "month"},
|
|
172
|
+
"y": {"aggregate": "count"},
|
|
173
|
+
"series": {"column": "status", "limit": 5, "other": true}
|
|
174
|
+
},
|
|
175
|
+
"options": {}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Adding chart types
|
|
180
|
+
|
|
181
|
+
A chart type is a Python class that proposes charts and compiles them to SQL, plus a JavaScript module that draws the result. Other plugins register theirs with the `register_chart_types` hook, which is how the built-in ones are registered too:
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from datasette import hookimpl
|
|
185
|
+
from datasette_charts import ChartType
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class SankeyChart(ChartType):
|
|
189
|
+
name = "sankey"
|
|
190
|
+
...
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@hookimpl
|
|
194
|
+
def register_chart_types(datasette):
|
|
195
|
+
return [SankeyChart]
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
The built-in chart types in `datasette_charts/chart_types/` are the reference for now. This interface is not stable yet.
|
|
199
|
+
|
|
200
|
+
## What is not built yet
|
|
201
|
+
|
|
202
|
+
From the spec, in rough order of how much it matters:
|
|
203
|
+
|
|
204
|
+
- **The background full tier and stored profiles.** Nothing is written to the internal database and no background task runs. Profiles are kept in memory for a few minutes. A source too large to profile under the time limit gets suggestions from a sample, or none.
|
|
205
|
+
- **The verify stage.** Candidates go straight from propose to rank, so nothing checks whether a time series is flat or two measures are related.
|
|
206
|
+
- **Precomputed thumbnails.** Every thumbnail runs its query when it scrolls into view.
|
|
207
|
+
- **The snapshot harness, contact sheet and benchmarks.**
|
|
208
|
+
- **Magic parameters in stored queries**, such as `:_actor_id`.
|
|
209
|
+
|
|
210
|
+
The map draws points over a coarse world land outline, which needs no network access. The spec leaves the choice of basemap open.
|
|
211
|
+
|
|
212
|
+
Charts are drawn with [Observable Plot](https://observablehq.com/plot/), which is bundled along with D3. See `datasette_charts/static/vendor/README.md` for versions and licenses.
|
|
213
|
+
|
|
214
|
+
## Development
|
|
215
|
+
|
|
216
|
+
To set up this plugin locally, first checkout the code. You can confirm it is available like this:
|
|
217
|
+
```bash
|
|
218
|
+
cd datasette-charts
|
|
219
|
+
# Confirm the plugin is visible
|
|
220
|
+
uv run --no-project --with-editable ~/dev/datasette --with-editable . datasette plugins
|
|
221
|
+
```
|
|
222
|
+
To run the tests, which also need Datasette's `main` branch for now:
|
|
223
|
+
```bash
|
|
224
|
+
uv run --no-project --with-editable ~/dev/datasette --with-editable . \
|
|
225
|
+
--with pytest --with pytest-asyncio pytest
|
|
226
|
+
```
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# datasette-charts
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/datasette-charts/)
|
|
4
|
+
[](https://github.com/datasette/datasette-charts/releases)
|
|
5
|
+
[](https://github.com/datasette/datasette-charts/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/datasette/datasette-charts/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
Suggests charts for any Datasette table, view or SQL query, and lets you build your own.
|
|
9
|
+
|
|
10
|
+
This is an early version of the design in [spec.md](spec.md): enough to try the idea out against small tables and queries. See [What is not built yet](#what-is-not-built-yet).
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
This needs a Datasette newer than 1.0a43: commit `56e02c1` or later, which is not yet in a release. Until it is, run against a checkout of Datasette's `main` branch. From a checkout of this repository:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
uv run --no-project --with-editable ~/dev/datasette --with-editable . datasette content.db
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`--no-project` matters here. Without it, uv also installs the latest stable Datasette into this project's own environment, and that copy is the one that gets imported.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
Install this plugin in the same environment as Datasette.
|
|
25
|
+
```bash
|
|
26
|
+
datasette install datasette-charts
|
|
27
|
+
```
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Every table, view and SQL query page gets a **Charts** panel at the top. It shows a row of suggested charts, best first, each with a thumbnail, a title and the reason it was suggested. Select one to see it at full size.
|
|
31
|
+
|
|
32
|
+
Beside the full-size chart is a builder for changing the chart type, columns, aggregates, time units and bins. "Build a chart" at the end of the row starts from scratch. Anything the plugin can suggest can be built by hand.
|
|
33
|
+
|
|
34
|
+
The chart you are looking at is kept in the page's URL fragment, so the address bar always holds a link to it.
|
|
35
|
+
|
|
36
|
+
When nothing is suggested for a table, the panel starts collapsed. Opening it still offers "Build a chart".
|
|
37
|
+
|
|
38
|
+
Charts never fetch every row. Each one compiles to a SQL query that returns a small, aggregated result, and that query is what "View and edit this chart's SQL" links to.
|
|
39
|
+
|
|
40
|
+
### Filters and facets
|
|
41
|
+
|
|
42
|
+
On a filtered table page the charts cover the rows the page shows. Column filters such as `?state=CA` and `?price__gt=5`, `?_search=`, `?_where=` and `?_through=` all apply, and so does anything you click in a facet. The suggestions are worked out again for the filtered rows, so a column that the filter has narrowed to a single value is no longer suggested.
|
|
43
|
+
|
|
44
|
+
The chart you select stays selected as you filter, facet, sort and page through a table: each new page opens with the same chart, now drawn from that page's rows. Close the chart to stop it coming back. This is remembered per table, in the browser tab.
|
|
45
|
+
|
|
46
|
+
### Chart types
|
|
47
|
+
|
|
48
|
+
| Type | Shows | Suggested when |
|
|
49
|
+
|------|-------|----------------|
|
|
50
|
+
| `histogram` | The distribution of a numeric column, on a log scale when it has a long tail | A measure has spread |
|
|
51
|
+
| `bar` | A count or an aggregate for each value of a category: the top N plus "Other" | A category has a workable number of values |
|
|
52
|
+
| `line` | A count or an aggregate over time, optionally split into series | A date or timestamp column spans several buckets |
|
|
53
|
+
| `scatter` | Two numeric columns: points for small sources, a binned heatmap for large ones | There are two measures |
|
|
54
|
+
| `map` | Latitude and longitude: points for small sources, grid cells for large ones | A latitude/longitude pair exists |
|
|
55
|
+
| `stacked_bar` | Stacked or grouped bars for two categories, or a category over time | There are two categories |
|
|
56
|
+
|
|
57
|
+
### How large sources are handled
|
|
58
|
+
|
|
59
|
+
Suggestions come from a profile of the source: row count, types, distinct counts, top values, ranges and detected dates. Profiling runs inline, under Datasette's SQL time limit.
|
|
60
|
+
|
|
61
|
+
- A source of up to 50,000 rows (and 1,000,000 cells) is profiled in full.
|
|
62
|
+
- A larger one is profiled from a sample of up to 10,000 rows. The panel says so when this happens. Charts still query every row.
|
|
63
|
+
- For a table, the sample is spread evenly across its rows. For a view, a query or a filtered table it is the first rows the source returns, which can be less representative.
|
|
64
|
+
|
|
65
|
+
To let more work happen inline, raise Datasette's time limit and the plugin's limits together:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
datasette data.db --setting sql_time_limit_ms 10000 -c datasette.yaml
|
|
69
|
+
```
|
|
70
|
+
```yaml
|
|
71
|
+
plugins:
|
|
72
|
+
datasette-charts:
|
|
73
|
+
inline_row_limit: 5000000
|
|
74
|
+
inline_cell_limit: 100000000
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Configuration
|
|
78
|
+
|
|
79
|
+
All settings are optional. These are the defaults:
|
|
80
|
+
|
|
81
|
+
```yaml
|
|
82
|
+
plugins:
|
|
83
|
+
datasette-charts:
|
|
84
|
+
max_suggestions: 8
|
|
85
|
+
inline_row_limit: 50000 # profile the whole source up to this many rows...
|
|
86
|
+
inline_cell_limit: 1000000 # ...and this many rows x columns
|
|
87
|
+
sample_rows: 10000 # sample size for larger sources
|
|
88
|
+
temporal_min_match_rate: 0.95 # share of values that must look like dates
|
|
89
|
+
thumbnail_row_limit: 100000 # above this, thumbnails load on hover
|
|
90
|
+
thumbnail_concurrency: 2
|
|
91
|
+
hard_max_rows: 5000 # cap on the rows any chart query returns
|
|
92
|
+
distinct_cap: 1000
|
|
93
|
+
top_values: 20
|
|
94
|
+
profile_budget_ms: null # default: twice sql_time_limit_ms, at least 5000
|
|
95
|
+
chart_time_limit_ms: null # default: sql_time_limit_ms
|
|
96
|
+
profile_cache_seconds: 300 # profiles are kept in memory this long
|
|
97
|
+
auto_suggest: true # false: wait until the panel is opened
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Dates are detected from values: ISO 8601 dates and datetimes always, and Unix timestamps and years when the column name or declared type also suggests a time. To correct a column, set its kind in table-level configuration, or `false` to say it is not a time:
|
|
101
|
+
|
|
102
|
+
```yaml
|
|
103
|
+
databases:
|
|
104
|
+
shop:
|
|
105
|
+
tables:
|
|
106
|
+
orders:
|
|
107
|
+
plugins:
|
|
108
|
+
datasette-charts:
|
|
109
|
+
temporal:
|
|
110
|
+
shipped: unix_seconds
|
|
111
|
+
build_number: false
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The kinds are `iso_date`, `iso_datetime`, `unix_seconds`, `unix_ms` and `year`.
|
|
115
|
+
|
|
116
|
+
## Permissions
|
|
117
|
+
|
|
118
|
+
- Charts for a table or view need `view-table`. They do not need `execute-sql`.
|
|
119
|
+
- Charts for a SQL query need `execute-sql`.
|
|
120
|
+
- Charts for a stored query need `view-query`.
|
|
121
|
+
- The compiled SQL is only shown to actors with `execute-sql`.
|
|
122
|
+
|
|
123
|
+
## Command line
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
datasette charts suggest data.db orders
|
|
127
|
+
datasette charts suggest data.db --sql "select * from orders where state = :s" -p s CA
|
|
128
|
+
datasette charts profile data.db orders
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`suggest` prints the ranked suggestions with the signals behind each score. `profile` prints the facts gathered about each column. Both take `--json`, and `--time-limit-ms 10000` to run with a longer SQL time limit.
|
|
132
|
+
|
|
133
|
+
## HTTP API
|
|
134
|
+
|
|
135
|
+
| Endpoint | Returns |
|
|
136
|
+
|----------|---------|
|
|
137
|
+
| `/-/charts/types.json` | The registered chart types |
|
|
138
|
+
| `/<db>/<table>/-/charts/suggest.json` | Suggestions for a table or view |
|
|
139
|
+
| `/<db>/-/charts/suggest.json?sql=...` | Suggestions for a SQL query |
|
|
140
|
+
| `/<db>/-/charts/data.json?source=...&spec=...` | The rows for one chart |
|
|
141
|
+
| `/<db>/-/charts/profile.json?source=...` | The profile of a source |
|
|
142
|
+
|
|
143
|
+
`source` is a JSON object such as `{"kind": "table", "table": "orders"}` or `{"kind": "query", "sql": "...", "params": {}}`.
|
|
144
|
+
|
|
145
|
+
A filtered table is a table source with the filters from the table page's query string as pairs: `{"kind": "table", "table": "orders", "filters": [["state", "CA"], ["price__gt", "5"]]}`. The table's own suggestions endpoint takes that query string directly, so `/<db>/<table>/-/charts/suggest.json?state=CA&price__gt=5` means the same rows as the table page with those parameters, and its response includes the `source` to pass to `data.json`.
|
|
146
|
+
|
|
147
|
+
`spec` is a chart in the JSON format the suggestions use:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
{
|
|
151
|
+
"v": 1,
|
|
152
|
+
"type": "line",
|
|
153
|
+
"encoding": {
|
|
154
|
+
"x": {"column": "created_at", "timeUnit": "month"},
|
|
155
|
+
"y": {"aggregate": "count"},
|
|
156
|
+
"series": {"column": "status", "limit": 5, "other": true}
|
|
157
|
+
},
|
|
158
|
+
"options": {}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Adding chart types
|
|
163
|
+
|
|
164
|
+
A chart type is a Python class that proposes charts and compiles them to SQL, plus a JavaScript module that draws the result. Other plugins register theirs with the `register_chart_types` hook, which is how the built-in ones are registered too:
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from datasette import hookimpl
|
|
168
|
+
from datasette_charts import ChartType
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class SankeyChart(ChartType):
|
|
172
|
+
name = "sankey"
|
|
173
|
+
...
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@hookimpl
|
|
177
|
+
def register_chart_types(datasette):
|
|
178
|
+
return [SankeyChart]
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The built-in chart types in `datasette_charts/chart_types/` are the reference for now. This interface is not stable yet.
|
|
182
|
+
|
|
183
|
+
## What is not built yet
|
|
184
|
+
|
|
185
|
+
From the spec, in rough order of how much it matters:
|
|
186
|
+
|
|
187
|
+
- **The background full tier and stored profiles.** Nothing is written to the internal database and no background task runs. Profiles are kept in memory for a few minutes. A source too large to profile under the time limit gets suggestions from a sample, or none.
|
|
188
|
+
- **The verify stage.** Candidates go straight from propose to rank, so nothing checks whether a time series is flat or two measures are related.
|
|
189
|
+
- **Precomputed thumbnails.** Every thumbnail runs its query when it scrolls into view.
|
|
190
|
+
- **The snapshot harness, contact sheet and benchmarks.**
|
|
191
|
+
- **Magic parameters in stored queries**, such as `:_actor_id`.
|
|
192
|
+
|
|
193
|
+
The map draws points over a coarse world land outline, which needs no network access. The spec leaves the choice of basemap open.
|
|
194
|
+
|
|
195
|
+
Charts are drawn with [Observable Plot](https://observablehq.com/plot/), which is bundled along with D3. See `datasette_charts/static/vendor/README.md` for versions and licenses.
|
|
196
|
+
|
|
197
|
+
## Development
|
|
198
|
+
|
|
199
|
+
To set up this plugin locally, first checkout the code. You can confirm it is available like this:
|
|
200
|
+
```bash
|
|
201
|
+
cd datasette-charts
|
|
202
|
+
# Confirm the plugin is visible
|
|
203
|
+
uv run --no-project --with-editable ~/dev/datasette --with-editable . datasette plugins
|
|
204
|
+
```
|
|
205
|
+
To run the tests, which also need Datasette's `main` branch for now:
|
|
206
|
+
```bash
|
|
207
|
+
uv run --no-project --with-editable ~/dev/datasette --with-editable . \
|
|
208
|
+
--with pytest --with pytest-asyncio pytest
|
|
209
|
+
```
|