This folder contains code associated with the Real Python tutorial How to Automate Your Workflow With Claude Code Hooks.
This is a small uv-managed project with three Claude Code hooks wired up in .claude/:
Stophook (notify_desktop.py): a desktop notification each time Claude finishes responding (macOS and Linux; plain-text fallback on other systems).PreToolUsehook (guard_pip.py): blockspip installand steers Claude towarduv.PostToolUsehook (format_with_ruff.py): formats the file Claude just edited with ruff.
Each hook is a small Python script in .claude/hooks/, wired up in settings.json.
You need Python 3.12 or later, uv, and ruff. Install ruff as a uv tool so the hook can call it from anywhere:
$ uv tool install ruffThen sync the project:
$ uv syncStart Claude Code in this folder:
$ claudeThen:
- Ask Claude to do anything, and you get a desktop notification when it finishes.
- Ask Claude to
pip install requests. The hook blocks pip, and Claude usesuv addinstead. - Ask Claude to edit a Python file. ruff reformats the file on save.
Run /hooks inside Claude Code to list the wired hooks, or start Claude Code with claude --debug-file /tmp/hooks-debug.txt and scan the log for [DEBUG] Hook lines to watch them fire.