Jupiter Notebook is a very comfortable tool especially for Python developers, but it also appears to be useful among other IT communities. Basically, it represents content visible in the browser. It's so simple and yet incredibly helpful! But these aren't its only benefits. In the article, I'll show you the basic concepts of Jupyter Notebook. I haven't been using it for too long, though, but I'd love to share my impressions with you!
How to get the Jupyter Notebook
To install the Jupyter Notebook on our machine we have to make sure about the following steps. To get the software we need minimum Python 3.3 or higher, or Python 2.7. There is also a new project, Jupyter Lab, but I’ll talk about it other time. The easiest way to get started with the Jupyter Notebook is installing Anaconda. Anaconda is an open source distribution for Python and R programming languages for scientific computing, e.g., machine learning, data science etc. This distribution contains some of the biggest Python libraries like Pandas, numpy, Matplotlib. So first, we need to install anaconda. Go to www.anaconda.com/products/individual and download a version for your operating system. I'd recommend using the graphical installer. After the installation, it’s time to install Jupyter Notebook. Open the terminal and use the following command:
conda install -c conda-forge notebook
Now, you can run the Jupyrer Notebook using command jupyter notebook.
After running the command, your browser will open up and that's what you should see on the screen:
This isn’t the Notebook yet. This is the Dashboard of your Notebook. In the address bar of your browser, you'll see http://localhost:8888/tree. It indicates that the content is being served from your local machine. Let’s create our first notebook file.
As you can see, I have two versions of Python in my Jupyter Notebook. I was experimenting with installation of different kernels. You can read more about this here: https://ipython.readthedocs.io/en/stable/install/kernel_install.html
After creating a new Python notebook, you should see an interface like the one below.
You need to know two main terms — cells and kernels.
- Kernel is a program that runs and introspects the user’s code.
- Cell is a multiline text input field and its contents can be executed
Let’s run our first line of code! Simple print in Python.
The name of cells is “In ”. The “In” stands for input. To add a new cell I used + in the jupyter bar. You can define methods, create variables or even import libraries in the cells. For example, you can import os module in the first cell but use it in the 10th cell. The notebook is your whole environment that depends on a kernel you're using.
To use Jupyter Notebook comfortably, I'd highly recommend learning the shortcuts. You definitely can feel the difference during work. Try running Command + Shift + P to see the shortcuts list or go to Help -> Keyboard Shortcuts.
In the Jupyter Notebook, we can see two modes: Command mode and Edit mode.
- Command mode (ESC) allows to manipulate cells
- Edit mode (Enter) allows to edit the content of the cell
Example with using Matplotlib and Pandas
Now, I'd like to show you some examples of my work with Jupyter Notebook. First, I’ll use Matplotlib to show you how Jupyter handles the figures.
Into the first cell, I imported matplotlib.pyplot and used alias plt. Then, I simply created three points on the chart. The Jupyter Notebook created an Out cell which drew the chart for me. It's very convenient, because I don’t need to worry about the data presentation. Let’s go to the next example where we’ll try to open a dataset from Kaggle in our notepad.
Just as before, I started from importing pandas module and used alias pd. Then, I created a dataframe with data from the avocado dataset. In the last cell, I used my variable to generate a table with the data. Jupyter Notebook presents data for us in a readable form.
The Jupyter Notebook is a great tool for creating concepts and testing the ideas quickly. Notebooks reduce context switching and emulate the real development of thoughts during the process. Jupyter Notebook can easily present your data in the cells. The notebooks can be shared with others using email, Dropbox, Github, and other channels. At the end, I'd like to mention Jupyter Lab. It's an extended notebook with various programming language integration. I hope you enjoyed the article!