|
| 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 (">>> "). |
| 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 | +:::{note} |
| 65 | +`sphinx-copybutton` only works on Python >= 3.6 |
| 66 | +::: |
| 67 | + |
| 68 | +You can install `sphinx-copybutton` with `pip`: |
| 69 | + |
| 70 | +```bash |
| 71 | +pip install sphinx-copybutton |
| 72 | +``` |
| 73 | + |
| 74 | +Or with `conda` via `conda-forge`: |
| 75 | + |
| 76 | +```bash |
| 77 | +conda install -c conda-forge sphinx-copybutton |
| 78 | +``` |
| 79 | + |
| 80 | +[Here's a link to the sphinx-copybutton GitHub repository](https://github.com/ExecutableBookProject/sphinx-copybutton). |
| 81 | + |
| 82 | +## Use |
| 83 | + |
| 84 | +In your `conf.py` configuration file, add `sphinx_copybutton` to your |
| 85 | +extensions list. E.g.: |
| 86 | + |
| 87 | +```python |
| 88 | +extensions = [ |
| 89 | + ... |
| 90 | + 'sphinx_copybutton' |
| 91 | + ... |
| 92 | +] |
| 93 | +``` |
| 94 | + |
| 95 | +When you build your site, your code blocks should now have little copy buttons |
| 96 | +to their right. Clicking the button will copy the code inside! |
| 97 | + |
| 98 | +See [](use.md) for more information about how to use `sphinx-copybutton`. |
| 99 | + |
| 100 | +```{toctree} |
| 101 | +:maxdepth: 2 |
| 102 | +use |
| 103 | +contribute/index |
| 104 | +reference/example |
| 105 | +``` |
| 106 | + |
| 107 | +## Inspiration |
| 108 | + |
| 109 | +The UI and design elements of `sphinx-copybutton` are heavily inspired by [GitHub's design choices](https://primer.style). |
| 110 | +The icon we use is from [Tabler's icons set](https://tablericons.com/). |
0 commit comments