{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Your First 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.\n", "* **Markdown** cells: when run, these cells are rendrered as markdown text (e.g. the current cell is a Markdown cell).\n", "* **Raw** cells: when run the text in these cells is simply left as is.\n", "\n", "**Markdown basics**\n", "Text in Markdown cells can be formated using the Markdown syntax. If you are unfamiliar with it, here is a (very) short primer to get you started:\n", "* Starting a line with 1-5 `#` characters creates headers. Lower number of `#` indicate higher levels of headings (e.g. `# Main header`, `## Second level of header`, `### Thrid level`, etc.).\n", "* Use `**double stars**` to **render text in bold**.\n", "* Use `*single stars*` or `_single underscores_` to _render text in italic_.\n", "* Use triple backticks \\`\\`\\` to render text as a code block (can be python or other languages), 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", "\n", "**Useful jupyter notebooks shortcuts**\n", "* Double-click a cell (or press Enter) to enter edit mode. \n", "* Press **Shift-Enter** to \"execute\" a cell: if the cell contains python code, the code will be executed. If the cell contains markdown text, the text will be rendered.\n", "\n", "\n", "During the course, feel free to add to the existing cells and create new cells. 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": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "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": [ "# Configuring Jupyter - Working Directory\n", "\n", "The easiest solution to starting Jupyter Notebook in a particular working directory:\n", "\n", "* open a terminal and change to the directory of interest\n", "* execute `jupyter notebook`\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 a 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", "# Configuring Jupyter - Default Browser\n", "\n", "If you prefer a specific browser for use with Jupyter Notebook, you can add the path to the browser executable/App in the config file as per above. \n", "\n", "The relevant line you are looking for is:\n", "\n", " c.NotebookApp.browser\n", " \n", "On OSX, you could use the following to switch to Chrome:\n", "\n", " c.NotebookApp.browser = u'open -a /Applications/Google\\ Chrome.app %s'\n", " \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", "## Managing environments\n", "\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", "## 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 the jupyter notebook.\n", "* Set it as a markdown cell.\n", "* Write your favorite quote in there and \"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 activate 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.5" } }, "nbformat": 4, "nbformat_minor": 4 }