Adding dynamic routing to the 'AnkiQt' class

Currently, the AnkiQt class contains hardcoded references to all pages displayed in the main window. I’d like to introduce dynamic routing to make it easier for addons to define their own pages or replace existing ones.

Here’s a first draft of what an addon using this system could look like:

from aqt import mw
from aqt.router import *

test = Widget()
test.html = """
<div style="text-align:center;">
  <div>First Page</div>
  <button onclick='return pycmd("open:second")'>GO next</button>
</div>
"""
page = Page("", test, "Decks")
mw.router.add_page(page)
    
test2 = Widget()
test2.html = """
<div style="text-align:center;">
  <div>Second Page</div>
  <button onclick='return pycmd("open:")'>GO back</button>
</div>
"""
page = Page("second", test2, "Second")
mw.router.add_page(page)

ezgif-14b0ae20ff08b6

Think this is worth finishing and PR’ing into Anki?

Later, I’d like to make the UI even more addon-friendly e.g. by introducing a registry of actions associated with each entity type (such as decks). Addons could contribute actions to this registry, and any UI component that works with a given entity could then dynamically populate context menus or buttons based on the registered actions.

2 Likes

Anki’s UI is being slowly migrated to Svelte. Introducing such API at this stage is probably not worth it since it’ll almost certainly need to be reworked in the near future.

2 Likes