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

Custom mime type for improved syntax highlighting of code when using outputs from IPython.display.Code #13795

Open
DonJayamanne opened this issue Oct 23, 2022 · 5 comments

Comments

@DonJayamanne
Copy link

DonJayamanne commented Oct 23, 2022

Today when users execute code to generate output with syntax highlighting, the output is HTML which contains highlighted using pygments lexer.
This looks great, however with multiple front ends have support for themes and different styles for highlithing the code, I was wondering whether it would be possible to generate a custom mime type that would return the raw code in with the mime type text/x-<language>.

This way the front ends, such as Jupyter Lab, VS Code, and others would then have the option to pick these strings and perform the necessary syntax highlithing instead, optionally overriding the highlithing performed by the kernel.

I personally believe this would be benficial to Jupyter Lab as well, as they too support custom mime type renderers and could render code better in different themes.

Note: Today VS Code automatically supports mime syntax highlighting of output mime types of type text/x-<language id>

I'm curious whether such a proposed change to the existing IPyhton.display.Code would be acceptable.
Note: To be clear, what I'm suggestting is returning an additional mime type of text/x-<language id> along with the todays mime types of text/html, text/latex, text/plain

See below screenshot

  • Left is what we get today
  • Right is the output if we generate text/x-python (similar mime types from Code)
    As you can see the output ends up being highlighted in VS Code with support for themes.

More information on the support for text/x-<language id> in VS Code can be found here https://code.visualstudio.com/api/extension-guides/notebook#rich-output
Screen Shot 2022-10-23 at 15 45 31

@DonJayamanne
Copy link
Author

DonJayamanne commented Oct 23, 2022

Her'es the proposed change to the IPython.display.Code class

Just add a new mime type (that should not impact any existing code nor output of Jupyter Lab/Jupyter Notebook or others)

    def _repr_mimebundle_(self, **kwargs):
        mime = "text/x-" + self.language
        data = {mime:self.data}
        return data

@krassowski
Copy link
Member

I personally think that this is a good idea. This would fix #11747.

@westurner
Copy link

Would this roughly double the size of jupyter kernel messages?

Can this be changed in (how many?) other jupyter notebook/lab implementations?

@Carreau

@Carreau
Copy link
Member

Carreau commented Mar 25, 2024

I bleieve this would be a 100% backward compatible change. This can even be implemented in a separate package for the time being and brought in IPython later.

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

No branches or pull requests

4 participants