JupyterLab

Unit 2

JupyterLab

It is time that we start introducing the coding environment we will use for programming during this course.

Terminal
cd $SCIPRO
conda activate scipro2024
jupyter-lab

This series of terminal commands will open the coding environment JupyterLab for you. It is a web-based application that starts a new internet browser window. While JupyterLab is running out of your terminal, you won’t be able to use the terminal anymore for any other tasks. That is ok, though, because you will have plenty of options to work with using the JupyterLab graphical user interface in your browser.

We will now slowly start to get familiar with the capabilities that JupyterLab offers us by doing some exercises.

The terminal within JupyterLab

Recap of exercise #01-01 ‘conda’
  1. Create a new conda environment testenv without specifying a python version for it, and also install the packages ipython and numpy into the environment. Do not yet activate it.
  2. Run conda --help in the terminal and examine the available conda commands.
  3. Run conda info --help and read the documentation.
  4. Use conda info to display a list of all environments currently installed on your computer.
    • How many environments are there?
    • Where are they installed?
  5. Activate testenv.
  6. Find out which version of python was installed in testenv. Use conda --help to find the correct command!
  7. Deactivate testenv. Make sure that no environment is activated anymore (i.e., no parentheses with an environment name are displayed before your command line).
  8. Remove testenv by running conda remove -n testenv --all.

Writing and executing a python program with JupyterLab

Open a new python file by clicking on the blue button with a + on the top left and then select Python File.

  1. Rename the file using the GUI (graphical user interface) of JupyterLab: solar_elev_angle_latitudes.py.
  2. Create a new directory (also using the GUI) and call it 02_unit.
  3. Move the python file into the new directory.

Now, we want to make use of a special feature that JupyterLab offers us. We can connect the python file with a python interpreter. To do so, click into the python file editor to activate it and then select File > Create Console for Editor.

A new popup window opens. You can just go ahead with the suggestion made by the window and start a Python 3 kernel.

In the following screenshot, I have copy-pasted our 4th version program of the solar elevation angle into the python file:

Executing select lines and blocks of code

Move your cursor to the first line that imports numpy. Then press [Shift+Enter]. The line gets executed by the python console below!

Similarly, if you highlight several lines of code and hit [Shift+Enter], the highlighted block of code gets executed.

In addition to executing commands from your python file, you can also use the prompt at the python console to execute commands. Analogously, use [Shift+Enter] to execute a command.

Recap of exercise #01-02 ‘solar elevation angle for different latitudes’

Write a program that computes and plots the solar elevation angle for Sept 16, 2023 at noon true local time (i.e., tau = 0, delta = 2.86) for all latitudes from the equator to the north pole in 5\(^\circ\) intervals. Use version 3 of our second program (in the slide deck First steps in python) to guide you.

Execute your program line-by-line using the python interpreter (python console) in JupyterLab. Then execute your program from the terminal in JupyterLab.

Showing a figure instead of printing it to a PNG file

We will get to advanced plotting of visuals in more detail later in the course. For now, though, try using the command plt.show() instead of plt.savefig() whenever you don’t want to save a PNG file, but when you want to display the figure within the python console in JupyterLab.

Exercise #02-01: A program with user input

Write a program that uses the input function to prompt a user for their name, record it in a variable called user_name, and then welcome them. The output should look like

Enter your name: Florian
How's it, Florian!

Since this program requires interactive user input, I suggest you execute it from the terminal in JupyterLab. Afterwards, also try to run it in an ipython session, and try to execute it with a console attached to your .py script.

Recording user input

The input function will always record user input into a string, even if the user types a number. If you asked for numeric input, make sure to convert the str to int or float before you do any calculations.

Using JupyterLab through an FHV web interface

FHV provides a web interface for all students to run JupyterLab sessions with python and all standard libraries pre-installed. All you need is a FHV user account and either a FHV PC in one of the computer pools or your own laptop with an internet and VPN connection to the FHV network.

FHV web interface JupyterLab

You can access the FHV JupyterLab web instance at https://jupyter.labs.fhv.at. Use your username without the domain part to log in.

Connecting to the FHV VPN

To access the FHV JupyterLab web instance you need to be connected to the FHV network directly or via VPN. You can find instructions for how to establish a VPN connection here.

Learning checklist

  • I know how to use Jupyter-lab as coding environment on my own computer or the FHV computer pool.
  • I can access the terminal within Jupyter-lab, create a python file, and attach an ipython console to the current script. This allows me to interactively write and execute python code.