Skip to content

Instantly share code, notes, and snippets.

Created June 5, 2015 21:50
Show Gist options
  • Save anonymous/ea1890bc3c08a26c2bd4 to your computer and use it in GitHub Desktop.
Save anonymous/ea1890bc3c08a26c2bd4 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": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting disqus.py\n"
]
}
],
"source": [
"%%file disqus.py\n",
"from jinja2 import Template\n",
"from IPython.display import (\n",
" display,\n",
" HTML,\n",
")\n",
"from IPython.core import magic\n",
"\n",
"tmpl = Template(\"\"\"\n",
"<div id=\"disqus_thread\"></div>\n",
"\n",
"<script type=\"text/javascript\">\n",
" var disqus_shortname = \"{{ shortname }}\";\n",
" {% if title %}\n",
" var disqus_title = \"{{ title }}\";\n",
" {% endif %}\n",
"</script>\n",
"<script src=\"https://{{ shortname }}.disqus.com/embed.js\" async=\"true\">\n",
"</script>\n",
"\n",
"<noscript>\n",
" Please enable JavaScript to view the\n",
" <a href=\"https://disqus.com/?ref_noscript\" rel=\"nofollow\">\n",
" comments powered by Disqus.\n",
" </a>\n",
"</noscript>\n",
"\"\"\")\n",
"\n",
"@magic.magics_class\n",
"class DisqusMagics(magic.Magics):\n",
" @magic.line_magic\n",
" def disqus(self, line):\n",
" line = line.strip().split(\" \", 1)\n",
" title = line[1] if len(line) > 1 else None\n",
" \n",
" display(HTML(tmpl.render(shortname=line[0],\n",
" title=title)))\n",
"def load_ipython_extension(ip):\n",
" ip.register_magics(DisqusMagics)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Installed disqus.py. To use it, type:\n",
" %load_ext disqus\n",
"The disqus extension is already loaded. To reload it, use:\n",
" %reload_ext disqus\n"
]
},
{
"data": {
"text/html": [
"\n",
"<div id=\"disqus_thread\"></div>\n",
"\n",
"<script type=\"text/javascript\">\n",
" var disqus_shortname = \"tonyfast\";\n",
" \n",
" var disqus_title = \"This is An Optional Title\";\n",
" \n",
"</script>\n",
"<script src=\"https://tonyfast.disqus.com/embed.js\" async=\"true\">\n",
"</script>\n",
"\n",
"<noscript>\n",
" Please enable JavaScript to view the\n",
" <a href=\"https://disqus.com/?ref_noscript\" rel=\"nofollow\">\n",
" comments powered by Disqus.\n",
" </a>\n",
"</noscript>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%install_ext ./disqus.py\n",
"%load_ext disqus\n",
"%disqus tonyfast This is An Optional Title"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment