How to Add a New LeetCode Problem Animation to the Repository: CLI Workflow Explained

Use the anima.py CLI manager to scaffold a new problem by running python anima.py new <id> "<title>", which auto-generates the required folder structure and markdown template.

The LeetCodeAnimation repository provides a Python-based automation tool that eliminates manual folder creation when contributing visual explanations. By using the built-in scaffolding system, contributors can add a new LeetCode problem animation in seconds while ensuring consistent directory layout across all submissions.

Prerequisites and Setup

Before generating new problem skeletons, install the required Python dependency. The automation script relies on the fire library declared in requirements.txt.

pip install -r requirements.txt

This single dependency powers the CLI interface defined in anima.py, the repository's entry point.

Scaffolding a New Problem with the CLI

The fastest way to add a new LeetCode problem animation is through the command-line interface. The new method in anima/__init__.py forwards arguments to the create_solution function implemented in anima/create.py.

Run the following command, replacing <id> with the LeetCode problem number and "<title>" with the exact problem name:

python anima.py new 1 "Two Sum"

Upon execution, the console outputs the creation path:


题解框架创建完毕,位于文件夹 /path/to/repo/0001-Two-Sum

How the Directory Structure Is Generated

The scaffolding process relies on deterministic path manipulation through two core classes defined in anima/model.py.

Slug Generation: The ProblemInfo.title_slug property converts the numeric ID and title into a standardized directory name (e.g., 0001-Two-Sum). This ensures every problem follows the same naming convention.

Folder Creation: The Solution._create_dirs method (also in anima/model.py) builds three subdirectories:

  • Animation/ – Stores visual assets (Animation.m4v, Animation.gif)
  • Article/ – Contains the markdown explanation
  • Code/ – Houses reference implementations in various languages

Customizing the Generated Content

After scaffolding, you must populate the placeholder files. The system copies a boilerplate markdown template from template/template.md via get_md_template_path in anima/base.py into Article/<slug>.md.

Open the generated markdown file and fill in the sections:


# LeetCode 第 1 号问题:两数之和

## 题目描述

...

## 题目解析

...

## 动画理解

![](../Animation/Animation.gif)

## 参考代码

```cpp
class Solution { … }

Replace the placeholder `../Animation/Animation.gif` reference with your actual animation file. Place the final video or GIF into the `Animation/` folder, and add any reference implementations to the `Code/` directory.

## Programmatic Alternative

If you prefer calling the manager from Python scripts rather than the shell, import the `Anima` class directly:

```python
from anima import Anima

# Scaffold problem #42

Anima().new('42', 'Trapping Rain Water')

This programmatic approach executes the same create_solution logic found in anima/create.py without shell invocation.

Key Source Files Reference

Understanding these files helps when debugging or extending the scaffolding behavior:

  • anima.py – Entry point that wires the CLI with the fire library
  • anima/create.py – Implements create_solution, the core scaffolding logic
  • anima/model.py – Defines ProblemInfo (slug generation) and Solution (directory creation via _create_dirs)
  • anima/base.py – Provides get_md_template_path for locating project resources
  • template/template.md – Boilerplate markdown inserted into every new article
  • Readme.md – Top-level summary that automatically indexes newly created problem folders

Summary

  • Install dependencies via pip install -r requirements.txt to access the fire CLI framework.
  • Run python anima.py new <id> "<title>" to invoke create_solution in anima/create.py and generate the folder structure.
  • Edit the scaffolded files in Article/, Animation/, and Code/ directories to complete your contribution.
  • Rely on deterministic paths – the ProblemInfo.title_slug logic ensures your submission integrates automatically with the repository's indexing system.

Frequently Asked Questions

What Python version is required to run the anima.py script?

The script uses standard Python 3 features and requires only the fire library listed in requirements.txt. Any Python 3.6+ environment should execute the scaffolding commands without compatibility issues.

Can I manually create the folder structure instead of using the CLI?

While possible, manual creation risks inconsistency with the ProblemInfo.title_slug format expected by the repository's indexing logic. The CLI ensures the 000X-Problem-Name directory pattern and creates required placeholder files like Animation.gif that the Readme.md summary tables reference.

Where does the markdown template come from?

The template is stored at template/template.md in the repository root. During execution, get_md_template_path in anima/base.py resolves this location and copies its contents into the new Article/<slug>.md file, providing consistent formatting for all problem explanations.

Do I need to update the root Readme.md after adding a new problem?

No. Because the scaffolding creates folders following the deterministic 000X-Title slug pattern, the repository's summary tables in Readme.md automatically detect and display newly added problems without manual index updates.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →