sphinx-copybutton

A Sphinx extension that adds a one-click copy button to your code blocks.

Library
PyPI
v0.5.2
270stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
50/100Fair
Development Activity24
Maintenance24
Community72
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
77/100Good
Architecture72
Code Quality76
Innovation74
Learning Curve85

sphinx-copybutton is a small Sphinx extension that adds a copy button to the code blocks in your documentation. Readers can copy an entire snippet to their clipboard with a single click, instead of manually selecting text — a small touch that noticeably improves the experience of code-heavy docs.

It is enabled with a single entry in your conf.py, works out of the box with Sphinx’s default and third-party themes, and offers CSS and JavaScript hooks to customize the button’s appearance and which prompt characters it strips when copying.

What You Get

  • A copy button automatically added to every code block in your Sphinx docs
  • Configurable stripping of prompt characters like >>> and $ from copied text
  • CSS and JavaScript hooks to restyle or fine-tune the button behavior

Common Use Cases

  • Letting readers copy shell commands and code snippets in one click
  • Stripping interactive prompts so pasted commands run without editing
  • Polishing developer documentation and tutorials with minimal effort

Under The Hood

Architecture

sphinx-copybutton is a Sphinx extension whose Python side (sphinx_copybutton/__init__.py) registers config values and injects a bundled JavaScript file and stylesheet into the built HTML. At runtime the client script (built from clipboard.js) scans code blocks, appends a button, and copies text via the browser clipboard API, applying the configured prompt-stripping regexes.

Tech Stack

A hybrid of Python (the Sphinx extension glue) and vanilla JavaScript plus CSS for the front-end button, with clipboard.js bundled for copy support. It is packaged with setup.py/setup.cfg and tested via tox, shipping static assets inside the Python package.

Code Quality

The codebase is small and focused, splitting a thin Python registration layer from the browser-side script, with documentation built by Sphinx itself as a living demo. Testing centers on doc builds and a bundled test.js rather than a large unit suite, appropriate for a presentation-layer extension.

API Design

The developer experience is close to ideal for its scope: one line in conf.py delivers the core feature, and a handful of well-documented options cover prompt stripping and styling without boilerplate. The ReadTheDocs site demonstrates every option live, making configuration easy to discover.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search