Skip to content

Instantly share code, notes, and snippets.

@jiffyclub
Created April 15, 2013 03:34
Show Gist options
  • Save jiffyclub/5385520 to your computer and use it in GitHub Desktop.
Save jiffyclub/5385520 to your computer and use it in GitHub Desktop.
Example usage of the pygments_magic IPython extension for showing source code files with syntax highlighting in IPython.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "pygments_magic"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The [pygments_magic](https://gist.github.com/jiffyclub/5385501) extension allows you to display source code files in IPython with\n",
"syntax highlighting. It requires the [Pygments](http://pygments.org) library.\n",
"\n",
"It can be installed with:\n",
" \n",
" %install_ext https://gist.github.com/jiffyclub/5385501/raw/pygments_magic.py"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext pygments_magic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `%highlight` magic uses terminal color codes and will work in any of IPython's front ends."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%highlight ipython_rocks.py"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\u001b[34;01mfrom\u001b[39;49;00m \u001b[39;49;00m\u001b[04m\u001b[36;01m__future__\u001b[39;49;00m \u001b[39;49;00m\u001b[34;01mimport\u001b[39;49;00m \u001b[39;49;00mprint_function\u001b[39;49;00m\n",
"\n",
"\u001b[34;01mdef\u001b[39;49;00m \u001b[39;49;00m\u001b[32;01mipython_rocks\u001b[39;49;00m(\u001b[39;49;00m)\u001b[39;49;00m:\u001b[39;49;00m\n",
" \u001b[39;49;00m\u001b[33m\"\"\"Yes, yes it does.\"\"\"\u001b[39;49;00m\n",
" \u001b[39;49;00m\u001b[34;01mprint\u001b[39;49;00m(\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m\u001b[33mIPython rocks!\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m)\u001b[39;49;00m\n",
"\n",
"ipython_rocks\u001b[39;49;00m(\u001b[39;49;00m)\u001b[39;49;00m\n",
"\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `%highlight_html` magic outputs HTML and is best used in the Notebook. `%highlight_html` can show line numbers or display with any avaiable\n",
"[Pygments style](http://pygments.org/docs/styles/#builtin-styles). See [these previews](http://blog.favrik.com/2011/02/22/preview-all-pygments-styles-for-your-code-highlighting-needs/) for some examples."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%highlight_html -n ipython_rocks.py"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<style>\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .hll { background-color: #ffffcc }\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 { background: #f8f8f8; }\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .c { color: #408080; font-style: italic } /* Comment */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .err { border: 1px solid #FF0000 } /* Error */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .k { color: #008000; font-weight: bold } /* Keyword */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .o { color: #666666 } /* Operator */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .cp { color: #BC7A00 } /* Comment.Preproc */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .cs { color: #408080; font-style: italic } /* Comment.Special */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gd { color: #A00000 } /* Generic.Deleted */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .ge { font-style: italic } /* Generic.Emph */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gr { color: #FF0000 } /* Generic.Error */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gi { color: #00A000 } /* Generic.Inserted */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .go { color: #808080 } /* Generic.Output */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gs { font-weight: bold } /* Generic.Strong */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .gt { color: #0040D0 } /* Generic.Traceback */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .kp { color: #008000 } /* Keyword.Pseudo */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .kt { color: #B00040 } /* Keyword.Type */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .m { color: #666666 } /* Literal.Number */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .s { color: #BA2121 } /* Literal.String */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .na { color: #7D9029 } /* Name.Attribute */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nb { color: #008000 } /* Name.Builtin */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .no { color: #880000 } /* Name.Constant */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nd { color: #AA22FF } /* Name.Decorator */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nf { color: #0000FF } /* Name.Function */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nl { color: #A0A000 } /* Name.Label */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .nv { color: #19177C } /* Name.Variable */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .w { color: #bbbbbb } /* Text.Whitespace */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .mf { color: #666666 } /* Literal.Number.Float */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .mh { color: #666666 } /* Literal.Number.Hex */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .mi { color: #666666 } /* Literal.Number.Integer */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .mo { color: #666666 } /* Literal.Number.Oct */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .sb { color: #BA2121 } /* Literal.String.Backtick */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .sc { color: #BA2121 } /* Literal.String.Char */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .s2 { color: #BA2121 } /* Literal.String.Double */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .sx { color: #008000 } /* Literal.String.Other */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .sr { color: #BB6688 } /* Literal.String.Regex */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .s1 { color: #BA2121 } /* Literal.String.Single */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .ss { color: #19177C } /* Literal.String.Symbol */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .vc { color: #19177C } /* Name.Variable.Class */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .vg { color: #19177C } /* Name.Variable.Global */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .vi { color: #19177C } /* Name.Variable.Instance */\n",
".pygmentsed4f20f4-d133-40ac-8958-95781ecf0955 .il { color: #666666 } /* Literal.Number.Integer.Long */\n",
"</style>\n",
"<table class=\"pygmentsed4f20f4-d133-40ac-8958-95781ecf0955table\"><tr><td class=\"linenos\"><div class=\"linenodiv\"><pre>1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7</pre></div></td><td class=\"code\"><div class=\"pygmentsed4f20f4-d133-40ac-8958-95781ecf0955\"><pre><span class=\"kn\">from</span> <span class=\"nn\">__future__</span> <span class=\"kn\">import</span> <span class=\"n\">print_function</span>\n",
"\n",
"<span class=\"k\">def</span> <span class=\"nf\">ipython_rocks</span><span class=\"p\">():</span>\n",
" <span class=\"sd\">&quot;&quot;&quot;Yes, yes it does.&quot;&quot;&quot;</span>\n",
" <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s\">&#39;IPython rocks!&#39;</span><span class=\"p\">)</span>\n",
"\n",
"<span class=\"n\">ipython_rocks</span><span class=\"p\">()</span>\n",
"</pre></div>\n",
"</td></tr></table>\n"
],
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x10d44c1d0>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%highlight_html -s monokai ipython_rocks.py"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<style>\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .hll { background-color: #49483e }\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb { background: #272822; color: #f8f8f2 }\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .c { color: #75715e } /* Comment */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .err { color: #960050; background-color: #1e0010 } /* Error */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .k { color: #66d9ef } /* Keyword */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .l { color: #ae81ff } /* Literal */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .n { color: #f8f8f2 } /* Name */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .o { color: #f92672 } /* Operator */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .p { color: #f8f8f2 } /* Punctuation */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .cm { color: #75715e } /* Comment.Multiline */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .cp { color: #75715e } /* Comment.Preproc */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .c1 { color: #75715e } /* Comment.Single */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .cs { color: #75715e } /* Comment.Special */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .ge { font-style: italic } /* Generic.Emph */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .gs { font-weight: bold } /* Generic.Strong */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .kc { color: #66d9ef } /* Keyword.Constant */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .kd { color: #66d9ef } /* Keyword.Declaration */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .kn { color: #f92672 } /* Keyword.Namespace */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .kp { color: #66d9ef } /* Keyword.Pseudo */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .kr { color: #66d9ef } /* Keyword.Reserved */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .kt { color: #66d9ef } /* Keyword.Type */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .ld { color: #e6db74 } /* Literal.Date */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .m { color: #ae81ff } /* Literal.Number */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .s { color: #e6db74 } /* Literal.String */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .na { color: #a6e22e } /* Name.Attribute */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nb { color: #f8f8f2 } /* Name.Builtin */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nc { color: #a6e22e } /* Name.Class */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .no { color: #66d9ef } /* Name.Constant */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nd { color: #a6e22e } /* Name.Decorator */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .ni { color: #f8f8f2 } /* Name.Entity */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .ne { color: #a6e22e } /* Name.Exception */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nf { color: #a6e22e } /* Name.Function */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nl { color: #f8f8f2 } /* Name.Label */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nn { color: #f8f8f2 } /* Name.Namespace */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nx { color: #a6e22e } /* Name.Other */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .py { color: #f8f8f2 } /* Name.Property */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nt { color: #f92672 } /* Name.Tag */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .nv { color: #f8f8f2 } /* Name.Variable */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .ow { color: #f92672 } /* Operator.Word */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .w { color: #f8f8f2 } /* Text.Whitespace */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .mf { color: #ae81ff } /* Literal.Number.Float */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .mh { color: #ae81ff } /* Literal.Number.Hex */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .mi { color: #ae81ff } /* Literal.Number.Integer */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .mo { color: #ae81ff } /* Literal.Number.Oct */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .sb { color: #e6db74 } /* Literal.String.Backtick */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .sc { color: #e6db74 } /* Literal.String.Char */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .sd { color: #e6db74 } /* Literal.String.Doc */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .s2 { color: #e6db74 } /* Literal.String.Double */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .se { color: #ae81ff } /* Literal.String.Escape */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .sh { color: #e6db74 } /* Literal.String.Heredoc */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .si { color: #e6db74 } /* Literal.String.Interpol */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .sx { color: #e6db74 } /* Literal.String.Other */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .sr { color: #e6db74 } /* Literal.String.Regex */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .s1 { color: #e6db74 } /* Literal.String.Single */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .ss { color: #e6db74 } /* Literal.String.Symbol */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .vc { color: #f8f8f2 } /* Name.Variable.Class */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .vg { color: #f8f8f2 } /* Name.Variable.Global */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .vi { color: #f8f8f2 } /* Name.Variable.Instance */\n",
".pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb .il { color: #ae81ff } /* Literal.Number.Integer.Long */\n",
"</style>\n",
"<div class=\"pygments1a3417e2-7ec3-4037-aba5-5b02b3e858eb\"><pre><span class=\"kn\">from</span> <span class=\"nn\">__future__</span> <span class=\"kn\">import</span> <span class=\"n\">print_function</span>\n",
"\n",
"<span class=\"k\">def</span> <span class=\"nf\">ipython_rocks</span><span class=\"p\">():</span>\n",
" <span class=\"sd\">&quot;&quot;&quot;Yes, yes it does.&quot;&quot;&quot;</span>\n",
" <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s\">&#39;IPython rocks!&#39;</span><span class=\"p\">)</span>\n",
"\n",
"<span class=\"n\">ipython_rocks</span><span class=\"p\">()</span>\n",
"</pre></div>\n",
"\n"
],
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x10d44c0d0>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment