Introduction to Jupyter Notebook
All of our work will be developed inside Jupyter Notebook. Jupyter has become the de facto standard for writing interactive data analysis scripts. Unfortunately, the default format for Jupyter notebooks is based on JSON. JSON is JavaScript Object Notation (https://www.json.org/json-en.html). This format is difficult to read, difficult to compare, and needs exporting to be fed into a normal Python interpreter. To obviate that problem, we will extend Jupyter with jupytext (https://jupytext.readthedocs.io/), which allows us to save Jupyter notebooks as normal Python programs. We will start with an overview of Jupyter Notebook, and then look into jupytext. Recall that we installed Jupyter Notebook in the first recipe of this chapter, when we installed the jupyterlab package using conda.
How to do it…
- To run Jupyter, on the Terminal, type the following:
jupyter notebook
This will open the Jupyter browser, and you will see a home page that looks something like this:
Figure 1.1 – The Jupyter browser home page
This home page gives you an overview of your files, so you can open, rename, and download them, and so on.
- Let’s click on one of the files and open it. We will see something like this:
Figure 1.2 – An example of a notebook
Here, we see a menu that allows us to save or download files and perform other actions. Each cell can be executed by clicking the play button. You can also run multiple cells. When you run a cell, you will see its output below.
In some cases, you may need to restart your kernel – use the Kernel | Restart Kernel... method.
Jupyter notebook resources
This would be a good time to pause and take some time to learn more about Jupyter notebooks. There are numerous keyboard shortcuts that are worth learning to speed up your development:
Tutorial: https://www.datacamp.com/tutorial/tutorial-jupyter-notebook
Keyboard Shortcuts: https://towardsdatascience.com/jypyter-notebook-shortcuts-bf0101a98330
Now that we have set up our Jupyter Notebook environment, let’s take a look at a handy tool called Jupytext.
Jupytext
Sometimes, you will want to convert your notebooks into formats other than ipynb – for example, you might want to get them into .py format. For this, we can use jupytext - https://github.com/mwouts/jupytext. This handy plugin will allow us to save Jupyter notebooks in formats other than .ipynb. Remember to get out of the Jupyter browser first. To do this on a Mac, you would close the Jupyter browser window, then go to the Terminal where you started it. Then, click Ctrl + Z to kill the process.
To install jupytext, we will run the following:
pip install jupytext
Now let’s start up the Jupyter browser again:
jupyter notebook
Now we’ve launched the Jupyter browser again, open the Welcome notebook. Go to the File | Jupytext menu. Here is what it looks like:
Figure 1.3 – The Jupytext menu within the Jupyter browser
To save your notebook in another format, you pair it and choose a format. For instance, if you choose to pair it with the light format, you will get a regular Python (.py) formatted file in your current working directory.
Here is what our Welcome.py file looks like in our working directory when paired with the light format:
Figure 1.4 – The Welcome.py notebook in the Light format produced by Jupytext
There are several other popular formats supported by Jupytext. You can read more about them here: https://jupytext.readthedocs.io/en/latest/index.html.
Warning
Remember that the recipes in this book are normally meant to be run inside Jupyter notebooks. This means, typically, we will not always use print to output content. In a notebook, if you simply put the name of a variable and run it, it will print out the result for you. If you are not using notebooks (e.g. you are writing Python scripts and executing them from the terminal), you may want to add print statements to your code. Even within a notebook, you may find it useful at times to add your own print statements to inspect variables and debug code.
In addition to the Jupyter browser, there is a more integrated environment called JupyterLab: https://jupyterlab.readthedocs.io/en/latest/. It allows you to run Terminals and other widgets inside the same environment as your notebook. To get to it, you can click View | Open JupyterLab. You can check it out if you are interested, but it is not necessary to get through the book.
A welcome notebook called Welcome.ipynb has been placed in the GitHub repository for this book in the Ch01 folder. You can use it to test out your notebook environment. This notebook also contains many handy links to help you learn Python and explore bioinformatics!
To recap everything, here are your main options for performing the recipes in this book:
|
System |
Components |
Pros |
Cons |
|
MacBook Pro Laptop |
Anaconda; pip; brew; Jupyter |
Best system for compatibility and ease of use |
You may not own one |
|
Mac Cloud Workstation or Mac AWS EC2 Instance |
Anaconda; pip; brew; Jupyter |
Convenient solution; identical to Mac laptop |
May incur some costs |
|
Windows Machine + Docker |
Docker |
Portable solution |
Some increased overhead |
|
Windows + VirtualBox or WSL2 |
Anaconda; pip; brew; Jupyter |
Let’s you interact with a Linux OS |
Some installation or compatibility issues may arise |
|
Linux Machine |
Anaconda; pip; brew; Jupyter |
Let’s you interact with a Linux OS |
Some installation or compatibility issues may arise |
Table 1.2 – System and OS options for use with this book
See also
- Python Introductory Course: https://www.udemy.com/course/python-for-absolute-beginners-u/
- Intermediate Python Course: https://www.udemy.com/course/100-days-of-code/
- For basic instructions on IPython magics, see: https://ipython.readthedocs.io/en/stable/interactive/magics.html
- A list of third-party extensions for IPython, including magic ones can be found at https://github.com/ipython/ipython/wiki/Extensions-Index
- For a list of JupyterLab widgets, see: https://github.com/search?q=topic%3Ajupyterlab-extension&type=Repositories
- For a deep dive into modern Python 3.12, check out Modern Python Cookbook by Steven F. Lott (Packt Publishing): https://www.amazon.com/Modern-Python-Cookbook-updated-techniques/dp/1835466389
Get This Book's PDF Version and Exclusive Extras
Scan the QR code (or go to packtpub.com/unlock). Search for this book by name, confirm the edition, and then follow the steps on the page.


Note: Keep your invoice handy. Purchases made directly from Packt don’t require an invoice.