summaryrefslogtreecommitdiff
path: root/PVCM/cama/en/ma1 Jupyter.ipynb
blob: 01e5c96ab204d72477d13d2a0df92c1b214227b6 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "# Using Jupyter\n",
    "\n",
    "#### Cells\n",
    "\n",
    "A Jupyter sheet is made up of cells that you can guess if you click on the text. There are different\n",
    "cell types that you choose with the drop-down menu in the middle of the icons at the top of the page:\n",
    "\n",
    "* Python code (default)\n",
    "* rich text ([Markdown](https://en.wikipedia.org/wiki/Markdown))\n",
    "* plain text (useful for deactivating a code cell)\n",
    "\n",
    "You can add\n",
    "a cell with the + symbol at the top of the page, delete a cell with the scissors, move a cell with the arrows and edit it by double-clicking on it. We will see that we execute the cells to obtain the result of the written code. All this can be found on the menu\n",
    "`Cell`.\n",
    "\n",
    "#### Kernel\n",
    "\n",
    "Jupyter takes its name from 3 languages, namely Julia, Python and R. Today the list of languages ​​that can be\n",
    "to use in a Jupyter sheet is much longer but in all cases you need a kernel which makes the interface\n",
    "between Jupyter and the language. In the case of Python the core is iPython. We choose it when creating the sheet\n",
    "and we can interact with it using the `Kernel` menu.\n",
    "\n",
    "The other menus are more classic and do not require explanation (forget `Widget`).\n",
    "\n",
    "#### Note\n",
    "\n",
    "If you do not know Python or Jupyter, it is advisable to come back here after reading the first chapter </a>.\n",
    "\n",
    "## Cell manipulation\n",
    "\n",
    "#### Status and execution\n",
    "\n",
    "* A cell can have 3 states:\n",
    "     * not selected (no frame around the cell)\n",
    "     * selected (blue frame or blue bar on the left)\n",
    "     * selected and in edit mode (change of background color and sometimes green frame)\n",
    "* Clicking selects a cell (or even puts it directly into edit mode for calculation cells)\n",
    "* Double click selects and enters editing mode (on a text area, Markdown syntax appears)\n",
    "* <kbd>Enter</kbd> like clicking allows you to activate the edition when <kbd>Esc</kbd> allows you to close the edition mode.\n",
    "* To make Python recompute the result of a cell\n",
    "     * <kbd>Shift</kbd>+<kbd>Enter</kbd> does the calculation and moves to the next cell\n",
    "     * <kbd>Crtl</kbd>+<kbd>Enter</kbd> does the calculation and stays on the cell\n",
    "     * <kbd>Alt</kbd>+<kbd>Enter</kbd> does the calculation and then adds an empty cell.\n",
    "\n",
    "#### Display\n",
    "  \n",
    "  * `[out]` To hide the results, end the last line of the cell with `;`.\n",
    "  * `[In]` To hide a cell, use 'Hide input' from nbextessions (see config in the Edit menu)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(2)\n",
    "1+1;   # remove the ; to see the difference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "## Configuration\n",
    "\n",
    "Jupyter has many commands to manipulate your note sheet and of course it is possible to\n",
    "set keyboard shortcuts.\n",
    "\n",
    "To display the list of commands (the *command palette*) click on the small keyboard in the icon bar.\n",
    "With Chrome, you can display this command palette with <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>.\n",
    "\n",
    "To display and define your keyboard shortcuts you can go to the help menu or type `shortcut` in\n",
    "the command palette.\n",
    "\n",
    "For example, for my use I redefined the cell type change with\n",
    "\n",
    "* <kbd>Crtl</kbd>+<kbd>1</kbd> for Code mode\n",
    "* <kbd>Crtl</kbd>+<kbd>2</kbd> for Markdown mode\n",
    "* <kbd>Crtl</kbd>+<kbd>3</kbd> for Raw mode (uninterpreted data)\n",
    "\n",
    "This works when the cell is selected but not in edit mode (so blue frame or blue bar)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "#### Exercise\n",
    "\n",
    "Choose your shortcuts to switch from one mode to another then switch the cell below to Code mode and execute it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "print('$qsD!Me+nsodd= 1l lre3W$'[-2:2:-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "Cell manipulation shortcuts are good to know or redefine when you start using Jupyter seriously."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "## IPython\n",
    "\n",
    "When using Python in Jupyter, we rely on [IPython](https://ipython.org/) which enriches Python.\n",
    "\n",
    "This gives the following functionality:\n",
    "\n",
    "* Completion by pressing <kbd>Tab</kbd>\n",
    "* Help with `?` and `??` for even more help with objects (or even code)\n",
    "* [Shell access](https://ipython.readthedocs.io/en/stable/interactive/shell.html) with `!`\n",
    "* Magic commands"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### Completion and help\n",
    "\n",
    "`a`<kbd>Tab</kbd> will give a list including `abs`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "For the help examples, you must launch the cells to see the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abs?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "To close the help window click on the cross at the top right of the window or use <kbd>Esc</kbd>.?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mot = \"Coucou\"\n",
    "mot.split?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### Shell under IPython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vlib = '/var/lib/'\n",
    "res = !ls $vlib\n",
    "res = res.grep('lib')\n",
    "for f in res:\n",
    "    print(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(res))\n",
    "res??"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### Magic commands\n",
    "\n",
    "Magic commands improve the use of Python. They start with\n",
    "\n",
    " * `%` and apply to the line\n",
    " * `%%` and apply to the cell (when it makes sense)\n",
    "\n",
    "The first magic command to run is `%quickref` to get the list of iPython possibilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%quickref"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "Here is a selection of some magic commands:\n",
    "    \n",
    "* `%time` to get the time the line takes (`%%time` for the execution time of the cell).\n",
    "* `%timeit` is like `%time` but it restarts the line several times to get a more accurate estimate\n",
    "* `%prun` to run the line through the Python profiler and see where it's taking time\n",
    "* `%matplotlib inline` so that the graphs are displayed in the Jupyter page\n",
    "* `%load` to load a Python file into the cell (comments %load once executed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!echo 'print(\"coucou\")' > /tmp/prog.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load /tmp/prog.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.linalg as lin\n",
    "\n",
    "%prun lin.solve(np.random.random([3000,3000]), np.random.random(3000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "{{ PreviousNext('Introduction.ipynb','../lesson1 Python basics/00 - Premiers calculs.ipynb') }}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}