Embedding Matplotlib Animations in Jupyter Notebooks

In his blog post Embedding Matplotlib Animations in IPython Notebooks, Jake VanderPlas presents a slick hack for embedding Matplotlib Animations in IPython Notebooks, which involves writing it as a video to a tempfile, and then re-encoding it in Base64 as a HTML5 Video.

Unfortunately (or rather fortunately), this hack has been largely rendered obsolete by the heavy development efforts dedicated to both Matplotlib and IPython Notebook (since renamed to Jupyter Notebook) in recent years. In particular, Matplotlib 1.5.1 now supports inline display of animations in the notebook with the to_html5_video method, which converts the animation to an h264 encoded video and embeddeds it directly in the notebook.

In this notebook, we reproduce Jake VanderPlas' blog post with this new feature.

Read more…

Installing Cartopy on Mac OSX (10.11)

So you develop on Mac OS X (10.11) and have installed the external dependencies (geos 3.5.0, proj 4.9.2) of Cartopy with Homebrew:

$ brew install geos
$ brew install proj

Moreover, you have created a virtualenv and installed the Python dependencies (Cython, NumPy):

$ mkvirtualenv cartopy_venv
(cartopy_venv)$ pip install cython
(cartopy_venv)$ pip install numpy

Yet, when you finally go install Cartopy, you still encounter the following seemingly inexplicable error:

(cartopy_venv)$ pip install cartopy
[...]
clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include -I./lib/cartopy -I-I/usr/local/Cellar/proj/4.9.2/include -I/usr/local/Cellar/geos/3.5.0/include -I/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c lib/cartopy/trace.cpp -o build/temp.macosx-10.11-x86_64-2.7/lib/cartopy/trace.o
lib/cartopy/trace.cpp:249:10: fatal error: 'proj_api.h' file not found
#include "proj_api.h"
         ^
1 error generated.
error: command 'clang' failed with exit status 1

----------------------------------------

Read more…

Workflow for keeping Nikola config file updated

For most, keeping Nikola up-to-date is usually a simple matter of running something like:

$ pip install --upgrade nikola

The same goes for its dependencies. However, one important thing that can get overlooked is the Nikola configuration file for your site (the conf.py file sitting at the root of your Nikola site directory), which is almost always updated with each major Nikola release.

Read more…

Louis does dotfiles... again

Switching to Zsh

First I created a fork of Oh My Zsh and added it as a submodule to my existing dotfiles repository under a directory named .oh-my-zsh:

$ cd $HOME/Dropbox/dotfiles
$ git submodule add https://github.com/ltiao/oh-my-zsh.git .oh-my-zsh

Since my dotfiles repo is a fork of Mathias Bynen's dotfiles, which is primarily for bash, there shouldn't be any conflicts. In fact, we should be able to switch seamlessly between using zsh and bash as the default shell.

$ cp .oh-my-zsh/templates/zshrc.zsh-template .zshrc
$ vim .zshrc
export ZSH=$HOME/Dropbox/dotfiles/.oh-my-zsh

Add or modify files in .oh-my-zsh/custom

Commit and push

$ stow --simulate --verbose=1 --ignore='(gitmodules|oh-my-zsh)' --dir=/Users/ltiao/Dropbox dotfiles
$ stow --verbose=1 --ignore='(gitmodules|oh-my-zsh)' --dir=/Users/ltiao/Dropbox dotfiles
$ chsh -s /bin/zsh

Switching to iTerm2

  1. Download and install iTerm2

  2. Key shortcuts

  3. Solarized Dark:

    $ curl -o "Solarized Dark.itermcolors" https://raw.githubusercontent.com/altercation/solarized/master/iterm2-colors-solarized/Solarized%20Dark.itermcolors
    

    Tomorrow Night Eighties:

    $ curl -o "Tomorrow Night Eighties.itermcolors" https://raw.githubusercontent.com/chriskempson/tomorrow-theme/master/iTerm2/Tomorrow%20Night%20Eighties.itermcolors
    
  1. Background Opacity
  2. Background dimming, animation

Themes

Bullet Train for oh-my-zsh

$ cd .oh-my-zsh/custom/themes/
$ curl -O https://raw.githubusercontent.com/caiogondim/bullet-train-oh-my-zsh-theme/master/bullet-train.zsh-theme
ZSH_THEME="bullet-train"

Powerline-compatible Fonts

  1. Download Powerline fonts

  2. Install a font. "Meslo LG S Regular for Powerline" is probably just as good as any.

    ../../images/install_font.thumbnail.png
  3. Configure iTerm2 to use font

    ../../images/iterm2_font.thumbnail.png

Creating custom (Unix) fortunes

Like others, I include fortune in my .bash_profile so that I am greeted with a random, witty and thought-provoking epigram upon starting a new shell session. Eventually, however, I found most of the epigrams to be mostly trite and vapid, and quickly grew tired of them. Instead of tinkering with the probabilities of sampling from the various datafiles available, I decided to roll with my own epigram datafile.

Read more…

Louis does dotfiles

../../images/bash.thumbnail.png

For a long time, I kept a small set of minimalistic dotfiles (.bash_profile, .gitconfig, .vimrc). You probably couldn't find anything in my .bash_profile other than exports and path definitions that were absolutely necessary to get tools such as Homebrew and virtualenvwrapper to work. Furthermore, I was dismissive towards spending any significant amount of time down the rabbit hole of perpetual dotfile customization and optimization.

Recently, while digging through my bookmarks saved from months ago, I came across GitHub does dotfiles. Needless to say, after perusing through some of the most popular dotfiles, dotfile utilities and tutorials, I came away quite impressed. It disabused me of my previous misbelief that dotfile calibration is ultimately a fruitless and ceaseless task, and I decided to give it another shot.

Read more…

Python SimpleHTTPServer Recipe: Serve specific directory

  • We need to be able to pass the path to the root of the directory tree we wish to serve.
  • We can only pass arguments to the ServerClass and not HandlerClass. Note however that HandlerClass is passed as an argument to ServerClass so we should be able to propagate the argument to HandlerClass.
  • The translate_path method of SimpleHTTPRequestHandler takes the /-separated path specified in the URL and prepends os.getcwd() to it. We just have to instead prepend the argument we propagated to SimpleHTTPRequestHandler.
  • Lastly we modify the test function to take multiple optional arguments (port and base path) using the excellent module argparse.

Read more…

Python SimpleHTTPServer Recipe: Enable CORS

Create a file, let's call it cors_http_server.py, with the code below:

#! /usr/bin/env python

from SimpleHTTPServer import SimpleHTTPRequestHandler, test


class CORSHTTPRequestHandler(SimpleHTTPRequestHandler):

    def end_headers(self):
        self.send_header('Access-Control-Allow-Origin', '*')
        super(CORSHTTPRequestHandler, self).end_headers(self)

if __name__ == '__main__':
    test(HandlerClass=CORSHTTPRequestHandler)

Read more…

Installing lxml on Mac OSX (10.11) inside a virtualenv with pip

The lxml library is a dependency for many popular Python projects such as Scrapy and Nikola. If you are a Mac OSX user, it's highly likely that you have encountered the following error when trying to install lxml or its dependants with pip:

In file included from src/lxml/lxml.etree.c:346:
$WORKON_HOME/venv_name/build/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
#include "libxml/xmlversion.h"
         ^
1 error generated.
error: command 'clang' failed with exit status 1

Read more…