| 1 |
# Sphinx-copybutton |
| 2 |
|
| 3 |
```{image} https://readthedocs.org/projects/sphinx-copybutton/badge/?version=latest |
| 4 |
:alt: Documentation |
| 5 |
:target: https://sphinx-copybutton.readthedocs.io/en/latest/?badge=latest |
| 6 |
``` |
| 7 |
|
| 8 |
```{image} https://img.shields.io/pypi/v/sphinx-copybutton.svg |
| 9 |
:alt: PyPi page |
| 10 |
:target: https://pypi.org/project/sphinx_copybutton |
| 11 |
``` |
| 12 |
|
| 13 |
```{image} https://img.shields.io/conda/vn/conda-forge/sphinx-copybutton.svg |
| 14 |
:alt: Conda Version |
| 15 |
:target: https://anaconda.org/conda-forge/sphinx-copybutton |
| 16 |
``` |
| 17 |
|
| 18 |
Sphinx-copybutton does one thing: add a little "copy" button to the right |
| 19 |
of your code blocks. That's it! It is a lightweight wrapper around the |
| 20 |
excellent (and also lightweight) Javascript library |
| 21 |
[ClipboardJS](https://clipboardjs.com/). |
| 22 |
|
| 23 |
**Here's an example** |
| 24 |
|
| 25 |
% This is stored in this issue: https://github.com/executablebooks/sphinx-copybutton/issues/157 |
| 26 |
|
| 27 |
```{image} https://user-images.githubusercontent.com/1839645/150200219-73663c59-08fd-4185-b157-62f3769c02ac.gif |
| 28 |
:alt: Copy Button Demo |
| 29 |
:width: 500px |
| 30 |
``` |
| 31 |
|
| 32 |
And here's a code block, note the copy button to the right! |
| 33 |
|
| 34 |
```bash |
| 35 |
copy me! |
| 36 |
``` |
| 37 |
|
| 38 |
If the code block overlaps to the right of the text area, you can just click |
| 39 |
the button to get the whole thing. |
| 40 |
|
| 41 |
```bash |
| 42 |
123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789 |
| 43 |
``` |
| 44 |
|
| 45 |
You can configure `sphinx-copybutton` to detect *input prompts* in code |
| 46 |
cells, and then both remove these prompts before copying, as well as skip |
| 47 |
lines that *don't* start with prompts (in case they are output lines). |
| 48 |
|
| 49 |
For example, this site has been configured to strip Python prompts (">>> ") and output lines. |
| 50 |
Try copy-pasting the code block below. |
| 51 |
|
| 52 |
```python |
| 53 |
>>> a = 2 |
| 54 |
>>> print(a) |
| 55 |
2 |
| 56 |
>>> |
| 57 |
>>> b = 'wow' |
| 58 |
>>> print(b) |
| 59 |
wow |
| 60 |
``` |
| 61 |
|
| 62 |
## Install |
| 63 |
|
| 64 |
You can install `sphinx-copybutton` with `pip`: |
| 65 |
|
| 66 |
```bash |
| 67 |
pip install sphinx-copybutton |
| 68 |
``` |
| 69 |
|
| 70 |
Or with `conda` via `conda-forge`: |
| 71 |
|
| 72 |
```bash |
| 73 |
conda install -c conda-forge sphinx-copybutton |
| 74 |
``` |
| 75 |
|
| 76 |
[Here's a link to the sphinx-copybutton GitHub repository](https://github.com/ExecutableBookProject/sphinx-copybutton). |
| 77 |
|
| 78 |
## Use |
| 79 |
|
| 80 |
In your `conf.py` configuration file, add `sphinx_copybutton` to your |
| 81 |
extensions list. E.g.: |
| 82 |
|
| 83 |
```python |
| 84 |
extensions = [ |
| 85 |
... |
| 86 |
'sphinx_copybutton' |
| 87 |
... |
| 88 |
] |
| 89 |
``` |
| 90 |
|
| 91 |
When you build your site, your code blocks should now have little copy buttons |
| 92 |
to their right. Clicking the button will copy the code inside! |
| 93 |
|
| 94 |
See [](use.md) for more information about how to use `sphinx-copybutton`. |
| 95 |
|
| 96 |
```{toctree} |
| 97 |
:maxdepth: 2 |
| 98 |
use |
| 99 |
contribute/index |
| 100 |
reference/example |
| 101 |
changelog |
| 102 |
``` |
| 103 |
|
| 104 |
## Inspiration |
| 105 |
|
| 106 |
The UI and design elements of `sphinx-copybutton` are heavily inspired by [GitHub's design choices](https://primer.style). |
| 107 |
The icon we use is from [Tabler's icons set](https://tablericons.com/). |