Getting the Answer Text of "Basic type in the answer" Cards

Hello Dear Anki Devs :slight_smile:

I am working on a Add-on for my university, in which I want to save some data about the card, the user answered. For Example, how long did the user took to answer, or what card the user answered.

What is my goal?
Save the text of the card in a temporary object. Including “Question” and “Answer” texts of the card.

What is my Problem?
The text I am getting from the Anki Card Object is the front HTML of the card with an ID-Tag, or sometimes empty. I could not figure out when it is empty. Maybe just a code error on my side.

What have I tried?
The following:

I found a method of extracting the “Question” of a card via python:

temp_card_data.question = data.render_output().question_text

which works as expected. The content of ‘temp_card_data.question‘ is the plain text the card holds in it’s “Question-Field” without HTML.

Then I tried the following for the “Answer” text, in this case for a simple ‘print’ statement:

print("Answer Text_'data.answer()': " + str(data.answer()))
        print("Answer Text_'render_output().answer_text': " + str(data.render_output().answer_text))

The first one results in the following console-output:

Answer Text_'data.answer()': <style>.card {
    font-family: arial;
    font-size: 20px;
    line-height: 1.5;
    text-align: center;
    color: black;
    background-color: white;
}
</style>Welche Frabe hat die Sonne?

<hr id=answer>

[[type:Back]]

It includes some formatting information, the “Question” text and ‘


‘

The second try ‘data.render_output().answer_text‘ results in this one:

Answer Text_'render_output().answer_text': Welche Frabe hat die Sonne?

<hr id=answer>

[[type:Back]]

Which is only missing the style-tag.

Question

How is it possible to get the clean rendered text of the “Answer” field? For “Basic type in the answer” Cards.

More Detail of the Card I am using to test this in the png’s.
I am happy for any bit of help and looking forward to chat, Moritz (:

Do you mean you want the type-in field rendered as HTML? That’s not available as a standalone method, but you can copy the code used in the reviewer to render the input:

Thank you for you’r reply (: I dont think I want the Input, when Input is what the user typed in.
Or I misunderstood you.

And I think I have a better example, to explain what I want to archive:

This is my Type in the Answer Card. It has the Question Text “Welche Farbe hat das Wasser?” and the Answer Text “Blau”.

Now, in my own code, which gets executed in the “reviewer_did_answer_trigger()“.

When I execute the following lines of Code on a Normal Card:

temp_card_data.question = data.render_output().question_text
temp_card_data.answer = data.render_output().answer_text

After that, the fields `temp_card_data.question` holds the String “Welche Farbe hat das Wasser?“
and the `temp_card_data.answer` holds the string “Blau“.

If I execute the lines on a “Type in the answer card” I get:
`temp_card_data.question` holds the String “Welche Farbe hat das Wasser?“
`temp_card_data.answer` holds the string “Welche Farbe hat das Wasser?


[[type:Back]]“.

But I would like to get “Blau”. And that is what confuses me.

If you have access to a Card object and you want to get fields contents, use something like this:

note = card.note()
field = note["Back"]
2 Likes

Thank you very much :grin: this was exactly what I searched for, I am very happy. Have a lovely week!

2 Likes