Skip to content

Instantly share code, notes, and snippets.

@z1lc
Created September 1, 2019 17:31
Show Gist options
  • Save z1lc/b00a9ff4898366268b00f0549ebdf64a to your computer and use it in GitHub Desktop.
Save z1lc/b00a9ff4898366268b00f0549ebdf64a to your computer and use it in GitHub Desktop.
Automatically run Anki Data Quality checks & display results in main window
import re
from aqt import mw
from aqt.deckbrowser import DeckBrowser
from aqt.qt import *
import aqt.forms
from aqt.utils import showInfo
PREFIX = 'zAQ:'
# Any saved filter that starts with PREFIX will be run as a data quality check.
def _renderStatsWithDQ(self):
original = orig_renderStats(self)
# _renderStats is triggered whenever note types change in the Add Cards window.
# We don't want to trigger at that time, so check to make sure the window is closed here.
if aqt.dialogs._dialogs['AddCards'][1] is None:
try:
searches = [search for search in self.mw.col.conf['savedFilters'].items() if search[0].startswith(PREFIX)]
except KeyError:
return original
if len(searches) == 0:
return original
violations_to_print = ""
total_violations = 0
for search in searches:
cur_violations = len(self.mw.col.findNotes(search[1]))
total_violations += cur_violations
if (cur_violations > 0):
violations_to_print += "{} for {}<br>".format(cur_violations, re.sub("zAQ: ", "", search[0]))
title = "<h1 style='color: red'>✘ {} DQ violation{}!</h1>".format(total_violations, "s" if total_violations > 1 else "")
if (total_violations == 0):
title = "<br><span style='color: green'>✔ All {} DQ checks passed.</span>".format(len(searches))
return "{}{}<div>{}</div>".format(original, title, violations_to_print)
else:
return original
orig_renderStats = DeckBrowser._renderStats
DeckBrowser._renderStats = _renderStatsWithDQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment