Jupyter notebook output cell syntax highlighting
ipython, jupyter-notebook
Solution
Marking the code properly as answer.
Thanks to the feedback I was able to do the task with the following code:
from pygments import highlight
from pygments.lexers import XmlLexer
from pygments.formatters import HtmlFormatter
import IPython
def display_xml_nice(xml_element):
formatter = HtmlFormatter()
xml_indented = xml_element.toprettyxml(indent=' ')
IPython.display.display(HTML('<style type="text/css">{}</style> {}'.format(
formatter.get_style_defs('.highlight'),
highlight(xml_indented, XmlLexer(), formatter))))
Thank you, Robert
Problem
I'm using Jupyter notebook (IPython on Python 2.7). I'd like to show some XML content dynamically, like: ``` print dom.toprettyxml(indent=' ') ``` But it's formatted as plaintext in output cell. I'd like to show it together with syntax highlighting. Is it possible? I've come across IPython.display with Markdown, Latex, and several others, but no XML. (HTML was there as well, but it didn't help me). Regards, Robert Update: Thanks to the feedback I was able to do the task with the following code: ``` from pygments import highlight from pygments.lexers import XmlLexer from pygments.formatters import HtmlFormatter import IPython def display_xml_nice(xml_element): formatter = HtmlFormatter() xml_indented = xml_element.toprettyxml(indent=' ') IPython.display.display(HTML('<style type="text/css">{}</style>{}'.format( formatter.get_style_defs('.highlight'), highlight(xml_indented, XmlLexer(), formatter)))) ```