Skip to content

Latest commit

 

History

History
779 lines (442 loc) · 23.7 KB

File metadata and controls

779 lines (442 loc) · 23.7 KB

JupyterLab IDE Ubuntu Setup

JupyterLab is a browser based IDE.

Miniforge Installation and Setup

The conda package manager will be used to create a new environment for JupyterLab. In order to use conda, Miniforge needs to be installed and preferably initialised. This was previously covered in:

Miniforge Install and Initialisation

In order to use TeX in matplotlib plots. TeX needs to be installed system wide using the operating systems package manager (which was also previously covered in the above):

sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic cm-super dvipng

Updating the conda Package Manager

The purpose of the base environment is to use the conda package manager to install packages in other Python environments. Before using the conda package manager, the conda package manager should be updated to the latest version:

conda update conda

img_001

Since Miniforge is used, the default channel will be the community channel conda-forge:

img_002

Input y in order to proceed:

img_003

The conda package manager is now up to date:

img_004

The terminal can be cleared by inputting:

clear

And environments can be listed by inputting:

conda env list

img_005#

Creating a JupyterLab conda-forge Environment

JupyterLab is mainly used with Python. To create a new environment for JupyterLab which includes the Python kernel, the following command can be used:

conda create -n jupyter-env jupyterlab jupyter cython seaborn scikit-learn pyarrow sympy openpyxl xlrd xlsxwriter lxml sqlalchemy tabulate nodejs ipywidgets plotly pyqt isort autopep8 ruff black jupyterlab-variableinspector jupyterlab_code_formatter jupyterlab-spellchecker jupyterlab-spreadsheet-editor

img_006

jupyterlab is the IDE itself. seaborn has numpy, pandas and matplotlib as dependencies and are the scientific libraries. scikit-learn is used for machine learning. pyarrow, openpyxl, xlrd, xlsxwriter, lxml, sqlalchemy, tabulate are for various file pandas formats. pyqt is for matplotlib's interactive backend and ffmpeg is for saving matplotlib animations.

jupyterlab-variableinspector, jupyterlab_code_formatter, jupyterlab-spellchecker, jupyterlab-spreadsheet-editor are common extensions for JupyterLab. In order for extensions to be installed, nodejs needs to be installed. The JupyterLab IDE and extensions are written in nodejs, which is a programming language used for web content. Knowledge of nodejs is not required to use Python with JupyterLab.

-n means name and jupyter-env is the name of the Python environment. Specifying an environment using -n means changes to that environment will be made opposed to base which is the currently activate environment.

The environment location will be listed, along with details about the packages to be installed:

img_007

Input y in order to proceed:

img_008

Launching JupyterLab

To activate jupyterlab-env input:

conda activate jupyter-env

img_009

The prefix will now show (jupyter-env) instead of (base):

img_010

This means that instead of searching the bin folder in (base) which is the miniforge3 folder:

img_011

The equivalent bin folder is used for the currently activated environment jupyter-env.

The envs folder contains the environments:

img_012

The jupyter-env folder is currently activated:

img_013

This means the current bin and lib folders are preferentially searched for binaries and programs:

img_014

The binary of interest is jupyter-lab:

img_015

And can be launched in the terminal by inputting:

jupyter-lab

img_016

JupyterLab is a browser based IDE. The server is ran in the terminal:

img_017

img_018

While the visual elements display in the browser:

img_019

File Explorer

To the left hand side is a file explorer which displays the current working directory in the terminal. The current working directory in the terminal was ~, the home the directory:

img_020

The Documents folder can be selected. JupyterLab can be used to open .py files, these display in a script editor which applies syntax highlighting:

img_021

Launcher

To the right hand side, a new launcher can be selected, this can be used to start a new terminal.

img_022

Script Editor and Terminal

The terminal is equivalent to a new session of the Linux terminal and starts with (base), the base conda-forge environment:

img_023

The jupyterlab-env can be activated and the script executed:

#%% Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#%% Set Style
sns.set_style('whitegrid')
#%% Create Data
x = np.array([0, 1, 2, 3, 4])
y = 2 * np.pi * np.sin(x)
#%% Plot
plt.plot(x, y)
plt.xlabel(R'$x$', usetex=True)
plt.ylabel(R'$x2 \pi x \sin{x}$', usetex=True)
conda activivate jupyter-env
python3 script.py

img_024

The plot in the script won't show unless the command:

plt.show()

is added.

img_025

The file menu can be used to save the script:

img_026

The plot now displays in an interactive window:

img_027

IPython Console

Code completion does not display for Python code, unless it is run in an interactive python console. Right click empty space in the script and select create console for editor:

img_028

Select IPython kernel:

img_029

A selection can be highlighted:

img_030

And run using, run selected code:

img_031

