Setting up Python and Jupyter Notebook on Mac

This page provides the instructions for how to install and run IPython and  Jupyter Notebook in a virtualenv  on Mac.

Most probably your Mac has already come with Python installed (see step 1 and step 2 below to check whether Python and Python 3 is installed on your mac, because my Mac book air has both Python and Python3.6 installed, I will go ahead to step 3 to install virtualenv).  

Open a terminal on your mac and type in Python and Python3 to see whether Python and Python3 installed on your mac. See below for example.

Follow the steps below for detailed instructions.

Step 1: Check whether Python is installed on your Mac

To test whether your Mac has Python 2.x or 3.x, look at the instructions below.

Open a terminal  (in your launchpad, type in terminal, and then click and open it.)

Type in python, if you see similar thing as the pic shown below, it means python 2.7 was installed on your Mac

type exit() to exit from the Python 2.7 environment

Type python3 in your terminal, if you see similar warning as the pic shown below, it means you will need to install Python 3 (see the steps below for installation). If you see older version (< 3.5) of python 3 installed on your mac, follow step 2 to install Python 3.5

Step 2: Install Python 3.5

I recommend you download Python 3.5.4 from here, if you do not have Python 3.x pre-installed on your mac.

https://www.python.org/downloads/mac-osx/ 

Click the installer and install it following the wizard.

When you finish the installation, type python3 in your terminal, if you see similar stuff in the pic below, Python 3.5 is installed successfully and you are ready to proceed to step 3.

Step 3: Install virtualenv 

Note: the command text in blue is the exact command you should type into your terminal, the same through this whole instruction.

Lipings-MacBook-Pro:~ Liping$ pip3 install virtualenv

Step 4: Setup virtualenv environment

Issue the command below to set up a virtualenv environment that we will use later, using the vitualenvwe installed in step 3 above.

$ virtualenv --system-site-packages -p python3 ~/ipy-jupyter-venv3

Step 5: Install IPython

Before installing IPython and Jupyter Notebook, be sure to activate your python virtual environment first.

$ source ~/ipy-jupyter-venv3/bin/activate 
(ipy-jupyter-venv3)$  # Your prompt should change

Type the following command to install IPython

(ipy-jupyter-venv3) liping:~pip3 install ipython

Step 6: Install Jupyter Notebook

Use the following command to install Jupyter Notebook

(ipy-jupyter-venv3) liping:~$ pip3 install jupyter

Step 7: Test jupypter notebook installation

(ipy-jupyter-venv3) liping:~$ which python3

/Users/Liping/ipy-jupyter-venv3/bin/python3
#for python 3.x
(ipy-jupyter-venv3) liping:~$ which ipython3

/Users/Liping/ipy-jupyter-venv3/bin/ipython3
#for python 3.x
(ipy-jupyter-venv3) liping:~$ which jupyter-notebook

/Users/Liping/ipy-jupyter-venv3/bin/jupyter-notebook

Step 8: Add Kernel

The Jupyter Notebook and other frontends automatically ensure that the IPython kernel is available. However, if you want to use a kernel with a different version of Python, or in a virtualenv environment, you’ll need to install that manually. 

We are using virutalenv, so we need to install IPython kernel in the virtualenv we created in Step 4 above.

(ipy-jupyter-venv3) liping:~$  python3 -m ipykernel install --user --name myipy_jupter_env3 --display-name "ipy-jupyter-venv3"

Installed kernelspec myipy_jupter_env3 in /Users/Liping/Library/Jupyter/kernels/myipy_jupter_env3

 

Step 9: Before we are running our jupyter Notebook, let us create a folder from which we will start our jupyter notebook.

The following command will change our directory to Desktop (cd refers to change directory)

(ipy-jupyter-venv3) Lipings-MacBook-Pro:~ Liping$ cd Desktop 

the following command will help us create a folder named Geog597_ML_session under our Desktop folder

(ipy-jupyter-venv3) Lipings-MacBook-Pro:Desktop Liping$ mkdir test_jupyter

the following command will change our directory into the folder we just created.

(ipy-jupyter-venv3) Lipings-MacBook-Pro:Desktop Liping$ cd test_jupyter/

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ 

Step 10: Run Jupyter Notebook

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ jupyter-notebook

Then your default browser should automatically open a web page similar to the one shown below.

If the web page does not show up automatically, just type localhost:8888 into your browser, you should see the page shown above.

 

Step 11: Testing and Using Jupyter Notebook

By this point you should have Jupyter Notebook running, and you should be connected to it using a web browser. Jupyter Notebook is very powerful and has many features. Below I will outline a few of the basic features to get you started using the notebook. Automatically, Jupyter Notebook will show all of the files and folders in the directory it is run from (for our case, it is empty now, because we have not put anything in that folder Desktop/test_jupyter we just created in step 9 ).

