Skip to content

Instantly share code, notes, and snippets.

@tkf
Last active February 13, 2017 01:57
Show Gist options
  • Save tkf/9de9158332df0ad69e7dd2f4b59982e1 to your computer and use it in GitHub Desktop.
Save tkf/9de9158332df0ad69e7dd2f4b59982e1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from IPython import display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from docutils.core import publish_string"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"rst = r\"\"\"\n",
"Rendering ReST in IPython\n",
"=========================\n",
"\n",
"Inline markups such as *emphasis*, **strong emphasis**, `interpreted text` and ``inline literal`` work as expected.\n",
"\n",
"Embedding code:\n",
"\n",
".. code:: python\n",
"\n",
" def fib(n):\n",
" if n > 1:\n",
" return fib(n-1) + fib(n-2)\n",
" else:\n",
" return n\n",
"\n",
"Embedding doctests:\n",
"\n",
">>> fib(4)\n",
"3\n",
">>> fib(-1) # oops!\n",
"-1\n",
"\n",
"Math rendering: :math:`e^{-i\\pi} = -1`.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n",
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n",
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n",
"<head>\n",
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n",
"<meta name=\"generator\" content=\"Docutils 0.13.1: http://docutils.sourceforge.net/\" />\n",
"<title>Rendering ReST in IPython</title>\n",
"<script type=\"text/javascript\" src=\"https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML\"></script>\n",
"<style type=\"text/css\">\n",
"\n",
"/*\n",
":Author: David Goodger (goodger@python.org)\n",
":Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $\n",
":Copyright: This stylesheet has been placed in the public domain.\n",
"\n",
"Default cascading style sheet for the HTML output of Docutils.\n",
"\n",
"See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to\n",
"customize this style sheet.\n",
"*/\n",
"\n",
"/* used to remove borders from tables and images */\n",
".borderless, table.borderless td, table.borderless th {\n",
" border: 0 }\n",
"\n",
"table.borderless td, table.borderless th {\n",
" /* Override padding for \"table.docutils td\" with \"! important\".\n",
" The right padding separates the table cells. */\n",
" padding: 0 0.5em 0 0 ! important }\n",
"\n",
".first {\n",
" /* Override more specific margin styles with \"! important\". */\n",
" margin-top: 0 ! important }\n",
"\n",
".last, .with-subtitle {\n",
" margin-bottom: 0 ! important }\n",
"\n",
".hidden {\n",
" display: none }\n",
"\n",
".subscript {\n",
" vertical-align: sub;\n",
" font-size: smaller }\n",
"\n",
".superscript {\n",
" vertical-align: super;\n",
" font-size: smaller }\n",
"\n",
"a.toc-backref {\n",
" text-decoration: none ;\n",
" color: black }\n",
"\n",
"blockquote.epigraph {\n",
" margin: 2em 5em ; }\n",
"\n",
"dl.docutils dd {\n",
" margin-bottom: 0.5em }\n",
"\n",
"object[type=\"image/svg+xml\"], object[type=\"application/x-shockwave-flash\"] {\n",
" overflow: hidden;\n",
"}\n",
"\n",
"/* Uncomment (and remove this text!) to get bold-faced definition list terms\n",
"dl.docutils dt {\n",
" font-weight: bold }\n",
"*/\n",
"\n",
"div.abstract {\n",
" margin: 2em 5em }\n",
"\n",
"div.abstract p.topic-title {\n",
" font-weight: bold ;\n",
" text-align: center }\n",
"\n",
"div.admonition, div.attention, div.caution, div.danger, div.error,\n",
"div.hint, div.important, div.note, div.tip, div.warning {\n",
" margin: 2em ;\n",
" border: medium outset ;\n",
" padding: 1em }\n",
"\n",
"div.admonition p.admonition-title, div.hint p.admonition-title,\n",
"div.important p.admonition-title, div.note p.admonition-title,\n",
"div.tip p.admonition-title {\n",
" font-weight: bold ;\n",
" font-family: sans-serif }\n",
"\n",
"div.attention p.admonition-title, div.caution p.admonition-title,\n",
"div.danger p.admonition-title, div.error p.admonition-title,\n",
"div.warning p.admonition-title, .code .error {\n",
" color: red ;\n",
" font-weight: bold ;\n",
" font-family: sans-serif }\n",
"\n",
"/* Uncomment (and remove this text!) to get reduced vertical space in\n",
" compound paragraphs.\n",
"div.compound .compound-first, div.compound .compound-middle {\n",
" margin-bottom: 0.5em }\n",
"\n",
"div.compound .compound-last, div.compound .compound-middle {\n",
" margin-top: 0.5em }\n",
"*/\n",
"\n",
"div.dedication {\n",
" margin: 2em 5em ;\n",
" text-align: center ;\n",
" font-style: italic }\n",
"\n",
"div.dedication p.topic-title {\n",
" font-weight: bold ;\n",
" font-style: normal }\n",
"\n",
"div.figure {\n",
" margin-left: 2em ;\n",
" margin-right: 2em }\n",
"\n",
"div.footer, div.header {\n",
" clear: both;\n",
" font-size: smaller }\n",
"\n",
"div.line-block {\n",
" display: block ;\n",
" margin-top: 1em ;\n",
" margin-bottom: 1em }\n",
"\n",
"div.line-block div.line-block {\n",
" margin-top: 0 ;\n",
" margin-bottom: 0 ;\n",
" margin-left: 1.5em }\n",
"\n",
"div.sidebar {\n",
" margin: 0 0 0.5em 1em ;\n",
" border: medium outset ;\n",
" padding: 1em ;\n",
" background-color: #ffffee ;\n",
" width: 40% ;\n",
" float: right ;\n",
" clear: right }\n",
"\n",
"div.sidebar p.rubric {\n",
" font-family: sans-serif ;\n",
" font-size: medium }\n",
"\n",
"div.system-messages {\n",
" margin: 5em }\n",
"\n",
"div.system-messages h1 {\n",
" color: red }\n",
"\n",
"div.system-message {\n",
" border: medium outset ;\n",
" padding: 1em }\n",
"\n",
"div.system-message p.system-message-title {\n",
" color: red ;\n",
" font-weight: bold }\n",
"\n",
"div.topic {\n",
" margin: 2em }\n",
"\n",
"h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,\n",
"h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {\n",
" margin-top: 0.4em }\n",
"\n",
"h1.title {\n",
" text-align: center }\n",
"\n",
"h2.subtitle {\n",
" text-align: center }\n",
"\n",
"hr.docutils {\n",
" width: 75% }\n",
"\n",
"img.align-left, .figure.align-left, object.align-left, table.align-left {\n",
" clear: left ;\n",
" float: left ;\n",
" margin-right: 1em }\n",
"\n",
"img.align-right, .figure.align-right, object.align-right, table.align-right {\n",
" clear: right ;\n",
" float: right ;\n",
" margin-left: 1em }\n",
"\n",
"img.align-center, .figure.align-center, object.align-center {\n",
" display: block;\n",
" margin-left: auto;\n",
" margin-right: auto;\n",
"}\n",
"\n",
"table.align-center {\n",
" margin-left: auto;\n",
" margin-right: auto;\n",
"}\n",
"\n",
".align-left {\n",
" text-align: left }\n",
"\n",
".align-center {\n",
" clear: both ;\n",
" text-align: center }\n",
"\n",
".align-right {\n",
" text-align: right }\n",
"\n",
"/* reset inner alignment in figures */\n",
"div.align-right {\n",
" text-align: inherit }\n",
"\n",
"/* div.align-center * { */\n",
"/* text-align: left } */\n",
"\n",
".align-top {\n",
" vertical-align: top }\n",
"\n",
".align-middle {\n",
" vertical-align: middle }\n",
"\n",
".align-bottom {\n",
" vertical-align: bottom }\n",
"\n",
"ol.simple, ul.simple {\n",
" margin-bottom: 1em }\n",
"\n",
"ol.arabic {\n",
" list-style: decimal }\n",
"\n",
"ol.loweralpha {\n",
" list-style: lower-alpha }\n",
"\n",
"ol.upperalpha {\n",
" list-style: upper-alpha }\n",
"\n",
"ol.lowerroman {\n",
" list-style: lower-roman }\n",
"\n",
"ol.upperroman {\n",
" list-style: upper-roman }\n",
"\n",
"p.attribution {\n",
" text-align: right ;\n",
" margin-left: 50% }\n",
"\n",
"p.caption {\n",
" font-style: italic }\n",
"\n",
"p.credits {\n",
" font-style: italic ;\n",
" font-size: smaller }\n",
"\n",
"p.label {\n",
" white-space: nowrap }\n",
"\n",
"p.rubric {\n",
" font-weight: bold ;\n",
" font-size: larger ;\n",
" color: maroon ;\n",
" text-align: center }\n",
"\n",
"p.sidebar-title {\n",
" font-family: sans-serif ;\n",
" font-weight: bold ;\n",
" font-size: larger }\n",
"\n",
"p.sidebar-subtitle {\n",
" font-family: sans-serif ;\n",
" font-weight: bold }\n",
"\n",
"p.topic-title {\n",
" font-weight: bold }\n",
"\n",
"pre.address {\n",
" margin-bottom: 0 ;\n",
" margin-top: 0 ;\n",
" font: inherit }\n",
"\n",
"pre.literal-block, pre.doctest-block, pre.math, pre.code {\n",
" margin-left: 2em ;\n",
" margin-right: 2em }\n",
"\n",
"pre.code .ln { color: grey; } /* line numbers */\n",
"pre.code, code { background-color: #eeeeee }\n",
"pre.code .comment, code .comment { color: #5C6576 }\n",
"pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }\n",
"pre.code .literal.string, code .literal.string { color: #0C5404 }\n",
"pre.code .name.builtin, code .name.builtin { color: #352B84 }\n",
"pre.code .deleted, code .deleted { background-color: #DEB0A1}\n",
"pre.code .inserted, code .inserted { background-color: #A3D289}\n",
"\n",
"span.classifier {\n",
" font-family: sans-serif ;\n",
" font-style: oblique }\n",
"\n",
"span.classifier-delimiter {\n",
" font-family: sans-serif ;\n",
" font-weight: bold }\n",
"\n",
"span.interpreted {\n",
" font-family: sans-serif }\n",
"\n",
"span.option {\n",
" white-space: nowrap }\n",
"\n",
"span.pre {\n",
" white-space: pre }\n",
"\n",
"span.problematic {\n",
" color: red }\n",
"\n",
"span.section-subtitle {\n",
" /* font-size relative to parent (h1..h6 element) */\n",
" font-size: 80% }\n",
"\n",
"table.citation {\n",
" border-left: solid 1px gray;\n",
" margin-left: 1px }\n",
"\n",
"table.docinfo {\n",
" margin: 2em 4em }\n",
"\n",
"table.docutils {\n",
" margin-top: 0.5em ;\n",
" margin-bottom: 0.5em }\n",
"\n",
"table.footnote {\n",
" border-left: solid 1px black;\n",
" margin-left: 1px }\n",
"\n",
"table.docutils td, table.docutils th,\n",
"table.docinfo td, table.docinfo th {\n",
" padding-left: 0.5em ;\n",
" padding-right: 0.5em ;\n",
" vertical-align: top }\n",
"\n",
"table.docutils th.field-name, table.docinfo th.docinfo-name {\n",
" font-weight: bold ;\n",
" text-align: left ;\n",
" white-space: nowrap ;\n",
" padding-left: 0 }\n",
"\n",
"/* \"booktabs\" style (no vertical lines) */\n",
"table.docutils.booktabs {\n",
" border: 0px;\n",
" border-top: 2px solid;\n",
" border-bottom: 2px solid;\n",
" border-collapse: collapse;\n",
"}\n",
"table.docutils.booktabs * {\n",
" border: 0px;\n",
"}\n",
"table.docutils.booktabs th {\n",
" border-bottom: thin solid;\n",
" text-align: left;\n",
"}\n",
"\n",
"h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,\n",
"h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {\n",
" font-size: 100% }\n",
"\n",
"ul.auto-toc {\n",
" list-style-type: none }\n",
"\n",
"</style>\n",
"</head>\n",
"<body>\n",
"<div class=\"document\" id=\"rendering-rest-in-ipython\">\n",
"<h1 class=\"title\">Rendering ReST in IPython</h1>\n",
"\n",
"<p>Inline markups such as <em>emphasis</em>, <strong>strong emphasis</strong>, <cite>interpreted text</cite> and <tt class=\"docutils literal\">inline literal</tt> work as expected.</p>\n",
"<p>Embedding code:</p>\n",
"<pre class=\"code python literal-block\">\n",
"<span class=\"keyword\">def</span> <span class=\"name function\">fib</span><span class=\"punctuation\">(</span><span class=\"name\">n</span><span class=\"punctuation\">):</span>\n",
" <span class=\"keyword\">if</span> <span class=\"name\">n</span> <span class=\"operator\">&gt;</span> <span class=\"literal number integer\">1</span><span class=\"punctuation\">:</span>\n",
" <span class=\"keyword\">return</span> <span class=\"name\">fib</span><span class=\"punctuation\">(</span><span class=\"name\">n</span><span class=\"operator\">-</span><span class=\"literal number integer\">1</span><span class=\"punctuation\">)</span> <span class=\"operator\">+</span> <span class=\"name\">fib</span><span class=\"punctuation\">(</span><span class=\"name\">n</span><span class=\"operator\">-</span><span class=\"literal number integer\">2</span><span class=\"punctuation\">)</span>\n",
" <span class=\"keyword\">else</span><span class=\"punctuation\">:</span>\n",
" <span class=\"keyword\">return</span> <span class=\"name\">n</span>\n",
"</pre>\n",
"<p>Embedding doctests:</p>\n",
"<pre class=\"doctest-block\">\n",
"&gt;&gt;&gt; fib(4)\n",
"3\n",
"&gt;&gt;&gt; fib(-1) # oops!\n",
"-1\n",
"</pre>\n",
"<p>Math rendering: <span class=\"math\">\\(e^{-i\\pi} = -1\\)</span>.</p>\n",
"</div>\n",
"</body>\n",
"</html>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"html = publish_string(rst, writer_name='html', settings_overrides={'math_output': 'mathjax'})\n",
"display.display_html(html, raw=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (unstable3)",
"language": "python",
"name": "unstable3-python"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment