Skip to content

Instantly share code, notes, and snippets.

@raghavrv
Created February 17, 2016 13:59
Show Gist options
  • Save raghavrv/0326d6212c1f20df3db8 to your computer and use it in GitHub Desktop.
Save raghavrv/0326d6212c1f20df3db8 to your computer and use it in GitHub Desktop.
Display CPP code with syntax highlighting in Jupyter Notebook
from IPython.display import Markdown, HTML, display_markdown
from glob import glob
cpp_markdown_template = """```cpp\n%s\n```\n<hr>"""
for i, problem in enumerate(
glob("/media/rvraghav93/code/projects/competitive_programming/codechef/*.cpp")):
with open(problem) as f:
code = f.read().splitlines()
url = code[0]
code_md = cpp_markdown_template % "\n".join(code[1:])
print i+1, url.strip('/ ')
display_markdown(Markdown(code_md))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment