Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to hide Python prompts #167

Open
heistermann opened this issue Feb 3, 2015 · 7 comments
Open

Add functionality to hide Python prompts #167

heistermann opened this issue Feb 3, 2015 · 7 comments
Labels
Feature New feature

Comments

@heistermann
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@notmatthancock
Copy link

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
Copy link

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
Copy link

tobiscode commented Jul 6, 2020

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature
Projects
None yet
Development

No branches or pull requests

9 participants