Now the libraries are imported, identifiers can be accessed from numpy by inputting:

np.↹

img_032

Data model identifiers can be accessed using:

np.__

img_033

The identifier __file__ is an instance and can be returned to an ipython cell output:

img_034

The IPython console is equivalent to opening an IPython console from the Linux terminal. Because the Linux the jupyter-env was activated when the jupyter-lab binary was launched, jupyter-env is the currently selected environment. To view a docstring, an identifier should be input with open parenthesis and + should be pressed:

np.arange(⇧+

img_035

A docstring can also be returned to an ipython cell:

np.arange?

img_036

img_037

Interactive Python Notebook

A Python Script file .py is essentially a text file which can be displayed in a text editor.

Code can also be written in an Interactive Python Notebook file .ipynb. The interactive Python notebook is written using nodejs which is a programming language used by the browser to display visual content, essentially as a website.

img_038

Although nodejs is used to display the contents of the .ipynb file in the browser. The user can create code cells which are written using Python or markdown cells are written using markdown. If a markdown cell is selected:

img_039

A title (H1) can be added using:

# JupyterLab Test

img_040

When this cell is run, by selecting the run button or pressing +:

img_041

The formatted markdown displays. (It can be edited again by double clicking it). Note to the left hand side, the table of contents tab displays the title:

img_042

A heading (H2) can be added using:

## Import Libraries

img_043

The libraries can be imported in a code cell using:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

img_044

Now that the libraries are imported, identifiers from them can be viewed:

np.↹

img_045

A new cell, will default to a Python cell, notice the markdown text has the wrong syntax highlighting:

img_046

A number of keyboard shortcuts are available, these can be viewed by selecting Help → Show Keyboard Shortcuts:

img_047

img_048

The keyboard shortcut Esc+m will toggle a cell to markdown. It can be run using +:

img_049

JupyterLab Variable Inspector

If the following three code cells are run:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style('whitegrid')

x = np.array([0, 1, 2, 3, 4])
y = 2 * np.pi * np.sin(x)

The variables x and y are created. Variables can be seen by right clicking some blank space in the notebook and selecting open variable inspector:

img_050

img_051

Since x and y are ndarray instances they can be inspected further by selecting the magnify icon:

img_052

img_053

img_054

Plotting

If the code to plot is added:

plt.plot(x, y)
plt.xlabel(R'$x$', usetex=True)
plt.ylabel(R'$x2 \pi x \sin{x}$', usetex=True)

Notice the plot is returned to the cell output as a static image:

img_055

Plot backends can be selected using the ipython magic.

inline is the default backend, displaying the plot as a static image:

%matplotlib inline

qtagg is the default backend used by a Python script, displaying the plot in its own interactive window:

%matplotlib qtagg

widget displays the plot interactively in a cell output:

%matplotlib widget

TeX

The TeX used in the figure, can be added to markdown:

'$2 \pi x \sin{x}$'

img_056

img_057

img_058

Cell Output Options

A docstring can be returned to a cell output using:

plt.plot?

Since this is a long docstring, the cell output can be right clicked:

img_059

And enable scrolling for output can be selected:

img_060

img_061

Cell Execution Order

If the code in first cell is modified to also import pandas and rerun, notice it is now updated from In [1] to In [6]:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

img_062

The ndarray instances x and y can also be returned to a cell output:

img_063

The DataFrame class is a commonly used data structure within a notebook and can be returned to a cell output:

df = pd.DataFrame({'x': x, 'y': y})
df

img_064

Or displayed in the variable inspector:

img_065

img_066

img_067

JupyterLab Spreadsheet Editor

If the DataFrame is exported to a csv file and xlsx file:

img_068

img_069

Notice that the csv file can be opened in JupyterLab because the spreadsheet editor extension is installed:

img_070

This extension unfortunately doesn't support the xlsx file:

img_071

The notebook file can be renamed in the file explorer. Notice the tab name is also updated, the tab name can also be used to rename the file:

img_072

img_073

img_074

Notebook and IPython Kernel

The notebook can be saved, using the file menu:

img_075

If the notebook is closed:

img_076

And reopened from the file explorer:

img_077

It displays in the same state as before however the IPython console associated with the notebook file has been exited:

img_078

The Kernel menu in the title bar can be used to Restart the Kernel and Clear All Cell Outputs:

img_079

Select Restart:

img_080

Now each cell can be run by selecting +:

img_081

img_082

To run a cell and insert a new one below, Alt+ can be used:

img_083

There is also the option to Restart the Kernel and Run All Cells:

img_084

Select Restart:

img_085

Notice all the cells (with code) are now run from top to bottom:

img_086

The run and the kernel menu have other options:

img_087

JupyterLab Code Formatter

The JupyterLab code formatter extension has been installed. If the code in In [3] is modified to have poor formatting. The cell can be right clicked and Format Cell can be selected:

img_088

Notice that the spacing in In [3] is now PEP8 compliant. If In [2] is now selected and formatted:

img_089

Notice the string quotations have been changed from "" to ''. This is because the formatting preference of black has been used, which differs from the formatting preference in the Python programming language itself:

img_090

In Settings, the Settings editor can be examined:

img_091

The JupyterLab Code Formatter extension can be selected. To the right the JSON settings editor can be selected:

img_092

img_093

If the system defaults are copied over to the user preferences, blacks, string_normalization (to double quotes) can be set to False:

img_094

Now when Format Cell is used, the string quotations will remain unchanged:

img_095

img_096

Note cells can be formatted individually, or the format notebook button cna be selected to format all notebook cells:

img_097

Markdown

Images can be inserted in a markdown. Usually they are stored in the same folder ad the notebook or markdown file or a subfolder:

img_098

JupyterLab can also be used to create or edit a markdown file. A new tab can be opened to display a new launcher and create a markdown file. It is common to have a readme.md in the folder of a Python project:

img_099

The following heading and image can be added:

# JupyterLab

## Insert Image

The *following* **figure** will be inserted:

![figure1](fig1.png)

Blank space in the markdown file can be right clicked and the Show Markdown Preview can be selected:

img_100

The table of contents displays the markdown headings in the markdown file:

img_101

Contextual Help

A new tab can be created to display a new launcher:

img_102

Contextual Help can be selected:

img_103

This will display the help of the currently selected identifier:

img_104

img_105

Exiting JupyterLab

To close JupyterLab, close all tabs and then the tab in the browser itself:

img_106

The visual elements are now closed but the server ran in the terminal still displays:

img_107

Press Ctrl+c to cancel the currently running operation in the terminal:

img_108

img_109

Jupyter Acronym

Jupyter is an acronym for Julia, Python et R and as the name suggests supports the three programming languages Julia, Python and R.

Although conda is strongly associated with Python it is actually a general purpose data science package manager and supports the R programming language.

Currently there is limited support for the Julia programming language supporting only Linux and Mac. The conda-forge package is a significantly older version of Julia.

R Kernel

If the jupyter-env is activated, the following R packages can be installed:

conda install r-irkernel jupyter-lsp-r r-tidyverse r-ggthemes r-palmerpenguins r-writexl 

img_110

Input y in order to proceed:

img_111

Now when the jupyter-lab binary is launched:

jupyter-lab

img_112

It has options for the R programming language:

img_113

Julia Kernel

conda has limited support for Julia. It is recommended to download the latest binary from Julia:

img_114

This can be extracted:

img_115

Giving a Julia subfolder:

img_116

Which can be copied to Home:

img_117

Notice it has its own bin and lib folder:

img_118

Installing the conda-forge package (not recommended at present):

conda activate jupyter-env
conda install julia

Will integrate these folders with the existing folders in the conda-forge environment jupyter-env. Checks need to be made to avoid naming conflicts, which is why the conda-forge package is out of date.

If blank space in the bin folder is right clicked and opened in the terminal:

img_119

The Julia binary can be started by using:

./julia

where ./ is an instruction to look for the binary in the currently selected directory:

img_120

This can be exited using:

exit()

img_121

img_122

In order to use Julia with JupyterLab, its binary folder needs to be added to the path. This can be done by modifying the .bashrc file. This is a hidden file withing the Home directory ~:

img_123

Open this file in the text editor:

img_124

Notice that a code block that has been added by Spyder to preferentially recognise the binary spyder as the standalone installer.

Below this is the conda initialisation which means a search for a binary will preferentially be searched in the activated conda-forge environment and then fallback to (base) and then fallback to the system binaries.

  • conda for example is only installed in (base) so a search for conda will search the currently selected environment jupyter-env, not find the binary conda and therefore use conda in the base environment.

  • The .bashrc preferences the standalone spyder and this version of spyder will be preferenced over a spyder binary installed in an activated conda environment.

img_125

The following block is added to the .bashrc file:

# >>> Added by Julia >>>
export PATH="$PATH:/path/to.<Julia directory>/bin
# <<< Added by Julia <<<

img_126

Where in this case /path/to.<Julia directory> is /home/philip/julia-1.11.2/bin

img_127

Now when the terminal is refreshed (closed and reopened). The binary julia is recognised:

julia

img_128

The Julia Kernel package needs to be added:

using Pkg
Pkg.add("IJulia")
exit()

img_129

img_130

Now when the jupter-env is activated and the jupyter-lab binary launched:

conda activate jupyter-env
jupyter-lab

img_131

It has options for the Julia programming language:

img_132

Return to Python Tutorials