Registering new Datasette plugin hooks by defining them in other plugins

I'm experimenting with a Datasette plugin that itself adds new plugin hooks which other plugins can then interact with.

It's called datasette-low-disk-space-hook, and it adds a new plugin hook called low_disk_space(datasette), defined in the datasette_low_disk_space_hook/ module.

The hook is registered by this code in datasette_low_disk_space_hook/

from datasette.utils import await_me_maybe
from datasette.plugins import pm
from . import hookspecs


This imports the plugin manager directly from Datasette and uses it to add the new hooks.

I was worried that the pm.add_hookspects(hookspecs) line was not guaranteed to be executed if that module had not been imported.

It turns out that having this entrpoints= line in is enough to ensure that the module is imported and the pm.add_hookspecs() line is executed:

from setuptools import setup

    # ...
    entry_points={"datasette": ["low_disk_space_hook = datasette_low_disk_space_hook"]},
    # ...

Created 2022-06-17T13:04:35-07:00 · Edit