Skip to content

Latest commit

 

History

History
806 lines (470 loc) · 23.1 KB

File metadata and controls

806 lines (470 loc) · 23.1 KB

JupyterLab IDE Windows 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. MikTeX needs to be installed and added to the system path (which was also previously covered in the above).

Update conda

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

(conda) Python 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 ipympl 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, ipympl is used for the widget 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

Updating JupyterLab

There is a new release of JupyterLab, approximately every month. To keep it up to date. Open up the Terminal, the base environment will be updated, use the following command to update conda to the latest version:

conda update conda

Then activate jupyter-env and search for updates to all packages:

conda activate jupyter-env
conda update --all

If there are troubles updating the environment, it can be removed and recreated from scratch using:

conda env remove -n jupyter-env

You may need to manually delete the residual jupyter-env folder after removing this environment:

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

Launching JupyterLab

The base (conda) Python environment is selected:

img_009

The jupyter-env environment can be activated. To activate jupyterlab-env input:

conda activate jupyter-env

img_010

If the environments are listed:

conda env list

Notice that the (conda) Python environment jupyter-lab has a * indicating it is activated. The directory is also shown:

img_011

This can be exploed in Windows Explorer by going to:

%UserProfile%\Miniforge3

img_012

Then looking at the envs subfolder which contains the (conda) Python environments:

img_013

jupyter-env can be examined:

img_014

Because it is activated, this folder will be preferenced alongside its associated script folders when looking for an application in the Windows Terminal. If the scripts folder is examined:

img_015

Notice that there is a juptyer-lab.exe. This can be pinned to the Start Menu:

img_016

More typically JupyterLab is opened in the terminal by inputting:

jupyter-lab

Note the .exe is typically dropped:

img_017

JupyterLab is a browser based IDE. The server is ran in the terminal, which will remain busy while the JupyterLab application is running:

img_018

While the visual elements display in the browser:

img_019

img_020

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 is the home the directory %UserProfile%. The Documents folder can be selected:

img_021

And script.py can be opened:

img_022

Script Editor and Terminal

The terminal is equivalent to a new session of the Windows terminal, using Powershell:

img_023

Notice it starts with base, the default (conda) Python environment:

img_024

(conda) Python environment jupyter-env can be activated using:

conda activate jupyter-env

img_025

The python.exe in the currently activated (conda) Python environment jupyter-env can be used to run the script.py from the currently selected folder using:

python script.py

Note that the .exe extension for python.exe is not required while the first input argument being provided to the python.exe requires its file extension script.py:

img_026

The script runs and the terminal moves onto the next prompt but the plot does not show:

img_027

The following command:

plt.show()

can be added to script.py:

img_028

This script.py file can be saved using the file menu:

img_029

When this script is rerun, notice the plot displays in a separate plot window, this plot window using the QtAgg backend:

img_030

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_031

Select IPython kernel:

img_032

Code can be highlighted:

img_033

The selection can be run by selecting run → run selected code:

img_034

This code shows as the input to In [1] which has no Out[1]:

img_035

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

np.↹

img_036

Data model identifiers can be accessed using:

np.__

img_037

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

img_038

The print function can be used to process the escape characters in the Windows path. To view a docstring, an identifier should be input with open parenthesis and ⇧+↹ should be pressed:

