## Exporting org-mode to Jupyter notebooks

Posted January 21, 2017 at 07:51 PM | categories: orgmode, python, jupyter, emacs | tags: | View Comments

Updated January 25, 2017 at 08:50 AM

## Table of Contents

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 .

## 1 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:

def objective(x): return x**2 - 4

Next, we use fsolve with an initial guess. We get fsolve from scipy.optimize.

from scipy.optimize import fsolve ans = fsolve(objective, 3) print(ans)

[ 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\)

```
ans, = fsolve(objective, -3)
print(ans)
```

-2.0

Now you see we get a float answer!

Here are some other ways to get a float:

ans = fsolve(objective, -3) print(float(ans)) print(ans[0])

-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.

## 2 Benefits of export to ipynb

- I can use org-mode
- And emacs
- and ipynb for teaching.

The export supports org-markup: **bold**, *italic*, underlined, and ~~strike~~.

We can use tables:

x | y |
---|---|

1 | 2 |

2 | 4 |

3 | 9 |

4 | 16 |

We can make plots.

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')

Even include HTML: <font color="red">Pay special attention to the axis labels!</font>

## 3 Limitations

- Only supports iPython blocks
- Does not do inline images in results
- Will not support src-block variables
- Currently only supports vanilla output results

## 4 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.

(export-ipynb-buffer)

Overall, this looks extremely promising to develop lecture notes and assignments in org-mode, but export them to Ipython notebooks for the students.

Copyright (C) 2017 by John Kitchin. See the License for information about copying.

Org-mode version = 9.0.3