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

Syntax highlighting doesn't work for Python cells with magic #59

Closed
marius311 opened this issue Aug 29, 2016 · 11 comments · Fixed by ipython/ipython#11384
Closed

Syntax highlighting doesn't work for Python cells with magic #59

marius311 opened this issue Aug 29, 2016 · 11 comments · Fixed by ipython/ipython#11384

Comments

@marius311
Copy link

If I have a cell in a Python 3 notebook that says e.g.

%%time
# some python code

then when rendered with nbsphinx, the # some python code looses its syntax highlighting. Could it be made so that the syntax highlighting is kept, the same way it does in Jupyter notebook itself?

Some info about my setup incase its just a bug:

nbsphinx==0.2.9
Sphinx==1.4.6
ipython==5.1.0
jupyter-client==4.3.0
jupyter-core==4.1.1
mgeier added a commit that referenced this issue Aug 30, 2016
@mgeier
Copy link
Member

mgeier commented Aug 30, 2016

The Jupyter Notebook and nbconvert seem to handle that differently. nbsphinx is normally closer to nbconvert, since it uses a lot of code from there. I think the highlighting in the Jupyter Notebook is done with client-side JavaScript, which cannot be re-used in nbsphinx.

I just enabled processing of cell magics in #60. However, the %%time magic still has no syntax highlighting, same as in nbconvert.

The ipython3 lexer seems to disable syntax highlighting when a cell magic is present.

I just saw that this was already reported to nbconvert (jupyter/nbconvert#105) and ipython (ipython/ipython#9099). Once this is resolved, it should automatically work in nbsphinx, too.

@marius311
Copy link
Author

Thats great, thanks for that. Will wait on the IPython fix then!

@uweschmitt
Copy link

Any updates here ? Still does not work with %%file.

@mgeier
Copy link
Member

mgeier commented Oct 5, 2017 via email

@mgeier
Copy link
Member

mgeier commented Oct 11, 2018

It mostly works now, the missing %%file stuff should be fixed in ipython/ipython#11384.

@mgeier
Copy link
Member

mgeier commented Oct 11, 2018

My PR has been merged, so I think this should be fine now.

If there is still some highlighting missing, please leave a comment!

@jeertmans
Copy link

jeertmans commented Aug 7, 2023

Hello @mgeier, I'm commenting this because I have the same issue, on a custom magic.

The syntax highlighting within the Jupyter document works fine:

image

But, when renderer using nbsphinx, it does not work:

image

Any idea on how to fix that?

Versions info:

Name: nbsphinx                                      
Version: 0.9.2                                      
Summary: Jupyter Notebook Tools for Sphinx
Home-page: https://nbsphinx.readthedocs.io/
Author: Matthias Geier                              
Author-email: Matthias.Geier@gmail.com
License: MIT                                        
---                                                 
Name: ipykernel                                     
Version: 6.25.1                                     
Summary: IPython Kernel for Jupyter
Home-page:                                          
Author:                                             
Author-email: IPython Development Team <ipython-dev@scipy.org>
License: BSD 3-Clause License
---
Name: ipython
Version: 8.12.2
Summary: IPython: Productive Interactive Computing
Home-page: https://ipython.org
Author: The IPython Development Team
Author-email: ipython-dev@python.org
License: BSD-3-Clause

@mgeier
Copy link
Member

mgeier commented Aug 7, 2023

What does nbconvert do?

See #59 (comment).

@jeertmans
Copy link

@mgeier nbconvert also fails: should I report that to them?

(Within the notebook viewer)

image

(When exported as HTML)

image

@mgeier
Copy link
Member

mgeier commented Aug 8, 2023

nbconvert also fails: should I report that to them?

Yes, or maybe even to IPython, see e.g. ipython/ipython#9099 and ipython/ipython#11137.

@jeertmans
Copy link

@mgeier done :-)

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

Successfully merging a pull request may close this issue.

4 participants