Leo now has two *experimental* commands:

- import-jupyter-notebook: imports .ipynb file into Leo
- export-jupyter-notebook: exports Leo outline to .ipynb file.

The import command requires that you have the IPython nbformat 
<https://github.com/jupyter/nbformat>package installed. At present, there 
are now plans to relax this requirement. And you should also have Jupyter 
<http://jupyter.org/> itself installed.

Yesterday's 21e9b51b9 commit marks the end of the first phase of the 
project. Round-trip tests pass on two real notebooks. More work is coming.

I've been working intensely on this project. Iirc, I started this project 
late Saturday,March 19. First commit was early Monday morning. However, I'm 
not sure how important these commands will be. There is a real chance that 
they will remain a curiosity...

This work has been valuable for me personally. I've gotten lots of 
experience with how Jupyter handles invalid .ipynb files :-) Along the way, 
I've gotten comfortable with Jupyter notebooks <http://jupyter.org/>, which 
are wildly popular <http://jupyter.org/about.html> among scientists and 
others. Here are some cool example notebooks 
<http://nb.bianp.net/sort/views/>.

At present, *these commands must work together*. In particular, it would be 
*hopeless* to export from any outline *except* an outline created by the 
import-jupyter-notebook command. That is, the export command is too 
"fragile".  I'll be fixing this today. Afaik, the import command is robust, 
but it's early days.

The rest of this post explains what these commands do, how they do it, why 
they do what they do, and coming improvements.


*import-jupyter-notebook*
.ipynb files are text files in json 
<https://docs.python.org/3/library/json.html>format. These files describe 
the IPython/Jupyter nodes, including lots of hidden or auxiliary data.

As you probably know, Jupyter notebooks look like a *flat* list of cells: 
unlike as in Leo, cells can not contain other nodes. So the import command 
is free to use the children of an imported cell (Leo node) to contain all 
the *hidden data* associated with the imported cell. Child nodes have 
headlines of the form:

    # json-type: json-key

This convention preserves crucial type information needed by the export 
command. *Do not change the names of these nodes*.

The top-level json elements for a cell are:

    # str: cell_type
    # dict: metadata
    # list: outputs
    # list: source
    # int: execution_count

The importer code is a relatively straightforward.  It takes the data 
returned from nbformat.reads 
<https://nbformat.readthedocs.org/en/latest/api.html#reading-and-writing> 
and produces the corresponding Leo nodes. However, the 'source' key creates 
body text instead of a child node. There is no # list: source node.

*export-jupyter-notebook*

This command does the reverse of the export command, using the outline 
created by the import command. This is more complicated than it might seem, 
even with *supposedly* full type information available.

The export command should be able to create a valid .ipynb file from any 
.leo outline. At present, the export command will fail badly if you change 
the hidden data in any significant way.

The export will soon generate all required data as needed.  This will allow 
the *import* code not to create essentially empty items. It will also allow 
you to create Jupyter notebooks without first importing an existing 
notebook.  Jupyter's json schemas 
<http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf>
 
specify all required and optional fields:

    nbformat\v4\nbformat.v4.schema.json



*Summary*It's not clear how important these commands will ever be.

The import command requires the nbformat package.

At present, the import and export commands are joined at the hip. This will 
likely be fixed today.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to