{"id": "writing_plugins:writing-plugins-extra-hooks", "page": "writing_plugins", "ref": "writing-plugins-extra-hooks", "title": "Plugins that define new plugin hooks", "content": "Plugins can define new plugin hooks that other plugins can use to further extend their functionality. \n datasette-graphql is one example of a plugin that does this. It defines a new hook called graphql_extra_fields , described here , which other plugins can use to define additional fields that should be included in the GraphQL schema. \n To define additional hooks, add a file to the plugin called datasette_your_plugin/hookspecs.py with content that looks like this: \n from pluggy import HookspecMarker\n\nhookspec = HookspecMarker(\"datasette\")\n\n\n@hookspec\ndef name_of_your_hook_goes_here(datasette):\n \"Description of your hook.\" \n You should define your own hook name and arguments here, following the documentation for Pluggy specifications . Make sure to pick a name that is unlikely to clash with hooks provided by any other plugins. \n Then, to register your plugin hooks, add the following code to your datasette_your_plugin/__init__.py file: \n from datasette.plugins import pm\nfrom . import hookspecs\n\npm.add_hookspecs(hookspecs) \n This will register your plugin hooks as part of the datasette plugin hook namespace. \n Within your plugin code you can trigger the hook using this pattern: \n from datasette.plugins import pm\n\nfor (\n plugin_return_value\n) in pm.hook.name_of_your_hook_goes_here(\n datasette=datasette\n):\n # Do something with plugin_return_value\n pass \n Other plugins will then be able to register their own implementations of your hook using this syntax: \n from datasette import hookimpl\n\n\n@hookimpl\ndef name_of_your_hook_goes_here(datasette):\n return \"Response from this plugin hook\" \n These plugin implementations can accept 0 or more of the named arguments that you defined in your hook specification.", "breadcrumbs": "[\"Writing plugins\"]", "references": "[{\"href\": \"https://github.com/simonw/datasette-graphql\", \"label\": \"datasette-graphql\"}, {\"href\": \"https://github.com/simonw/datasette-graphql/blob/main/README.md#adding-custom-fields-with-plugins\", \"label\": \"described here\"}, {\"href\": \"https://pluggy.readthedocs.io/en/stable/#specs\", \"label\": \"Pluggy specifications\"}]"}