Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index.html is not reliably loaded from a plugin #697

Closed
simonw opened this issue Mar 8, 2020 · 7 comments
Closed

index.html is not reliably loaded from a plugin #697

simonw opened this issue Mar 8, 2020 · 7 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Mar 8, 2020

Lots of detail in simonw/datasette-search-all#2 - short version is that I have a plugin with its own index.html template and Datasette intermittently fails to load it and uses the default index.html that ships with Datasette instead.

Related:

@simonw simonw added the bug label Mar 8, 2020
@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

I think the bug is in this code:

datasette/datasette/app.py

Lines 612 to 623 in 7508477

default_templates = str(app_root / "datasette" / "templates")
template_paths = []
if self.template_dir:
template_paths.append(self.template_dir)
template_paths.extend(
[
plugin["templates_path"]
for plugin in get_plugins(pm)
if plugin["templates_path"]
]
)
template_paths.append(default_templates)

I added some debug logging to it and found that the bit that returns the plugin["templates_path"] list is producing this:

plugin_template_paths: [
  '/Users/simonw/Dropbox/Development/datasette/datasette/templates',
  '/Users/simonw/Dropbox/Development/datasette/datasette/templates',
  '/Users/simonw/Dropbox/Development/datasette-search-all/datasette_search_all/templates'
]

@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

More debugging code shows this:

[
    (
        {
            "name": "datasette.facets",
            "static_path": "/Users/simonw/Dropbox/Development/datasette/datasette/static",
            "templates_path": "/Users/simonw/Dropbox/Development/datasette/datasette/templates",
        },
        "/Users/simonw/Dropbox/Development/datasette/datasette/templates",
    ),
    (
        {
            "name": "datasette.sql_functions",
            "static_path": "/Users/simonw/Dropbox/Development/datasette/datasette/static",
            "templates_path": "/Users/simonw/Dropbox/Development/datasette/datasette/templates",
        },
        "/Users/simonw/Dropbox/Development/datasette/datasette/templates",
    ),
    (
        {
            "name": "datasette-search-all",
            "static_path": None,
            "templates_path": "/Users/simonw/Dropbox/Development/datasette-search-all/datasette_search_all/templates",
            "version": "0.1",
        },
        "/Users/simonw/Dropbox/Development/datasette-search-all/datasette_search_all/templates",
    ),
]

So the problem is that the default plugins that ship with Datasette are adding a template path to the lookup list!

@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

I think the bug is in this code:

def get_plugins(pm):
plugins = []
plugin_to_distinfo = dict(pm.list_plugin_distinfo())
for plugin in pm.get_plugins():
static_path = None
templates_path = None
try:
if pkg_resources.resource_isdir(plugin.__name__, "static"):
static_path = pkg_resources.resource_filename(plugin.__name__, "static")
if pkg_resources.resource_isdir(plugin.__name__, "templates"):
templates_path = pkg_resources.resource_filename(
plugin.__name__, "templates"
)
except (KeyError, ImportError):
# Caused by --plugins_dir= plugins - KeyError/ImportError thrown in Py3.5
pass
plugin_info = {
"name": plugin.__name__,
"static_path": static_path,
"templates_path": templates_path,
}
distinfo = plugin_to_distinfo.get(plugin)
if distinfo:
plugin_info["version"] = distinfo.version
plugin_info["name"] = distinfo.project_name
plugins.append(plugin_info)
return plugins

@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

I need that code to set static_path and templates_path to None for plugins that ship as part of Datasette.

@simonw simonw closed this as completed in e1b5339 Mar 8, 2020
simonw added a commit that referenced this issue Mar 8, 2020
@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

I'm going to release this as 0.37.2.

This is the first time I've released a version NOT from master (since I don't want to ship the --memory feature for Cloud Run until 0.38 - #694). So I've cut a v0.37.2 release branch instead.

@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

Cancel that plan: I'm pretty sure the Travis configuration that publishes a demo to Zeit Now and builds a Docker image isn't designed to handle releases that don't correspond to current master. I guess I'll release 0.38 instead.

@simonw
Copy link
Owner Author

simonw commented Mar 8, 2020

Shipped in Datasette 0.38.

simonw added a commit to simonw/datasette-search-all that referenced this issue Mar 8, 2020
Needed this bug fix: simonw/datasette#697

Closes #2
simonw added a commit that referenced this issue Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant