Mw.progress.start() is not showing progress

So I’m running some code in my addon which has a seriously long wait time. I want to have a progress bar come up that shows that the operation is running and progress is happening so I used the following code:

def generate_meanings() -> None:
    ids = mw.col.find_notes("tag:i+1")
    mw.progress.start(label="Updating Notes", max=len(ids), immediate=True).show()
    for i, _id in enumerate(ids): 
        note = mw.col.getNote(_id)
        if i % 500 == 0:
            mw.progress.update(value=i)
        if not note["Meaning"] and note["Focus"] and ("," not in note["Focus"]):
            search_word = note["Focus"]
            meaning_list = return_search(search_word)
            for meaning in meaning_list:
                note["Meaning"] += meaning + "<br>"
            note.flush()
    mw.progress.finish()
    showInfo("All Meanings Added")

the progress bar opens but then it just goes not responding and the window itself doesn’t update. I’m super new to programming anki addons so no sure how to fix this

I believe you need to run the function in a background thread for the progress dialog to show up correctly. Try something like this (not tested):

def generate_meanings() -> None:
    ids = mw.col.find_notes("tag:i+1")
    mw.taskman.run_on_main(lambda: mw.progress.start(label="Updating Notes", max=len(ids), immediate=True))
    for i, _id in enumerate(ids): 
        note = mw.col.getNote(_id)
        if i % 500 == 0:
            mw.taskman.run_on_main(lambda: mw.progress.update(value=i))
        if not note["Meaning"] and note["Focus"] and ("," not in note["Focus"]):
            search_word = note["Focus"]
            meaning_list = return_search(search_word)
            for meaning in meaning_list:
                note["Meaning"] += meaning + "<br>"
            note.flush()
    def on_finish():
        mw.progress.finish()
    	showInfo("All Meanings Added")
    mw.taskman.run_on_main(on_finish)

mw.taskman.run_in_background(generate_meanings)

You have to be careful not to run any GUI operations inside generate_meanings() without using run_on_main() in this case, as that can cause crashes and hang-ups. Please see Background Operations - Writing Anki Add-ons

2 Likes