Anki prompts that the card is imported successfully, but the card is not there

I use genanki library to generate a anki deck, when i import it to anki, it prompts successfully added, but when there is nohting in anki, i search it in the browser, there is no any card.Anyone can help? Thanks in advance!


Here is my main code:

import inspect
import random
import genanki
import inspect


# Filename of the Anki deck to generate
deck_filename = "prefix_and_suffix123.apkg"

# Title of the deck as shown in Anki
anki_deck_title = "prefix_and_suffix123"

# type of the card model
anki_model_name = "prefix_and_suffix123"

# Create the deck model

model_id = random.randrange(1 << 30, 1 << 31)

style = inspect.cleandoc("""
    .card {
        font-family: helvetica neue;
        font-size: 14px;
        line-height: 200%;
    }

   .cloze {
        font-weight: bold;
        color: blue;
    }
    
    .box {
            text-align: center;
            margin: auto;
    }

    .examples {
            margin: auto;
            margin-top: 50px;
            text-align: left;
            max-width: fit-content;
            border: solid 0.5px rgb(221, 205, 205);
            padding: 20px;
    }
""")

anki_model = genanki.Model(
    model_id,
    anki_model_name,
    fields=[
        {'name': 'Text'}, {'name': 'Extra'}
    ],
    templates=[
        {
            "name": "Cloze Card",
            # question
            "qfmt": inspect.cleandoc("""
                <div class="box">
                {{cloze:Text}}
                </div>
            """),

            # anwser
            "afmt": inspect.cleandoc("""
                <div class="box">
                {{cloze:Text}}<br>
                {{Extra}}
                </div>
            """),
        }
    ],
    css=style,
)


# The list of flashcards
anki_notes = []


def add_anki_notes(note_info):
    anki_note = genanki.Note(
        model=anki_model,
        fields=[note_info, ''],
    )
    anki_notes.append(anki_note)


def write_to_file():
    anki_deck = genanki.Deck(model_id, anki_deck_title)
    anki_package = genanki.Package(anki_deck)

    # Add flashcards to the deck
    for anki_note in anki_notes:
        anki_deck.add_note(anki_note)

    # Save the deck to a file
    anki_package.write_to_file(deck_filename)

    print("Created deck with {} flashcards".format(len(anki_deck.notes)))

You’re not adding any notes to the deck package you’re generating. add_anki_notes is never called in your code.

1 Like

It’s also possible that genanki is doing something incorrectly - the best person to ask about it would be its author.

I omitted the card text. I solved this by specify model_type=1(cloze type, default is basic type) when I instantiate genanki.Model().