Anaconda Navigator
- In this course we use Anaconda application, which includes all the necessary packages
- Installation guide:
- Download the appropriate version for your operating system here
- Run the installation program
- Start Anaconda
- You should now have the dashboard view open similar to the example below
- We will use JupyterLab development environment from (marked with red rectangle)
Changing the default working directory
- The default working directory in JupyterLab will point to your currect user's home directory
- In Windows environments this is C:\Users\username (where username is the name of your Windows user account)
- For Mac users the default directory will be in /Users/username
- If you want to change this directory follow the instructions below
- Open Anaconda Prompt (anaconda3)
- Run the following command: jupyter lab --generate-config
- This will create a new configuration file C:\Users\username\.jupyter\jupyter_lab_config.py
- Open this file for editing
- Search the file for the following line: #c.ServerApp.notebook_dir = '' (see example pic below)
- Add the path of your choice inside quotes, for example c.ServerApp.notebook_dir = 'C:/jupyter_files' (Important: Use forward slashes (/) for the path!)
- Uncomment the line by removing the # character from the beginning of the line
- Save and exit the file
- Restart the JupyterLab and verify that it opens your newly defined path as a default working directory
Testing the Jupyterlab
- Open JupyterLab from the dashboard
- Open new notebook by pressing Python 3 button under the Notebook section
- New file called Untitled.ipynb should appear in the leftmost tree view
- Let's create a small Python function to test our environment
- Click the cell and add the following content
def calculate(a,b):
return a*(a+b)
- Press Esc or click outside the cell to return back to command mode
- Now press b from keyboard to add a new cell
- Click the new cell for entering to edit mode
- Call the previously created function by adding this function call to your new cell: calculate(2,5)
- Press Esc or click outside the cell to return back to command mode
- Run all cells by pressing Ctrl + a (select all cells) and after this Ctrl + Enter (run selected cells)
- Your output should now be similar to example below
JupyterLab keyboard shortcuts
- Below are some useful keyboard shortcuts for operating with notebook file
Keyboard shortcut |
Description |
Enter |
Enter edit mode |
Esc |
Enter command mode |
b |
Create a new cell below the selected cell |
a |
Create a new cell above the selected cell |
c |
Copy selected cell |
x |
Cut selected cell |
v |
Paste cell |
dd |
Delete selected cell |
z |
Undo previous delete |
Ctrl + s |
Save notebook |
Ctrl + a |
Select all cells |
Shift + Enter |
Run selected cells |
m |
Set selected cell as markdown cell |
y |
Set selected cell as Python code cell |
Debugging feature in JupyterLab
- JupyterLab includes debugging feature, which you can utilise for finding errors and bugs from your sourcecode
- In some of the newer versions of jupyter kernel, you need to enable the debugger feature first by editing the kernel.json file located in the following path (Windows environment example):
C:\Users\username\anaconda3\share\jupyter\kernels\python3\kernel.json
- It might be located in some other path also so use operating system search feature for finding the kernel.json file if path example shown above does not provide any results
- After locating the kernel.json file, open it with editor of your choice and change the value for "debugger" key to true (see the example below).
- Save changes and restart Jupyter Lab for changes to take effect
- Enable debugger by clicking the bug icon in the top right corner (see example image below)
- You should now see the debugger panel on the right
- Below is an example that you may try in order to familiarise yourself with the debugger
def categorize_numbers(lst):
lst_even = []
lst_odd = []
for num in lst:
if num % 2 == 0:
lst_even.append(num)
else:
lst_odd.append(num)
return {"even":lst_even,"odd":lst_odd}
numbers = [0,1,2,3,4,5,6,7,8,9]
categorize_numbers(numbers)
- The function will be used for categorising elements from input list into two lists depending whether the processed number is even or odd
- When all numbers are processed, the function will return a dictionary-type object containing both lists with keys "even" and "odd"
- Before running the notebook, let's insert one breakpoint inside our function (line 5)
- Now when all cells are run again, lists lst_even and lst_odd will be empty when checked from the rightmost panel (see image below)
- This is view is called VARIABLES and there you can inspect variable content at any given point during your run
- The view in the image above is called tree view
- Another way of inspecting the variables is a table view (see image below)
- Below the forementioned panel you can find a view called CALLSTACK (see image below)
- It has several buttons for managing the running of the code during debugging
- As can be seen from the CALLSTACK view, categorize_numbers was first called from the line 2 on our second notebook cell
- The current function is listed at the top of our stack (categorize_numbers) where code run has been stopped on line 5
- The image below contains the last two views: BREAKPOINTS and SOURCE
- On BREAKPOINTS view we see our attached breakpoint on line 5
- SOURCE view shows the line from the source code where inspection is currently stopped
- Now by running the code forward with Next button (F10) we can see that
- From the image below we can see that processed number (in variable num) is currently 4
- Both lists have been updated in the variables view
- lst_even = [0,2,4]
- lst_odd = [1,3]