{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Jupyter Notebooks " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Acknowledgements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The material in this tutorial is specific to PYNQ. Wherever possible, however, it re-uses generic documentation describing Jupyter notebooks. In particular, we have re-used content from the following example notebooks:\n", "\n", "1. What is the Jupyter Notebook?\n", "1. Notebook Basics\n", "1. Running Code\n", "1. Markdown Cells\n", "\n", "The original notebooks and further example notebooks are available at [Jupyter documentation](http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/examples_index.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are reading this documentation from the webpage, you should note that the webpage is a static html version of the notebook from which it was generated. If the PYNQ platform is available, you can open this notebook from the getting_started folder in the PYNQ Jupyter landing page. \n", "\n", "The Jupyter Notebook is an **interactive computing environment** that enables users to author notebook documents that include:\n", "\n", "* Live code\n", "* Interactive widgets\n", "* Plots\n", "* Narrative text\n", "* Equations\n", "* Images\n", "* Video\n", "\n", "These documents provide a **complete and self-contained record of a computation** that can be converted to various formats and shared with others electronically, using version control systems (like git/[GitHub](http://github.com)) or [nbviewer.jupyter.org](http://nbviewer.jupyter.org)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Components" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Jupyter Notebook combines three components:\n", "\n", "* **The notebook web application**: An interactive web application for writing and running code interactively and authoring notebook documents.\n", "* **Kernels**: Separate processes started by the notebook web application that runs users' code in a given language and returns output back to the notebook web application. The kernel also handles things like computations for interactive widgets, tab completion and introspection. \n", "* **Notebook documents**: Self-contained documents that contain a representation of all content in the notebook web application, including inputs and outputs of the computations, narrative text, equations, images, and rich media representations of objects. Each notebook document has its own kernel." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Notebook web application" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The notebook web application enables users to:\n", "\n", "* **Edit code in the browser**, with automatic syntax highlighting, indentation, and tab completion/introspection.\n", "* **Run code from the browser**, with the results of computations attached to the code which generated them.\n", "* See the results of computations with **rich media representations**, such as HTML, LaTeX, PNG, SVG, PDF, etc.\n", "* Create and use **interactive JavaScript widgets**, which bind interactive user interface controls and visualizations to reactive kernel side computations.\n", "* Author **narrative text** using the [Markdown](https://daringfireball.net/projects/markdown/) markup language.\n", "* Build **hierarchical documents** that are organized into sections with different levels of headings.\n", "* Include mathematical equations using **LaTeX syntax in Markdown**, which are rendered in-browser by [MathJax](http://www.mathjax.org/)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Kernels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Notebook supports a range of different programming languages. For each notebook that a user opens, the web application starts a kernel that runs the code for that notebook. Each kernel is capable of running code in a single programming language. There are kernels available in the following languages:\n", "\n", "* Python https://github.com/ipython/ipython\n", "* Julia https://github.com/JuliaLang/IJulia.jl\n", "* R https://github.com/takluyver/IRkernel\n", "* Ruby https://github.com/minrk/iruby\n", "* Haskell https://github.com/gibiansky/IHaskell\n", "* Scala https://github.com/Bridgewater/scala-notebook\n", "* node.js https://gist.github.com/Carreau/4279371\n", "* Go https://github.com/takluyver/igo\n", "\n", "PYNQ is written in Python, which is the default kernel for Jupyter Notebook, and the only kernel installed for Jupyter Notebook in the PYNQ distribution. \n", "\n", "Kernels communicate with the notebook web application and web browser using a JSON over ZeroMQ/WebSockets message protocol that is described [here](http://ipython.org/ipython-doc/dev/development/messaging.html). Most users don't need to know about these details, but its important to understand that kernels run on Zynq, while the web browser serves up an interface to that kernel." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Notebook Documents" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notebook documents contain the **inputs and outputs** of an interactive session as well as **narrative text** that accompanies the code but is not meant for execution. **Rich output** generated by running code, including HTML, images, video, and plots, is embedded in the notebook, which makes it a complete and self-contained record of a computation. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you run the notebook web application on your computer, notebook documents are just **files** on your local filesystem with a **.ipynb** extension. This allows you to use familiar workflows for organizing your notebooks into folders and sharing them with others." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notebooks consist of a **linear sequence of cells**. There are four basic cell types:\n", "\n", "* **Code cells:** Input and output of live code that is run in the kernel\n", "* **Markdown cells:** Narrative text with embedded LaTeX equations\n", "* **Heading cells:** Deprecated. Headings are supported in Markdown cells\n", "* **Raw cells:** Unformatted text that is included, without modification, when notebooks are converted to different formats using nbconvert\n", "\n", "Internally, notebook documents are [JSON](http://en.wikipedia.org/wiki/JSON) data with binary values [base64](http://en.wikipedia.org/wiki/Base64) encoded. This allows them to be **read and manipulated programmatically** by any programming language. Because JSON is a text format, notebook documents are version control friendly.\n", "\n", "**Notebooks can be exported** to different static formats including HTML, reStructeredText, LaTeX, PDF, and slide shows ([reveal.js](http://lab.hakim.se/reveal-js/#)) using Jupyter's `nbconvert` utility. Some of documentation for Pynq, including this page, was written in a Notebook and converted to html for hosting on the project's documentation website. \n", "\n", "Furthermore, any notebook document available from a **public URL or on GitHub can be shared** via [nbviewer](http://nbviewer.ipython.org). This service loads the notebook document from the URL and renders it as a static web page. The resulting web page may thus be shared with others **without their needing to install the Jupyter Notebook**.\n", "\n", "GitHub also renders notebooks, so any Notebook added to GitHub can be viewed as intended. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Notebook dashboard" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Notebook server runs on the ARMĀ® processor of the board. You can open the notebook dashboard by navigating to [pynq:9090](http://pynq:9090) when your board is connected to the network. \n", "The dashboard serves as a home page for notebooks. Its main purpose is to display the notebooks and files in the current directory. For example, here is a screenshot of the dashboard page for an example directory:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The top of the notebook list displays clickable breadcrumbs of the current directory. By clicking on these breadcrumbs or on sub-directories in the notebook list, you can navigate your filesystem.\n", "\n", "To create a new notebook, click on the \"New\" button at the top of the list and select a kernel from the dropdown (as seen below)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notebooks and files can be uploaded to the current directory by dragging a notebook file onto the notebook list or by the \"click here\" text above the list.\n", "\n", "The notebook list shows green \"Running\" text and a green notebook icon next to running notebooks (as seen below). Notebooks remain running until you explicitly shut them down; closing the notebook's page is not sufficient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To shutdown, delete, duplicate, or rename a notebook check the checkbox next to it and an array of controls will appear at the top of the notebook list (as seen below). You can also use the same operations on directories and files when applicable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see all of your running notebooks along with their directories, click on the \"Running\" tab:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This view provides a convenient way to track notebooks that you start as you navigate the file system in a long running notebook server." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview of the Notebook UI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you create a new notebook or open an existing one, you will be taken to the notebook user interface (UI). This UI allows you to run code and author notebook documents interactively. The notebook UI has the following main areas:\n", "\n", "* Menu\n", "* Toolbar\n", "* Notebook area and cells\n", "\n", "The notebook has an interactive tour of these elements that can be started in the \"Help:User Interface Tour\" menu item." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modal editor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Jupyter Notebook has a modal user interface which means that the keyboard does different things depending on which mode the Notebook is in. There are two modes: edit mode and command mode." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Edit mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Edit mode is indicated by a green cell border and a prompt showing in the editor area:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When a cell is in edit mode, you can type into the cell, like a normal text editor." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
" ```javascript\n",
" console.log(\"Hello World\")\n",
" ```\n",
" \n",
"\n",
"Gives:\n",
"\n",
"```python\n",
"print \"Hello World\"\n",
"```\n",
"\n",
"```javascript\n",
"console.log(\"Hello World\")\n",
"```\n",
"\n",
"And a table like this: \n",
"\n",
" \n",
" ```\n",
"\n",
" | This | is |\n",
" |------|------|\n",
" | a | table| \n",
"\n",
" ```\n",
" \n",
"\n",
"A nice HTML Table:\n",
"\n",
"| This | is |\n",
"|------|------|\n",
"| a | table| \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### General HTML"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because Markdown is a superset of HTML you can even add things like HTML tables:\n",
"\n",
"| Header 1 | \n", "Header 2 | \n", "
|---|---|
| row 1, cell 1 | \n", "row 1, cell 2 | \n", "
| row 2, cell 1 | \n", "row 2, cell 2 | \n", "