* DONE Exporting org-mode to Jupyter notebooks
CLOSED: [2017-01-21 Sat 19:51]
:PROPERTIES:
:categories: emacs,orgmode,python,jupyter
:date: 2017/01/21 19:51:23
:updated: 2017/01/25 08:50:18
:END:
#+BEGIN_EXPORT md
# A notebook from org-mode

#+END_EXPORT
I am going to use Jupyter notebooks to teach from this semester. I really dislike preparing notebooks though. A browser is a really poor editor, and I really dislike Markdown. Notebooks do not seem to have any real structure in them, e.g. the collapsible outline that I am used to in org-mode, so for long notebooks, it is difficult to get a sense for the structure. I am anticipating spending up to 80 hours preparing notebooks this semester, so today I worked out some code to export org-mode to an ipython notebook!
This will let me use the power tools I am accustomed to for the creation of IPython notebooks for my students, and perhaps others who do not use org-mode.
Jupyter notebooks are just json files, so all we need to do is generate it from an org document. The basic strategy was to build up a lisp data structure that represents the notebook and then just convert that data structure to json. I split the document up into sequential markdown and code cells, and then encode those in the format required for the notebook (json).
So, here is an example of what can be easily written in org-mode, posted to this blog, and exported to an IPython notebook, all from one org-document.
Check out the notebook: [[./exporting-orgmode-to-ipynb.ipynb]].
** Solve a nonlinear problem
Consider the equation $x^2 = 4$. Find a solution to it in Python using a nonlinear solver.
To do that, we need to define an objective function that will be equal to zero at the solution. Here is the function:
#+BEGIN_SRC ipython :session
def objective(x):
return x**2 - 4
#+END_SRC
#+RESULTS:
Next, we use fsolve with an initial guess. We get fsolve from [[https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve][scipy.optimize]].
#+BEGIN_SRC ipython :session
from scipy.optimize import fsolve
ans = fsolve(objective, 3)
print(ans)
#+END_SRC
#+RESULTS:
: [ 2.]
That should have been an obvious answer. The answer is in brackets because fsolve returns an array. In the next block we will unpack the solution into the answer using the comma operator. Also, we can see that using a different guess leads to a different answer. There are, of course, two answers: $x = \pm 2$
#+BEGIN_SRC ipython :session
ans, = fsolve(objective, -3)
print(ans)
#+END_SRC
#+RESULTS:
: -2.0
Now you see we get a float answer!
Here are some other ways to get a float:
#+BEGIN_SRC ipython :session
ans = fsolve(objective, -3)
print(float(ans))
print(ans[0])
#+END_SRC
#+RESULTS:
: -2.0000000000000084
: -2.0
It is worth noting from the first result that fsolve is iterative and stops when it reaches zero within a tolerance. That is why it is not exactly -2.
** Benefits of export to ipynb
1. I can use org-mode
2. And emacs
3. and ipynb for teaching.
The export supports org-markup: *bold*, /italic/, _underlined_, and +strike+.
We can use tables:
#+caption: A table of squares.
| x | y |
|---+----|
| 1 | 2 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
We can make plots.
#+BEGIN_SRC ipython :session
import numpy as np
t = np.linspace(0, 2 * np.pi)
x = np.cos(t)
y = np.sin(t)
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.axis('equal')
plt.xlabel('x')
plt.ylabel('y')
plt.savefig('circle.png')
#+END_SRC
#+RESULTS:
[[./circle.png]]
Even include HTML:
Pay special attention to the axis labels!
** Limitations
- Only supports iPython blocks
- Does not do inline images in results
- Will not support src-block variables
- Currently only supports vanilla output results
** Summary
The code that does this is here: [[./ox-ipynb.el]]. After I use it a while I will put it in scimax. There are some tricks in it to fix up some markdown export of latex fragments and links with no descriptions.
I just run this command in Emacs to get the notebook. Even it renders reasonably in the notebook.
#+BEGIN_SRC emacs-lisp
(export-ipynb-buffer)
#+END_SRC
Overall, this looks extremely promising to develop lecture notes and assignments in org-mode, but export them to Ipython notebooks for the students.