summaryrefslogtreecommitdiff
path: root/PVCM/cama/en/ma1 Jupyter.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'PVCM/cama/en/ma1 Jupyter.ipynb')
-rw-r--r--PVCM/cama/en/ma1 Jupyter.ipynb344
1 files changed, 344 insertions, 0 deletions
diff --git a/PVCM/cama/en/ma1 Jupyter.ipynb b/PVCM/cama/en/ma1 Jupyter.ipynb
new file mode 100644
index 0000000..01e5c96
--- /dev/null
+++ b/PVCM/cama/en/ma1 Jupyter.ipynb
@@ -0,0 +1,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": []
+ }
+ ]
+} \ No newline at end of file