1.5. Learning how to work in Jupyter Lab

In the next week, we are going to get a “functional” start on Python, i.e. get just enough practice on essential actions so that we can cover later lessons. Only practice and time can make you comfortable and fast with Python.

You’ll get much more practice just going through classes as this semester proceeds, so when you are frustrated, or stuck, just remember to keep going!

Python is

  • general-purpose programming language conceived in 1989 by Dutch programmer Guido van Rossum.

  • free and open source

  • has simple syntax

  • object oriented: EVERYTHING IS AN OBJECT

  • popular among the largest tech firms in the world: Google, Amazon, Uber, Dropbox, Reddit, Youtube, …

  • popular in the scientific community: academia, NASA, CERN, Wall Street, …

One notable drawback: when your code creates outputs (figures, data, or tables) you must output it to new files. Enter…

Jupyter Notebooks (.ipynb files)

  • is one type of file that can have Python code within it

  • allows users to mix code and formatted text and mathematical expressions (and thus produces elegant and easy-to-digest reports)

  • results in files that are easy to share and rerun (and thus easy to collaborate with)

  • is great for new coders

Tip

In fact, almost every page on this website is a Jupyter Notebook! Notice how there are headers, full text formatting, media inserts, and also code snippets and output. This means readers see output immediately after the relevant code, and makes understanding code much easier.

Jupyter Lab

  • is how we will create and modify our Jupyter notebooks

  • is browser based

  • powerfully flexible and extendable (it has “powerups” we can activate)

1.5.1. Jupyter Lab Basics

1.5.1.1. Opening Jupyter Lab

  • Windows: Open Anaconda Prompt and type jupyter lab and hit enter

  • Mac: Open a terminal window and type jupyter lab and hit enter

1.5.1.2. The JLab interface

The left sidebar has several menus (you can expand and collapse each by clicking on them)

  • The file browser: Navigating to my project folder

  • Kernels: A list of running code

  • Commands: Has a lot of actions, and short cuts are listed

  • Extensions: VERY useful to make JLab more powerful

In the main work area

  • you arrange documents (notebooks, text files, etc.) and other activities (terminals, code consoles, etc.) into panels (up to 4 panels)

  • each panel can have many tabs open

  • JLab can display most image files and text/code files

1.5.1.3. Creating a folder

  1. Find your class notes folder by navigating inside of the “file browser” (click on the folder icon on the left part of the screen)

    • After you click into a folder, you can go back up by clicking on the parent folder’s name in the file path

  2. Create a folder: right click in file explorer pane and select “New Folder”

    • For our purposes, do this and name the new folder “notes”

1.5.1.4. Creating a notebook file

In the folder you want the new file to be in,

  • Hit the \(+\) button in the upper left, then under “Notebook”, click on “Python 3”

  • Rename it to “Using JupyterLab-Basics” (right click the file in the file browser, or the tab the file opened in, and pick “rename”)

1.5.1.5. Editing notebook files

Jupyter notebook uses a modal editing system.

This means that the effect of typing at the keyboard depends on which mode you are in. The two modes are

  1. When you are typing inside a cell, you are in Edit mode

    • I.e. you’re typing markdown or python code

    • You can tell you are in edit mode because there is a dark border around the cell and its contents are sharp

    • Also, the bottom of the panel says “Mode: Edit”

    • Switch into edit mode by hitting Enter or double clicking in the cell.

  2. Command mode means that keystrokes are interpreted by Jupyter as “commands” on the notebook itself

    • Example of a command: inserting a new cell, or executing the code in a cell

    • You can tell you are in command mode because the cell and its contents are fuzzy

    • Also, the bottom of the panel says “Mode: Command”

    • Switch into command mode by hitting ESC

The modal behavior of the Jupyter notebook is a little tricky at first but very efficient when you get used to it.

I have two suggestions to learn more. First, old Jupyter (not “Jupyter Lab”, just “Jupyter”) has a great walk through of working with notebook files. To go through it,

  1. Under the help tab, click “Launch Classic Notebook”

  2. A new tab opened up.

  3. In the upper right area, click on “New”, and select “Python 3”.

  4. A new tab opened up with an untitled notebook file.

  5. Click “help” in the upper bar, and then “User Interface tour”.

Secondly, watch this video from 5:25 on. It’s very good!

Tip

You can speed up youtube videos to save time. Click the “gear” icon in the lower right of the video, then open submenu “Playback speed”.

1.5.2. Shortcuts

