{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Jupyter Notebook\n", "\n", "Jupyter Notebooks are made of a successions of **cells**, that contain either code or text/comments. The type of the cell can be set via the drop-down menu found at the top of the notebook (`Code`, `Markdown` or `Raw`), and determines what happens when the cell is **run**:\n", "* **Code** cells: the content of code cells is executed as python code (e.g. the cell below this one).\n", "* **Markdown** cells: when run, these cells are rendrered as markdown text (e.g. the current cell is a Markdown cell). These are cells that you will use to comment your code (e.g. this cell).\n", "* **Raw** cells: when run the text in these cells is simply left as is." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# This is a Code cell.\n", "# Short comments can be added to code cells by starting a line with a \"#\" character, \n", "# but in general comments for your notebook are best put in Markdown cells.\n", "greetings=\"Hello, world!\"\n", "print(greetings)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Command vs Edit mode\n", "In a Jupyter Notebook, a cell can be in either of 2 modes:\n", "* In **command mode**, a cell has a **blue border** and is in its run/rendered state (i.e. you see its content as code that is run, or as Markdown text that is rendered). In this mode, inputs from the keyboard do not affect the content of the cell, but are instead interpeted by Jupyter as shortcuts (see below for a list shortcuts).\n", "* In **edit mode**, a cell has a **green border**. In this mode, inputs from the keyboard interpreted as characters to be typed into the cell.\n", "\n", "\n", "### Markdown basics\n", "Text in Markdown cells can be formated using the standard Markdown syntax. If you are unfamiliar with it, here is a short primer to get you started:\n", "* Starting a line with 1 to 5 `#` characters creates headers. Lower number of `#` indicate higher levels of headings (e.g. `# Main header`, `## Second level of header`, `### Thrid level of header`, etc.).\n", "* Text quoted in `**double stars**` or `__double underscores__` is **rendered in bold**.\n", "* Text quoted in `*single stars*` or `_single underscores_` is _rendered in italic_.\n", "* Text quoted in single backticks is `rendered as inline code`.\n", "* Text quoted in triple backticks \\`\\`\\` is rendered as a code block (can be python or other languages), \n", " as illustated here. Note that the cell must be in edit mode for you to see the backticks.\n", " **important:** this type of code is _**not**_ executed when a Markdown cell is run.\n", "\n", "```python\n", "# This is rendered as a code block, but this code is not executed.\n", "greetings = \"Hello, world!\"\n", "```\n", "\n", "* Start a line with a single star `* ` + space, or dash `- ` + space, to create a bullet point \n", " list (just like this line).\n", "* Start a line with `1. `, `2. `, `3. `, etc. to create numbered lists. Example:\n", "\n", "```\n", "# This will render as a bullet point list:\n", "* first bullet in list.\n", "* second bullet.\n", "* third bullet.\n", "* ...\n", "\n", "# This will render as a numbered list:\n", "1. first item in list.\n", "2. second item.\n", "3. third item.\n", "```\n", "\n", "* Start a line with `> ` to display it in \"quote\" formating. This slightly indents the line and adds \n", " a grey bar to its left, as in this example:\n", " > Ignorance more frequently begets confidence than does knowledge. *Charles Darwin*\n", "\n", "\n", "### Useful jupyter notebooks shortcuts\n", "* **Double-click** a cell, or press **Enter**, to enter edit mode. \n", "* Press **Shift-Enter** or **Ctrl-Enter** to \"run\" a cell: if the cell contains python code, the code \n", " will be executed. If the cell contains markdown text, the text will be rendered. \n", " With **Shift-Enter**, after running the cell, the focus will move to the next cell below, while \n", " with **Ctrl-Enter** the focus stays on the same cell (this is e.g. useful if you quickly want to check \n", " your Markdown formating, then continue editing the same cell again simply by pressing **Enter**).\n", "* Press **Esc** (escape key) to exit edit mode without running the cell.\n", "\n", "Here are some useful shortcuts for when you are in **command mode** (all these actions can also be done using the **Edit menu** found at the top of the Jupyter notebook interface):\n", "* `m`: change cell to **Markdown** cell.\n", "* `y`: change cell to **code** cell.\n", "* up/down arrow: change focus to the cell above/below.\n", "* `a`: create new cell above the currently selected cell (**a** as in **a**bove)\n", "* `b`: insert new cell below the currently selected cell (**b** as in **b**elow)\n", "* `dd`: delete selected cell.\n", "* `z`: undo delete cell.\n", "* `h`: display the full list of shortcuts.\n", "\n", "\n", "### Ready !\n", "During the course, feel free to add to modify cells in the notebooks we provide, or add new ones. The explanations and subsequent discussion are not presented exahustively here. You should customize your notes as you wish, provided it results in a clear record of everything we worked on. \n", "\n", "You can always download this notebook again, but it's probably a good moment to **make a copy**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Configuring Jupyter\n", "\n", "### Select working directory\n", "\n", "The easiest solution to start Jupyter Notebook in a particular working directory is to:\n", "* open a terminal and change to the directory of interest.\n", "* execute `jupyter notebook`.\n", "\n", "You can also execute `jupyter notebook` anywhere, then manually browse to the diretory containing your Jupyter Notebook files (`.ipynb` extension).\n", "\n", "Alternatively you can create and use a config file. Open a shell or command prompt (cmd in Windows) and type:\n", " \n", " jupyter notebook --generate-config\n", " \n", "This will create a configuration file, which normally lives at: \n", "* Windows: `C:\\Users\\\\.jupyter\\jupyter_notebook_config`\n", "* UNIX (Linux, Mac): `~/.jupyter/jupyter_notebook_config.p`\n", "\n", "Find the following line (usually 179):\n", "\n", " #c.NotebookApp.notebook_dir = ''\n", "\n", "And change this to:\n", "\n", " c.NotebookApp.notebook_dir = ''\n", "\n", "Be sure to use forward slashes in your path.\n", "\n", "Yet another approach is to use the Python module called `OS` (more on modules later):\n", "```python\n", " import os\n", " print(\"current directory :\",os.getcwd())\n", " os.chdir('')\n", " print(\"new directory :\",os.getcwd())\n", "```\n", "\n", "\n", "### Set default browser\n", "If you prefer a specific browser for use with Jupyter Notebook, you can set the path to that browser's executable/App in the config file as per above. The variable to modify in the config file is `c.NotebookApp.browser`.\n", "* On Mac OSX e.g., you could use the following to set Chrome as default browser: \n", " `c.NotebookApp.browser = u'open -a /Applications/Google\\ Chrome.app %s'`.\n", "* Otherwise just set the path to the executable of your favourite browser." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conda Notes - envs and packages\n", "\n", "It should go without saying - *read the docs!* https://conda.io/docs/user-guide\n", "\n", "\n", "### Managing environments\n", "Creating an environment with a specific version of Python:\n", "\n", " conda create --name myenv python=2.7.11\n", " \n", "To avoid any potential conflicts between modules, it is good practice to build the environment with all the modules you will need at the same time. *eg:*\n", "\n", " conda create --name myenv python=3.4 numpy scipy matplotlib pandas scikit-learn\n", "\n", "Which environments are installed?\n", "\n", " conda info --envs\n", " \n", "Which packages are installed?\n", "\n", " conda list --name myenv\n", " \n", "Activating and deactivating an environment (Windows):\n", "\n", " # from the Anaconda Prompt\n", " activate myenv\n", " deactivate\n", " \n", "Activating and deactivating an environment (Unix-type):\n", "\n", " source activate myenv\n", " source deactivate\n", "\n", "\n", "### Installing modules/packages\n", "\n", "There are two package managers associated with Python - `conda` (bundled with Anaconda) and `pip`. Generally they play nicely together. The pseudo-consensus seems to be to use conda for Python-only packages. Some packages which require compilation (thus a clear picture of which other library dependencies are on your computer) are better installed with pip.\n", "\n", " conda install mypkg\n", " pip install mypkg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Exercises\n", "\n", "## exercise 0.1\n", "\n", "* Create a new cell in this Jupyter Notebook.\n", "* Set it as a markdown cell.\n", "* Add a title to your cell.\n", "* Write your favorite quote, possibly with some formating (e.g. some words in bold).\n", "* Add the authors' name in italic.\n", "* \"run the cell\".\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise 0.2\n", "\n", "* Create a new cell in the jupyter notebook.\n", "* Set it as a code cell.\n", "* Print \"Hello, world!\" to the screen (remember to run the cell to actually execute your code)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }