From 4d119cada250d3545269334ed949888e3d88cfe3 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 2 Feb 2021 16:27:18 +0100 Subject: [PATCH] Add ephemeral card method --- pylib/anki/notes.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index 456fd28bd..5c626a49d 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -4,6 +4,7 @@ from __future__ import annotations import pprint +import copy from typing import Any, List, Optional, Sequence, Tuple import anki # pylint: disable=unused-import @@ -75,6 +76,27 @@ class Note: def joinedFields(self) -> str: return joinFields(self.fields) + def ephemeral_card(self, ord=0, *, fill_empty=False) -> anki.cards.Card: + card = anki.cards.Card(self.col) + card.ord = ord + card.did = 1 + + model = self.model() + template = copy.copy(model["tmpls"][ord]) + # may differ in cloze case + template["ord"] = card.ord + + output = anki.template.TemplateRenderContext.from_card_layout( + self, + card, + notetype=model, + template=template, + fill_empty=fill_empty, + ).render() + card.set_render_output(output) + card._note = self + return card + def cards(self) -> List[anki.cards.Card]: return [self.col.getCard(id) for id in self.card_ids()]