How to create decks in anki using it's API?

Below give is a function

def __create_decks(decks: list) -> bool:
    """Create decks from the list provide with 
    Returns True if decks are successfully created
    Returns False if decks are not created successfully"""
    for deck_name in decks:
        deck = mw.col.decks.newDyn(deck_name)
        mw.col.decks.save([deck])
    return True

But it gives me the following error

Caught exception:
Traceback (most recent call last):
  File "C:\Users\amanr\AppData\Roaming\Anki2\addons21\myaddon2\main.py", line 23, in create_multiple_decks
    if __create_decks(info):
  File "C:\Users\amanr\AppData\Roaming\Anki2\addons21\myaddon2\main.py", line 37, in __create_decks
    mw.col.decks.save([deck])
  File "anki.decks", line 98, in save
  File "anki.decks", line 263, in update
  File "anki._backend_generated", line 366, in add_or_update_deck_legacy
  File "anki._backend", line 156, in _run_command
anki.errors.BackendError: JsonError { info: "invalid type: sequence, expected a map at line 1 column 0" }

If someone knows why this error is generated and how to fix it then please help

Use AnkiConnect

I think you didn’t get it.
I’m creating an addon which will work in anki and at some point in time addon needs to create anki decks from a list of deck_names. That’s what I want!

.newDyn() creates a filtered deck, and it returns the deck id, while .save() actually expects you to pass it a DeckDict object. If you really mean to create filtered decks, then just remove the .save() call and your code should work (it’s not actually needed here).

If you want to create normal decks though, try this modified function:

def __create_decks(decks: list) -> bool:
    for deck_name in decks:
        mw.col.decks.id(deck_name)
    return True

.save() is not needed here too. Consult the source to see where it’s useful: anki/decks.py at main · ankitects/anki · GitHub

1 Like

Thank You sooooooo much.
Just because of you, my addon is finally completed. Is there anything I can do for you like help is some hardware-based software development?

1 Like

Thank you. I see you have some cool embedded system projects in your GitHub. I don’t do a lot of hardware/low-level programming but it’s a field I’m very interested in getting into. Maybe you’ll find something useful in my related projects.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.