Overview ================================================================================= |CIBadge|_ Notes ----- The basic unit in Anki is the ``Note``, which contains a fact to memorize. ``Note`` s correspond to one or more ``Card`` s. Here's how you create a ``Note``: :: my_note = genanki.Note( model=my_model, fields=['Capital of Argentina', 'Buenos Aires']) You pass in a ``Model``, discussed below, and a set of ``fields`` (encoded as HTML). Models ------ A ``Model`` defines the fields and cards for a type of ``Note``. For example: :: my_model = genanki.Model( 1607392319, 'Simple Model', fields=[ {'name': 'Question'}, {'name': 'Answer'}, ], templates=[ { 'name': 'Card 1', 'qfmt': '{{Question}}', 'afmt': '{{FrontSide}}
`` for images.
You *cannot* put ``
`` will *not* work. Media files should have unique filenames.
Note GUIDs
----------
``Note`` s have a ``guid`` property that uniquely identifies the note. If you import a new note that has the same GUID as an
existing note, the new note will overwrite the old one (as long as their models have the same fields).
This is an important feature if you want to be able to tweak the design/content of your notes, regenerate your deck, and
import the updated version into Anki. Your notes need to have stable GUIDs in order for the new note to replace the
existing one.
By default, the GUID is a hash of all the field values. This may not be desirable if, for example, you add a new field
with additional info that doesn't change the identity of the note. You can create a custom GUID implementation to hash
only the fields that identify the note:
::
class MyNote(genanki.Note):
@property
def guid(self):
return genanki.guid_for(self.fields[0], self.fields[1])
sort_field
----------
Anki has a value for each ``Note`` called the ``sort_field``. Anki uses this value to sort the cards in the Browse
interface. Anki also is happier if you avoid having two notes with the same ``sort_field``, although this isn't strictly
necessary. By default, the ``sort_field`` is the first field, but you can change it by passing ``sort_field=`` to ``Note()``
or implementing ``sort_field`` as a property in a subclass (similar to ``guid``).
You can also pass ``sort_field_index=`` to ``Model()`` to change the sort field. ``0`` means the first field in the Note, ``1`` means the second, etc.
YAML for Templates (and Fields)
-------------------------------
You can create your template definitions in the YAML format and pass them as a ``str`` to ``Model()``. You can also do this
for fields.
Using genanki inside an Anki addon
----------------------------------
``genanki`` supports adding generated notes to the local collection when running inside an Anki 2.1 addon (Anki 2.0
may work but has not been tested). See the [`.write_to_collection_from_addon() method`](
https://github.com/kerrickstaley/genanki/blob/0c2cf8fea9c5e382e2fae9cd6d5eb440e267c637/genanki/__init__.py#L275).
FAQ
---
My field data is getting garbled
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If fields in your notes contain literal ``<``, ``>``, or ``&`` characters, you need to HTML-encode them: field data is HTML, not plain text. You can use the [`html.escape`](https://docs.python.org/3/library/html.html#html.escape) function.
For example, you should write
::
fields=['AT&T was originally called', 'Bell Telephone Company']
or
::
fields=[html.escape(f) for f in ['AT&T was originally called', 'Bell Telephone Company']]
This applies even if the content is LaTeX; for example, you should write
::
fields=['Piketty calls this the "central contradiction of capitalism".', '[latex]r > g[/latex]']
Publishing to PyPI
------------------
If your name is Kerrick, you can publish the ``genanki`` package to PyPI by running these commands from the root of the ``genanki`` repo:
::
rm -rf dist/*
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
Note that this directly uploads to prod PyPI and skips uploading to test PyPI.
.. |CIBadge| image:: https://github.com/kerrickstaley/genanki/actions/workflows/ci.yml/badge.svg
:alt: CI
.. _CIBadge: https://github.com/kerrickstaley/genanki/actions/workflows/ci.yml