readthedocs / sphinx_rtd_theme

Sphinx theme from Read the Docs
https://sphinx-rtd-theme.readthedocs.io/
MIT License
4.76k stars 1.73k forks source link

Add functionality to hide Python prompts #167

Open heistermann opened 9 years ago

heistermann commented 9 years ago

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:

 <script type="text/javascript" src="{{ pathto('_static/js/copybutton.js', 1) }}"></script>

Thanks for considering the request! If you prefer, I could also set up a PR.

francisco-dlp commented 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.

matthew-brett commented 8 years ago

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.

francisco-dlp commented 8 years ago

Thanks @matthew-brett, your solution works for me too.

notmatthancock commented 6 years ago

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.

phausamann commented 4 years ago

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')
tobiscode commented 4 years ago

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) 👍

jurasofish commented 4 years ago

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).