{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Nikola makes sharing Jupyter (IPython) Notebooks easy. You can render a static page like this and provide a link to download the notebook for interactive usage. However, a major downside with this is that the reader must set up his/her own Python environment which has all the dependencies installed and has to know how to run notebooks. It'd be much simpler if the reader could just click a link and an interactive notebook opened in the browser without him/her needing to do anything else. Well, I just discovered a service which easily enables this: [Binder](http://mybinder.org).\n", "\n", "\n", "\n", "In Binder, you provide your GitHub repository and then anyone can use URL http://mybinder.org/repo/GITHUB_USERNAME/GITHUB_REPOSITORY to access the notebooks in that repository interactively. Anyone going to that URL will get a separate isolated environment, so modifying the notebooks won't mess up anything. You can access specific notebooks by appending the path to a notebook to the URL. Simple.\n", "\n", "This post is a Jupyter Notebook so you can click \"Run Notebook\" link in the top menu to run it interactively in Binder. To have some Python code for you to play with, here's a few lines of code:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "42.0" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.sqrt(1764)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This post gives you instructions on how to add links to Binder into your Nikola powered blog. I assume that you have set up the blogging environment similarly as described in my [previous post](how-to-blog-with-jupyter-ipython-notebook-and-nikola). But first, go to [Binder](http://mybinder.org) and build your GitHub repository. I chose `requirements.txt` do define the dependencies. After building, follow these instructions to add the links to your posts." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Adding links to Binder\n", "\n", "Assuming you have created a custom theme for your site, you need to overwrite a few parts in your templates. First, create a template directory for your theme if it doesn't exist:\n", "\n", "```\n", "mkdir themes/YOUR_THEME_NAME/templates\n", "```\n", "\n", "We need to make minor changes to a few template, so we'll copy them and then modify. Copy `post.tmpl` of `bootstrap3-jinja` theme and `post_header.tmpl` of `base-jinja` theme to your `templates` directory. These are the files used by `bootstrap3-jinja` theme (which is the parent theme of my custom theme). You can see and download the full versions of the modified templates from [GitHub](https://github.com/jluttine/jaakkoluttinen.fi/tree/master/themes/jaakko/templates/). For clarity, I show here only the lines that have been modified. In `post_header.tmpl`:\n", "\n", "```\n", "... (to macro html_sourcelink)\n", "{% if post.compiler.name == \"ipynb\" %} \n", "
\n", "{% endif %}\n", "...\n", "```\n", "\n", "In `post.tmpl`:\n", "\n", "```\n", "... (to block content)\n", "{% if post.compiler.name == \"ipynb\" %} \n", "\n", "This post is a Jupyter Notebook.\n", "You can download it or\n", "run it interactively in Binder.\n", "
\n", "{% endif %}\n", "\n", "... (to block sourcelink)\n", "{% if post.compiler.name == \"ipynb\" %} \n", "