2) Jupyter notebooks

Related links:

Why Jupyter notebooks?

Jupyter notebooks (formerly ipython notebooks) provide convenient platforms for getting to know python (or one of the other languages supported by them, including R and Julia–see FYI), especially for making and going through tutorials.

FYI: The name Jupyter is an indirect acronyum of the three core languages it was designed for: JUlia, PYThon, and R and is inspired by the planet Jupiter.

Some great examples of the use of juypter notebooks for sharing knowledge and tutorials include:

All class notes and homework assignments in this course are created as jupyter notebooks. As you have seen, they can be rendered into html, or we can work with them directly as notebooks. You will be creating and editing notebooks throughout this class.

To run jupyter notebooks, you must have python installed. To check if you have it installed, you can type:

$ which python

If you have python installed:

It will tell you were it is installed. Skip the next section.

Otherwise, nothing will be returned.

If you do not have python installed:

I highly recommend using conda to do so.

Download and install miniconda for Python3, 32 or 64 bit depending on your system, from http://conda.pydata.org/miniconda.html

For Windows users, who do not know what to choose for 32 or 64 bit, it is strongly recommended to read the second question of this FAQ first: http://windows.microsoft.com/en-us/windows/32-bit-and-64-bit-windows

Run the installer and select yes to add conda to the PATH variable.

If you have installed from a Linux shell, either open a new shell to have an updated PATH, or update your PATH variable by source ~/.bashrc (or .tcsh, .csh - whichever shell you are using).

Checking your version

Now, whether you just installed python or already had it installed, check the version by entering the command python -V or python --version. Anything 3.4 or above it good for this class.

A note on organization

It is always easiest to start with organization in mind, rather than make many files and then think about how you want them named and organized. Practice organization with files you use for this class! We will talk more about this at a few different points in this class.

Creating a new jupyter notebook

The first step is to see if you have this capability installed already (it likely is). Again, we’ll use the which jupyter trick to quickly check if you have jupyter installed. If not, install it with conda:

$ conda install jupyter

FYI: You can use jupyter notebooks with multiple versions of python, R, and Julia. We will only use python 3 and markdown (simple language for text formatting) in our notebooks for this class. If you are interested in other capability, see https://jupyter.readthedocs.io/en/latest/install-kernel.html

In your terminal window, nativate to a good place for saving jupyter notebooks that you create and edit. Save a copy of HW01.ipynb and lecture01_shell.ipynb to that directory. You can download it from github or from Canvas. We’ll start by exploring these notebooks, as well as creating a new notebook. Launch jupyter notebooks by entering the command:

$ jupyter notebook

You see something like the following in your terminal window:

truffle:2_notebooks hbmayes$ jupyter notebook [I 17:28:55.267 NotebookApp] Serving notebooks from local directory: /Users/hbmayes/bee/code/latex/che_696_private/practice/2_notebooks [I 17:28:55.267 NotebookApp] 0 active kernels [I 17:28:55.267 NotebookApp] The Jupyter Notebook is running at: http://localhost:8890/?token=89e889e0edd31f172b8d74a30c196a3fac04a0c262395f4d [I 17:28:55.267 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

And in your default browser, a new window with a screen like this:

JupyterNotebookLaunch

JupyterNotebookLaunch

Click on the “New” dropdown. You only need to have python 3 as an option:

JupyterNotebookLaunch

JupyterNotebookLaunch

Select that option, and a new window with a new notebook will be launched:

JupyterNotebookLaunch

JupyterNotebookLaunch

Note the dropdown menu with different options for each “cell”. Change the first cell to “Markdown” from the default initial “code”. Type something in the first cell. Rename the file. Save it, then from the file menu, let’s select “Halt and Close”. We’ll explore the two existing notebooks before returning to this practice notebook. Open “HW01.ipynb”.

Edit and command modes

The keyboard icon takes you to a list of keyboard commands that will save you time. There are many “cheatsheets” you can reference (use if you find any useful), including:

As with vim there is a command mode and an edit (insert) mode. If you are in edit mode, esc switches you to command mode, and enter puts you back into edit mode. The pencil (or lack thereof) indicates if you are in edit mode (or not).

Edit mode allows you to use keystrokes to change the type of cell, insert cells, etc., more quickly than switching to the mouse and using the GUI options.

The code cells would be for running python. Since we have not yet begun covering python, my notebooks so far only are either markdown or RawNBConvert. I use the second type of option for showing code snippits. My explanations, rants, and raves are in markdown cells.

FYI: The default cell type for a new cell is code. Want to change that? Follow these instructions: https://stackoverflow.com/questions/40382454/jupyter-notebook-new-cell-type-default

What is Markdown?

For a full definition and guide, see https://daringfireball.net/projects/markdown/ There is also a nice informal definition on Wikipedia:

Markdown is a lightweight markup language with plain text formatting syntax. It is designed so that it can be converted to HTML and many other formats using a tool by the same name. Markdown is often used to format readme files, for writing messages in online discussion forums, and to create rich text using a plain text editor.

This ability to have easy-to-format text that lends well to conversion to html is why I’m making my class notes in jupyter notebooks, and why I think they are great for writing tutorials.

Some potentialy useful cheatsheets on markdown:

In addition to using notebooks for class notes and HW, we will start programming Python in notebooks, before switching over to using an IDE (integrated development environment).

Extensions to jupyter notebooks

FYI There are available unofficial extensions to jupyter notebooks. I, for example, would like to have my spelling checked.

Here is how to install this capability:

$ pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master $ pip install jupyter_nbextensions_configurator $ jupyter contrib nbextension install --user $ jupyter nbextensions_configurator enable --user

Now to enable the spellchecker extension:

$ jupyter nbextension enable spellchecker/main

One last cool note about ipynbs, for those who love LaTex

Beautiful equations can written in Markdown cells using LaTex surrounded by \\\( ... \\\) or surrounded by single dollar signs ($) for in-line code and double for rendering on a new line. For example:

\\( P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)} \\)

Will be rendered as: \( P(A :raw-latex:`\mid `B) = :raw-latex:`frac{P(B mid A) , P(A)}{P(B)}` \)

If surrounded by $’s: \(( P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)}\)

If surrounded by $$’s:

\[( P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)}\]

While I did not add LaTex to our quite-full syllabus, I highly recommend it.