To create a new notebook file, select New > ipy-jupyter-venv3 from the top right pull-down menu (Note: this is the so called kernel we installed in Step 8 above):

This will open a notebook. We can now run Python code in the cell or change the cell to markdown (markdown is for note, not for code). For example, change the first cell to accept Markdown by clicking Cell > Cell Type > Markdown from the top navigation bar, or by click the Markdown shown in the pic below. We can now write notes using Markdown,  for example, type the following into the cell after changing it to markdown:

# Testing

Hit Ctrl + Enter, you will see it the text changed into heading style.

          Then insert one cell by the menu shown below (insert Cell Below).

type in the following

a = 5
b = 10
print (a+b)

and then hit Ctrl + Enter.  You should see the following.

Step 12: Stop jupyter Notebook.

To stop the Jupyter Notebook process, press CTRL+C,

type Y, and hit ENTER to confirm. The following will be displayed:

Step 13: Exit virtualenv environment

Once you are done, remember to exit your virtualenv using the following command:

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$

deactivate

Note that your prompt changes back (see the pic below)

Step 14: Instructions for using Jupyter Notebook next time

Next time when you need to use you jupyther notebook, following the steps summarized below.

Lipings-MacBook-Pro:~ Liping$ source ~/ipy-jupyter-venv3/bin/activate  

(ipy-jupyter-venv3)$  # Your prompt should change

Change directory to where you would like to start your jupyter-notebook

(ipy-jupyter-venv3) Lipings-MacBook-Pro:~ Liping$ cd Desktop/test_jupyter

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ 

Run jupyter notebook:

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ jupyter-notebook

 

Congratulations, you have now set up ipython and jupyter notebook on your mac!

Tips about Jupyter Notebook

This post provides some tips about Jupyter Notebook.

  • When you open a pdf file in a browser via your running Jupyter Notebook.

You may encounter some error similar to this “Error! (file) is not UTF-8 encoded”,

the solution:

check the address in your browser, for example, for the following case,

http://localhost:8000/edit/confusionmatrix.pdf

if I change the edit in the url address given above to  view

http://localhost:8000/view/confusionmatrix.pdf

You will see your beautiful fig or document in pdf format.

  • Another similar situation:

When you open a html file in your browser via your running Jupyter Notebook, and you would like to edit the html file, instead of seeing its display,

the solution:

change view to edit in the url address of the file, see the following for an example, then you should be able to edit your html file.

http://localhost:8000/view/index.html 

to

http://localhost:8000/edit/index.html

Happy Jupyter Notebooking!

Run Jupyter notebook from terminal with tmux

This post will walk you through how to run a Jupyter notebook script from terminal with tmux (check here for my post about tmux usage).

When you are running Jupyter on a remote server or on cluster/ cloud resources, there are situations where you would like the Jupyter on the remote server or cluster continue running without termination when you shut down your laptop or desktop that you used to access the remote server. tmux will help with this.

In this post, we cover how to let your jupyter notebook running on a remote server continue running without termination via tmux.

Step 1: connect to your remote server with port forwarding

check the Step 5-2 in my post here about setting up Jupyter notebook for how to access your remote server with port forwarding, if you are not familiar with it.

Step 2: install tmux 

 check here for my post about tmux installation and usage

Step 3: install runipy  python package

Check here for runipy installation and usage.

Step 4: in your terminal type the following command, then it will go into tmux window

$ tmux

Step 5: Start jupyter notebook within your tmux session with the following command

$ jupyter notebook --no-browser

The –no-browser option prevents Jupyter from automatically opening a browser window.

Let this terminal stay running.

Step 6: from your laptop, ssh to your remote server (does not need port forwarding this time)

Step 7: cd to where the jupyter notebook script located that you would like to run from terminal

If you do not know what does cd mean and do, check my post for a list of commonly used Linux commands.

Step 8:  use the following command to run your ipynb script (this will save the output of each cell back to the notebook file)

$ runipy -o MyNotebook.ipynb 

To save the notebook output as a new notebook, run:

$ runipy MyNotebook.ipynb OutputNotebook.ipynb

If your ipynb script without any error itself, it should be running on the server now.

Step 9:  Things to pay attention to:

Do not close the terminal where you run the ipynb script within tmux session on your computer that you used to connect to the remote server, that will cause the termination of running the ipynb. But you can make your laptop in sleep or even shut down the computer, the tmux session will keep the ipynb running on your remote server and save the output in the ipynb.

References:

https://www.datasciencebytes.com/bytes/2015/12/18/using-jupyter-notebooks-securely-on-remote-linux-machines/ 

http://forums.fast.ai/t/ipython-notebook-on-a-remote-server-with-tmux/10044/2

