Skip to content

Commit 73dd150

Browse files
authored
tryfirst and trylast example
1 parent 1cc2a70 commit 73dd150

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pluggy/multiple-hooks-same-file.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,26 @@ def filters_from_request_2(request, database, datasette):
2626
Which allows you to write more than one plugin implementation function in the same Python module file.
2727

2828
Note that the `specname` feature requires [Pluggy 1.0.0](https://github.com/pytest-dev/pluggy/blob/main/CHANGELOG.rst#pluggy-100-2021-08-25) or higher.
29+
30+
These can be combined with `tryfirst=` and `trylast=`. This example adds one link at the start of the Datasette application menu and one at the end, using the [menu_links hook](https://docs.datasette.io/en/stable/plugin_hooks.html#menu-links-datasette-actor-request).
31+
32+
```python
33+
from datasette import hookimpl
34+
35+
36+
@hookimpl(specname="menu_links", tryfirst=True)
37+
def menu_links_1(datasette):
38+
return [
39+
{"href": datasette.urls.path("/"), "label": "Home"},
40+
]
41+
42+
43+
@hookimpl(specname="menu_links", trylast=True)
44+
def menu_links_2():
45+
return [
46+
{
47+
"href": "http://www.example.com/",
48+
"label": "Link at the end",
49+
},
50+
]
51+
```

0 commit comments

Comments
 (0)