{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Jupyter Notebook crash course" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*We assume you are looking at this using Jupyter Notebook. If not, scroll to \"How to run a notebook\".*" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Jupyter Notebooks are *one* of many ways to interact with a Python interpreter.\n", "\n", "On the surface, they look like documents in a web browser." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Notebooks allow to mix *Markdown cells*, like this one..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# with code cells, like this one.\n", "x = 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Code cells can be executed by clicking them and pressing Shift+Return. Results will be displayed directly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x * 2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Editing cells\n", "\n", "Cells can be edited by clicking them and pressing Return.\n", "\n", "The marking will become green when editing.\n", "\n", "To stop editing, press Esc, or run the cell. Note that the marker will become blue again.\n", "\n", "When pressing Shift+Return in a Markdown cell, the text will be rendered.\n", "\n", "When editing a code cell, the Tab key triggers autocompletion, and Shift+Tab brings up the documentation for the object at the cursor." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Adding and removing cells\n", "\n", "When running the last cell in a notebook, Shift+Return will insert a new cell.\n", "\n", "To insert a cell *above* or *below* the current one, press *a* or _b_.\n", "\n", "To delete the current cell, press *dd* (i.e., twice *d*).\n", "\n", "To cut and paste, use *x* and _v_ (just the letter, without holding the ctrl key).\n", "\n", "If the new cell is of the wrong type, press *m* for markdown, or _y_ for code." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Going fancy\n", "\n", "Markdown cells support lots of formatting: **bold**, *italic*, ~~strikeout~~\n", "\n", "* Bullet\n", "* points\n", "\n", "\n", "1. numerated\n", "2. lists\n", "\n", "\n", "```python\n", "def python_code(a, b):\n", " # with syntax highlighting\n", " return 0\n", "```\n", "\n", "And look, here's a formula: $e^{i \\pi} + 1 = 0$\n", "\n", "---\n", "\n", "Doubleclick the cell to see how it's done. Check [the Jupyter Notebook documentation](https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Working%20With%20Markdown%20Cells.html) for more Markdown tricks." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "# Going fancy with code\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "x = 2*np.pi * np.arange(-2,2,0.01)\n", "y = np.sinc(x)\n", "\n", "plt.plot(x, y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Controlling the \"kernel\"\n", "\n", "Code cells are run by a Python interpreter (in the order that *you* run them). It will maintain state in between." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = x + 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = x * 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The interpreter (called \"kernel\" in this context) can be restarted from the \"Kernel\" menu on the top." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## How does this all work?\n", "\n", " - Jupyter notebook runs a web server on your machine that you access with your browser.\n", " - Code cells are passed to a Python interpreter when run. Results are passed on to the web client.\n", " - Jupyter notebooks are stored as JSON files that you can pass around. They will include all cell outputs that were visible when saving." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## How to run a notebook \n", "\n", "1. Make sure you have Jupyter Notebook installed (in the currently active environment). This is not part of this guide.\n", "2. Navigate to the path where your notebooks live (files with `.ipynb` extension), e.g.:\n", " - ```cd path-to-jupyter-notebook-you-wanna-run```\n", "3. Run Jupyter Notebook\n", " - ```jupyter notebook```\n", "\n", "The Jupyter Notebook web interface is now accessible via ```localhost:8888```. It should start automatically in a browser. You can also install `jupyterlab` and run ```jupyter lab``` for a more elaborate web interface." ] } ], "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.6.9" }, "livereveal": { "height": 1200, "width": 1920 } }, "nbformat": 4, "nbformat_minor": 4 }