Checking from command line if Jupyter server is running and kill if needed

This post provides instructions on how to check whether a Jupyter server is running from command line and kill if needed.

Normally, you can kill a Jupyter server from the same terminal window where you launched your Jupyter notebook by hit CTRL + C, then type yes, to shut down the kernels of Your jupyter notebook.

But, there are situations where you want to know whether a Jupyter-notebook running on your remote server, but the Jupyter notebook was started on another desktop (e.g., your office desktop), (and now you are working at home from your laptop, and want to check whether the notebook is still running).

After you login to your Server where you Jupyter notebook was installed and running, you can use the following command to list runing notebooks.

$ jupyter notebook list

You will see a list of running notebooks in the terminal, if you have several running ones.

You can use the following command to kill specific notebook (identified by the port it runs the jupyter) that you would like to stop.

$ jupyter notebook stop 8888

P.S.:

Each server should start on a new port. jupyter notebook list is reading a set of data files – each notebook server you run writes a file when it starts up, and attempts to remove it when it shuts down. If you see different listed servers on the same port, that means some of them exited without successful removal of the file when it created (for example, unexpected shut down of the notebook would cause this happens).

 

References:

https://github.com/jupyter/notebook/issues/1950

https://github.com/jupyter/notebook/issues/2844

 

 

Run Jupyter Notebook script from terminal

Normally people run jupyter notebook via browser, but in some situation, we will need to run it from terminal, for example, when running the script takes long time.

This post introduces how to run a jupyter notebook script from terminal.

Solution  I:

runipy can do this. runipy will run all cells in a notebook. If an error occurs, the process will stop.

  • Install  runipy package
$ pip3 install runipy # for python 3.x 
$ pip install runipy  # for python 2.x
  • runipy command-line usages

 

  • To run a .ipynb file as a script, run:
$ runipy MyNotebook.ipynb
  • To save the output of each cell back to the notebook file, run:
$ runipy -o MyNotebook.ipynb
  • To save the notebook output as a new notebook, run:
$ runipy MyNotebook.ipynb OutputNotebook.ipynb
  • To run a .ipynb file and generate an HTML report, run:
$ runipy MyNotebook.ipynb --html report.html

 

Solution  II:

The latest versions of jupyter comes with  the nbconvert command tool for notebook conversion allows us  to do this without any extra packages.

Just go to your terminal and type:

$ jupyter nbconvert --to notebook --execute mynotebook.ipynb --output mynotebook.ipynb

This will open the notebook, execute it, capture new output, and save the result in mynotebook.nbconvert.ipynb. By default, nbconvert will abort conversion if any exceptions occur during execution of a cell. If you specify --allow-errors (in addition to the --execute flag) then conversion will continue and the output from any exception will be included in the cell output.

if you meet this error,

raise exception(“Cell execution timed out”)

$ jupyter nbconvert --to notebook --execute --allow-errors --ExecutePreprocessor.timeout=180 mynotebook.ipynb 

You can use the –inplace flag as well:

$ jupyter nbconvert --to notebook --execute --inplace mynotebook.ipynb

 

check here for more (updated) usages about nbconvert jupyter command tool.

 

References:

https://pypi.python.org/pypi/runipy

http://nbconvert.readthedocs.io/en/latest/usage.html#convert-notebook 

Can I run Jupyter notebook cells in commandline?

 

 

 

 

Saving IPython/Jupyter notebook as PDF on Ubuntu

When you would like to save your Jupyter notebook as a PDF file, and you encouter the following problems on Ubuntu OS. This post is for you.

The solution:

XeLatex is a part of texlive-xetex package.

To install on Ubuntu, run the following command: 

$ sudo apt-get install texlive-xetex

Now you can download  your ipynb file as PDF!

Jupyter and JupyterHub

This post provides an introduction to Jupyter and the comparison between  Jupyter and JupyterHub.

Jupyter is a browser-based interactive notebook for programming, mathematics, and data science. It supports a number of languages via plugins (“kernels”), such as Python, Ruby, Haskell, R, Scala and Julia.

JupyterHub: a multi-user server, manages and proxies multiple instances of the single-user Jupyter notebook server.

Pros of Jupyter:

  • convenient for debugging – does not need to rerun the whole script when debugging a small fragment of the code.
  • convenient for reporting

 

Cons of Jupyter:

  • It messes with your version control.

  • Code can only be run in chunks

  • It’s difficult to keep track

  • Code often ends up very fragmented

  • The output is incomplete

  • Potential security risks?

    The notebook opens a http port. Pray to lord it will not land on 0.0.0.0 host. In that case the whole universe has access to your notebook and thus to your system.

 

References:

http://opiateforthemass.es/articles/why-i-dont-like-jupyter-fka-ipython-notebook/