by Martin Lellep

# Exporting Anki statistics manually

In order to document what I was able to achieve while learning Italian for one month, I wanted to be able to export my Anki statistics to a data format that could be processed and plotted manually. It turns out that there is no feature in the Anki desktop client that enables the easy export of the computed statistics data that is shown on the statistics page.

However, I found a hack-ish workaround by tweaking the original Anki code that computes the statistics in order to export the data myself. This article is a quick explanation of how to export the statistics that is generated by Anki.

Here we go:

1. Find the Anki installation by typing whereis anki in your Linux terminal. My output is:

\$ whereis anki
anki: /usr/bin/anki /usr/share/anki /usr/share/man/man1/anki.1.gz


Checking these paths reveals that /usr/share/anki is the path that contains the source files of the Anki desktop client. These files are written in Python. Therefore, it is easy to modify them.

2. Within /usr/share/anki, the file /usr/share/anki/anki/stats.py contains all the code that computes the statistics and outputs the data to the corresponding window of the Anki desktop client.

3. The code in stats.py is self-explanatory and can be modified to one’s liking. Let me state one example here: The method dueGraph() is modified to export the statistics data that it computes anyway. It should be denoted at that point that Anki informs you about errors if erroneous code has been added to any function - that makes debugging your added code quite comfortable. The code is changed as follows:

# Original version
def dueGraph(self):
...
txt += self._dueInfo(tot, len(totd)*chunk)
return txt

# Hacked version
def dueGraph(self):
...
txt += self._dueInfo(tot, len(totd)*chunk)
# << Start of new code
import os
with open(os.path.join('/home/<user>', 'dueGraph.dat'), 'a') as f:
f.write(json.dumps(data, indent=4))
# End of new code >>
return txt

4. The additional code block creates a file /home/<user>/dueGraph.dat where <user> must be replaced by your username. This file contains the data that is used to generate the plot called “Forecast” in the statistics window.

5. The original plot called “Forecast” is shown below.

6. The data that is written to /home/<user>/dueGraph.dat can be used easily to generate a plot that is to your liking based on the underlying data. That is shown in the following figure. This figure has been generated with a simple Python script using matplotlib and the exported data of my Anki deck.

7. We are done.

Concluding, it is possible to adapt the original Anki source code to export the statistics data manually, which, ultimately, enables one to create custom plots around that data. While the above procedure shows how already computed data is exported, own statistics based on the raw data in the Anki database could be computed and exported as well. However, attention must be brought to the fact that the plotted data of the above two figures appears not to be the same. I was not able to spot any mistakes from my side or apparent code passages that modify the data after I save it manually in the Anki source code.

Finally, this article shows that exporting the data is a straightforward process but caution is advised when using the data without any prior inspection.

Kudos go to Leonard Salewski for his valuable tip to study Anki plugins in order to understand the inner workings of Anki, which led to this hack of the stats.py file, and also for general discussions about Anki to enhance the personal learning process.

Subscribe via email to get notified about new blog posts