In [1]:
# By convention, import all modules at the top of the notebook so they don't get in the way later
# Numeric and symbolic libraries
import numpy as np
import sympy as sp
import pandas as pd
# Plotting libraries
import matplotlib.pyplot as plt
import bokeh.plotting as blt
# Needed only in Jupyter to render properly in-notebook
%matplotlib inline
blt.output_notebook()
A Jupyter notebook is organized into "cells" that execute independently. Double-clicking in a cell places it in "edit" mode. Shift-enter executes the cell. Other key comamnd help is available in the Help menu at the top of the notebook.
Each cell can be marked as a Markdown cell or a Code cell. Code cells execute python code directly by default, though there are number of back-ends for different languages that are becoming available. One should organize the cells in a logical way, so that each cell is a self-contained logical chunk of either text or code.
Markdown is a simplified markup language for html, which focuses on content-oriented readability. The main header of the notebook should be prepended with "#" to indicate an h1 header environment. Sections should be prepended with "##" to indicate an h2 header environment. Subsections should be prepended with "###" etc. Text with no prepended symbol renders as a p (paragraph) environment. Make sure you put blank lines between different Markdown environments so the parser renders it properly.
Markdown cells can also render $\LaTeX$ math environments by enclosing the code in dollar-signs: \$code here\$. For example, $\sum_{n=1}^\infty \frac{x^n}{n!} = \exp(x)$. If you desire a separated equation that is centered on the page, this is accomplished by doubling the dollar-signs: \$\$code here\$\$. For example, $$\int_a^b \! \frac{\mathrm{d}f(x)}{\mathrm{d}x}\,\mathrm{d}x \equiv f(b) - f(a)$$
When preparing a Jupyter notebook, treat it like a professional report. It should be a well-formatted, aesthetic-looking document that describes exactly what you are doing and why. Large blocks of code should be imported as modules, rather than copy-pasted whole-sale into the notebook. Any code that appears in the notebook should be short, sweet, and to the point. As an example, here is a demonstration of the plotting package Matplotlib, which is designed to be as similar as possible to the way MATLAB plots:
In [2]:
# Plot a sum of sines using Matplotlib
x = np.linspace(0,10,100)
f = 2 * np.sin(2*x) + 3 * np.cos(x)
fig = plt.figure(1)
plt.plot(x, f)
plt.xlabel("x")
plt.ylabel("f(x)")
plt.show(fig)
Code block in notebooks should not be much longer than the above. Ideally, they should be single function calls. Longer code blocks should be put into modules with extension .py and then imported at the beginning of the notebook. The notebook itself should focus on results, not on reading long blocks of code.
For another example using the interactive plotting package Bokeh instead of Matplotlib:
In [3]:
# Grab the current AAPL stock data using the data-processing package pandas, then plot it with Bokeh
AAPL = pd.read_csv(
"http://ichart.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000&d=0&e=1&f=2010",
parse_dates=['Date']
)
p = blt.figure(width=800, height=250, title="AAPL Ticker", x_axis_type="datetime")
p.xaxis.axis_label = "Date"
p.yaxis.axis_label = "Stock value"
p.line(AAPL['Date'], AAPL['Close'], color='navy', alpha=0.5)
blt.show(p)
To include web images, the format is as follows (note the lack of quotes around the image filename):
In the File menu, you can Download your notebook as a number of different formats if desired, including pdf via $\LaTeX$. A notebook that is properly formatted with Markdown headings and paragraphs should generally produce a reasonably good looking pdf document. However, interactive content like Bokeh above can cause problems, so think about this during your notebook composition.