-
-
Save tonyfast/eeed58903980e863559c to your computer and use it in GitHub Desktop.
Disqus Magic for IPython notebooks
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Using Disqus in IPython Notebooks\n", | |
"\n", | |
"Install and load the [magics class](http://nbviewer.ipython.org/gist/anonymous/ea1890bc3c08a26c2bd4). Use line magic to create a comment stream for your [Disqus](https://disqus.com/) username with the post title following.\n", | |
"\n", | |
"* [Jupyter-nbviewer Issue](https://github.com/jupyter/nbviewer/issues/80#issuecomment-109449442)\n", | |
"* [Original Anonymous Magics Gist](https://gist.github.com/anonymous/ea1890bc3c08a26c2bd4)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Installed disqus.py. To use it, type:\n", | |
" %load_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 https://gist.githubusercontent.com/tonyfast/eeed58903980e863559c/raw/disqus.py\n", | |
"%load_ext disqus\n", | |
"%disqus tonyfast This is An Optional Title" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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 | |
} |
This file contains hidden or 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
from jinja2 import Template | |
from IPython.display import ( | |
display, | |
HTML, | |
) | |
from IPython.core import magic | |
tmpl = Template(""" | |
<div id="disqus_thread"></div> | |
<script type="text/javascript"> | |
var disqus_shortname = "{{ shortname }}"; | |
{% if title %} | |
var disqus_title = "{{ title }}"; | |
{% endif %} | |
</script> | |
<script src="https://{{ shortname }}.disqus.com/embed.js" async="true"> | |
</script> | |
<noscript> | |
Please enable JavaScript to view the | |
<a href="https://disqus.com/?ref_noscript" rel="nofollow"> | |
comments powered by Disqus. | |
</a> | |
</noscript> | |
""") | |
@magic.magics_class | |
class DisqusMagics(magic.Magics): | |
@magic.line_magic | |
def disqus(self, line): | |
line = line.strip().split(" ", 1) | |
title = line[1] if len(line) > 1 else None | |
display(HTML(tmpl.render(shortname=line[0], | |
title=title))) | |
def load_ipython_extension(ip): | |
ip.register_magics(DisqusMagics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment