Skip to content

Getting started with the VRE

JupyterLab launcher

The JupyterLab launcher gives access to the main features of the VRE. If the launcher is not visible, click File -> New Launcher (or Ctrl+Shift+L)

In that tutorial, the 3 most useful tools are:

  • Create a Jupyter notebook
  • Execute the virtual desktop
  • Start a terminal

VRE Launcher

Virtual Desktop

The Virtual Desktop can be used to launch specific tools, such as a GIS software, office tools, etc.

VirtualDesktop

Terminal

The terminal is a classic Bash command processor, that runs in a text window. Its documentation can be found here : https://www.gnu.org/software/ bash/manual/bash.html

Terminal

Jupyter notebooks

The Jupyter notebook is a powerful scripting tool.

Notebook documents (or “notebooks”, all lower case) are documents produced by the Jupyter Notebook App. They contain both computer code (e.g. python) and rich text elements (paragraph, equations, figures, links, etc…). Notebook documents are both human-readable documents containing the analysis description and the results (figures, tables, etc..) as well as executable documents which can be run to perform data analysis.

The code is split in "cells" that can contain either:

  • Code : to execute Python code. When the code is run, the notebook displays the output below the code cell that generated it.
  • Markdown : to display information in place where the cell is run

Starting a new notebook

To create a new notebook, simply click "File" -> "New" -> "Notebook".

A new notebook will be created in a separate tab.

To rename this new notebook, right-click its tab and select Rename.

Rename

How to run a notebook ?

You can run the notebook document step-by-step (one cell at a time) by pressing "shift + enter" on the keyboard or by clicking the "play" button in the top menu

You can run the whole notebook in a single step by clicking on the menu Cell -> Run All.

title

The above (and current) cells are markdown cells, where text can be formatted (bullet points, numbering, images, etc.)

Below is a code cell:

print("Hello World")
Hello World

Keyboard shortcuts are available, the full list can be accessed by clicking the "commands" button on the left panel

Commands

Kernels

A notebook kernel is a “computational engine” that executes the code contained in a Notebook document. The ipython kernel, used in this demo, executes python code. Kernels for many other languages exist.

When you open a Notebook document, the associated kernel is automatically launched. When the notebook is executed the kernel performs the computation and produces the results. Depending on the type of computations, the kernel may consume significant CPU and RAM.

To change or restart a Kernel, use the dedicated menu

ChangeKernel

To restart the kernel (i.e. the computational engine), click on the menu Kernel -> Restart. This can be useful to start over a computation from scratch (e.g. variables are deleted, open files are closed, etc…).

The "Table of Contents" button in the left menu opens a navigation panel that can be used to navigate.

The navigation is done thanks to different headings levels, within markdown cells. Using # symbols (e.g. the first level is "# Text")

Click on a section to navigate directly.

ToC

Basic python code execution

Create the variable "sum", and display the result by simply entering its name on a separate line.

Remember : to execute the cell below, click the play button or use the keyboard shortcut "shift + Enter"

sum = 3 + 5
sum
8

Note that once a cell is executed, all the objects (variables) created are stored into memory. For example, the variable "sum" created above can be called again:

sum
8

We can define methods:

def compute_sum(a, b):
    output = a + b
    return output
compute_sum(3, 9)
12

Or classes:

# A Sample class with init method
class Person:

    # init method or constructor
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Sample Method
    def say_hi(self):
        print("Hello, my name is", self.name)
        print("My age is", self.age)


# Create object Person with attributes "Michael" and "44"
p = Person("Michael", 44)
# Display info
p.say_hi()
Hello, my name is Michael
My age is 44

Display an object value:

p.age
44

Note that if the kernel is restarted, everything is cleaned up. To compute "p.age" again, all the above cells must me re-executed.

Notebooks are super handy

Markdown

Jupyter notebooks accept markdown to enhance the output. This can be useful to write interactive documentation for example.

Some examples of simple markdown in code cells output are given below:

from IPython.display import Markdown, display

display(Markdown("**BOLD TEXT**"))
display(Markdown("*Italic Text*"))

Compare text with the print function and display:

print("Hello world")
display(Markdown("Hello world"))

A more complex example with nice looking colors:

def printmd(string, color=None):
    colorstr = "<span style='color:{}'>{}</span>".format(color, string)
    display(Markdown(colorstr))


printmd("This line is blue", color="blue")
printmd("**This line is bold and red**", color="red")
printmd("This is a mixture of **bold**, *italic* text, <br>Heure is a line break<br>")

The function printmd can be used in place of print, without specifying a color to display nicer cell output:

printmd("Hello world")

Markdown can also link to other web pages.

To enter a link, simply write the address between brackets and the short name between square brackets.

[web pages](https://jupyter.org/)

Display equations

using Markdown, it is possible to display LaTeX equations:

\begin{equation}
e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i
\end{equation}

equation

Widgets

Jupyter notebooks can be enhanced with widgets

The cell below defines several widgets:

import ipywidgets as widgets
from IPython.display import display

style = {"description_width": "initial"}
widget1 = widgets.ToggleButtons(
    options=["Option 1", "Option 2", "Option 3"],
    value="Option 1",  # Default value
    description="Choose an option",
    disabled=False,
    button_style="danger",
    tooltips=["Tooltip for option 1", "Tooltip for option 2", "Tooltip for option 3",],
    style=style,
)

widget2 = widgets.Checkbox(
    value=True, description="This is a checkbox", disabled=False, style=style,
)

widget3 = widgets.IntSlider()
widgets

display(widget1)
display(widget2)
display(widget3)
print("The first widget value is: ", widget1.value)
print("State of the second widget: ", widget2.value)
print("Value of third widget: ", widget3.value)

Other interesting stuff

Display progress bars

A simple progress bar:

from tqdm.notebook import tqdm, trange
import time

for i in tqdm(range(100), desc="Just waiting"):
    time.sleep(0.1)

We can also use nested progress bars:

for i in trange(4, desc="Main computation"):
    for j in trange(5, desc="Intermediate computations"):
        time.sleep(0.5)
    if i == 3:
        print("Done !")

Using shell commands

Simply start a code cell with an exclamation mark to run a shell command:

!ls -al
!echo "I can print here"

Magic commands

Magic commands are special commands that help with productivity.

For example, to display all variables:

%who

To measure the execution time of a line:

%time time.sleep(1)

To measure the execution time of a cell:

%%time
time.sleep(1)
time.sleep(.5)