1.6. 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

  • a 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 (markdown) 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.6.1. Jupyter Lab Basics

1.6.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.6.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

  • 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.6.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 the file explorer pane and select “New Folder”

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

1.6.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.6.1.5. Editing notebook files

Jupyter notebook uses a modal editing system.

This means that the effect of typing 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

Note

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

1.6.1.6. A good and short video walkthrough

Watch this from 1:51 on. Try to repeat everything you see in that video in JupyterLab on your computer.

Tip

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

1.6.2. Resources, help, and documentation

Important

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 Jupyter Lab 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 Jupyter Lab 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 Jupyter Lab alone.

Tip

These are the most helpful commands and tricks you can deploy while you begin work in this class!

Description

How

Example

Syntax guides for functions

shift + tab

type len(+shift + tab

⭐ ⭐ Automatic if you finish the Jupyter Lab set up

type len(

Sometimes fastest: ?

type len? then run the code

⭐ ⭐ JLab’s help menu at the top has links to official docs for Pandas, Python, Jupyter Lab

Autocompletion suggestions

tab

type import pan + tab

⭐ ⭐ Automatic if you finish the Jupyter Lab set up

type import pan

A search bar for Jupyter Lab commands

cmd + shift + c

Type cmd + shift + c, then “move”

1.6.3. Shortcuts

The ROI on learning these shortcuts VERY high

A tip: In Jupyterlab, pressing cmd + shift + C brings up a search bar so you can search for commands you can run, and it will list shortcuts if available.

Note

I use CTRL because I use Windows. Use cmd on Mac.

1.6.3.1. Shortcuts when you are in Command Mode

  • Y change cell to a code block

  • M change cell to a markdown block

  • shift + enter run cell, cursor moves to/select cell below

  • ctrl + enter run cell, cursor stays in current 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)

1.6.3.2. Shortcuts when you are in Edit Mode

  • cmd + / to comment and uncomment 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.6.4. My Jupyter Lab Set up

You can add extra features to Jupyter Lab by installing extensions. (You can see your installed extensions by clicking the “puzzle” icon on the left sidebar menu within JupyterLab.)

The extensions I have installed are below. Follow these instructions to install them.

extension

notes

jupyterlab_spellchecker

highlights non-words in Markdown cells

variableInspector

right click on a file, and click “Open Variable Inspector” to get a new pane that allows you to see inside of data objects

this is very useful

Note 1: be careful with large datasets
Note 2: It shows the data as it is when you open the viewer, so you have to reopen it to see any changes

jupyterlab-lsp

see the doc: code suggestions, some error detection, and help while writing code

jupyterlab_code_formatter

when I type ctrl + shift + F , this automatically formats my code cells

Students love this!

note: this formats code within a cell, but will not move code between cells

You’ll see what these do as we go.

1.6.5. A good trick: How do I copy code from one file to another?

Note

A common thing you’ll want to do is copy chunks of lecture files into your own code (to work on problems, borrow code, etc.) so you don’t have to retype big blocks of code.

I think this is the easiest way to copy parts of lecture files into your notes:

  1. Clone the lecture repo to your computer.

  2. Open the code you’re working on in Jupyter Lab.

  3. Open the lecture code you’re working on in Jupyter Lab in a new tab in Jupyter Lab.

  4. Drag the lecture code “tab” to the right until it snaps into a new panel.

  5. Click and hold to the left of the block you want to drag into your code and drag it into your code.

Pro Tip

You can select a bunch of code blocks and drag them all at once. To do that, click to the left of a cell you want, hold the shift button, and then click the up or down arrow on your keyboard until all the cells you want are highlighted. Then do step 5 above.

Want another amateur youtube video? Here’s one of me showing those steps!

1.6.6. Optional: Magic commands

(Students in prior years have not used these commands, but I’m leaving this here in case.)

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 commands much, but those that do find them very helpful.)

1.6.7. Credits

This page borrows from the first chapter of the Python Data Science Handbook and QuantEcon.