mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
* extra_template_vars plugin hook Closes #541 * Workaround for cwd bug Based on https://github.com/pytest-dev/pytest/issues/1235#issuecomment-175295691
31 lines
897 B
Python
31 lines
897 B
Python
import json
|
|
from datasette.utils.asgi import Response
|
|
from .base import BaseView
|
|
|
|
|
|
class JsonDataView(BaseView):
|
|
name = "json_data"
|
|
|
|
def __init__(self, datasette, filename, data_callback):
|
|
self.ds = datasette
|
|
self.filename = filename
|
|
self.data_callback = data_callback
|
|
|
|
async def get(self, request, as_format):
|
|
data = self.data_callback()
|
|
if as_format:
|
|
headers = {}
|
|
if self.ds.cors:
|
|
headers["Access-Control-Allow-Origin"] = "*"
|
|
return Response(
|
|
json.dumps(data),
|
|
content_type="application/json; charset=utf-8",
|
|
headers=headers,
|
|
)
|
|
|
|
else:
|
|
return await self.render(
|
|
["show_json.html"],
|
|
request=request,
|
|
context={"filename": self.filename, "data": data},
|
|
)
|