mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
JSON version of our homepage
Now available at http://localhost:8006/.json
Tested by tests added in 407795b612
This commit is contained in:
parent
407795b612
commit
ad8b5d3bd2
1 changed files with 19 additions and 7 deletions
|
|
@ -200,7 +200,7 @@ class IndexView(HTTPMethodView):
|
||||||
self.jinja = datasette.jinja
|
self.jinja = datasette.jinja
|
||||||
self.executor = datasette.executor
|
self.executor = datasette.executor
|
||||||
|
|
||||||
async def get(self, request):
|
async def get(self, request, as_json):
|
||||||
databases = []
|
databases = []
|
||||||
for key, info in sorted(ensure_build_metadata(self.files).items()):
|
for key, info in sorted(ensure_build_metadata(self.files).items()):
|
||||||
database = {
|
database = {
|
||||||
|
|
@ -217,11 +217,23 @@ class IndexView(HTTPMethodView):
|
||||||
'total_rows': sum(info['tables'].values()),
|
'total_rows': sum(info['tables'].values()),
|
||||||
}
|
}
|
||||||
databases.append(database)
|
databases.append(database)
|
||||||
return self.jinja.render(
|
if as_json:
|
||||||
'index.html',
|
return response.HTTPResponse(
|
||||||
request,
|
json.dumps(
|
||||||
databases=databases,
|
{db['name']: db for db in databases},
|
||||||
)
|
cls=CustomJSONEncoder
|
||||||
|
),
|
||||||
|
content_type='application/json',
|
||||||
|
headers={
|
||||||
|
'Access-Control-Allow-Origin': '*'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return self.jinja.render(
|
||||||
|
'index.html',
|
||||||
|
request,
|
||||||
|
databases=databases,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def favicon(request):
|
async def favicon(request):
|
||||||
|
|
@ -445,7 +457,7 @@ class Datasette:
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
self.jinja.add_env('escape_css_string', escape_css_string, 'filters')
|
self.jinja.add_env('escape_css_string', escape_css_string, 'filters')
|
||||||
app.add_route(IndexView.as_view(self), '/')
|
app.add_route(IndexView.as_view(self), '/<as_json:(.jsono?)?$>')
|
||||||
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
|
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
|
||||||
app.add_route(favicon, '/favicon.ico')
|
app.add_route(favicon, '/favicon.ico')
|
||||||
app.static('/-/static/', str(app_root / 'datasette' / 'static'))
|
app.static('/-/static/', str(app_root / 'datasette' / 'static'))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue