mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Added template_debug setting, closes #654
This commit is contained in:
parent
ceef5ce684
commit
d54318fc7f
6 changed files with 61 additions and 23 deletions
|
|
@ -27,7 +27,7 @@ jobs:
|
||||||
- npm install -g now
|
- npm install -g now
|
||||||
- python tests/fixtures.py fixtures.db fixtures.json
|
- python tests/fixtures.py fixtures.db fixtures.json
|
||||||
- export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7`
|
- 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
|
- stage: release tagged version
|
||||||
if: tag IS present
|
if: tag IS present
|
||||||
python: 3.6
|
python: 3.6
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,11 @@ CONFIG_OPTIONS = (
|
||||||
False,
|
False,
|
||||||
"Force URLs in API output to always use https:// protocol",
|
"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}
|
DEFAULT_CONFIG = {option.name: option.default for option in CONFIG_OPTIONS}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import csv
|
import csv
|
||||||
import itertools
|
import itertools
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
|
|
@ -138,29 +139,28 @@ class BaseView(AsgiView):
|
||||||
)
|
)
|
||||||
extra_template_vars.update(extra_vars)
|
extra_template_vars.update(extra_vars)
|
||||||
|
|
||||||
return Response.html(
|
template_context = {
|
||||||
await template.render_async(
|
**context,
|
||||||
{
|
**{
|
||||||
**context,
|
"app_css_hash": self.ds.app_css_hash(),
|
||||||
**{
|
"select_templates": select_templates,
|
||||||
"app_css_hash": self.ds.app_css_hash(),
|
"zip": zip,
|
||||||
"select_templates": select_templates,
|
"body_scripts": body_scripts,
|
||||||
"zip": zip,
|
"extra_css_urls": self._asset_urls("extra_css_urls", template, context),
|
||||||
"body_scripts": body_scripts,
|
"extra_js_urls": self._asset_urls("extra_js_urls", template, context),
|
||||||
"extra_css_urls": self._asset_urls(
|
"format_bytes": format_bytes,
|
||||||
"extra_css_urls", template, context
|
"database_url": self.database_url,
|
||||||
),
|
"database_color": self.database_color,
|
||||||
"extra_js_urls": self._asset_urls(
|
},
|
||||||
"extra_js_urls", template, context
|
**extra_template_vars,
|
||||||
),
|
}
|
||||||
"format_bytes": format_bytes,
|
if request.args.get("_context") and self.ds.config("template_debug"):
|
||||||
"database_url": self.database_url,
|
return Response.html(
|
||||||
"database_color": self.database_color,
|
"<pre>{}</pre>".format(
|
||||||
},
|
escape(json.dumps(template_context, default=repr, indent=4))
|
||||||
**extra_template_vars,
|
)
|
||||||
}
|
|
||||||
)
|
)
|
||||||
)
|
return Response.html(await template.render_async(template_context))
|
||||||
|
|
||||||
|
|
||||||
class DataView(BaseView):
|
class DataView(BaseView):
|
||||||
|
|
|
||||||
|
|
@ -209,3 +209,22 @@ itself will result in new, uncachcacheed URL paths.
|
||||||
::
|
::
|
||||||
|
|
||||||
datasette mydatabase.db --config hash_urls:1
|
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
|
||||||
|
|
|
||||||
|
|
@ -1287,6 +1287,7 @@ def test_config_json(app_client):
|
||||||
"truncate_cells_html": 2048,
|
"truncate_cells_html": 2048,
|
||||||
"force_https_urls": False,
|
"force_https_urls": False,
|
||||||
"hash_urls": False,
|
"hash_urls": False,
|
||||||
|
"template_debug": False,
|
||||||
} == response.json
|
} == response.json
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1073,3 +1073,16 @@ def test_zero_results(app_client, path):
|
||||||
soup = Soup(response.text, "html.parser")
|
soup = Soup(response.text, "html.parser")
|
||||||
assert 0 == len(soup.select("table"))
|
assert 0 == len(soup.select("table"))
|
||||||
assert 1 == len(soup.select("p.zero-results"))
|
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>{")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue