JupyterLab in Galaxy

Author(s) orcid logoAnne Fouilloux avatar Anne Fouilloux
Editor(s) orcid logoHelena Rasche avatar Helena Rasche
Overview
Creative Commons License: CC-BY Questions:
  • How can I manipulate data using JupyterLab in Galaxy?

  • How can I start a notebook in JupyterLab?

  • How can I import/export dataset from/to my history to/from the notebook?

  • How can I save my notebook to my history?

Objectives:
  • Launch JupyterLab in Galaxy

  • Start a notebook

  • Import libraries

  • Use get() to import datasets from your history to the notebook

  • Use put() to export datasets from the notebook to your history

  • Save your notebook into your history

Time estimation: 1 hour
Supporting Materials:
Published: Mar 5, 2020
Last modification: Oct 18, 2022
License: Tutorial Content is licensed under Creative Commons Attribution 4.0 International License. The GTN Framework is licensed under MIT
purl PURL: https://gxy.io/GTN:T00153
rating Rating: 5.0 (1 recent ratings, 2 all time)
version Revision: 8

JupyterLab is an Integrated Development Environment (IDE). Like most IDEs, it provides a graphical interface for R/Python, making it more user-friendly, and providing dozens of useful features. We will introduce additional benefits of using JupyterLab as you cover the lessons.

Comment

This tutorial is significantly based on the JupyterLab documentation “The JupyterLab Interface” section.

Agenda

In this tutorial, we will cover:

  1. JupyterLab
    1. Start your first notebook
    2. Graph Display in JupyterLab with Python
  2. Interaction between JupyterLab and Galaxy
    1. Import / export Data
    2. Save the Notebook in your history

JupyterLab

Opening up JupyterLab is quite easy:

Hands-on: Launch JupyterLab

Currently JupyterLab in Galaxy is available on Live.useGalaxy.eu, usegalaxy.org and usegalaxy.eu.

Hands-on: Run JupyterLab
  1. Interactive Jupyter Notebook. Note that on some Galaxies this is called Interactive JupyTool and notebook:
  2. Click Run Tool
  3. The tool will start running and will stay running permanently

    This may take a moment, but once the Executed notebook in your history is orange, you are up and running!

  4. Click on the User menu at the top and go to Active Interactive Tools and locate the JupyterLab instance you started.
  5. Click on your JupyterLab instance

If JupyterLab is not available on the Galaxy instance:

  1. Start Try JupyterLab

You should now be looking at a page with the JupyterLab interface:

Jupyterlab default session.

As shown on the figure above, JupyterLab interface is made of 3 main areas:

  • The menu barat the top
  • The left side bar with in particular the File Browser
  • The main work area in the central panel

Start your first notebook

Now that we are ready to start exploring JupyterLab, we will want to keep a record of the commands we are using. To do this we can start a Notebook.

Hands-on: Start a notebook
  1. On the left side bar, in the File Browser, double-click on the file called ipython_galaxy_notebook.ipynb. This will open the default notebook in the main work area.
  2. If ipython_galaxy_notebook.ipynb does not exist (for instance on Try JupyterLab) then click on + (top left) to start The launcher and then Python icon in the Notebook section to create a new blank notebook.

A new notebook appears in the centre panel. Before we go any further, you should learn to save your script.

Hands-on: Save a Python notebook
  1. Click the File menu and select **Save Notebook As… Alternatively, you can also:
    • Click the galaxy-save icon (Save the notebook contents and create checkpoint) in the bar above the first line in the script editor
    • Click the File menu and select Save Notebook
    • Type CTRL+S (CMD+S on OSX)
  2. In the Save Notebook As window that opens, name your file ipython_galaxy_notebok Alternatively, you can also rename your Jupyter Notbook:
    • Right click on the name (Untitled.ipynb) in the bar above the first line in the script editor and select Rename Notebook

The new script ipython_galaxy_notebook.ipynb should appear in the File Browser* in the left panel. By convention, Jupyter notebooks end with the file extension .ipynb independently of the programming language (R, Python, Octave, Julia, etc.).

jupyterlab default notebook.

Comment: Note: supported programming languages

Depending on your JupyterLab instance, the list of supported programming languages may vary. On Live.useGalaxy.eu, the following programming languages are currently supported:

  • Python 3
  • Julia
  • R
  • Octave
  • Ansible
  • Bash
  • SciJava

By default, a Python notebook is started. Don’t worry if you are not familiar with Python programming language, it is not necessary for this tutorial. The same functionalities applies for any available programming languages.

Comment: Note: switching to another programming language

Once you have created a Notebook, you can switch to another available programming language (Switch kernel).

  1. On the top right of your running Notebook, click on Python 3. A new window pops up: jupyterlab switch kernel.
  2. In this new window, click on Python 3 to select an available programming language (R, octave, Julia, etc.).
  3. Click on Select to activate your selection. The pop-up window closes and you are ready to use your notebook with the selected programming language. Alternatively, you can also:
  4. Click on + (top left menu) to start the Launcher. The list of available programming language is given in the Notebook section.
  5. Click on icon of your choice in the Notebook section.
  6. A new notebook is created with the programming language of your choice.
Hands-on: Import Python libraries
  1. Click on a cell of your notebook to edit it (verify that it is defined as a “Code” cell)
  2. Enter the following lines:
    import numpy as np
    import matplotlib.pyplot as plt
    
  3. Shift+Return to run the cell or click on the run cell button.

Graph Display in JupyterLab with Python

In this tutorial we are going to simply plot a simple graph using generated data.

Hands-on: Draw a simple plot
  1. Generate a simple dataset

    x = np.linspace(0, 2, 100)
    y = x**2
    
  2. Create your figure with the command:

    fig, ax = plt.subplots( nrows=1, ncols=1 ,figsize=(15, 10) )
    
    • nrows=1, ncols=1 means you will have one plot in your figure (one row and one column)
    • figsize parameter determine the size of the figure
  3. Draw the plot with the command

    plt.plot(x, y);
    

    Simple plot in Jupyter.

Interaction between JupyterLab and Galaxy

Import / export Data

You can import data from Galaxy history using the get(12) command, with the number of your dataset in the history (If you are working on a collection, unhide datasets to see their numbers).

If you want to save a file you generated in your notebook, use the put("file_name") command. That is what we are going to do with our distribution plot.

Hands-on: Save an Jupyter generated image into a Galaxy History
  1. Create an image file with the figure you just draw with the command fig.savefig('simpleplot.png')
  2. Export your image into your history with the command put('simpleplot.png')

Save the Notebook in your history

Once you are done with you analysis or anytime during the editing process, you can save the notebook into your history using the put("ipython_galaxy_notebook.ipynb"). If you create additional notebooks with different names, make sure you save them all before you quit JupyterLab.

This will create a new notebook .pynb file in your history every time you click on this icon.

Hands-on: Closing JupyterLab
  1. Click User: Active Interactive Tools

  2. Tick galaxy-selector the box of your Jupyter Interactive Tool, and click Stop