A few basic reminders on working within a Jupyter Notebook

Jupyter Notebook Basics

Some notes to keep handy as a reminder of the basics in using a Jupyter notebook

What is a Jupyter notebook?

According to the Web App’s main page: The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, machine learning and much more.

In my own words: Application for creating and sharing documents that contain:

  • live code
  • equations
  • visualizations
  • explanatory text

Home page: http://jupyter.org/

Notebook tutorials

Handy write ups on specific skills or applications of Jupyter Notebooks

Notebook Users

  • students, readers, viewers, learners
    • read a digital book
    • interact with a “live” book
  • notebook developers
    • create notebooks for students, readers, …

Notebooks cells

Notebooks are composed of cells. These can be modified for myriad forms of content

  • Code cells
    • execute interactively on computer (Python, or many other languages)
    • memory across notebook (variable from one cell can be used in a later)
  • Markdown cells
    • documentation / “narrative” cells
      • Can be used to decorate and guide a reader through a notebook

All cells have the following basic structure:

{
  "cell_type" : "name",
  "metadata" : {},
  "source" : "single string or [list, of, strings]",
}

Markdown cells

markdown, as defined in GitHub-flavored markdown, and implemented in marked

{
  "cell_type" : "markdown",
  "metadata" : {},
  "source" : ["some *markdown*"],
}

Code cells

contain source code in the language of the document’s associated kernel, and a list of outputs associated with executing that code. They also have an execution_count, which must be an integer or null

{
  "cell_type" : "code",
  "execution_count": 1, # integer or null
  "metadata" : {
      "collapsed" : True, # whether the output of the cell is collapsed
      "autoscroll": False, # any of true, false or "auto"
  },
  "source" : ["some code"],
  "outputs": [{
      # list of output dicts (described below)
      "output_type": "stream",
      ...
  }],
}

Code cell outputs

code cell outputs correspond to messages produced as a result of executing the cell

stream output

{
  "output_type" : "stream",
  "name" : "stdout", # or stderr
  "text" : ["multiline stream text"],
}

the keys stream key was changed to name to match the stream message in nbformat: 4.0

display_data

Rich display outputs, as created by display_data messages, contain data keyed by mime-type

{
  "output_type" : "display_data",
  "data" : {
    "text/plain" : ["multiline text data"],
    "image/png": ["base64-encoded-png-data"],
    "application/json": {
      # JSON data is included as-is
      "json": "data",
    },
  },
  "metadata" : {
    "image/png": {
      "width": 640,
      "height": 480,
    },
  },
}

Following cells are “live” cells

print ("Hello Jupyter World!; You are helping me learn")
Hello Jupyter World!; You are helping me learn
(5+7)/4
3
import numpy as np
my_first_array = np.arange(11)
print (my_first_array)
[ 0  1  2  3  4  5  6  7  8  9 10]

Related

comments powered by Disqus