1.5.2.1. Command Mode shortcuts

  • shift + enter run cell, move to/select cell below

  • ctrl + enter run cell

  • A insert cell above

  • B insert cell below

  • C copy cell

  • V paste cell

  • D , D (hit D twice) delete selected cell

  • shift + M merge selected cells, or current cell with cell below if only one cell selected

  • I , I interrupt kernel

  • 0 , 0 restart kernel (with dialog)

  • Y change cell to code mode

  • M change cell to markdown mode (good for documentation)

1.5.2.2. Edit mode shortcuts

  • cmd + / toggle comment lines

  • tab code completion or indent ⭐ ⭐

  • shift + tab tooltip (syntax help) ⭐ ⭐

  • ctrl + shift + - split cell

  • shift + enter run cell, move to/select cell below

  • ctrl + enter run cell

1.5.2.3. Magic commands

Magic commands are commands in Jupyter Lab that add convenience on top of python functionality. A nice introduction to magics is here.

The most useful magic commands are

  • %pwd - what is the working directory at this stage of the code?

  • %ls - what is in the working directory

  • %timeit and %%timeit - times the code. % times a single line of code, and %% times the entire cell

  • %conda - runs the “conda” package manager within IPython (Conda installs, runs and updates packages)

  • %debug - starts the debugging interface

  • %load - loads a python file

  • %%file will save the cell (and just that cell) to a python file

  • You can ask IPython for help on magics! E.g. %timeit?

  • IPython can list all magics %lsmagic and describe them all %magic (although just going to the official documentation is probably better)

(I don’t use magic commanads much, but those that do find them very helpful.)

1.5.3. Resources, help, and documentation

I’m proficient enough with python to teach this class. But I have committed to memory VERY few the details about how to use most functions in python. When I want to do something, I often have to check the “proper syntax” for a function.

The help and documentation section of the “Python Data Science Handbook” by Jake VanderPlas begins by saying:

If you read no other section in this chapter, read this one: I find the tools discussed here to be the most transformative contributions of IPython to my daily workflow.

When a technologically-minded person is asked to help a friend, family member, or colleague with a computer problem, most of the time it’s less a matter of knowing the answer as much as knowing how to quickly find an unknown answer. In data science it’s the same: searchable web resources such as online documentation, mailing-list threads, and StackOverflow answers contain a wealth of information, even (especially?) if it is a topic you’ve found yourself searching before. ~~Being an effective practitioner of data science~~ Being efficient in many aspects of life these days is less about memorizing the tool or command you should use for every possible situation, and more about learning to effectively find the information you don’t know, whether through a web search engine or another means.

One of the most useful functions of IPython/Jupyter is to shorten the gap between the user and the type of documentation and search that will help them do their work effectively. While web searches still play a role in answering complicated questions, an amazing amount of information can be found through IPython alone.

The official IPython documentation has the same gist. So let’s call the “Five most helpful commands in IPython:

command

description

Example

shift + tab

if you’re inside a function, syntax guides ⭐

type len(+shift + tab

?

Python documentation

len? then hit enter

tab

What can you type next? Autocompletion suggestions (REALLY HELPFUL!) ⭐

import pan + tab

?

Python documentation

len?

%quickref

Quick reference.

.

help()

Python’s own help system.

help(len)

object?

Details about ‘object’, use ‘object??’ for extra details

my_list?

Python Data Science Handbook elaborates nicely on ? and tab completion.

If we have time, click that last link and check out tab completion. If not, everyone should do this after class. I find tab-completion absolutely essential, and think you’ll be happy to know it!

1.5.4. My Jupyter Lab Set up

Dark Mode (Settings -> JupyterLab Theme -> JupyterLab Dark)

Currently installed extensions (which you can see by clicking the “puzzle” icon on the left sidebar menu):

  • @ijmbarr/jupyterlab_spellchecker (can directly install from the “extensions menu”)

    • Mac users: Your TA had to install node.js in order to install the spell checker extension. MacOS users who also installed homebrew can install this in terminal via brew install node .

  • nbdime-jupyterlab (can directly install from the “extensions menu”)

Extensions I like, but are not currently installed for one reason or another

  • variableInspector: helps you see inside of data objects which is amazing, but has special install instructions

  • jupyterlab-system-monitor: helps you see how much of your CPU and meory are in use

  • toc (table of contents, helps explore files quicker, but is default installed in latest JLab

1.5.5. Credits

This lecture borrowed from the first chapter of the Python Data Science Handbook and QuantEcon.