from anki.collection import Collection
from aqt.operations import QueryOp
from aqt.utils import showInfo
from aqt import mw
def my_background_op(col: Collection, note_ids: list[int]) -> int:
# some long-running op, eg
for id in note_ids:
note = col.get_note(note_id)
# ...
return 123
def on_success(count: int) -> None:
showInfo(f"my_background_op() returned {count}")
def my_ui_action(note_ids: list[int]):
op = QueryOp(
# the active window (main window in this case)
parent=mw,
# the operation is passed the collection for convenience; you can
# ignore it if you wish
op=lambda col: my_background_op(col, note_ids),
# this function will be called if op completes successfully,
# and it is given the return value of the op
success=on_success,
)
# if with_progress() is not called, no progress window will be shown.
# note: QueryOp.with_progress() was broken until Anki 2.1.50
op.with_progress().run_in_background()