Anaconda logo

Anaconda Navigator

  • In this course we use Anaconda application, which includes all the necessary packages
  • Installation guide:
    1. Download the appropriate version for your operating system here
    2. Run the installation program
    3. Start Anaconda
    4. You should now have the dashboard view open similar to the example below

Anaconda dashboard

  • 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


  1. Open Anaconda Prompt (anaconda3)
  2. Run the following command: jupyter lab --generate-config
  3. This will create a new configuration file C:\Users\username\.jupyter\jupyter_lab_config.py
  4. Open this file for editing
  5. Search the file for the following line: #c.ServerApp.notebook_dir = '' (see example pic below)
  6. Add the path of your choice inside quotes, for example c.ServerApp.notebook_dir = 'C:/jupyter_files' (Important: Use forward slashes (/) for the path!)
  7. Uncomment the line by removing the # character from the beginning of the line
  8. Save and exit the file
  9. Restart the JupyterLab and verify that it opens your newly defined path as a default working directory

Testing the Jupyterlab#

  1. Open JupyterLab from the dashboard
  2. Open new notebook by pressing Python 3 button under the Notebook section
  3. New file called Untitled.ipynb should appear in the leftmost tree view
  4. Let's create a small Python function to test our environment
  5. Click the cell and add the following content
  6. def calculate(a,b):
        return a*(a+b)
    
  7. Press Esc or click outside the cell to return back to command mode
  8. Now press b from keyboard to add a new cell
  9. Click the new cell for entering to edit mode
  10. Call the previously created function by adding this function call to your new cell: calculate(2,5)
  11. Press Esc or click outside the cell to return back to command mode
  12. Run all cells by pressing Ctrl + a (select all cells) and after this Ctrl + Enter (run selected cells)
  13. Your output should now be similar to example below

Notebook example

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

Edit kernel.json

  • 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)

Enable debugger

  • 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)

Breakpoint insertion

  • 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

JupyterLab empty lists

  • The view in the image above is called tree view
  • Another way of inspecting the variables is a table view (see image below)

JupyterLab empty lists (table view)

  • 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

JupyterLab debugger tools

  • 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

Breakpoint inspection in JupyterLab

  • 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]

Updated variables view in debugger