Two questions about Anki's source code and Python: How to store and retrieve the number of cards I didn't review then display in the deckbrowser? How to use the variable `thetime` in `stats.py's` function `todayStats` to make a pomodoro like timer?

1. How to store and retrieve the number of cards I didn’t review then display it in the Deck Browser?

I’ve been customizing Anki’s Deck Browser and Reviewer in the last few days to make it display some stats about my Collection of cards and my study routine.

One of the things I have in mind but have no idea about how to “materialize” is to display the number of cards I didn’t review for each one of the last 7 days, as well as to be able to access the respective data from previous weeks.

I already know that I can get access to new remaining, learn remaining and review remaining data for the current day trough the todayStats() function as well as trough mw.col._backend.congrats_info().new_remaining, mw.col._backend.congrats_info().learn_remaining and mw.col._backend.congrats_info().review_remaining respectively (if you know a better way to do it, please share it with me).

However, I didn’t find a way to retrieve the correspondent data from the previous days.

So, my idea is

  1. To store the data from the current day somewhere (I’ve seen that the add-on Countdown to events and exams use a .db file to store data, but I’ve also read that it’s possible to use JSON to do the same thing - if I understood it correctly),

  2. Then make Anki access and display it in the Deck Browser (if I studied all the cards, display a :white_check_mark:, if I studied zero cards, display a :x:, if I left some (but not all) cards due, display the number of cards left).

  3. Click in a button somewhere in the Deck Browser (or maybe the Stats link in the toolbar) to open a window that displays the data from the previous weeks (Date - Day of the week - Number of cards left).

My questions are:

  1. How to make Anki store the number of cards that I didn’t study every day, alongside with the respective date and day of the week?
  2. How to retrieve the data from the previous 7 days?
  3. How to display it in the Deck Browser?
    3.1. To be more precise: What is the correct procedure to append the code to the Deck Browser, as I’ve got errors while trying to append the settings button from the Countdown to Events and Exams add-on to the Reviewer?
  4. Are there a better way to do it than this one I’m suggesting?

> 2. How to use the variable thetime in stats.py's function todayStats to make a pomodoro like timer?

I’ve read some tutorials about how to create a pomodoro timer using Python, but all of them relies on the function time.sleep() to work, which doesn’t suit my needs, as I want a timer that uses the time I really spent reviewing cards as reference.

The only way I know (that I hope is viable) to achieve this is through the variable thetime, that stores the time (in seconds) spent reviewing the cards (the function

mw.col.db.first(
            """select count(), sum(time)/1000 from revlog where id > ?""",
            (mw.col.sched.dayCutoff - 86400) * 1000)[1]

does the same).

This is what I have in mind:

  • Every “X” minutes I really spent studying, a dialog dialog box opens and asks if I want to take a break.

  • If I click the button “Yes”, it opens the Deck Browser, if I click “No”, it continues displaying the Reviewer until it’s time to show the dialog box again.

  • Repeat this “N” times or until I finish all the reviews scheduled for the current day (If there’s still some cards due after “N” repetitions, ask me if I want the timer to keep working. If I finish all the reviews before “N” repetitions, stop the timer and show a dialog box congratulating me for being so efficient).

Again, I don’t know how to do it.

Edit: Just learned about Timebox. So, this problem is solved for me.

Can you help me with any of this?

I haven’t looked into Anki’s revlog database in detail, so I’m likely to be completely wrong, but I’m guessing that maybe it’s not easy (or impossible?) to specify a date in the past and calculate the number of cards that are due on that date but were not reviewed.

If what you are trying to create is not an add-on to be shared on ankiweb, but for personal use, a possible workaround might be to use some automation software to run Anki at a fixed time every day and store the data you need in any data format (sql, csv, json, python’s pickle, etc.) and retrieve it later.

You can use deck_browser_will_render_content hook.

1 Like

That’s the case and your suggestion does sound good. Could you please tell me more about what automation softwares I could use (maybe link some websites here if possible)?

I have another add-on idea that I think could take advantage of this automation suggestion of yours. Its task is to identify the cards that I answered “Good” or “Easy” and then, the next time I reviewed it, I answered “Hard” or “Again”, and keep repeating this pattern over time in a “forgetting loop”. Do you think it’s a task possible to achieve with the data that Anki stores?

I mean, the add-on “See Previous Card Ratings in Reviewer” already gets this kind of information, but is it possible to write a code to identify the pattern I described before?

Here’s an exemple from the add-on’s page.
127709005-7ccb7806-2eb2-4f58-96ba-d259542512d1

I think Task Scheduler for Windows and cron for Linux are the most widely used job schedulers. I don’t know about Mac ones as I don’t have a Mac.

Yes, it should be possible. The history of each review is recorded in the revlog table in Anki’s database in detail, including timestamp, rating, ease factor, etc., so it should be very easy to retrieve. For more information about revlog, please search for “revlog” in the following page:

By the way, you can view the rating history of a card even without the add-on in either of the following ways:

  • Open Anki’s Browser → select a single card → right click → Info...
  • Click the More button at the bottom right of the review screen → Card Info
1 Like

That sounds promising. Thank you for your help!