Programmatic card templates

I have now created various note types and templates and have dealt with the logic behind them. Would not call myself a master, but I was able to gather some insights.

I have noticed that especially with note types with a large number of fields and therefore often card templates, the creation process and editing process is significantly hampered. Even for note types with 10 card templates, it is very time-consuming to make changes that affect all templates, because each template must be called and adjusted individually.

Since this cannot be solved simply by Copy and Past (due to the field variables), I suggest that an automated method be added to the current manual creation of card templates.

The goal is that both procedures generate the same outcome, which means that automatically generated card templates can/should be displayed in the same user mask. With the only difference that the automatically created ones are exclusively customizable programmatically.

Since there is a lot of Python in use anyway, one could imagine the following approach.

In principle, only a specific function name and a specific function signature would have to be specified/standardized. Afterwards “raise” returns would have to be caught AND/OR the return value.

import typing

def template(fields:typing.List[field]=[]) -> TemplateNote:
	function gets list of field names and creates and returns 0<n card templates.

	list of field names provided in note index order
	# ...
	return []

class field:
	def __init__(self, name, index, content): = name # field name
		self.index = index # field index within note
		self.content = content # raw field content
	def name(self) -> str:
	def index(self) -> int:
		return self.index # 0<n
	def content(self) -> str:
		return self.content

class TemplateCard:
	def template(self) -> str:
		return "" # returns template as string

class TemplateNote:
	def style(self) -> str:
		returns css styling as string; this method could later be moved inside TemplateCard for card specific styling
		return ""
	def templates(self) -> typing.List[TemplateCard]:
		return [] # returns list of all templates created

This could pose significant risks to the user due to insufficient resource constraints on the function.
Solution: Therefore, the function should not have access to resources or data other than those provided by the function arguments and those in the script itself.

Ohhh … card template needs of course the following signature

class TemplateCard:
	def templateFront(self) -> str:
		return "" # returns template as string
	def templateBack(self) -> str:
		return "" # returns template as string

The classes/exchange format definitions are provided as part of the ANKI environment and do not have to be redefined by the user. Consequently, only the “template” function would be defined by the user.

Programming language:
… and can of course also be implemented in JS, since it is used anyway in many places and users thus do not have to learn another programming language.

Anki already has a Python API that can be used to create notetypes programmatically. Check the LPCG add-on’s code for an example: GitHub - sobjornstad/AnkiLPCG: Addon for dae/anki for studying lyrics and poetry

1 Like

That’s true, but I was thinking, as described above, of a solution that does not require an addon or the creation of a custom addon, but is accessible in the “normal” user interface.