DEV Community

Otavio Monteagudo
Otavio Monteagudo

Posted on • Edited on

Supercharged Studying with Anki, ChatGPT and Python

Introduction

Much has been discussed on how ChatGPT can be used by students for cheating, but it is hard to overstate how much of a fantastic studying tool it can be. In this article we'll discuss how to instruct ChatGPT to give us a nicely formatted list of information that should be memorized, which will then be used as input in a python program that will output an Anki package that we'll then load to Anki to help us remember the concepts through memory triggers.

Anki is a powerful open-source flashcard application that helps users memorize information more efficiently using spaced repetition. It is available on multiple platforms, including Windows, macOS, Linux, iOS, and Android. Anki allows users to create their own decks of flashcards containing text, images, audio, and even LaTeX equations.

We will demonstrate how to create Anki cards programmatically using Python and the Genanki library. We will create an Anki deck for learning the Linux filesystem directories and their descriptions. The deck will contain basic and reverse type cards for each directory.

Anki Overview

Learning how to properly use Anki is a skill in itself. If you have never heard of it, it is a spaced-repetition software that can help you memorize related pieces of information. Here is a good video to get you started. In this program we will use the concepts of Anki models, cards, notes, decks and packages to create our card set.

  1. Anki Models: An Anki model, also known as a note type, defines the structure and layout of a set of cards. Each model consists of a set of fields, which store the information to be learned, and a set of card templates, which determine how that information is displayed on the cards. Models allow users to create different types of cards and layouts for various learning needs, such as basic cards, reverse cards, and cloze deletion cards.

  2. Anki Cards: An Anki card is a digital flashcard containing information to be learned. Each card has a front side, which usually contains a question or a prompt, and a back side, which contains the answer or additional information related to the front side. Anki cards can contain text, images, audio, and even LaTeX equations. Cards are generated from models and their associated fields, which determine the content and layout of the cards.

  3. Anki Notes: An Anki note is a single piece of information that is used to generate one or more flashcards or Anki cards. Each note is based on a specific model (also known as a note type), which defines the structure, layout, and fields for that note. Fields store the actual content of the note, such as questions, answers, or prompts, while the model determines how that content is displayed on the generated cards.

  4. Anki Decks: An Anki deck is a collection of cards on a specific topic or subject. Decks are used to organize cards into meaningful groups, making it easier for users to focus on a particular area of study. Anki allows users to create and customize their own decks, which can be shared with others, imported, or exported. Users can study one deck at a time or multiple decks simultaneously, depending on their learning goals and preferences.

  5. Anki Packages: An Anki package is a single file containing one or more decks, along with their associated cards, models, media files, and other related data. Anki packages have the file extension ".apkg" and can be easily shared, imported, or exported between users and devices. Anki packages are a convenient way to distribute decks and related content, as they bundle all necessary information in a single file. When importing an Anki package, the decks, cards, and models it contains are added to the user's existing collection.

ChatGPT as a Studying Sidekiq

ChatGPT can be seen as a humanoid Google. It won't do the work for you and it certainly won't do the thinking for you. However, if you know how to phrase your questions, it can give you some pretty good answers, and in the format you would like them to be, which makes inserting these answers in a program that much easier.

In this example, I'm going to ask a question regarding the Linux filesystem, I expect the answer to be a list of the directories accessible from the root folder, with an explanation of why they are there. If it doesn't give you the answer you want, remember to be more specific.

Chat GPT answer on the Linux filesystem

This is a partial screenshot, the answer goes all the way to 14, the /var directory used to store fluid data files that are supposed to change during a system use session, and the last one in alphabetical order.

So far, so good. But if we want to insert this information into a program, we need to have it formatted. This is something in which Chat GPT shines. If you receive, say, a paragraph as an answer, you can just ask the robot to rephrase it as an ordered list, and then ask it to send each item as an array of tuples for instance. here's an example based on the last answer:

Image description

Remember to always review the answers before uploading them to Anki, as you'll hammer the returned information into your brain; you will not want to sabotage your knowledge-gathering during your studies.

This is enough for now, let's get to coding.

Installing Genanki

To get started, you'll need to install the genanki library, which is a Python library for creating Anki decks and cards programmatically. You can install it using pip:

pip install genanki
Enter fullscreen mode Exit fullscreen mode

Creating Anki Cards and Deck

Now that we have the genanki library installed, let's create a Python script that generates the Anki cards and deck for Linux filesystem directories.

Preparations

Here we'll import the library and paste the ChatGPT answer. It will be used soon as a parameter to the functions provided by genanki.

import genanki

# List of Linux directories and their descriptions
linux_dirs = [
    ('/', 'Root directory, starting point of the filesystem hierarchy.'),
    ('/bin', 'Contains essential system command executables.'),
    ('/sbin', 'Contains essential system administration command executables.'),
    ('/boot', 'Contains files needed to start the boot process.'),
    ('/etc', "Contains system-wide configuration files and scripts."),
    ('/dev', 'Contains device files representing hardware devices.'),
    ('/home', 'Contains personal directories for each user.'),
    ('/lib', 'Contains shared libraries and kernel modules.'),
    ('/opt', 'Optional directory for storing third-party software.'),
    ('/proc', 'Virtual filesystem providing an interface to kernel internal data structures.'),
    ('/sys', 'Virtual filesystem providing an interface to kernel internal data structures for devices, drivers, and other components.'),
    ('/tmp', 'Temporary directory for storing files deleted after a system reboot.'),
    ('/usr', 'Contains user-related files, shared libraries, header files, documentation, and non-essential software binaries.'),
    ('/var', 'Contains variable data files, such as logs, databases, and mail spools.'),
]
Enter fullscreen mode Exit fullscreen mode

