datasette icon indicating copy to clipboard operation
datasette copied to clipboard

instance_metadata not found

Open phildini opened this issue 9 months ago • 3 comments

STR:

  1. create a metadata.json with the below
  2. datasette {some database} -c metadata.json
  3. Browse to http://127.0.0.1:8001/meetings/upcoming, see the error

Hello! Running on datasette 1.0a17, with the following metadata:

{
    "databases": {
        "meetings": {
            "queries": {
                "upcoming": {
                    "sql": "select meeting, date, count(page) as pages from agendas where date >= current_date group by date, meeting order by date desc",
                    "title": "Upcoming agendas"
                }
            }
        }
    }
}

When I browse to http://127.0.0.1:8001/meetings/upcoming, I get the following errors:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1768, in route_path
    response = await view(request, send)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1961, in async_view_fn
    response = await async_call_with_supported_arguments(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/utils/__init__.py", line 1022, in async_call_with_supported_arguments
    return await fn(*call_with)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/views/table.py", line 753, in table_view
    response = await table_view_traced(datasette, request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/views/table.py", line 782, in table_view_traced
    resolved = await datasette.resolve_table(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1661, in resolve_table
    raise TableNotFound(db.name, table_name)
datasette.utils.asgi.TableNotFound: Table not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/utils/asgi.py", line 481, in __call__
    return await self.asgi(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/utils/asgi.py", line 186, in __call__
    await self.app(scope, receive, send)
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/asgi_csrf.py", line 144, in app_wrapped_with_csrf
    await app(scope, receive, wrapped_send)
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1725, in __call__
    return await self.route_path(scope, receive, send, path)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1774, in route_path
    return await self.handle_404(request, send, exception)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1891, in handle_404
    await self.handle_exception(request, send, exception or NotFound("404"))
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/app.py", line 1900, in handle_exception
    response = await await_me_maybe(hook)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/utils/__init__.py", line 116, in await_me_maybe
    value = await value
            ^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/handle_exception.py", line 65, in inner
    await template.render_async(
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1318, in render_async
    return self.environment.handle_exception()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/datasette/templates/error.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "templates/datasette/base.html", line 7, in top-level template code
    <meta name="description" content="{{ instance_metadata.title }} -- Meeting minutes, agendas, and civic transparency">
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/phildini/civicband/corkboard/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 490, in getattr
    return getattr(obj, attribute)
           ^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'instance_metadata' is undefined

Reading through the code I think that somehow there's an internal table for metadata that isn't being created?

phildini avatar Mar 20 '25 18:03 phildini

Can you share the base.html template you're using? I dont think the instance_metadata variable exists in that template context.

Also yes, canned queries are "config" now, which you can load like so:

uvx --with 'datasette>=1.0a' datasette meetings.db -c metadata.json

And http://127.0.0.1:8001/meetings/upcoming works as expected. Though I think the custom template might be the root cause here?

asg017 avatar Mar 20 '25 18:03 asg017

@asg017 I don't see where instance_metadataexists in the current base.html?

Anyway, here's my current base.html https://github.com/civicband/corkboard/blob/main/templates/datasette/base.html

phildini avatar Mar 20 '25 18:03 phildini

ooooh ok, It's defined here: https://github.com/civicband/corkboard/blob/87a8b0cb9175f784f54486a6757bee2046c931ed/plugins/template_vars.py#L8C1-L8C74

So if I run:

 uvx --with 'datasette==1.0a17' datasette meetings.db -c config.json

Where meetings.db is a blank database ( CREATE TABLE agendas(foo);), and config.json has:

{
    "databases": {
        "meetings": {
            "queries": {
                "upcoming": {
                    "sql": "select * from agendas",
                    "title": "Upcoming agendas"
                }
            }
        }
    }
}

Then I can navigate to The query page http://127.0.0.1:8001/meetings/upcoming just fine.

But, when I add those custom templates + plugins, that's where I see this error. A new templates/base.html page with these contents, and a minimal plugins/plugin.py plugin with the following contents:

from datasette import hookimpl

@hookimpl
def extra_template_vars(datasette):
    async def metadata():
        return {
            "instance_metadata": await datasette.get_instance_metadata(),
        }

    return metadata

And running it again with:

 uvx --with 'datasette==1.0a17' datasette meetings.db -c config.json --template-dir templates --plugins-dir plugins

Then I see that error.

Wait... now I don't see it?? I'm a bit confused now... But can at least confirm that queries can definitely be defined in config, and there's seems to be some unrelated issue with instance_metadata, but cant reliably get an error message with a good trace

asg017 avatar Mar 20 '25 19:03 asg017