You create an Anki add-on by writing a Python script and placing it in the specific Anki addons directory on your computer. Because Anki is built with Python, any script you place in that folder runs automatically when the program starts, allowing you to add new buttons, automate card tagging, or change how the interface looks.
Anki is a powerful tool, but its default settings do not cover every possible study workflow. Add-ons allow you to modify the behavior of the software without changing the core source code. To start, you need to understand that Anki treats each add-on as a Python package. This means that as long as you have a folder containing a file named __init__.py, Anki will execute the code inside that file during the startup sequence.
The system is split into two main parts. The backend handles the database and the scheduling algorithm. The frontend handles the user interface and the rendering of cards. Most students want to create add-ons that interact with the frontend, which is managed by the aqt module. If you want to change how cards are stored or how the SRS algorithm works, you will interact with the anki module.
Before you write a single line of code, you must find where Anki looks for these scripts. The path varies depending on your operating system.
We will start with a simple "Hello World" add-on. This script will trigger a popup message the moment you open Anki. This confirms that your environment is set up correctly and that Anki is reading your folder.
Navigate to the addons21 folder mentioned above. Create a new folder inside it. Give it a clear name, such as "my_first_addon". Do not use spaces in the folder name, as this can sometimes cause issues with Python imports.
Inside your "my_first_addon" folder, create a new text file and rename it to __init__.py. This specific filename is required. Python looks for this file to know where the package starts. You can use any text editor, but Visual Studio Code or PyCharm are better because they highlight syntax errors.
Copy and paste the following code into your __init__.py file:
In this code, mw stands for MainWindow. It is the primary object that represents the Anki application. showInfo is a utility function that creates a simple alert box. When Anki starts, it imports this file and immediately executes the showInfo function.
Save the file and launch Anki. If you already have Anki open, you must restart it. You should see a popup window with your message. If it does not appear, check the Anki logs or ensure the file is named exactly __init__.py and not __init__.py.txt.
"I spent hours manually tagging my medical school decks. I followed a basic guide to create a simple Python script that auto-tags cards based on keywords in the PDF source. It saved me about 30 minutes of clicking every single day."
- Sarah, USMLE Student
Once you have a popup working, you will want to do things that actually help you study. This requires using hooks. Hooks are specific points in the Anki execution process where you can "hook" your own code to run. For example, you can tell Anki to run a script every time a user finishes reviewing a card.
The gui_hooks module is where most of the magic happens. If you want to add a button to the main window, you use a hook that triggers when the main window is initialized. Here is a conceptual example of how to add a custom menu item:
This script does four things. First, it defines a function that contains the action you want to perform. Second, it creates an action object using Anki's actionManager. Third, it attaches that action to the "Tools" menu in the top bar. Fourth, it tells Anki to run the setup_menu function as soon as the main window is ready.
To modify cards, you need to access the collection. The collection is the database that holds all your notes and cards. You can use mw.col to perform queries. For example, you can find all cards in a specific deck and change their tags in bulk. This is useful for students who move from one study phase to another and need to reorganize thousands of cards at once.
Different exams require different types of data. Depending on what you are studying, your add-on goals should change.
Medical students deal with massive volumes of terminology. A useful add-on for this group is one that automatically links a keyword to a medical dictionary or a database like PubMed. You can write a script that scans the current card for specific medical terms and adds a hyperlink to the card's HTML content. This reduces the time spent switching between Anki and a browser.
Law students often have cards with very long text blocks (case summaries). An add-on that implements a "collapse" or "expand" toggle for long text fields can make the review process much cleaner. You can achieve this by injecting custom CSS and JavaScript into the card template via a Python script.
CPA students often need to perform calculations. While Anki is primarily for recall, a custom add-on could integrate a simple calculator popup that stays on top of the review window. This prevents the distraction of picking up a physical calculator or opening a separate app during a timed study session.
Writing add-ons is a great way to optimize your workflow, but it does not solve the hardest part of using Anki: the actual creation of the cards. Many students spend more time typing and formatting cards than they do actually studying them. Even with a custom add-on to manage tags, you still have to manually extract information from textbooks and PDFs.
This is where StudyCards AI fits into the process. Instead of spending hours manually creating cards or writing complex scripts to import data, StudyCards AI converts your PDFs directly into high-quality flashcards. You can then export these cards to Anki in seconds. By combining AI-generated cards with your own custom workflow add-ons, you remove the manual labor from both ends of the study process.
Focus your energy on learning the material, not on the tedious task of typing it into a database. Let AI handle the card creation while you focus on the review.
No. You only need a basic understanding of Python. If you can handle variables, functions, and lists, you can create simple automation scripts. For more complex UI changes, you might need a bit of HTML and CSS knowledge.
On Windows, it is usually in %APPDATA%\Anki2\addons21. On macOS, it is in ~/Library/Application Support/Anki2/addons21. On Linux, look in ~/.local/share/Anki2/addons21.
Yes. You can zip your add-on folder and send it to others, or you can upload it to AnkiWeb. Once uploaded, other users can install it by entering a specific code into the "Get Add-ons" menu.
Simple scripts have almost no impact on performance. However, if you write a script that performs heavy database queries every time a card is shown, you may notice a slight lag during reviews.
Yes, but JavaScript is used for the card templates and the browser view, not for the core add-on logic. The add-on itself is written in Python, but that Python code can be used to inject JavaScript into the cards you review.
Generate Anki flashcards free