Added template_debug setting, closes #654

pull/546/merge
Simon Willison 2019-12-22 16:04:45 +00:00
rodzic ceef5ce684
commit d54318fc7f
6 zmienionych plików z 61 dodań i 23 usunięć

Wyświetl plik

@ -27,7 +27,7 @@ jobs:
- npm install -g now
- python tests/fixtures.py fixtures.db fixtures.json
- export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7`
- datasette publish nowv1 fixtures.db -m fixtures.json --token=$NOW_TOKEN --branch=$TRAVIS_COMMIT --version-note=$TRAVIS_COMMIT --name=datasette-latest-$ALIAS --alias=latest.datasette.io --alias=$ALIAS.datasette.io
- datasette publish nowv1 fixtures.db -m fixtures.json --token=$NOW_TOKEN --branch=$TRAVIS_COMMIT --version-note=$TRAVIS_COMMIT --name=datasette-latest-$ALIAS --alias=latest.datasette.io --alias=$ALIAS.datasette.io --extra-options='--config template_debug:1'
- stage: release tagged version
if: tag IS present
python: 3.6

Wyświetl plik

@ -126,6 +126,11 @@ CONFIG_OPTIONS = (
False,
"Force URLs in API output to always use https:// protocol",
),
ConfigOption(
"template_debug",
False,
"Allow display of template debug information with ?_context=1",
),
)
DEFAULT_CONFIG = {option.name: option.default for option in CONFIG_OPTIONS}

Wyświetl plik

@ -1,6 +1,7 @@
import asyncio
import csv
import itertools
import json
import re
import time
import urllib
@ -138,29 +139,28 @@ class BaseView(AsgiView):
)
extra_template_vars.update(extra_vars)
return Response.html(
await template.render_async(
{
**context,
**{
"app_css_hash": self.ds.app_css_hash(),
"select_templates": select_templates,
"zip": zip,
"body_scripts": body_scripts,
"extra_css_urls": self._asset_urls(
"extra_css_urls", template, context
),
"extra_js_urls": self._asset_urls(
"extra_js_urls", template, context
),
"format_bytes": format_bytes,
"database_url": self.database_url,
"database_color": self.database_color,
},
**extra_template_vars,
}
template_context = {
**context,
**{
"app_css_hash": self.ds.app_css_hash(),
"select_templates": select_templates,
"zip": zip,
"body_scripts": body_scripts,
"extra_css_urls": self._asset_urls("extra_css_urls", template, context),
"extra_js_urls": self._asset_urls("extra_js_urls", template, context),
"format_bytes": format_bytes,
"database_url": self.database_url,
"database_color": self.database_color,
},
**extra_template_vars,
}
if request.args.get("_context") and self.ds.config("template_debug"):
return Response.html(
"<pre>{}</pre>".format(
escape(json.dumps(template_context, default=repr, indent=4))
)
)
)
return Response.html(await template.render_async(template_context))
class DataView(BaseView):

Wyświetl plik

@ -209,3 +209,22 @@ itself will result in new, uncachcacheed URL paths.
::
datasette mydatabase.db --config hash_urls:1
.. _config_template_debug:
template_debug
--------------
This setting enables template context debug mode, which is useful to help understand what variables are available to custom templates when you are writing them.
Enable it like this::
datasette mydatabase.db --config template_debug:1
Now you can add ``?_context=1`` or ``&_context=1`` to any Datasette page to see the context that was passed to that template.
Some examples:
* https://latest.datasette.io/?_context=1
* https://latest.datasette.io/fixtures?_context=1
* https://latest.datasette.io/fixtures/roadside_attractions?_context=1

Wyświetl plik

@ -1287,6 +1287,7 @@ def test_config_json(app_client):
"truncate_cells_html": 2048,
"force_https_urls": False,
"hash_urls": False,
"template_debug": False,
} == response.json

Wyświetl plik

@ -1073,3 +1073,16 @@ def test_zero_results(app_client, path):
soup = Soup(response.text, "html.parser")
assert 0 == len(soup.select("table"))
assert 1 == len(soup.select("p.zero-results"))
def test_config_template_debug_on():
for client in make_app_client(config={"template_debug": True}):
response = client.get("/fixtures/facetable?_context=1")
assert response.status == 200
assert response.text.startswith("<pre>{")
def test_config_template_debug_off(app_client):
response = app_client.get("/fixtures/facetable?_context=1")
assert response.status == 200
assert not response.text.startswith("<pre>{")