Setup Jupyter Python Notebooks

Jupyter Notebooks are a web application that allows creating and sharing documents that contain live (python) code, equations, visualizations and explanatory text. It is an ideal vehicle to distribute worksheets with equation derivations and graphs and can even contain interactive elements.

Installing Jupyter

MOOSE comes bundled with a miniconda install. To install additional python modules such as the python based Jupyter system it is suggested to create a custom conda environment


conda create -n jupyter --clone=/opt/moose/miniconda

The new environment needs to be activated and the necessary python packages installed


source activate jupyter
conda install jupyter sympy

Running the Notebook server

In the directory in which the notebook files are to be stored execute


jupyter-notebook

A browser window should open with a list of notebooks (*.ipynb files) and a few UI elements.

Starting a new notebook

From the _New_ dropdown in the top right select _Python 2_ under _Notebooks_. A new browser tab with an empty notebook should open.

Start by importing sympy in In [1]: and enabling LaTeX rendering for equations.


from sympy import *
from sympy import init_printing
init_printing(use_latex=True)

Next define a few variables and a function (for which we only declare an argument but not an implementation yet)


c, eta = symbols("c eta")
h = Function("h")(eta)

Now lets define an expression f using these components


f = h*c**2 + (1-h)*(1-c)**2
f

The lone f on the second line causes the expression to be printed in Out [3]:. We can now build a few symbolic derivatives of the expression f with respect to c and eta


(diff(f, c), diff(f,eta))

This should yield

Enabling and disabling the environment

To switch back and forth between the pristine MOOSE miniconda environment the jupyter environment use the following commands


source activate jupyter
```
```bash
source deactivate jupyter