Created
February 6, 2014 22:15
-
-
Save jgosmann/8853594 to your computer and use it in GitHub Desktop.
Code to include formatted sources in IPython notebooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Some code do include nicely formatted source code listings of external | |
# files. Unfortunately, these listings will not be included using | |
# nbconvert to convert the notebook to a PDF. | |
import IPython | |
from pygments import highlight | |
from pygments.lexers import PythonLexer | |
from pygments.formatters import HtmlFormatter | |
def include_listing(filename): | |
with open(filename) as f: | |
code = f.read() | |
formatter = HtmlFormatter() | |
return IPython.display.HTML('<style type="text/css">{}</style>{}'.format( | |
formatter.get_style_defs('.highlight'), | |
highlight(code, PythonLexer(), formatter))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment