CSS in IPython display with XML

124 views
Skip to first unread message

Jonathan Robie

unread,
Nov 29, 2017, 6:30:58 PM11/29/17
to Project Jupyter
I am writing some notebooks for processing Greek syntax trees using XPath / XQuery:


I can pretty print the XML like this:

from pygments import highlight
from pygments.lexers import XmlLexer
from pygments.formatters import HtmlFormatter
import IPython

def xml_display(xml):
    formatter = HtmlFormatter()
    display(
        IPython.display.HTML('<style type="text/css">{}</style>{}'.format (
            formatter.get_style_defs('.highlight'),
            highlight(xml, XmlLexer(), formatter))))

def xquery_display(query):
    xml_display(xquery(query))

But I also want to be able to display results using .css as though they were in a browser.  This looks like it should be straightforward, but I'm failing to see the hook that I need.

This works fine, but without a custom .css stylesheet:

def boxwood(xml):
    formatter = HtmlFormatter()
    display(HTML(xml))

I want to add the .css stylesheet to this.  Where do I do that?

Thanks!

Jonathan

Thomas Kluyver

unread,
Nov 30, 2017, 6:12:24 AM11/30/17
to Project Jupyter
Like your previous example, I think - assemble an HTML fragment including a <style> tag, and then display that.

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/43b5073b-09a2-449c-b1e4-6c95ab3871ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Robie

unread,
Nov 30, 2017, 8:03:06 AM11/30/17
to Project Jupyter
Thanks - that should have been obvious, but it wasn't at the time.  This works, loading two external stylesheets:

treedown = "/Users/jonathan/git/greek-new-testament/syntax-trees/nestle1904-lowfat/xml/treedown.css"
boxwoodcss = "/Users/jonathan/git/greek-new-testament/syntax-trees/nestle1904-lowfat/xml/boxwood.css"

def boxwood(xml):
    formatter = HtmlFormatter()
    display(HTML('<style type="text/css">{}{}</style>{}'.format(
        open(treedown).readlines(),
        open(boxwoodcss).readlines(),
        xml)))

Jonathan
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages