Skip to main content

fastanki

fastanki reads and writes Anki’s collection format and speaks the AnkiWeb sync protocol directly, in Python. There’s no dependency on the Anki application or its Rust library: your cards live in a small sqlite file of fastanki’s own, and reach your desktop and phone the same way any Anki client’s changes do, by syncing through AnkiWeb. Media files sync too: add_media a picture or sound, cite it in a field, and it reaches your other devices alongside the cards.

Usage

Installation

Install latest from pypi

$ pip install fastanki

Documentation

from fastanki import *
import os, tempfile

Functional API

add_card lets you create a new card with a single function call. Just pass your field values as keyword arguments. By default it uses the Basic note type and Default deck, but you can specify any model, deck, or tags you like.

notezh = add_card(fields={'Front':'你好','Back':'hello'})

find_cards searches your collection and returns a list of Card objects. Criteria are keyword arguments, all optional and combined with AND:

  • deck='Spanish' matches that deck and its subdecks
  • tag='vocab' matches a tag
  • added_days=7 matches cards added in the last week
  • is_due=True matches cards due for review
  • Any other keyword is a field name, matched as a case-insensitive substring: Front='hello'
  • where="n.id=?", args=(nid,) drops through to SQL over notes n joined with cards c
cards = find_cards(deck='Default')
cards
[Card(1764738198390, nid=1764738198390, due=49, ivl=0, queue=0)]
cards[0]

Card 1764738198390 (nid: 1764738198390, due: 49, ivl: 0d, queue: 0)

find_card_ids(deck='Default')
[1764738198390]

find_notes takes the same criteria and returns one Note per matching note, where find_cards may return several cards for a note (a Cloze note generates one card per cloze number, for instance).

notes = find_notes(fields={'Back':'hello'})
notes
[Note(1764738198390, Front='你好', Back='hello', tags=[])]
note = notes[0]
note

Front: 你好 | Back: hello

find_note_ids(fields={'Back':'hello'})
[1764738198390]

update_note modifies an existing note’s fields and/or tags. Pass either a Note object or a note ID, along with any fields you want to change as keyword arguments. For tags:

  • tags=['a','b'] — replaces all tags
  • add_tags='newtag' — adds without removing existing tags
update_note(note, Back="updated answer", tags='testtag')

Front: 你好 | Back: updated answer | 🏷️ testtag

update_note(note, add_tags='moretagz')

Front: 你好 | Back: updated answer | 🏷️ testtag, moretagz

get_note(note.id)

Front: 你好 | Back: updated answer | 🏷️ moretagz, testtag

del_note([notezh, note])

✓ 1 change(s)

sync connects to AnkiWeb: pass your credentials the first time, and they’re saved (as a host key, not your password) for later calls. The first sync of a fresh collection is a full download of your existing AnkiWeb collection; after that, syncs exchange deltas in both directions. fastanki will never replace a non-empty server collection without an explicit upload=True. Each sync also syncs media: to put an image or sound on a card, add_media the file and cite the returned name in a field with <img src="name"> or [sound:name].

sync(user=os.environ['ANKI_USER'], passw=os.environ['ANKI_PASS'])  # first time
sync()  # after that
host_number: 5

Tool use

anki_tools()
&`[add_fb_card, find_notes, find_note_ids, find_cards, find_card_ids, get_note, del_card, update_fb_note, sync]`

Here are the available tools: &[add_card, add_fb_card, add_cloze_card, find_notes, find_note_ids, find_cards, find_card_ids, get_note, del_note, update_fb_note, sync].

Try to find all my notes. List the IDs and contents you see.

Delete them.

Try finding all notes again.

Try adding a note of your choice using add_fb_card and tell me the id.

Try finding all notes again.

OK try get_note with it.

Delete it now.

OK create, update, and verify a note now.

Try the various find ones that we haven’t done yet.

Sure. Delete that note, then sync.

OO API

The functional API opens and closes the collection on every call. For a batch of work, Collection keeps it open, and everything the functions above do is a method here.

col = Collection.open()
col.path.name
col.notetypes(), col.decks()
n = col.add(Front='adiós', Back='goodbye', deck='Spanish::Vocab', tags=['spanish'])
n
col.due_counts('Spanish')
col.find_notes(deck='Spanish')
col.remove_deck('Spanish')
col.close()

Collection is also a context manager, so a one-shot batch reads naturally:

with Collection.open() as c: c.add(Front='hola', Back='hello')

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastanki-0.0.4.tar.gz (55.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastanki-0.0.4-py3-none-any.whl (55.7 kB view details)

Uploaded Python 3

File details

Details for the file fastanki-0.0.4.tar.gz.

File metadata

  • Download URL: fastanki-0.0.4.tar.gz
  • Upload date:
  • Size: 55.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for fastanki-0.0.4.tar.gz
Algorithm Hash digest
SHA256 07925ad61d694cf13d35fc9625606a436cd2de208f648c123533b63b43b7852c
MD5 81bba1baac529f197006bbd482ecacdc
BLAKE2b-256 edab74645c748bdb9731b3896162bd905aee3b1134045877e4c60df90f857f56

See more details on using hashes here.

File details

Details for the file fastanki-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: fastanki-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 55.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for fastanki-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5defb1220e5ae885cc281b844f4ef0eb349e518ff63aea2cd58393a9a972aea5
MD5 1573dd0dd6fd83535a429a1dabee53ed
BLAKE2b-256 b18c6ab47f2dda300cb86ebc54e45a68a9366bcfc3a4bb023a3a8fd6263575cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page