anki.collection#
Attributes#
Classes#
Functions#
|
Module Contents#
- anki.collection.HelpPage#
- anki.collection.SearchNode#
- anki.collection.Progress#
- anki.collection.EmptyCardsReport#
- anki.collection.GraphPreferences#
- anki.collection.CardStats#
- anki.collection.Preferences#
- anki.collection.UndoStatus#
- anki.collection.OpChanges#
- anki.collection.OpChangesOnly#
- anki.collection.OpChangesWithCount#
- anki.collection.OpChangesWithId#
- anki.collection.OpChangesAfterUndo#
- anki.collection.BrowserRow#
- anki.collection.BrowserColumns#
- anki.collection.StripHtmlMode#
- anki.collection.ImportLogWithChanges#
- anki.collection.ImportAnkiPackageRequest#
- anki.collection.ImportAnkiPackageOptions#
- anki.collection.ExportAnkiPackageOptions#
- anki.collection.ImportCsvRequest#
- anki.collection.CsvMetadata#
- anki.collection.DupeResolution#
- anki.collection.Delimiter#
- anki.collection.TtsVoice#
- anki.collection.GetImageForOcclusionResponse#
- anki.collection.AddImageOcclusionNoteRequest#
- anki.collection.GetImageOcclusionNoteResponse#
- anki.collection.AddonInfo#
- anki.collection.CheckForUpdateResponse#
- anki.collection.MediaSyncStatus#
- anki.collection.FsrsItem#
- anki.collection.FsrsReview#
- anki.collection.SearchJoiner#
- anki.collection.ExportLimit#
- class anki.collection.ComputedMemoryState#
- desired_retention: float#
- stability: float | None = None#
- difficulty: float | None = None#
- decay: float | None = None#
- class anki.collection.AddNoteRequest#
- note: anki.notes.Note#
- deck_id: anki.decks.DeckId#
- class anki.collection.Collection(path: str, backend: anki._backend.RustBackend | None = None, server: bool = False)#
Bases:
anki._legacy.DeprecatedNamesMixin- static initialize_backend_logging() None#
Enable terminal logging. Must be called only once.
- db: anki.dbproxy.DBProxy | None = None#
- server = False#
- path = b'.'#
- tr#
- media#
- models#
- decks#
- tags#
- conf#
- name() Any#
- weakref() Collection#
Shortcut to create a weak reference that doesn’t break code completion.
- property backend: anki._backend.RustBackend#
- format_timespan(seconds: float, context: anki.lang.FormatTimeSpan.Context.V = FormatTimeSpan.INTERVALS) str#
- latest_progress() Progress#
- sched_ver() Literal[1, 2]#
For backwards compatibility, the v3 scheduler currently returns 2. Use the separate v3_scheduler() method to check if it is active.
- upgrade_to_v2_scheduler() None#
- v3_scheduler() bool#
- set_v3_scheduler(enabled: bool) None#
- property crt: int#
- property mod: int#
- save(**args: Any) None#
- autosave() None#
- close(downgrade: bool = False) None#
Disconnect from DB.
- close_for_full_sync() None#
- reopen(after_full_sync: bool = False) None#
- set_schema_modified() None#
- mod_schema(check: bool) None#
Mark schema modified. GUI catches this and will ask user if required.
- schema_changed() bool#
True if schema changed since last sync.
- usn() int#
- create_backup(*, backup_folder: str, force: bool, wait_for_completion: bool) bool#
Create a backup if enough time has elapsed, and rotate old backups.
If force is true, the user’s configured backup interval is ignored. Returns true if backup created. This may be false in the force=True case, if no changes have been made to the collection.
Throws on failure of current backup, or the previous backup if it was not awaited.
- await_backup_completion() None#
Throws if backup creation failed.
- export_collection_package(out_path: str, include_media: bool, legacy: bool) None#
- import_anki_package(request: ImportAnkiPackageRequest) ImportLogWithChanges#
- export_anki_package(*, out_path: str, options: ExportAnkiPackageOptions, limit: ExportLimit) int#
- get_csv_metadata(path: str, delimiter: Delimiter | None) CsvMetadata#
- import_csv(request: ImportCsvRequest) ImportLogWithChanges#
- export_note_csv(*, out_path: str, limit: ExportLimit, with_html: bool, with_tags: bool, with_deck: bool, with_notetype: bool, with_guid: bool) int#
- export_card_csv(*, out_path: str, limit: ExportLimit, with_html: bool) int#
- import_json_file(path: str) ImportLogWithChanges#
- import_json_string(json: str) ImportLogWithChanges#
- export_dataset_for_research(target_path: str, min_entries: int = 0) None#
- get_image_for_occlusion(path: str | None) GetImageForOcclusionResponse#
- add_image_occlusion_notetype() None#
Add notetype if missing.
- add_image_occlusion_note(notetype_id: int, image_path: str, occlusions: str, header: str, back_extra: str, tags: list[str]) OpChanges#
- get_image_occlusion_note(note_id: int | None) GetImageOcclusionNoteResponse#
- update_image_occlusion_note(note_id: int | None, occlusions: str | None, header: str | None, back_extra: str | None, tags: list[str] | None) OpChanges#
- get_card(id: anki.cards.CardId | None) anki.cards.Card#
- update_cards(cards: collections.abc.Sequence[anki.cards.Card], skip_undo_entry: bool = False) OpChanges#
Save card changes to database.
- update_card(card: anki.cards.Card, skip_undo_entry: bool = False) OpChanges#
Save card changes to database.
- get_note(id: anki.notes.NoteId) anki.notes.Note#
- update_notes(notes: collections.abc.Sequence[anki.notes.Note], skip_undo_entry: bool = False) OpChanges#
Save note changes to database.
- update_note(note: anki.notes.Note, skip_undo_entry: bool = False) OpChanges#
Save note changes to database.
- nextID(type: str, inc: bool = True) Any#
- reset() None#
- new_note(notetype: anki.models.NotetypeDict) anki.notes.Note#
- add_note(note: anki.notes.Note, deck_id: anki.decks.DeckId) OpChangesWithCount#
- add_notes(requests: collections.abc.Iterable[AddNoteRequest]) OpChanges#
- remove_notes(note_ids: collections.abc.Sequence[anki.notes.NoteId]) OpChangesWithCount#
- remove_notes_by_card(card_ids: list[anki.cards.CardId]) None#
- card_ids_of_note(note_id: anki.notes.NoteId) collections.abc.Sequence[anki.cards.CardId]#
- defaults_for_adding(*, current_review_card: anki.cards.Card | None) anki.notes.DefaultsForAdding#
Get starting deck and notetype for add screen. An option in the preferences controls whether this will be based on the current deck or current notetype.
- default_deck_for_notetype(notetype_id: anki.models.NotetypeId) anki.decks.DeckId | None#
If ‘change deck depending on notetype’ is enabled in the preferences, return the last deck used with the provided notetype, if any..
- note_count() int#
- is_empty() bool#
- card_count() Any#
- remove_cards_and_orphaned_notes(card_ids: collections.abc.Sequence[anki.cards.CardId]) OpChangesWithCount#
You probably want .remove_notes_by_card() instead.
- set_deck(card_ids: collections.abc.Sequence[anki.cards.CardId], deck_id: int) OpChangesWithCount#
- get_empty_cards() EmptyCardsReport#
- after_note_updates(nids: list[anki.notes.NoteId], mark_modified: bool, generate_cards: bool = True) None#
If notes modified directly in database, call this afterwards.
- find_cards(query: str, order: bool | str | BrowserColumns = False, reverse: bool = False) collections.abc.Sequence[anki.cards.CardId]#
Return card ids matching the provided search.
To programmatically construct a search string, see .build_search_string().
If order=True, use the sort order stored in the collection config If order=False, do no ordering
If order is a string, that text is added after ‘order by’ in the sql statement. You must add ‘ asc’ or ‘ desc’ to the order, as Anki will replace asc with desc and vice versa when reverse is set in the collection config, eg order=”c.ivl asc, c.due desc”.
If order is a BrowserColumns.Column that supports sorting, sort using that column. All available columns are available through col.all_browser_columns() or browser.table._model.columns and support sorting cards unless column.sorting_cards is set to BrowserColumns.SORTING_NONE, .SORTING_NOTES_ASCENDING, or .SORTING_NOTES_DESCENDING.
The reverse argument only applies when a BrowserColumns.Column is provided; otherwise the collection config defines whether reverse is set or not.
- find_notes(query: str, order: bool | str | BrowserColumns = False, reverse: bool = False) collections.abc.Sequence[anki.notes.NoteId]#
Return note ids matching the provided search.
To programmatically construct a search string, see .build_search_string(). The order parameter is documented in .find_cards().
- find_and_replace(*, note_ids: collections.abc.Sequence[anki.notes.NoteId], search: str, replacement: str, regex: bool = False, field_name: str | None = None, match_case: bool = False) OpChangesWithCount#
Find and replace fields in a note. Returns changed note count.
- field_names_for_note_ids(nids: collections.abc.Sequence[int]) collections.abc.Sequence[str]#
- find_dupes(field_name: str, search: str = '') list[tuple[str, list]]#
- build_search_string(*nodes: str | SearchNode, joiner: SearchJoiner = 'AND') str#
Join one or more searches, and return a normalized search string.
To negate, wrap in a negated search term:
term = SearchNode(negated=col.group_searches(…))
Invalid searches will throw an exception.
- group_searches(*nodes: str | SearchNode, joiner: SearchJoiner = 'AND') SearchNode#
Join provided search nodes and strings into a single SearchNode. If a single SearchNode is provided, it is returned as-is. At least one node must be provided.
- join_searches(existing_node: SearchNode, additional_node: SearchNode, operator: Literal['AND', 'OR']) str#
AND or OR additional_term to existing_term, without wrapping existing_term in brackets. Used by the Browse screen to avoid adding extra brackets when joining. If you’re building a search query yourself, you probably don’t need this.
- replace_in_search_node(existing_node: SearchNode, replacement_node: SearchNode) str#
If nodes of the same type as replacement_node are found in existing_node, replace them.
You can use this to replace any “deck” clauses in a search with a different deck for example.
- all_browser_columns() collections.abc.Sequence[BrowserColumns]#
- get_browser_column(key: str) BrowserColumns | None#
- browser_row_for_id(id_: int) tuple[collections.abc.Generator[tuple[str, bool, BrowserRow], None, None], BrowserRow, str, int]#
- load_browser_card_columns() list[str]#
Return the stored card column names and ensure the backend columns are set and in sync.
- set_browser_card_columns(columns: list[str]) None#
- load_browser_note_columns() list[str]#
Return the stored note column names and ensure the backend columns are set and in sync.
- set_browser_note_columns(columns: list[str]) None#
- get_config(key: str, default: Any | None = None) Any#
- set_config(key: str, val: Any, *, undoable: bool = False) OpChanges#
Set a single config variable to any JSON-serializable value. The config is currently sent on every sync, so please don’t store more than a few kilobytes in it.
By default, no undo entry will be created, but the existing undo history will be preserved. Set undoable=True to allow the change to be undone; see undo code for how you can merge multiple undo entries.
- remove_config(key: str) OpChanges#
- all_config() dict[str, Any]#
This is a debugging aid. Prefer .get_config() when you know the key you need.
- get_config_bool(key: anki.config.Config.Bool.V) bool#
- set_config_bool(key: anki.config.Config.Bool.V, value: bool, *, undoable: bool = False) OpChanges#
- get_config_string(key: anki.config.Config.String.V) str#
- set_config_string(key: anki.config.Config.String.V, value: str, undoable: bool = False) OpChanges#
- get_aux_notetype_config(id: anki.models.NotetypeId, key: str, default: Any | None = None) Any#
- set_aux_notetype_config(id: anki.models.NotetypeId, key: str, value: Any, *, undoable: bool = False) OpChanges#
- get_aux_template_config(id: anki.models.NotetypeId, card_ordinal: int, key: str, default: Any | None = None) Any#
- set_aux_template_config(id: anki.models.NotetypeId, card_ordinal: int, key: str, value: Any, *, undoable: bool = False) OpChanges#
- load_balancer_enabled#
- fsrs_short_term_with_steps_enabled#
- stats() anki.stats.CollectionStats#
- card_stats_data(card_id: anki.cards.CardId) anki.stats_pb2.CardStatsResponse#
Returns the data required to show card stats.
If you wish to display the stats in a HTML table like Anki does, you can use the .js file directly - see this add-on for an example: https://ankiweb.net/shared/info/2179254157
- get_review_logs(card_id: anki.cards.CardId) collections.abc.Sequence[anki.stats_pb2.CardStatsResponse.StatsRevlogEntry]#
- studied_today() str#
- undo_status() UndoStatus#
Return the undo status.
- add_custom_undo_entry(name: str) int#
Add an empty undo entry with the given name. The return value can be used to merge subsequent changes with merge_undo_entries().
You should only use this with your own custom actions - when extending default Anki behaviour, you should merge into an existing undo entry instead, so the existing undo name is preserved, and changes are processed correctly.
- merge_undo_entries(target: int) OpChanges#
Combine multiple undoable operations into one.
After a standard Anki action, you can use col.undo_status().last_step to retrieve the target to merge into. When defining your own custom actions, you can use add_custom_undo_entry() to define a custom undo name.
- undo() OpChangesAfterUndo#
Returns result of backend undo operation, or throws UndoEmpty.
- redo() OpChangesAfterUndo#
Returns result of backend redo operation, or throws UndoEmpty.
- op_made_changes(changes: OpChanges) bool#
- fix_integrity() tuple[str, bool]#
Fix possible problems and rebuild caches.
Returns tuple of (error: str, ok: bool). ‘ok’ will be true if no problems were found.
- optimize() None#
- set_user_flag_for_cards(flag: int, cids: collections.abc.Sequence[anki.cards.CardId]) OpChangesWithCount#
- set_wants_abort() None#
- i18n_resources(modules: collections.abc.Sequence[str]) bytes#
- abort_media_sync() None#
- abort_sync() None#
- full_upload_or_download(*, auth: anki.sync.SyncAuth | None, server_usn: int | None, upload: bool) None#
- sync_login(username: str, password: str, endpoint: str | None) anki.sync.SyncAuth#
- sync_collection(auth: anki.sync.SyncAuth, sync_media: bool) anki.sync.SyncOutput#
- sync_media(auth: anki.sync.SyncAuth) None#
- sync_status(auth: anki.sync.SyncAuth) anki.sync.SyncStatus#
- media_sync_status() MediaSyncStatus#
This will throw if the sync failed with an error.
- ankihub_login(id: str, password: str) str#
- ankihub_logout(token: str) None#
- get_preferences() Preferences#
- set_preferences(prefs: Preferences) OpChanges#
- render_markdown(text: str, sanitize: bool = True) str#
Not intended for public consumption at this time.
- compare_answer(expected: str, provided: str, combining: bool = True) str#
- extract_cloze_for_typing(text: str, ordinal: int) str#
- compute_memory_state(card_id: anki.cards.CardId) ComputedMemoryState#
- fuzz_delta(card_id: anki.cards.CardId, interval: int) int#
The delta days of fuzz applied if reviewing the card in v3.
- startTimebox() None#
- timeboxReached() Literal[False] | tuple[Any, int]#
Return (elapsedTime, reps) if timebox reached, or False.
- log(*args: Any, **kwargs: Any) None#
- undo_name() str | None#
Undo menu item name, or None if undo unavailable.
- newNote(forDeck: bool = True) anki.notes.Note#
Return a new note with the current model.
- addNote(note: anki.notes.Note) int#
- remNotes(ids: collections.abc.Sequence[anki.notes.NoteId]) None#
- card_stats(card_id: anki.cards.CardId, include_revlog: bool) str#
- cardStats(card: anki.cards.Card) str#
- updateFieldCache(nids: list[anki.notes.NoteId]) None#
- genCards(nids: list[anki.notes.NoteId]) list[int]#
- emptyCids() list[anki.cards.CardId]#
- setMod() None#
- flush() None#
- anki.collection.pb_export_limit(limit: ExportLimit) anki.import_export_pb2.ExportLimit#