JupyterLab is a browser based IDE.
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
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
Since Miniforge is used, the default channel will be the community channel conda-forge
:
Input y
in order to proceed:
The conda package manager is now up to date:
The terminal can be cleared by inputting:
clear
And environments can be listed by inputting:
conda env list
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
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:
Input y
in order to proceed:
To activate jupyterlab-env
input:
conda activate jupyter-env
The prefix will now show (jupyter-env)
instead of (base)
:
This means that instead of searching the bin folder in (base)
which is the miniforge3
folder:
The equivalent bin folder is used for the currently activated environment jupyter-env
.
The envs
folder contains the environments:
The jupyter-env
folder is currently activated:
This means the current bin and lib folders are preferentially searched for binaries and programs:
The binary of interest is jupyter-lab
:
And can be launched in the terminal by inputting:
jupyter-lab
JupyterLab is a browser based IDE. The server is ran in the terminal:
While the visual elements display in the browser:
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:
The Documents folder can be selected. JupyterLab can be used to open .py
files, these display in a script editor which applies syntax highlighting:
To the right hand side, a new launcher can be selected, this can be used to start a new terminal.
The terminal is equivalent to a new session of the Linux terminal and starts with (base)
, the base conda-forge
environment:
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
The plot in the script won't show unless the command:
plt.show()
is added.
The file menu can be used to save the script:
The plot now displays in an interactive window:
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:
Select IPython kernel:
A selection can be highlighted:
And run using, run selected code:
Now the libraries are imported, identifiers can be accessed from numpy by inputting:
np.↹
Data model identifiers can be accessed using:
np.__↹
The identifier __file__
is an instance and can be returned to an ipython cell output:
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(⇧+↹
A docstring can also be returned to an ipython cell:
np.arange?
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.
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:
A title (H1) can be added using:
# JupyterLab Test
When this cell is run, by selecting the run button or pressing ⇧
+↵
:
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:
A heading (H2) can be added using:
## Import Libraries
The libraries can be imported in a code cell using:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
Now that the libraries are imported, identifiers from them can be viewed:
np.↹
A new cell, will default to a Python cell, notice the markdown text has the wrong syntax highlighting:
A number of keyboard shortcuts are available, these can be viewed by selecting Help → Show Keyboard Shortcuts:
The keyboard shortcut Esc
+m
will toggle a cell to markdown. It can be run using ⇧
+↵
:
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:
Since x
and y
are ndarray
instances they can be inspected further by selecting the magnify icon:
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:
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
The TeX used in the figure, can be added to markdown:
'$2 \pi x \sin{x}$'
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:
And enable scrolling for output can be selected:
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
The ndarray
instances x
and y
can also be returned to a cell output:
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
Or displayed in the variable inspector:
If the DataFrame
is exported to a csv
file and xlsx
file:
Notice that the csv file can be opened in JupyterLab because the spreadsheet editor extension is installed:
This extension unfortunately doesn't support the xlsx
file:
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:
The notebook can be saved, using the file menu:
If the notebook is closed:
And reopened from the file explorer:
It displays in the same state as before however the IPython console associated with the notebook file has been exited:
The Kernel menu in the title bar can be used to Restart the Kernel and Clear All Cell Outputs:
Select Restart:
Now each cell can be run by selecting ⇧
+↵
:
To run a cell and insert a new one below, Alt
+↵
can be used:
There is also the option to Restart the Kernel and Run All Cells:
Select Restart:
Notice all the cells (with code) are now run from top to bottom:
The run and the kernel menu have other options:
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:
Notice that the spacing in In [3]
is now PEP8 compliant. If In [2]
is now selected and formatted:
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:
In Settings, the Settings editor can be examined:
The JupyterLab Code Formatter extension can be selected. To the right the JSON settings editor can be selected:
If the system defaults are copied over to the user preferences, blacks, string_normalization (to double quotes) can be set to False:
Now when Format Cell is used, the string quotations will remain unchanged:
Note cells can be formatted individually, or the format notebook button cna be selected to format all notebook cells:
Images can be inserted in a markdown. Usually they are stored in the same folder ad the notebook or markdown file or a subfolder:
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:
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:
The table of contents displays the markdown headings in the markdown file:
A new tab can be created to display a new launcher:
Contextual Help can be selected:
This will display the help of the currently selected identifier:
To close JupyterLab, close all tabs and then the tab in the browser itself:
The visual elements are now closed but the server ran in the terminal still displays:
Press Ctrl
+c
to cancel the currently running operation in the terminal:
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.
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
Input y
in order to proceed:
Now when the jupyter-lab
binary is launched:
jupyter-lab
It has options for the R programming language:
conda
has limited support for Julia. It is recommended to download the latest binary from Julia:
This can be extracted:
Giving a Julia subfolder:
Which can be copied to Home:
Notice it has its own bin and lib folder:
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:
The Julia binary can be started by using:
./julia
where ./
is an instruction to look for the binary in the currently selected directory:
This can be exited using:
exit()
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 ~
:
Open this file in the text editor:
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 forconda
will search the currently selected environmentjupyter-env
, not find the binaryconda
and therefore useconda
in thebase
environment. -
The
.bashrc
preferences the standalonespyder
and this version of spyder will be preferenced over a spyder binary installed in an activated conda environment.
The following block is added to the .bashrc
file:
# >>> Added by Julia >>>
export PATH="$PATH:/path/to.<Julia directory>/bin
# <<< Added by Julia <<<
Where in this case /path/to.<Julia directory>
is /home/philip/julia-1.11.2/bin
Now when the terminal is refreshed (closed and reopened). The binary julia
is recognised:
julia
The Julia Kernel package needs to be added:
using Pkg
Pkg.add("IJulia")
exit()
Now when the jupter-env
is activated and the jupyter-lab
binary launched:
conda activate jupyter-env
jupyter-lab
It has options for the Julia programming language: