Merge pull request #236 from parente/mpl-inline

Default matplotlib to inline backend
This commit is contained in:
Min RK
2016-07-12 16:59:09 -05:00
committed by GitHub
2 changed files with 69 additions and 4 deletions

View File

@@ -15,6 +15,8 @@ RUN apt-get update && \
USER $NB_USER
# Install Python 3 packages
# Remove pyqt and qt pulled in for matplotlib since we're only ever going to
# use notebook-friendly backends in these images
RUN conda install --quiet --yes \
'ipywidgets=5.1*' \
'pandas=0.18*' \
@@ -32,12 +34,15 @@ RUN conda install --quiet --yes \
'dill=0.2*' \
'numba=0.23*' \
'bokeh=0.11*' \
'h5py=2.5*' \
&& conda clean -tipsy
'h5py=2.5*' && \
conda remove --quiet --yes --force qt pyqt && \
conda clean -tipsy
# Activate ipywidgets extension in the environment that runs the notebook server
RUN jupyter nbextension enable --py widgetsnbextension --sys-prefix
# Install Python 2 packages
# Remove pyqt and qt pulled in for matplotlib since we're only ever going to
# use notebook-friendly backends in these images
RUN conda create --quiet --yes -p $CONDA_DIR/envs/python2 python=2.7 \
'ipython=4.2*' \
'ipywidgets=5.1*' \
@@ -57,12 +62,17 @@ RUN conda create --quiet --yes -p $CONDA_DIR/envs/python2 python=2.7 \
'numba=0.23*' \
'bokeh=0.11*' \
'h5py=2.5*' \
'pyzmq' \
&& conda clean -tipsy
'pyzmq' && \
conda remove -n python2 --quiet --yes --force qt pyqt && \
conda clean -tipsy
# Add shortcuts to distinguish pip for python2 and python3 envs
RUN ln -s $CONDA_DIR/envs/python2/bin/pip $CONDA_DIR/bin/pip2 && \
ln -s $CONDA_DIR/bin/pip $CONDA_DIR/bin/pip3
# Configure ipython kernel to use matplotlib inline backend by default
RUN mkdir -p $HOME/.ipython/profile_default/startup
COPY mplimporthook.py $HOME/.ipython/profile_default/startup/
USER root
# Install Python 2 kernel spec globally to avoid permission problems when NB_UID

View File

@@ -0,0 +1,55 @@
"""Startup script for IPython kernel.
Installs an import hook to configure the matplotlib backend on the fly.
Originally from @minrk at
https://github.com/minrk/profile_default/blob/master/startup/mplimporthook.py
Repurposed for docker-stacks to address repeat bugs like
https://github.com/jupyter/docker-stacks/issues/235.
"""
import sys
from IPython import get_ipython
class MatplotlibFinder(object):
"""Import hook that notices when matplotlib.pyplot or pylab is imported
and tries to configure the matplotlib backend appropriately for the
environment.
"""
_called = False
def find_module(self, fullname, path=None):
if self._called:
# already handled
return
if fullname not in ('pylab', 'matplotlib.pyplot'):
# not matplotlib
return
# don't call me again
self._called = True
try:
# remove myself from the import hooks
sys.meta_path = [loader for loader in sys.meta_path if loader is not self]
except ValueError:
pass
ip = get_ipython()
if ip is None:
# not in an interactive environment
return
if ip.pylab_gui_select:
# backend already selected
return
if hasattr(ip, 'kernel'):
# default to inline in kernel environments
ip.enable_matplotlib('inline')
else:
print('enabling matplotlib')
ip.enable_matplotlib()
# install the finder immediately
sys.meta_path.insert(0, MatplotlibFinder())