Jupyter Lab Notes#

Congratulations on reaching this point where you can now view, edit and run Jupyter Notebooks within Jupyter Lab.

The course uses Jupyter notebooks for lectures, code demonstrations, and exercises. You should run these notebooks yourself. Do not be afraid to make changes, try different code snippets than what is provided, and experiment.

The Jupyter environment follows a very familiar user interface pattern - the main difference is the ability to execute code within the different cells. Jupyter notebooks are composed of a sequence of cells. Each of those cells can either be programming code or markdown. You will need to execute all of the code cells - primarily in order, but you can modify and repeat running cells. With the markdown cells, we will provide content (e.g., this cell), instructions, or exercises to perform.

A particular cell can be in one of two modes:

  1. Command

  2. Edit

In either of these modes:

  • Shift+Enter Run the current cell and select the cell below

  • Alt+Enter Run the current cell and insert a cell below

  • Ctrl+s Saves the current notebook (Command(⌘)+s on a Mac)

In the command mode:

  • To see all shortcuts, type H

  • Switch to the edit mode by clicking the mouse on the cell or typing Enter

  • Use the up and down arrow keys to move among the cells

  • Press a to insert a cell above the current one

  • Press b to insert a cell below the current one

  • To change the cell’s type to markdown, press m

  • To change the cell’s type to code, press y

In the edit mode:

  • Press Esc to go into the command mode

  • Press Tab for code completion or indent

Displaying Line Numbers:
Jupyter can display line numbers by default.

For JupyterLab, from the “Settings” menu, select “Advanced Settings Editor”. Next, select “Notebook” from along the left-hand side. Then click “Show Line Numbers”.

For "classic" Jupyter, select the "View" menu and then "Toggle Line Numbers".

If you execute notebooks within VS Code, press Shift+l while in command mode. You can also press the Settings icon (the gear) in the upper right-hand corner and then "Show Notebook Line Numbers".

JupyterLab User Guide

Install Dependencies#

To ensure your Python environment has the required dependencies installed for these notebooks, execute the following cell.

Note: These commands are only necessary if you use your own environment. If you followed installation and start instructions for this guide, the dependencies have already been installed.

1import sys
2!{sys.executable} -m pip install --upgrade pip setuptools wheel
3!{sys.executable} -m pip install -r requirements.txt
Hide code cell output
Requirement already satisfied: pip in /Users/jbslanka/Documents/GitHub/jupyternotebooks/venv/lib/python3.12/site-packages (24.0)
Requirement already satisfied: setuptools in /Users/jbslanka/Documents/GitHub/jupyternotebooks/venv/lib/python3.12/site-packages (69.5.1)
Requirement already satisfied: wheel in /Users/jbslanka/Documents/GitHub/jupyternotebooks/venv/lib/python3.12/site-packages (0.43.0)
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

The last cell’s output depends upon whether you already have various modules installed.

The first line allows the script(program) utilize the sys module, which provides access to various parameters and settings used by the python interpreter.

The second line ensures that the Python environment has the latest versions of the Python package manager(pip) and associated tools.

The third line uses the current Python interpreter for the specific environment in which this Jupyter notebook executes. The line then uses the pip module to install the latest version of the packages listed in the requirements.txt file. (Programmers can also run pip as a command-line program.) pip will install any dependencies required by those packages - so the output contains references to pandas, beautifulsoup4, soupsieve, and other libraries.

For those familiar with Python, you may ask why not just execute !pip install package_name. The exclamation mark ! tells the notebook to run the rest of the command as a shell command (i.e., run this command-line). Depending upon your environment, if the Python interpreter runs within a different virtual environment than the Jupyter server, pip will install the dependency in the wrong location. For more information, Jake VanderPlas describes this on his blog in much greater detail. In addition, Jake VanderPlas has written an excellent book - Python Data Science Handbook, 2nd Ed O’Reilly Amazon, which you may want to examine.

Within a Jupyter lab cell, you can use the pip magic command:

%pip install -r ../requirements.txt

Updating the Guide#

Periodically, we will make updates to the guide. To get the latest version, follow these steps:

  1. Open your terminal (wsl) window

  2. Execute cd ~/fintech/guide

  3. Execute git reset --hard. This removes any local changes that you have made to the guide. You can attempt get the latest version without running this command. However if you run into any merge conflicts, you will either need to handle those conflicts or run that command.

  4. Execute git pull to bring down the latest changes from the git server to your computer.

Notebook Conventions#

For each notebook, run the code cells from top to bottom.

We did design some of the notebook cells to cause errors when executed. Sometimes, these errors just display specific output for your awareness. Other times you should make corrections until the code runs correctly. Some cells are blank for you to enter code.

Hyperlinks exist to outside resources throughout these notebooks. Unless explicitly mentioned, these links provide background information and are not essential.

The terms Python and the Python Interpreter are used interchangeably.

Time to start learning Python …