summaryrefslogtreecommitdiff
path: root/book/module1/jupyter_lab_notebook.tex
blob: 5c06f5de7edf9534e2aabd5b8a4ff24decfbf2d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
\subsection{Introduction}\label{introduction}

Jupyter Notebooks are often used for data science and scientific
computing such as machine learning as the interactive design allow you
to experiment easily with your code. For our purpose, we will use
Notebooks as it's a useful tool to learn how to code as well as writing
reports.

\emph{Note on the difference between Notebook and Lab: Jupyter Notebook
offers a simplified, lightweight notebook authoring experience, where
as, JupyterLab offers a feature-rich, tabbed multi-notebook editing
environment with additional tools like a customizable interface layout
and system console}

\subsection{Setup and Installation}\label{setup-and-installation}

Jupyter Notebooks can be installed either from the Anaconda Navigator
home page or directly from your Conda terminal.

Terminal: \texttt{conda\ install\ conda-forge::jupyterlab}

\subsection{Notebook Basics}\label{notebook-basics}

\begin{itemize}
\tightlist
\item
  Creating a new notebook (\texttt{.ipynb})
\item
  Types of cells:

  \begin{itemize}
  \tightlist
  \item
    Code
  \item
    Markdown
  \item
    Raw
  \end{itemize}
\item
  Running a cell: \texttt{Shift\ +\ Enter}
\item
  Adding/removing cells
\item
  Restarting the kernel
\item
  Saving and auto-checkpoints
\end{itemize}

Jupyter Notebooks are files which allows you to combine \emph{Code} and
\emph{Markdown} cells in one single document. The code cells, allow you
to interactively run python code and print and plot data in your
document. If you wish to update or change data your code you can re-run
the cell to update the output. The markdown cells allows you to write
text, titles and insert images in your documentation using the markup
language \emph{Markdown}.

To start a new notebook select
\texttt{File\ \textgreater{}\ New\ \textgreater{}\ Notebook} or right
click the file browser and select \texttt{New\ notebook}, this will
prompt you to select a kernel (the Jupyter notebook ``engine''). For
now, just select the default Kernel 3. This will start a new fresh
kernel for us to use. Next, it's recommended to rename the file.

Now that we have a blank notebook we can start to add cells. Add a cell
and change the type to Markdown. Add a title with the hash symbol
(\texttt{\#}). As shown below.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{\# Title here}
\end{Highlighting}
\end{Shaded}

Press \texttt{Shift\ +\ Enter} to run the cell. You just entered created
your first markdown cell. Now let's do the same but instead select code
as the cell type, we're going to add some python code to the document.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x }\OperatorTok{=} \DecValTok{4}
\NormalTok{y }\OperatorTok{=} \DecValTok{3}

\NormalTok{x}\OperatorTok{**}\DecValTok{2}\OperatorTok{+}\DecValTok{2}\OperatorTok{*}\NormalTok{y}
\end{Highlighting}
\end{Shaded}

Again, run the cell and see what happens. You should've gotten an output
of \texttt{22}. You can now use the notebook as a calculator, but there
is so much more we can do.

The order of running code matters. Think of the code cells as code
snippets. Every time you run a cell variable will be updated. This means
that the current state of all variables, functions, and imports depends
on the history of what cells have been executed and in what order. In
other words, if you run a later cell before running an earlier one that
defines a variable or function it needs, you will get an error. If you
change a variable in one cell and rerun it, that new value immediately
affects the results of any cells that use that variable afterward ---
but not any previously run results unless you rerun them too. Variables
and imports persist in memory between cells, but only based on the
current session state --- if you restart the kernel, you lose all
previous definitions unless you re-run the necessary cells.

Because of this, it's best practice to; Run cells in order, restart the
kernel and run all cells
(\texttt{Kernel\ -\textgreater{}\ Restart\ \&\ Run\ All}) to make sure
everything works cleanly and predictably and lastly, initialize
important variables or imports in early cells, so they are always
defined before they are needed.

\subsection{Writing and Running Code}\label{writing-and-running-code}

\begin{itemize}
\tightlist
\item
  Python syntax: - \texttt{print("Hello,\ world!")} - Variables and
  functions - Loops and conditionals
\item
  Importing libraries: - \texttt{import\ numpy\ as\ np} -
  \texttt{import\ pandas\ as\ pd} -
  \texttt{import\ matplotlib.pyplot\ as\ plt}
\end{itemize}

\subsection{Using Markdown}\label{using-markdown}

\begin{itemize}
\tightlist
\item
  Headers: \texttt{\#}, \texttt{\#\#}, \texttt{\#\#\#}
\item
  Bullet lists: \texttt{-\ item}
\item
  Numbered lists: \texttt{1.\ item}
\item
  Emphasis: \emph{italic}, \textbf{bold}, \texttt{monospace}
\item
  LaTeX equations:

  \begin{itemize}
  \tightlist
  \item
    Inline: \texttt{\$E\ =\ mc\^{}2\$}
  \item
    Block:
    \texttt{\$\$\textbackslash{}int\_0\^{}\textbackslash{}infty\ e\^{}\{-x\^{}2\}\ dx\ =\ \textbackslash{}frac\{\textbackslash{}sqrt\{\textbackslash{}pi\}\}\{2\}\$\$}
  \end{itemize}
\item
  Embedding links and images
\end{itemize}

\subsection{Interactive Widgets
(optional)}\label{interactive-widgets-optional}

Install \texttt{ipywidgets} from your package manager

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{import}\NormalTok{ ipywidgets }\ImportTok{as}\NormalTok{ widgets}
\NormalTok{widgets.IntSlider()}
\end{Highlighting}
\end{Shaded}

Example using interact:

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{from}\NormalTok{ ipywidgets }\ImportTok{import}\NormalTok{ interact}
\NormalTok{interact(}\KeywordTok{lambda}\NormalTok{ x: x}\OperatorTok{**}\DecValTok{2}\NormalTok{, x}\OperatorTok{=}\DecValTok{5}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\subsection{Exporting and Sharing}\label{exporting-and-sharing}

By default, jupyter auto-saves your notebooks as you work.

\begin{itemize}
\tightlist
\item
  File \textgreater{} Download As:

  \begin{itemize}
  \tightlist
  \item
    Notebook (\texttt{.ipynb})
  \item
    HTML
  \item
    PDF (requires LaTeX)
  \item
    Markdown
  \end{itemize}
\end{itemize}