Progress indicator (mw.progress.start) in .45+ / basic printing support

to show that some longer action was running you could use some code like this in the past:

mw.progress.start()
# some action
mw.progress.finish()

see e.g. Damien’s Basic Printing Support add-on, source.

Up to version .44 this code in an add-on gave you a spinning wheel while the action was running, after .45+ this rotating wheel is no longer shown so that there’s no feedback that the action is running.

How would you fix the code for .45+? Is there another add-on that actually shows the rotating wheel during longer running actions on versions .45+?

I think you’re supposed to use QueryOp or CollectionOp now.
Also see Add-on porting notes for Anki 2.1.45.

3 Likes

I added some notes about this to the docs today:

https://addon-docs.ankiweb.net/background-ops.html

3 Likes

(the fact that direct calls worked in the past was due to ugly hacks that updated the UI when progress.update() or the DB progress hook was called - these resulted in choppy updates, and could lead to crashes)

1 Like

dae: Thank you very much. Your step-by-step is very useful for someone like me.

Rumo: I could/should have thought of checking this …

@dae Could you add some documentation on how to update the progress bar in this case?

Also I think there is some errors in the documentation code. The function is name “my_background_op” in the def but “my_background_operation” elsewhere. Also the loop variable is called id, but then the loop uses “note_id”.

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_operation**(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()

Thank you, I’ve fixed the incorrect function name. That page has an example of how progress can be updated from within the op - see the “run_on_main” part.

1 Like