Anki 2.1.20: Updates to the hook system

Damien Elmes's Avatar

Damien Elmes

17 Jan, 2020 04:35 AM

Anki 2.1.20 introduces a new hook system that allows for better code completion and type checking than the old system. For a short explanation and link to some sample add-ons, please see

Add-ons using the old hooks should continue to work, as the new hooks will automatically call the old hooks in most cases, and a few runHook/runFilter calls have been left in the codebase in the places where the new hooks differ significantly from the old ones.

There are a bunch of hooks that previously were called with no arguments, such as when a new deck is created. The new hooks will tend to pass the newly created item into the callback, but will not do so for the legacy hooks, to avoid breaking old code.

If anyone has feedback on the naming or the arguments of the new hooks, please let me know during the beta testing period, so they can be changed prior to add-on authors relying on them.

To go along with these changes, the policy on adding new hooks has also changed. In the past, Anki's add-on documentation suggested hooks were best left for code paths that multiple add-ons wanted to extend, but this resulted in an over-reliance on monkey patching, and has made things fragile. The add-on documentation has been updated to encourage add-on authors to submit pull requests for any hooks they need, and the hope is that the bulk of add-ons can move away from monkey patching in the future.

Feedback welcome!

  1. 1 Posted by mnating on 17 Feb, 2020 10:20 AM

    mnating's Avatar

    I initially thought this Anki 2.1.20 update broke the Japanese Support (3918629684) add-on and the Kanji Colorizer stroke order diagrams (1964372878).

    These add-ons operate when editing notes. For example with the Japanese Support plugin, if I type お風呂に入った。 into a field named Expression, and the note has a field named Reading which is empty, then the addon generates the text お 風呂[ふろ]に 入[はい]った。. in the reading field. On 2.1.19, the generated text immediately appears in the reading field. In 2.1.20, the text is apparently immediately generated, but it does not appear until I click to another note then click back to the original note.

    Not sure whether this was intentional, but I wanted to point it out as I think it's rather inconvenient. (Sometimes I want the reading auto-generated, but want to append a few notes of my own after the auto-gen is complete. Now this process takes two extra clicks).

  2. Support Staff 2 Posted by Damien Elmes on 17 Feb, 2020 11:13 PM

    Damien Elmes's Avatar

    Thanks for the report, this is a bug in the handling of legacy filters, and should be fixed in the next beta.

  3. 3 Posted by dmg on 21 Feb, 2020 05:34 PM

    dmg's Avatar

    Is there a function to test what version of anki the plug in is running?

    thank you!

  4. 4 Posted by dmg on 21 Feb, 2020 07:08 PM

    dmg's Avatar

    I found the answer that seems to work in multiple versions of anki:

       from anki.utils import pointVersion
        from anki.utils import versionWithBuild
        version = versionWithBuild().split()[0]
  5. 5 Posted by mnating on 26 Feb, 2020 02:03 AM

    mnating's Avatar

    @Damien Thanks for the report, this is a bug in the handling of legacy filters, and should be fixed in the next beta.

    I can confirm it's fixed in 2.1.21beta2.

Comments are closed, but you can start a new discussion.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac