Jupyter Notebooks : a simple setup guide for you!

K-ashif
5 min readJun 8, 2020

--

This piece will describe the steps on How you can setup Jupyter Notebooks in your environment and get started with it.

If you have used Jupyter Notebooks previously, you’ll probably know how powerful and useful this tool can be. Those of you, who don't know about Jupyter Notebooks or haven't used it before, this is the time to kickstart your journey with Jupyter Notebooks. Please consider giving it a shot!

What is Jupyter Notebook?

Official Jupyter Notebook Introduction summarizes it pretty well.

The notebook extends the console-based approach to interactive computing in a qualitatively new direction, providing a web-based application suitable for capturing the whole computation process: developing, documenting, and executing code, as well as communicating the results. The Jupyter notebook combines two components:

A web application: a browser-based tool for interactive authoring of documents which combine explanatory text, mathematics, computations and their rich media output.

Notebook documents: a representation of all content visible in the web application, including inputs and outputs of the computations, explanatory text, mathematics, images, and rich media representations of objects.

In future, I will cover some interesting every day use-cases that you can solve using Jupyter Notebooks and how to take advantage of this tool in various ways. We will also try to understand how we can leverage jupyter notebooks capabilities to replace routine time consuming tasks.

Prerequisites

As per Jupyter official installation guide, there is only a single prerequisite to install Jupyter notebook installed in your environment i.e. “Python (Python 3.3 or greater, or Python 2.7)”

Since most of the OS comes with pre-installed python now, I will not cover the python installation process in this article. However if you need to install python manually, you can refer Python official documentation.

For this article, configuration is based on CentOS and Python3.7 but steps shouldn’t differ much if you are on Python2.7 version and using different OS.

Installation

Step 1: Setup python virtualenv

We will use python virtualenv to install Jupyter Notebook and I suggest you to follow the same approach. virtualenv creates an isolated python virtual environment and eliminates the unwanted issues that can arise due to multiple python (and python-packages) versions installed on your machine.

for example: Let’s say an existing script on your machine depends on MarkupSafe package version 1.1.0 however after you install jupyter package MarkupSafe package gets updated to 1.1.1, this can cause unwanted issue which we want to avoid. So, A general thumb-rule is — ‘Use virtualenv for your Python ventures’.

Let’s create virtualenv, activate it and install jupyter.

$ virtualenv -p /usr/local/bin/python3.7 /home/kashif/jupyter-nb $ source jupyter-nb/bin/activate   # activate virtualenv(jupyter-nb)$ python --version     # check Python version
Python 3.7.7

Lets try to understand what we did in previous step:

  1. We created python virtualenv with default python3.7 in /home/kashif/jupyter-nb target dir. We used -p to tell virtualenv which python version to use (remember we discussed above about the advantages of virtualenv). In my case, I had couple of different python version installed and I wanted to pick python3.7 .
  2. We activated the virtualenv (observe jupyter-nb prefix).
  3. We checked the default python version if it is correct.

Step 2: Install Jupyter package

In previous step we created and activated the jupyter-nb virtualenv, we will now install ‘jupyter’ package in this environment.

(jupyter-nb)$ pip install jupyter

Default pip will point to python3.7 in jupyter-nb virtualenv otherwise you may have to use pip3 to install python3 packages. See below

(jupyter-nb) (base)$ pip --version
pip 20.1.1 from /home/kashif/jupyter-nb/lib/python3.7/site-packages/pip (python 3.7)

Step 3: Running Jupyter Notebooks

In this section we will cover how to run Jupyter notebook.

  1. Lets create Jupyter Notebook workspace directory, this directory would be used by Jupyter web browser as a default.

$mkdir /home/kashif/nb-workdir

2. Now we will setup a password for our jupyter notebook first (we will need this password while logging into notebook browser console).

(jupyter-nb)$ jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /home/kashif/.jupyter/jupyter_notebook_config.json# You can see from above message, encrypted(sha) password is stored in 'jupyter_notebook_config.json', let's check(jupyter-nb) (base)$ cat
/home/kashif/.jupyter/jupyter_notebook_config.json
{
"NotebookApp": {
"password": "sha1:xxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxx"
}
}

3. Now, let’s start jupyter notebook . We will use machine’s ip-address 192.168.10.1 and an available port 7600 to start Notebook. We will also use the workspace directory we created in Step 1 i.e /home/kashif/nb-workdir

(jupyter-nb)$ jupyter notebook --ip=192.168.10.1 --port=7600 --notebook-dir=/home/kashif/nb-workdir[I 07:58:54.148 NotebookApp] Serving notebooks from local directory: /home/kashif/nb-workdir[I 07:58:54.148 NotebookApp] The Jupyter Notebook is running at:
[I 07:58:54.148 NotebookApp] http://192.168.10.1:7600/
[I 07:58:54.148 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 07:58:54.152 NotebookApp] No web browser found: could not locate runnable browser.

You can just use following command $jupyter notebook to start notebook from current directory and by default it will start notebook running on http://localhost:8888 without login prompt.

Alternatively, you can also use-config option which will read from config instead of command-line arguments.

Explore more options with $jupyter notebook help

Jupyter Notebook should now be running on your machine which can be accessed at http://192.168.10.1:7600/ in your web browser (provide password from Step 2).

After you login, you will see Jupyter web console. At this step, you should try exploring different options and tabs available, navigate through it.

Exploring Jupyter

You can do lot of stuff here, add different files and directories, view them in browser, share with your team with weblink, checkout the cool web terminal interface.

In below screen you see two .ipynb files, you can get these files from following GitHub link https://github.com/kahmad-dev/jupyter-notebooks and then upload (check next image) .

# Upload files to Jupyter

  • Opening sample .ipynb file

Introduction_to_Notebooks.ipynb How_to_run_code_in_notebooks.ipynb

Checkout above sample .ipynb files (at GitHub ). There is a short tutorial embed in those notebook files, check them out.

  • Jupyter Web Terminal

This is one of the coolest feature Jupyter Notebook offers, try it.

To launch a terminal web terminal in browser, click on New --> Terminal

--

--

K-ashif

A software engineer who likes to share thoughts and experiences.