Skip to content

Commit

Permalink
Added template_debug setting, closes #654
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 22, 2019
1 parent ceef5ce commit d54318f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
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}

Expand Down
44 changes: 22 additions & 22 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import csv
import itertools
import json
import re
import time
import urllib
Expand Down Expand Up @@ -138,29 +139,28 @@ async def render(self, templates, request, context):
)
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):
Expand Down
19 changes: 19 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
13 changes: 13 additions & 0 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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>{")

0 comments on commit d54318f

Please sign in to comment.