print(⇧+

img_039

A docstring can also be printed to a cell output using:

print?

img_040

A longer docstring can be examined:

plt.plot(⇧+

img_041

And printed to an ipython cell output:

plt.plot?

img_042

All the code can be run by selecting run → run all code:

img_043

Notice that the plot is displayed using the inline backend where it is essentially nested as a static image in the ipython cell output:

img_044

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.

Open a new launcher and select new notebook:

img_045

Rename the notebook using the file explorer:

img_046

img_047

A notebook consists of cells which can either be Python code (the default) or markdown which is essentially used to document around the code:

img_048

Markdown can be used to create a title:

img_049

A heading can be added using:

# JupyterLab Test

img_050

When it is run using the run button, notice the formatted markdown displays showing the text as Heading 1:

img_051

Double clicking the heading returns to the raw markdown:

img_052

Inputting Esc+y toggles the cell to a code cell, notice the syntax highlighting changes as the # in Python code means a comment:

img_053

Esc+m toggles the cell back to a markdown cell, notice the syntax highlighting changes as the # means Heading 1:

img_054

The markdown cell can be run using the shortcut key ↹+↵ and another Heading 2 can be added:

## Import Libraries

and run using ↹+↵:

img_055

JupyterLab has a navigation pane which displays the table of contents in a notebook file:

img_056

To run a cell, the shortcut key ↹+↵ can be used. To run a cell and insert a blank one below it instead use Alt+↵:

img_057

The + button will also add a blank cell:

img_058

The shortcuts to run the selected cell, run selected cell and insert below and run selected cell and do not advance can be seen on the run menu:

img_059

The cell can be deleted by pressing the delete button or using the shortcut key Ctrl+d:

img_060

Code can be added to a cell:

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

img_061

Identifiers can be viewed by inputting a prefix e.g. np. followed by a :

img_062

A docstring can be examined by inputting a function with open parenthesis and pressing ⇧+↹, for example:

np.arange(⇧+↹

img_063

The following cells can be added:

## Set Style
sns.set_style('whitegrid')
## Create Data
x = np.array([0, 1, 2, 3, 4, 5])
y = 2 * np.pi * np.sin(x)

img_064

The variables x and y can be displayed on the Variable Inspector, right click blank space in the notebook and select open Variable Inspector:

img_065

The Variable Inspector opens in another tab which can be repositioned:

img_066

img_067

Some variables that are Collections can be further examined:

img_068

A plot can be added using:

## Plot Data
plt.plot(x, y)
plt.xlabel(R'$x$', usetex=True)
plt.ylabel(R'$2 \pi \sin(x)$', usetex=True)

img_069

By default the plot displays inline, in the cell output as a static image:

img_070

The backend can be changed to interactive python matplotlib:

## Plot Data Widget
%matplotlib ipympl
plt.plot(x, y)
plt.xlabel(R'$x$', usetex=True)
plt.ylabel(R'$2 \pi \sin(x)$', usetex=True)

which gives limited interactivity:

img_071

img_072

To use another backend, the kernel needs to be restarted. Select Kernel → Restart Kernel and all Cell Outputs of all Cells:

img_073

Select restart:

img_074

The backend can be changed to Qt, which displays the plot in a seperate interactive window which gives more interactivity:

## Plot Data Qt

```python
%matplotlib qtagg
plt.plot(x, y)
plt.xlabel(R'$x$', usetex=True)
plt.ylabel(R'$2 \pi \sin(x)$', usetex=True)

img_075

Note the TeX added to the plot can also be used in markdown cell:

$2 \pi \sin(x)$

img_076

Additional markdown such as a table can be added:

|num|number|
|---|---|
|1|one|
|2|two|
|3|three|

img_077

img_078

The \* is a special character in markdown and can be used to make bold and italic text as well as list of bullet points:

**bold**, *italic*, ***bold-italic***, ~~strike-through~~
* one
* two 
* three

It can be inserted as an escape character using:

\*

img_079

img_080

A docstring can be displayed in a cell output:

plt.plot?

img_081

Often scrolling outputs are enabled for such cells. Allowing the docstring to be seen without taking too much emphasis in the overall notebook. Right click the Cell Output and select Enable Scrolling for Outputs:

img_082

img_083

The file menu can be used to save the notebook:

img_084

When a notebook is closed and JupyterLab is closed and then JupyterLab is relaunched and the notebook opened, the notebook will display output from the previous session. However the ipython console associated with the notebook will have ended:

img_085

Select Kernel → Restart Kernel and all Cell Outputs of all Cells:

img_086

img_087

To run a cell one by one, the shortcut key ↹+↵ can be used:

img_088

Variables can be viewed in cell outputs:

x
y

img_089

A DataFrame can be constructed and viewed in a cell output using:

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

df

img_090

img_091

This can be saved to a file using:

img_092

df.to

img_093

And selecting the identifier to_csv and supplying a path to the file:

df.to_csv('df.csv')

img_094

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

img_095

Unfortunately this extension has no support for Excel files:

img_096

If the code in the following cell is formatted poorly such as:

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

img_097

The format notebook button can be selected:

img_098

Note the formatting is corrected however all the string quotations are changed to "" instead of Python's default '' because the black formatter is used by default.

This can be changed by selecting Settings → Settings Editor:

img_099

Then selecting the Jupyter CCode Formatter:

img_100

Often the JSON settings editor is more useful:

img_101

The System Defaults JSON can be copied to the User Preferences and string_normalization can be assigned to False:

img_102

Now formatting can be used without changing the single quotations to double quotations:

img_103

img_104

The following image can be examined:

img_105

A link to it can be inserted using:

[fig_1](./fig1.png)

img_106

Clicking on this link opens the file:

img_107

img_108

Putting a ! in front of the link will instead embed it:

![fig_1](./fig1.png)

img_109

img_110

Show contextual help opens a contextual help pane which updates every time an identifier is selected:

img_111

img_112

To close JupyterLab, the close button can be used on the browser tab:

img_113

Although the visual elements of JupyterLab are closed, the server is still running in the Windows Terminal. Press Ctrl+c to cancel tthe current operation:

img_114

A new prompt displays:

img_115

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 (conda) Python environment 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_116

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

img_117

Input y in order tp proceed:

img_118

When JupyterLab is launched using:

jupyter-lab

img_119

Notice that there is now an option for both a Python and R console:

img_120

Julia (Jupyter Kernel)

Unfortunately Julia isn't available on conda-forge for Windows and has to be installed using the Windows Store:

img_121

img_122

img_123

Julia will be added to the system path. Open the Windows Terminal and input:

julia

img_124

To install the Julia Kernel input:

using Pkg
Pkg.add("IJulia")

img_125

Julia can be exited using:

exit()

img_126

When JupyterLab is launched using:

jupyter-lab

img_127

The Julia, Python et R consoles are available:

img_128

Return to Python Tutorials