Open heistermann opened 9 years ago
I have copybutton.js in _static
and the following in conf.py
:
def setup(app):
app.add_javascript('copybutton.js')
I was hoping that this would provide the feature without needing to change the sphinx_rtd_theme. However, although it works with other themes, it doesn't with this one.
I found that, in order to get copybutton.js
to work, I had to update the default copy of jquery.js
that Sphinx provided. Specifically, I copied http://www.scipy-lectures.org/_static/jquery.js to my project _static
directory.
Thanks @matthew-brett, your solution works for me too.
This hasn't been discussed since 2015 apparently; however, this is the only discussion of this issue I can really find on the web. I figured I should add my experience to the mix.
The copybutton.js
file specifically selects python 2 and 3 in the line:
var div = $('.highlight-python .highlight,' +
'.highlight-python3 .highlight')
In my HTML, the code block are class highlight-default
, rather than highlight-python
(despite being Python code blocks). I replace the above line with
var div = $('.highlight-default > .highlight')
and the >>>
toggle switch appears and works as intended.
To add to @notmatthancock's comment, in my case the div class was highlight-pycon
(using sphinx-doctest).
So my code looks like:
var div = $('.highlight-python .highlight,' +
'.highlight-python3 .highlight,' +
'.highlight-pycon .highlight')
To add to @notmatthancock's comment, in my case the div class was
highlight-pycon
(using sphinx-doctest).So my code looks like:
var div = $('.highlight-python .highlight,' + '.highlight-python3 .highlight,' + '.highlight-pycon .highlight')
And I'm just going to add that I also had to extend the .highlight-pycon
to .highlight-pycon3
for Python 3 (also Sphinx doctest) 👍
For anyone looking for this functionality as an installable extension I've created sphinx toggleprompt https://github.com/jurasofish/sphinx-toggleprompt
doesn't currently cover all the div classes you guys have mentioned but they're trivial to add (will create an issue and get around to it sometime).
I very much like the possibility to hide
>>>
prompts in the new SciPy documentation. Here is an example of what I mean:http://docs.scipy.org/doc/numpy/user/misc.html#examples
This way, you could easily paste code into interactive sessions.
But I also like the rtd theme! It would be quite easy to incorporate this functionality. You would just have to copy the copybutton.js file to the
sphinx_rtd_theme/static/js
directory.Then you need to add the following line after line 143 of theme.html:
Thanks for considering the request! If you prefer, I could also set up a PR.