Here we have to generate a model ID for the cards we are creating, which has to be unique. You will also have to give a name to your model, create fields to be populated by content and then declare a template for your note using these fields; remember that a note is basically a form for cards associated with that note and we will have two cards for each note since the directory name is a memory trigger for the directory description and vice-versa.

# Define Anki note model
model_id = 1607392319
model = genanki.Model(
    model_id,
    'Linux filesystem folders',
    fields=[
        {'name': 'Directory'},
        {'name': 'Description'},
    ],
    templates=[
        {
            'name': 'Card 1',
            'qfmt': '{{Directory}}',
            'afmt': '{{Description}}',
        },
        {
            'name': 'Card 2',
            'qfmt': '{{Description}}',
            'afmt': '{{Directory}}',
        },
    ])
Enter fullscreen mode Exit fullscreen mode

We'll finally create the deck and give it an ID (which, you guessed it, must be unique) and also give it a name. We will then iterate through our list of tuples and create a note for each tuple, declaring the note's model and inputting our content as fields ('Directory' and 'Description' respectively).
We'll finally write a package containing our single deck. If you have an Anki installation, just double click on the generated file and it should open in the program, ready for studying.

# Generate Anki cards and add them to a deck
deck_id = 2059400110
deck = genanki.Deck(deck_id, 'Linux Filesystem')

for dir_name, description in linux_dirs:
    note = genanki.Note(model=model, fields=[dir_name, description])
    deck.add_note(note)

# Save the deck to an Anki package (*.apkg) file
genanki.Package(deck).write_to_file('linux_filesystem.apkg')
Enter fullscreen mode Exit fullscreen mode

Full Script

import genanki

# List of Linux directories and their descriptions
linux_dirs = [
    ('/', 'Root directory, starting point of the filesystem hierarchy.'),
    ('/bin', 'Contains essential system command executables.'),
    ('/sbin', 'Contains essential system administration command executables.'),
    ('/boot', 'Contains files needed to start the boot process.'),
    ('/etc', "Contains system-wide configuration files and scripts."),
    ('/dev', 'Contains device files representing hardware devices.'),
    ('/home', 'Contains personal directories for each user.'),
    ('/lib', 'Contains shared libraries and kernel modules.'),
    ('/opt', 'Optional directory for storing third-party software.'),
    ('/proc', 'Virtual filesystem providing an interface to kernel internal data structures.'),
    ('/sys', 'Virtual filesystem providing an interface to kernel internal data structures for devices, drivers, and other components.'),
    ('/tmp', 'Temporary directory for storing files deleted after a system reboot.'),
    ('/usr', 'Contains user-related files, shared libraries, header files, documentation, and non-essential software binaries.'),
    ('/var', 'Contains variable data files, such as logs, databases, and mail spools.'),
]

# Define Anki note model
model_id = 1607392319
model = genanki.Model(
    model_id,
    'Simple Model',
    fields=[
        {'name': 'Directory'},
        {'name': 'Description'},
    ],
    templates=[
        {
            'name': 'Card 1',
            'qfmt': '{{Directory}}',
            'afmt': '{{Description}}',
        },
        {
            'name': 'Card 2',
            'qfmt': '{{Description}}',
            'afmt': '{{Directory}}',
        },
    ])

# Generate Anki cards and add them to a deck
deck_id = 2059400110
deck = genanki.Deck(deck_id, 'Linux Filesystem')

for dir_name, description in linux_dirs:
    note = genanki.Note(model=model, fields=[dir_name, description])
    deck.add_note(note)

# Save the deck to an Anki package (*.apkg) file
genanki.Package(deck).write_to_file('linux_filesystem.apkg')
Enter fullscreen mode Exit fullscreen mode

Limitations of this method

This script is pretty good, I use it very often, but let's not pretend it is perfect.
You will have to generate the deck every time you want to add stuff programatically. You can also get the Deck ID and generate more cards to a new package with the same ID and add them, but this can lead to errors if you are not careful.
Also, the input method is currently an array of tuples. You will have to edit the script every time to update the array with a new deck. You could also copy and paste it to a text file and read the input from there, and also randomly generate a model ID so you can just paste the card contents to your text file and have the package generate from a single script run. This is somewhat subjective so I left it out on purpose.

Conclusion

Anki has many types of cards, the main ones being the basic (memory trigger + answer) and cloze (one or more memory triggers embedded in an outline). This example uses the basic and reversed card, because both the 'question' and 'answer' can be a memory trigger for each other; if you want a simple, basic card you should consult the example at Genanki's README.
I hope this script serves as at least a starting point for your own applications of programmable Anki, it is probably my main study method nowadays, and it got a whole more powerful after ChatGPT. Have fun with your coding.

Top comments (0)