Jupyter Notebook is a Python environment to help you explore and analyze data using Python. It is an expressive platform that helps you to communicate ideas with code and graphs. Jupyter combines live code and textual explanations along with data visualizations to make documents come alive. In this tutorial, we’ll take a look at getting started with Jupyter Notebook. We’ll install Jupyter notebook in a virtualenv which we learned about recently.
Install Jupyter Notebook In A VirtualEnv
Virtual environments in Python are fantastic, and it makes sense to use one when working with Jupyter Notebooks. If unfamiliar, first go ahead and set up a python virtual environment, then get it activated. In your terminal, navigate to the root directory of the virtual environment you’ll use. Ours just happens to be named vrequests from some prior Python Requests tutorials. At the prompt, type:
pip install jupyter notebook
You will see all kinds of software being installed.
Launch Jupyter Notebook
Once the installation is complete, you can launch Juypter Notebook with this command:
Now a browser window should open and display the Jupyter environment.
From here we can create a new Python3 Jupyter notebook to work with by selecting new->Python3.
This should open a new Browser tab which presents a Jupyter Notebook where we can enter Python code.
Naming The Notebook
To name the notebook, click on the text that shows Untitled1, and you will be provided a modal to enter a new name and click Rename. We can name this notebook Hello_Jupyter.
Run Python Code in Jupyter Notebook
With a fresh Jupyter Notebook running and named, we can now enter some Python code into a cell. When ready, click on the Run button in Jupyter, and you’ll see the result of the Python code right below where you entered it. A quicker way to run the python code is to simply hold down the shift key, and then hit Enter on the keyboard. Both approaches result in the same thing.
Jupyter has a feature called checkpoints. This can be accessed by clicking on File->Save and Checkpoint.
A checkpoint in Jupyter Notebook is a backup copy or version of the file that allows you to try out your changes to the notebook and then revert back to the last checkpoint that you created if you like. This gives your Jupyter Notebook a small amount of version control functionality. You can store one previous checkpoint per file. When you do revert to a prior checkpoint, Jupyter will alert you that a revert can not be undone.
This is the Jupyter Interface. When we created a new Jupyter Notebook it was opened in a new browser window. The original browser window is still running, and in this original tab is the Jupyter Interface. Since we launched Jupyter from a virtual environment, we see the directories that are present in any python virtual environment. Those are the etc, Include, Lib, Scripts, share, and tcl. We also see the Hello_Jupyter.ipynb file. This is the Notebook that we created and renamed earlier. There are three different tabs that we can see. Those are the Files tab, Running Tab, and Clusters tab. The Files tab shows all the files in the current directory. The Running tab shows any currently running Notebooks. The Clusters tab, provided by iPython parallel, is for parallel computing environments. We won’t be looking at that now. Here we can see that the Hello_Jupyter.ipynb notebook is running. We can shut it down if we like by clicking on the Shutdown button.
In the Files tab, if you select a particular file you will be provided with some options. Here we select the Hello_Jupyter.ipynb notebook file by clicking in the select box. Once we do that, we get new options of Duplicate, Shutdown, View, Edit, or Trash.
Duplicate does exactly what you think it would. Shutdown will shut-down the currently selected notebooks. View is simply another way to open the selected notebook. Edit sounds like it’s going to open up the notebook so you can work on it, but it actually opens up the raw json file. You probably won’t need to work on your notebook this way but know that you can view and edit the raw json file, if you ever need or want to. The trash icon on the right will delete the files selected.
Clicking on Edit produces this result if you’re curious.
"Hi from Jupyter!\n"
"print('Hi from Jupyter!')"
"display_name": "Python 3",
Using Various Libraries In Jupyter
We tested out a few simple lines of Python code just to show how to run code in a cell. It is easy to include any Python library you want to use in your Jupyter Notebook. Pandas, NumPy, and Matplotlib are common ones that get used. We can also make use of other libraries. For example, we can borrow some code from the Beautiful Soup tutorial, and test it out in our Jupyter notebook. We can see that in order for the code to run properly, both the Requests, and Beautiful Soup librares are imported. Sure enough, Jupyter is able to properly syntax highlight the code, and produce a nice result all in the Browser. No third party Integrated Development Environment required for this to work!
How To Install Jupyter Notebook In Virtualenv Summary
There are a few ways to install Jupyter Notebook so that you can work with this popular Python tool. One popular way is by using Anaconda Distribution. We opted for a different route, that being simply installing Jupyter Notebook in a Python virtual environment. This approach is a bit more slimmed down, and lets you get started with Jupyter without having to install all of the associated software that comes packaged with Anaconda.