NEW, addon to create subdecks of different levels and presets - to mimic supermemo's "importance"

hi,

i use win10+anki23.10+fsrs.
previously discussed that to mimic supermemo’s “importance”, in the setup above
i’ll create subdecks of lv1a, lv1b, lv1c, lv1d, … lv2d, then lv3, lv4.

i asked LLMs (mainly C3.5 sonnet thru cursor) to make a addon to create subdecks, and also use pre-existing presets for that.

the code is here:

from aqt import mw
from aqt.qt import QAction, QInputDialog
from aqt.utils import showInfo
from anki.hooks import wrap
from anki.decks import DeckManager

def check_and_create_subdecks(deck_id, level):
    # Define the required subdeck names for each level
    subdeck_names = {
        "lv1": [
            "lv1-pri-highest",
            "lv1a-pri-highest",
            "lv1b-pri-high",
            "lv1c-pri-nml",
            "lv1d-pri-low"
        ],
        "lv2": [
            "lv2-pri-nml",
            "lv2a-pri-nml",
            "lv2b-pri-low",
            "lv2c-pri-lowest",
            "lv2d-pri-least"
        ],
        "lv3": ["lv3-pri-lowest"],
        "lv4": ["lv4-pri-least"]
    }

    if level not in subdeck_names:
        showInfo("Invalid level selected.")
        return

    # Get the full name of the selected deck
    deck = mw.col.decks.get(deck_id)
    parent_deck_name = deck['name']

    # Get all existing deck names
    existing_decks = mw.col.decks.all_names()

    # Get all existing config/preset names
    all_configs = mw.col.decks.all_config()
    config_dict = {conf['name']: conf['id'] for conf in all_configs}

    # Initialize a list to keep track of created subdecks
    created_subdecks = []
    missing_presets = []

    # Check for each required subdeck
    for subdeck_name in subdeck_names[level]:
        full_subdeck_name = f"{parent_deck_name}::{subdeck_name}"
        if full_subdeck_name not in existing_decks:
            # Create the new subdeck
            new_deck_id = mw.col.decks.id(full_subdeck_name)
            
            # Try to find and assign the matching preset
            if subdeck_name in config_dict:
                deck = mw.col.decks.get(new_deck_id)
                deck['conf'] = config_dict[subdeck_name]
                mw.col.decks.update(deck)
                created_subdecks.append(subdeck_name)
            else:
                missing_presets.append(subdeck_name)
                created_subdecks.append(f"{subdeck_name} (with default preset)")

    if created_subdecks:
        mw.col.reset()
        message = f"The following subdecks were created under '{parent_deck_name}':\n" + "\n".join(created_subdecks)
        if missing_presets:
            message += "\n\nWARNING: Could not find matching presets for:\n" + "\n".join(missing_presets)
    else:
        message = f"All subdecks for {level} already exist under '{parent_deck_name}'."
    
    showInfo(message)

def on_deck_selected():
    # Get the currently selected deck ID
    deck_id = mw.col.decks.current()['id']
    if not deck_id:
        showInfo("Please select a deck before running this add-on.")
        return

    # Prompt the user to select the level
    levels = ["lv1", "lv2", "lv3", "lv4"]
    level, ok = QInputDialog.getItem(mw, "Select Level", "Choose the level:", levels, 0, False)
    if ok and level:
        check_and_create_subdecks(deck_id, level)
    else:
        showInfo("Operation cancelled.")

def add_menu_option():
    action = QAction("Check and Create Subdecks", mw)
    action.triggered.connect(on_deck_selected)
    mw.form.menuTools.addAction(action)

# Initialize the add-on by adding the menu option
add_menu_option()

what it does by LLMs:



This addon creates a hierarchical deck structure in Anki. Here's what it does specifically:

1. **Adds a Menu Option**:
   - Adds "Check and Create Subdecks" to Anki's Tools menu

2. **Creates Organized Subdecks**:
   When you run it, it:
   - Asks you to choose a level (1-4)
   - Creates subdecks under your selected parent deck
   - Uses existing presets with matching names

3. **Subdeck Structure**:

Parent Deck
├── Level 1 Option:
│ ├── lv1-pri-highest
│ ├── lv1a-pri-highest
│ ├── lv1b-pri-high
│ ├── lv1c-pri-nml
│ └── lv1d-pri-low

├── Level 2 Option:
│ ├── lv2-pri-nml
│ ├── lv2a-pri-nml
│ ├── lv2b-pri-low
│ ├── lv2c-pri-lowest
│ └── lv2d-pri-least

├── Level 3 Option:
│ └── lv3-pri-lowest

└── Level 4 Option:
└── lv4-pri-least


4. **Important Features**:
- Only creates subdecks that don't already exist
- Looks for existing presets with matching names (e.g., "lv1a-pri-highest" deck will use "lv1a-pri-highest" preset)
- Won't modify any existing decks
- Shows a warning if it can't find matching presets

5. **Usage**:
1. Select a parent deck in Anki
2. Go to Tools → Check and Create Subdecks
3. Choose a level (1-4)
4. It will create the appropriate subdecks and link them to matching presets

This is useful for organizing cards into different priority levels, assuming you've already set up the corresponding presets in Anki.

i asked if it’s safe:



Yes, this addon is safe for your Anki database for several reasons:

1. **Read-Only for Existing Content**:
   - It only READS existing decks and presets
   - Does NOT modify any existing decks
   - Does NOT modify any existing presets
   - Does NOT touch your cards or notes

2. **Safe Operations**:
   - Only uses official Anki API methods (`mw.col.decks`)
   - Uses standard Anki deck creation methods
   - Only adds new subdecks if they don't exist

3. **Built-in Safeguards**:
   ```python
   # Checks if deck exists before doing anything
   if full_subdeck_name not in existing_decks:
       # Only creates if it doesn't exist
  1. Reversible Actions:
    • If you don’t like the created subdecks, you can safely delete them
    • Deleting these subdecks won’t affect your cards or other decks
    • No destructive operations in the code

However, as with any Anki addon, it’s always good practice to:

  1. Keep regular backups of your Anki collection
  2. Test new addons on a small deck first
  3. Update Anki and addons regularly

If you’re still concerned, you can first try it on a test deck to see how it works before using it on your main decks.


if someone could have a look, or be interested to use (i am using it), will be good.
thanks

i am a quite conservative guy and tends to keep things inside myself.

however anki community is one that i cant resist to submerge myself into.

quite many users contributed addons that i cant live without, along anki/ankidroid.