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!
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 (
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
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.
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¶
188.8.131.52. Opening Jupyter Lab¶
Windows: Open Anaconda Prompt and type
jupyter laband hit enter
Mac: Open a terminal window and type
jupyter laband hit enter
184.108.40.206. 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
220.127.116.11. Creating a folder¶
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
Create a folder: right click in file explorer pane and select “New Folder”
For our purposes, do this and name the new folder “notes”
18.104.22.168. 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”)
22.214.171.124. 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
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
Enteror double clicking in the cell.
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
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,
Under the help tab, click “Launch Classic Notebook”
A new tab opened up.
In the upper right area, click on “New”, and select “Python 3”.
A new tab opened up with an untitled notebook file.
Click “help” in the upper bar, and then “User Interface tour”.
Secondly, watch this video from 5:25 on. It’s very good!
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”.
Below, cmd and CTRL are interchangeable. Use cmd on Mac and CTRL on Windows.
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.
126.96.36.199. Shortcuts when you are in Command Mode¶
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)
188.8.131.52. 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
184.108.40.206. 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- 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
%%filewill save the cell (and just that cell) to a python file
You can ask IPython for help on magics! E.g.
IPython can list all magics
%lsmagicand 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.6.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:
shift + tab
if you’re inside a function, syntax guides ⭐
What can you type next? Autocompletion suggestions (REALLY HELPFUL!) ⭐
Python’s own help system.
Details about ‘object’, use ‘object??’ for extra details
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.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.
variableInspector: helps you see inside of data objects (amazing, but be careful with large datasets)
jupyterlab_code_formatter: with one click, will automatically format your code nicely!
You’ll see what these do as we go.
1.6.5. How do I copy code from one file to another?¶
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:
Clone the lecture repo to your computer.
Open the code you’re working on in Jupyter Lab.
Open the lecture code you’re working on in Jupyter Lab in a new tab in Jupyter Lab.
Drag the lecture code “tab” to the right until it snaps into a new panel.
Click and hold to the left of the block you want to drag into your code and drag it into your code.
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!