IPython 渲染#
import IPython
def code2html(code):
"""Helper function to use pygments to turn the code string into highlighted html."""
import pygments
from pygments.lexers import Python3Lexer
from pygments.formatters import HtmlFormatter
formatter = HtmlFormatter()
html = pygments.highlight(code, Python3Lexer(), formatter)
return "<style>%s</style>%s\n" % (formatter.get_style_defs(".highlight"), html)
code = """
a = 1
b = 2
c = a * b
d << 3
"""
IPython.display.HTML(code2html(code))
a = 1
b = 2
c = a * b
d << 3