TITLE: Special DocOnce features for Jupyter notebooks
AUTHOR: Hans Petter Langtangen at Simula & University of Oslo
DATE: today

TOC: on

DocOnce enables turning book chapters, manuals, research papers, and in fact
any type of document into Jupyter Notebooks (formerly knowns as IPython
Notebooks). This note outlines some special features that one should
be aware of and that can be used to tune the notebook typesetting.

======= Interactive sessions =======

By default, interactive Python sessions in `!bc pyshell` and `!bc ipy`
environments are (for the `ipynb` format)
split such that the output is removed and each
input part is a separate cell. This means that when executing
all the cells, one recreates the entire interactive session
with all the output. Below is an example.

===== Solving the world's simplest differential equation =====
label{ipynb:de:simplest}

Let us explore SymPy to solve

!bt
\[ y' = y,\quad y(0)=y_0 = 2\thinspace . \]
!et


!bc pyshell
>>> from sympy import *
>>> init_printing(use_latex=True)  # find the best method to print
>>> t = symbols('t', real=True, positive=True)
>>> y = symbols('y', cls=Function)
>>> # Solve differential equation using dsolve
>>> eq = diff(y(t), t) - y(t)
>>> print eq
-y(t) + Derivative(y(t), t)
>>> sol = dsolve(eq)
>>> print sol
y(t) == C1*exp(t)
>>> y = sol.rhs          # grab right-hand side of equation
>>> # Determine integration constant C1 from initial condition
>>> C1 = symbols('C1')
>>> y0 = 2
>>> eq = y.subs(t, 0) - y0  # equation for initial condition
>>> print eq
C1 - 2
>>> sol = solve(eq, C1)     # solve wrt C1
>>> print sol
[2]
>>> y = y.subs(C1, sol[0])  # insert C1=2 in solution
>>> print y
2*exp(t)
>>> print latex(y)
2e^{t}
!ec

The DocOnce input syntax of the first part of the above session looks like
this

!bc do
|bc pyshell
>>> from sympy import *
>>> t = symbols('t', real=True, positive=True)
>>> y = symbols('y', cls=Function)
>>> # Solve differential equation using dsolve
>>> eq = diff(y(t), t) - y(t)
>>> print eq
-y(t) + Derivative(y(t), t)
>>> sol = dsolve(eq)
>>> print sol
y(t) == C1*exp(t)
...
...
|ec
!ec
That is, the interactive session looks exactly as it does in the terminal
window with the primitive Python shell.

We can, alternatively, use IPython syntax:

!bc do
|bc ipy
In [1]: from sympy import *
In [2]: t = symbols('t', real=True, positive=True)
In [3]: y = symbols('y', cls=Function)
In [4]: # Solve differential equation using dsolve
In [5]: eq = diff(y(t), t) - y(t)
In [6]: print eq
Out[6]: -y(t) + Derivative(y(t), t)
In [7]: sol = dsolve(eq)
In [8]: print sol
Out[8]: y(t) == C1*exp(t)
|ec
!ec
This last `ipy` environment results in exactly the same interactive session
in all formats except `ipynb` where the output is removed and the
input is split over *two* cells. In format _${FORMAT}_ the above
block is rendered as

!bc ipy
In [1]: from sympy import *
In [2]: t = symbols('t', real=True, positive=True)
In [3]: y = symbols('y', cls=Function)
In [4]: # Solve differential equation using dsolve
In [5]: eq = diff(y(t), t) - y(t)
In [6]: print eq
Out[6]: -y(t) + Derivative(y(t), t)
In [7]: sol = dsolve(eq)
In [8]: print sol
Out[8]: y(t) == C1*exp(t)
!ec

There is an option `--ipynb_split_pyshell=off` that can be given to
`doconce format ipynb` when compiling documents and that turns off
the behaviour that interactive sessions are split into multiple
cells. The result is then one single cell, and if we have
``printing'' as in `>>> eq`, there will be no output, except from
the last one, in the output field in the notebook. This is usually
not the behavior you want.

% if EXTRA:
===== Another example on interactive session =====

Next is an example on solving the diffusion equation

!bt
\[
\frac{\partial u}{\partial t} =
\alpha\frac{\partial^2 u}{\partial x^2}, \quad  x\in (0,L),\ t\in (0,T],
\]
!et
in $[0,L]$
with oscillatory boundary condition $u(0,t)=U_0 + a\sin(\omega t)$,
$\partial u(L,t)/\partial x = 0 $, and initial condition $u(x,0)=U_0$.
We have an ansatz for the solution and use SymPy to do the boring
work:

!bc pyshell
>>> from sympy import *
>>> x, t = symbols('x t', real=True)
>>> k, a, A, w, U_0, b = symbols('k a A w U_0 b', positive=True, real=True)
>>>
>>> # Anstaz: A and omega specified (BC U_0 + A*sin(w*t)), k and b free prms
>>> u = U_0 - A*exp(-b*x)*sin(k*x - w*t)
>>>
>>> def pde(u, x, t):
...     return diff(u, t) - a*diff(u, x, x)
...
>>> R = pde(u, x, t)
>>> R = simplify(R)
>>> print 'R:', R
R: A*(-a*(-b**2*sin(k*x - t*w) + 2*b*k*cos(k*x - t*w) + k**2*sin(k*x - t*w)) + w*cos(k*x - t*w))*exp(-b*x)
>>> # Determine k and b from the coefficients of sin and cos
>>> eq_cos = R.subs(cos(k*x - w*t), 1).subs(sin(k*x - w*t), 0)
>>> eq_sin = R.subs(sin(k*x - w*t), 1).subs(cos(k*x - w*t), 0)
>>> eq_cos = simplify(eq_cos)
>>> eq_sin = simplify(eq_sin)
>>> print eq_cos
A*(-2*a*b*k + w)*exp(-b*x)
>>> print eq_sin
A*a*(b**2 - k**2)*exp(-b*x)
>>> sol = solve([eq_cos, eq_sin], [k, b])
>>> sol = [simplify(s) for s in sol]
>>> print sol
[(sqrt(2)*sqrt(w)/(2*sqrt(a)), sqrt(2)*sqrt(w)/(2*sqrt(a)))]
>>>
!ec
% endif

===== Showing an interactive session as pure text =====

Sometimes one wants to show an interactive session exactly as it looks like,
with the input and the output.
This can be done in the notebook by using the `pyshell-t`
or `ipy-t` environments (`-t` for *plain text* display).
With this DocOnce input,

!bc do
|bc pyshell-t
>>> a = 1
>>> b = 2
>>> a + b
3
|ec
!ec
we get the plain text

!bc pyshell-t
>>> a = 1
>>> b = 2
>>> a + b
3
!ec

The `-t` postfix can be used for any code that you want to display as
text rather than as an executable cell. For example, with `!bc pycod-t`
we create verbatim text and not a cell:

!bc pycod-t
import MySpecialModule as m
print m.main()
!ec


======= Ordinary code blocks =======

For ordinary book and manual writing, interactive sessions are used
when the results of statements are important. Otherwise one applies
standard code blocks. A standard code block can for example be

!bc do
|bc pycod
x = np.linspace(0, 4*np.pi, 501)
y = np.exp(-0.5*x)*np.sin(np.pi*x)
plt.plot(x, y)
|ec
The result of this code segment appears in Figure ref{myfig}.

FIGURE: [fig/myfig, width=500 frac=0.8] Plot. label{myfig}
!ec
This snippet turns out fine in all formats, except the notebook.
The problem with notebooks is two-fold:

 o The snippet does not run without import of `numpy` and `matplotlib`.
 o The snippet results in a plot automatically, and with the figure
   in addittion, we get two plots.

The remedy for problem 1 is to use *hidden code blocks*, notified as
`!bc pyhid` for Python code and `!bc Xhid` in general for language `X`:

!bc do
|bc pyhid
import numpy as np
import matplotlib.pyplot as plt
|ec
!ec
Such code blocks are invisible in all formats except for `ipynb`.
Books, manuals, and research papers will very often contain code
snippets that do not run without (extensive) extra code. This exatra
code must be provided in hidden code blocks for successful conversion
to notebooks. If these cells take too much attention (that is probably
why they were left out of the text...), one can insert some comments
to explain that fact.

Problem 2 is solved using one of the preprocessors and an if test on
the format, e.g., with Preprocess:

!bc do
# #if FORMAT != 'ipynb'
The result of this code segment appears in Figure ref{myfig}.

FIGURE: [fig/myfig, width=500 frac=0.8] Plot. label{myfig}
# #endif
!ec

Now we can write the complete code segment with a preceding hidden
block for import and the if test. Below is the rendering of
this in the format _${FORMAT}_.

# Note: since Preprocess is used in verbatim block above, we must compile
# this document with --no_preprocess and therefore use Mako if test
# to test on the format

!bc pyhid
import numpy as np
import matplotlib.pyplot as plt
!ec

!bc pycod
x = np.linspace(0, 4*np.pi, 501)
y = np.exp(-0.5*x)*np.sin(np.pi*x)
plt.plot(x, y)
!ec
% if FORMAT != 'ipynb':
The result of this code segment appears in Figure ref{myfig}.

FIGURE: [https://raw.githubusercontent.com/hplgit/doconce/master/doc/src/ipynb/fig/sinplot, width=400 frac=0.6] Plot. label{myfig}
% else:
Note that `%matplotlib inline` is automatically inserted before the
first import of `matplotlib` in a DocOnce-generated notebook such
that all plots are inlined.
% endif

(Hidden code blocks are also relevant for RunestoneInteractive
books, which are made from Sphinx output.)

======= Other special options for notebooks =======

===== Figures and movies =====

Figures and movies can be implemented in several ways in notebooks, depending
on the value of the options `--ipynb_figure=` and `ipynb_movie=`. For
the former we have the values

 * `md`: plain Markdown syntax for a figure, with no possibility to adjust
   the size (default)
 * `imgtag`: `<img ...>` tag in HTML taking the specified width into account
 * `Image`: Python notebook cell with `Image` object

Below is an example
% if FORMAT != 'ipynb':
rendered in format _${FORMAT}_.
% else:
with `--ipynb_figure=${ipynb_figure}`.
% endif

FIGURE: [https://raw.githubusercontent.com/hplgit/doconce/master/doc/src/manual/fig/wave1D.png, width=400]


For the movies we have the values

 * `md`: raw HTML code with `iframe` tag - not relevant for the notebook
 * `HTML`: raw HTML code with `iframe` tag
    embedded in the `HTML` object from the notebook (default)
 * `HTML-YouTube`: as `HTML` but use an `IPython.display.YouTubeVideo`
   object to display YouTube videos
 * `ipynb`: use `IPython.display.YouTubeVideo` object for YouTube videos,
   and use an `HTML` object with `video` tag for local movies

Below is an example
% if FORMAT == 'ipynb':
with `--ipynb_movie=${ipynb_movie}`.
Execute the cell to create the YouTube video object.
% else:
rendered in format _${FORMAT}_.
% endif

MOVIE: [http://youtu.be/PtJrPEIHNJw, width=640 height=480]


===== Admonitions =====

Markdown has no support for admonitions while DocOnce has extensive
support. Some methods for simulating admonitions in notebooks have
therefore been implemented. These are specified by the `--ipynb_admon=`
command-line option.

 * `quote`: typeset admon as Markdown quote (special font and gray vertical bar on
   the left)
 * `paragraph`: typeset admon as a plain paragraph with a heading if any (default)
 * `hrule`: use a horozontal rule to surround the heading and the text

Note that quotes in `!bc quote` environments
are always typeset as Markdown quotes.

Here are two examples
% if FORMAT == 'ipynb':
typeset with `--ipynb_admon=${ipynb_admon}`.
% else:
rendered in format _${FORMAT}_ using the default admon typesetting.
% endif

!bwarning Notebooks have limited support for typesetting admons
Admon environments must be simulated using Markdown quote environment,
a plain paragraph, or decorations with HTML `<hr>` hrules.
!ewarning

!bnotice Splitting documents
Formats like `html` and `sphinx` support splitting documents into multiple
web pages. The `!split` specification has no support in notebooks. This
means that long documents *must* be long notebooks.
!enotice

======= Equation references =======

Markdown (and thereby the Jupyter Notebook)
does not support references to equations.

!bt
\begin{align}
a &= 1,
label{eq1}\\
b & =2
label{eq2}
\end{align}
!et
Can we refer to (ref{eq1}) and (ref{eq2}) in format `ipynb`?
Yes, in DocOnce-extended `ipynb` format, but not when writing
notebooks interactively in the browser and using Markdown with LaTeX.

======= Translating notebooks to DocOnce =======

===== The `ipynb2doconce` tool =====

There is a program `doconce ipynb2doconce` for translating a notebook
file to DocOnce format:

!bc sys
Terminal> doconce ipynb2doconce notebook.ipynb
!ec
The output file `notebook.do.txt` should be an ordinary DocOnce file.
With an optional argument `--cell_delimiter`, comment lines a la `# ---------- code cell` act in the as delimiter between the *markdown* and *code* cells in the
`.do.txt` file.

===== Special comment annotations in the `.ipynb` file =====

In order to translate a DocOnce document to the Jupyter Notebook format
and back again, some information needs to be coded as comments
in the notebook and such that it can be brought back to DocOnce syntax.
For example, index workds (`idx`) and labels are coded in comments, so
are the original DocOnce figure and movie commands (which contain more
information than what the notebook normally can make use of).
Comments in the `.ipynb` file starting with `dom:` (DocOnce Metadata)
contain coded information that can be translated back to DocOnce (see
below for examples).

It is possible to write ordinary DocOnce documents with index words, labels,
cross references, etc.; translate the document to a notebook; edit
and expand the notebook interactively; and convert the notebook to
a DocOnce document again - with preservation of index words, labels, etc.,
despite the fact that the notebook does not know about such syntax
and construction. If you want to insert title, authors, date,
index words, labels,
figures, or movies in the notebook, use these types of constructions
while writing a markdown cell in the notebook:

!bc
<!-- dom:TITLE: Here goes the title -->
# Here goes the title
<!-- dom:AUTHOR: HPL Email:hpl@simula.no at Simula & UiO -->
<!-- Author: --> **HPL** (email: `hpl@simula.no`), Simula and UiO

<!-- dom:AUTHOR: Kaare Dump at Segfault, Cyberspace -->
<!-- Author: --> **Kaare Dump**, Segfault, Cyberspace

Date: **May 25, 2015**

# Section heading
<!-- dom:label{mysubsec} -->
<!-- dom:idx{headigs} -->
<!-- dom:idx{section} -->

Some text...

<!-- dom:FIGURE: [fig/myfig, width=500 frac=0.6] This is a caption. -->
<!-- begin figure -->
<p>This is a caption.</p>
<img src="fig/myfig.png" width=500>
<!-- end figure -->
!ec
It is important to respect whitespace exactly as shown in this example.
The `dom:` syntax must be the valid DocOnce syntax used when translating
the document back to DocOnce format, while the Markdown lines coming
after are the ones that can been seen in the present version of
the notebook (these are removed forever when converting the notebook to
DocOnce format).

!bwarning Mako code is lost!
Any Preprocess or Mako code one has in a DocOnce document is lost forever when
converting to a Jupyter Notebook.
!ewarning

As a specific example on generating a notebook and converting it back,
consider the "`example.do.txt`": "https://github.com/hplgit/doconce/blob/master/doc/src/ipynb/example.do.txt" file:

!bc sys
Terminal> cp example.do.txt tmp1.do.txt
Terminal> doconce format ipynb tmp1
Terminal> cp tmp1.ipynb tmp2.ipynb
Terminal> doconce ipynb2doconce tmp2.ipynb
Terminal> doconce diff tmp1.do.txt tmp2.do.txt
Termonal> google-chrome tmp_diff_tmp2.do.txt.html
!ec
