Copy multi-line Python code
Situation
I would like to add a copy button to Python code of the following form
Code example
============
.. code-block:: python
>>> import pandas as pd
>>> data = dict(
... movie = ['Rambo', 'Commando', 'Mad Max'],
... actor = ['Sylvester Stallone', 'Arnold Schwarzenegger', 'Mel Gibson'],
... year = [1982, 1985, 1980]
... )
>>> df = pd.DataFrame(data=data)
>>> print(df)
movie actor year
0 Rambo Sylvester Stallone 1982
1 Commando Arnold Schwarzenegger 1985
2 Mad Max Mel Gibson 1980
I added the following to my conf.py
extensions = [
'sphinx_copybutton',
]
copybutton_prompt_text = ">>> "
copybutton_only_copy_prompt_lines = True
I get the following output, when clicking the copy button in the generated HTML
import pandas as pd
data = dict(
... movie = ['Rambo', 'Commando', 'Mad Max'],
... actor = ['Sylvester Stallone', 'Arnold Schwarzenegger', 'Mel Gibson'],
... year = [1982, 1985, 1980]
... )
df = pd.DataFrame(data=data)
print(df)
movie actor year
0 Rambo Sylvester Stallone 1982
1 Commando Arnold Schwarzenegger 1985
2 Mad Max Mel Gibson 1980
Problem
As expected the lines starting with >>> are copied and the prompt is removed. But
-
Lines starting with
...are kept as they are. I'm surprised that they aren't removed by thecopybutton_only_copy_prompt_linesoption. In any case: What I actually want is to treat...also as prompt, i.e. keep the lines but remove the.... This might be related to #52 -
The output table at the end, is also copied. Again this should not happen with the
copybutton_only_copy_prompt_linesoption present.
Suggestions
Im not very good at Js but if you guys acknowledge the issue I can make a PR. But if its easy for you, go ahead :)
Related
- https://github.com/choldgraf/sphinx-copybutton/issues/52 (?)
hey! thanks for checking this out. I think this might be fixed on master, could you try again running with the master branch? And if that fixes it, maybe it's time to cut a release...
Hey, thanks for your reply. The result on master is
import pandas as pd
data = dict(
df = pd.DataFrame(data=data)
print(df)
So, it's better I'd say - exactly as expected. Now I only need to be able to add ... to the recognized command prompts.
Nice! OK so I think the issue here is that right now there is only one kind of prompt text allowed. This could probably be expanded to be a list of prompt text instead of a single string.
The relevant code is probably here: https://github.com/choldgraf/sphinx-copybutton/blob/master/sphinx_copybutton/_static/copybutton.js_t#L68
Perhaps we could write a function like match_prompt_text_with_line(line, prompt_text) where prompt_text could be a list, and the function would loop through that list and return True if the line starts with any of the prompt text items? That could then replace the simple startswith check here: https://github.com/choldgraf/sphinx-copybutton/blob/master/sphinx_copybutton/_static/copybutton.js_t#L77
I just wanted to :heavy_plus_sign: this issue for the same reasons as @joleroi and follow any developments. We also have such issues for our code examples in pyhf, so this would be great to add in a future release.
This could probably be expanded to be a list of prompt text instead of a single string.
This was my naive attempt, so seems perfect. I would generally offer to give it a stab myself, but probably can't timewise for the next week or so.
cc @kratsg @lukasheinrich (edit: though I now see they are already active on Issue #52)
I made a quick-and-dirty fix to add another prompt besides the current one. Have this in conf.py and you can have both prompts in effect equally:
copybutton_prompt_text = ">>> "
copybutton_prompt_text1 = "... "
The code is at https://github.com/mhuang001/sphinx-copybutton An example: https://fdi.readthedocs.io/en/latest/usage/quickstart.html BTW you can see a yellow version of the grey icon I have made for dark background.
https://huggingface.co/transformers/ suffers from this problem as well: https://github.com/huggingface/transformers/issues/5930
Scikit-learn also has this issue. Adding copybutton_line_continuation_character = "\n" seems to fix it, if you don't want to add a line continuation character like \:
copybutton_prompt_text = r">>> |\.\.\. "
copybutton_prompt_is_regexp = True
copybutton_line_continuation_character = "\n"