# Jupyter

## Table of Contents

- Additional Jupyter resources or tricks
  - Tips and tricks
  - Running R and Python in the same notebook
  - Exporting a notebook
- Installing Jupyter on your own machine
  - Installing jupyter
  - Using R
  - Exporting your notebooks with latex
  - Interacting with GitLab
  - Interesting extensions to improve notebook readability
- Installing JupyterHub Benoit

## Additional Jupyter resources or tricks

### Tips and tricks

The following webpage lists several Jupyter tricks (in particular, it illustrates many `Ipython magic` commands) that should improve your efficiency (note that this blog post is about two years old so some of the tricks may have been integrated in the default behavior of jupyter now).

### Running R and Python in the same notebook

The best solution to this is to install rpy2. On my machine, I have installed the `python3-rpy2` debian package with `apt-get install`. E.g.,

    sudo apt-get install python3-rpy2 python3-tzlocal

An other (not really recommanded) alternative consists in going through the python package manager with

    pip3 install rpy2

Then you'll be able to use both languages in the same notebook by:

1. Loading `rpy2`:

       %load_ext rpy2.ipython

2. Using the `%R` Ipython magic:

       %R
       summary(cars)

   Python objects can then even be passed to R as follows (assuming `df` is a pandas dataframe):

       %%R -i df
       plot(df)

### Exporting a notebook

Obviously, you can convert to html or pdf using the using the `File > Download as > HTML` (or `PDF`) menu option. This can also be done from the command line with the following command:

    ipython3 nbconvert --to pdf Untitled.ipynb

If you want to use a specific style, then the nbconvert exporter should be customised. This is discussed and demoed here. We encourage you to simply read the doc of nbconvert.

Instead of going directly through LaTeX and playing too much with the nbconvert exporter, an other option consists in exporting to Markdown and playing with pandoc. Both approaches work, it's rather a matter of taste.

## Installing Jupyter on your own machine

### Installing jupyter

Here is what you should install:

    sudo apt-get install jupyter-notebook python3-pip python3-matplotlib python3-numpy

On my machine, I got the version 5.4.1:

    jupyter-notebook --version

    5.4.1

The ipython notebook can then be run with the following command:

    jupyter-notebook

### Using R

If you want to use the R kernel in jupyter, follow these instructions.

Alternatively, you can install `rpy2` (see the Running R and Python in the same notebook section).

    sudo apt-get python3-rpy2

### Exporting your notebooks with latex

Here is what I had to install on my recent debian machine to make sure the notebook export via latex works:

    sudo apt-get install wkhtmltopdf
    sudo apt-get install texlive-xetex

### Interacting with GitLab

Here is our jupyter extension that allows to git push/pull from the notebooks:

    jupyter nbextension install https://raw.githubusercontent.com/brospars/nb-git/master/nb-git.js
    jupyter nbextension enable nb-git

### Interesting extensions to improve notebook readability

Here are two interesting extensions that can improve readability:

- Code folding

      pip3 install jupyter_contrib_nbextensions
      # jupyter contrib nbextension install --user # not done yet

- Hiding code

      sudo pip3 install hide_code
      sudo jupyter-nbextension  install --py hide_code
      jupyter-nbextension  enable --py hide_code
      jupyter-serverextension enable --py hide_code

## Installing JupyterHub Benoit

> Benoit may give a few hints on this.