@jupytext: final touches

71 views
Skip to first unread message

Edward K. Ream

unread,
Oct 26, 2024, 8:42:21 AM10/26/24
to leo-editor

Many thanks for all your excitement and kind words. I share that excitement. @jupytext may be Leo's most important addition since @clean.


If I understand correctly, the remaining rough edges involve creating .ipynb files and their corresponding @jupytext nodes.


Creating .ipynb files in Jupyter


We can sidestep problems creating .ipynb files as follows:


- Create the x.ipynb file in Jupyter.

- Create the @jupytext x.ipynb node in Leo, without saving the outline.

- Populate the @jupytext x.ipynb node using refresh-from-disk.


refresh-from-disk will put the entire file into the root @jupytext node. You can then create organizer nodes as you like. Saving the outline will update x.ipynb. After that, everything will just work.


Creating .ipynb files in Leo


Naturally, we want to create .ipynb files safely from within Leo. Imo, Leo should write @jupytext x.ipynb as follows. There are two cases to consider:


Case 1: x.ipynb exists


This is the easy case. Just create an empty @jupytext x.ipynb node. As usual, when you save the outline Leo will warn that the corresponding .ipynb file exists. You should not overwrite the file but instead use refresh-from-disk.


Case 2: x.ipynb does not exist


When x.ipynb does not exist, it's slightly more challenging for Leo to create a valid .ipynb file. There are two sub-cases to consider:


1. If the @jupytext tree is completely empty, Leo should create a valid .ipynb file. That is, the file should contain nothing but the Jupyter prefix. Rather than hard-coding this prefix, Leo should use the contents of @data jupyter-prefix.


2. Otherwise (the @jupytext tree contains text), Leo should write the @jupytext tree, possibly with a warning that creating an .ipynb file by hand isn't a good idea.


Summary


- Creating .ipynb files in Jupyter just works now.

- Leo can easily create valid .ipynb files from empty @jupytext trees.

- Leonistas should avoid creating .ipynb files by hand, that is, by saving non-empty @jupytext trees.


I welcome all your comments and suggestions.


Edward

Edward K. Ream

unread,
Oct 26, 2024, 8:55:51 AM10/26/24
to leo-editor
On Saturday, October 26, 2024 at 7:42:21 AM UTC-5 Edward K. Ream wrote:

> Leonistas should avoid creating .ipynb files...by saving non-empty @jupytext trees.

On second thought, I have no idea why I said that :-) If Leonistas create invalid .ipynb files, Jupyter will tell them so. What's the big deal?

Edward

Edward K. Ream

unread,
Oct 26, 2024, 2:06:26 PM10/26/24
to leo-editor
On Saturday, October 26, 2024 at 7:42:21 AM UTC-5 Edward K. Ream wrote:

> The remaining rough edges involve creating .ipynb files and their corresponding @jupytext nodes.


I have just completed all planned work on PR #4119:


- Fixed a bug that prevented writing non-existent .ipynb files. This bug probably caused great confusion.

- Added support for @data jupyter-prefix. This support required 5 new lines of code.

- Added @data jupyter-prefix to LeoSettings.leo. The postscript lists its contents.


Leo will write these data (and set the body text of the @jupytext node) when writing an empty @jupytext file.


Summary


The "ekr-4117-pair-jupyter-files" branch should be safe to use. Please test this branch and report any problems immediately.


I plan to merge this branch into "devel" tomorrow.


Please keep those comments and questions coming.


Edward


P.S. Here are the contents of @data jupyter-prefix in LeoSettings.leo. They came from a minimal .ipynb file, created by Jupyter.


# Leo created this prefix from @data jupyter-prefix.

# Jupyter's 'New Python Text Notebook with Percent Format` created this file.

# ---

# jupyter:

# jupytext:

# formats: py:percent

# text_representation:

# extension: .py

# format_name: percent

# format_version: '1.3'

# jupytext_version: 1.16.4

# kernelspec:

# display_name: Python 3 (ipykernel)

# language: python

# name: python3

# ---

# %%


EKR

lewis

unread,
Nov 1, 2024, 4:37:50 AM11/1/24
to leo-editor
I am trying out @jupytext but having some issues.

Leo is build 4e4dba800c
jupytext is installed.
I downloaded a sample jupyter file.

The procedure from above:

- Create the x.ipynb file in Jupyter.
- Create the @jupytext x.ipynb node in Leo, without saving the outline.
- Populate the @jupytext x.ipynb node using refresh-from-disk.

refresh-from-disk will put the entire file into the root @jupytext node. You can then create organizer nodes as you like. Saving the outline will update x.ipynb. After that, everything will just work.



Followed the procedure using the sample file:
Created a node with headline:
@jupytext N://leo//Jupyter//Plotting_with_Matplotlib.ipynb

Populate the @jupytext x.ipynb node using refresh-from-disk (Rt-click over node > refresh-from-disk)

Received a message in Leo log pane:
NotSupportedNBFormatVersion: Notebooks in nbformat version 3.0 are not supported by Jupytext. Please convert your notebooks to nbformat version 4 with 'jupyter nbconvert --to notebook --inplace'

Converted the file to version 4:
>jupyter nbconvert --to notebook --inplace Plotting_with_Matplotlib.ipynb
[NbConvertApp] Converting notebook Plotting_with_Matplotlib.ipynb to notebook
[NbConvertApp] Writing 86618 bytes to Plotting_with_Matplotlib.ipynb

I assume the conversion has succeeded.
Open Leo and get log pane message "updating: N:/leo/Jupyter/Plotting_with_Matplotlib.ipynb"
The node is empty. However when I try to populate the @jupytext x.ipynb node using refresh-from-disk I still have a node with no body text.

Starting Leo again from console I have a message in console (not log pane):
"updating: N:/leo/Jupyter/Plotting_with_Matplotlib.ipynb
read_into_root Invalid external file: N:/leo/Jupyter/Plotting_with_Matplotlib.ipynb"


For me, refresh-from-disk does not put the entire file into the root @jupytext node.
Am I doing something wrong?

Thomas Passin

unread,
Nov 1, 2024, 7:15:19 AM11/1/24
to leo-editor
I'm not sure about you doing anything "wrong", but try importing the .ipynb file from Leo's File/Import Files/Import  Any File menu.  It's been working well for me.

Edward K. Ream

unread,
Nov 1, 2024, 4:08:28 PM11/1/24
to leo-e...@googlegroups.com
On Fri, Nov 1, 2024 at 3:37 AM lewis <lewi...@fastmail.com.au> wrote:

> I am trying out @jupytext but having some issues.
...
> For me, refresh-from-disk does not put the entire file into the root @jupytext node.

With the latest code, refresh-from-disk should break the file into nodes.  Do you see that behaviour?

Edward

HaveF HaveF

unread,
Nov 1, 2024, 7:07:08 PM11/1/24
to leo-editor
On Friday, November 1, 2024 at 4:37:50 PM UTC+8 lewis wrote:

Received a message in Leo log pane:
NotSupportedNBFormatVersion: Notebooks in nbformat version 3.0 are not supported by Jupytext. Please convert your notebooks to nbformat version 4 with 'jupyter nbconvert --to notebook --inplace'

I've never seen such a warning when testing -- and I certainly haven't tested older versions of the ipynb files. My jupyter version:

╰─$ jupyter --version
Selected Jupyter core packages...
IPython: 8.25.0
ipykernel: 6.29.4
ipywidgets: 8.1.3
jupyter_client: 8.6.2
jupyter_core: 5.7.2
jupyter_server: 2.14.1
jupyterlab: 4.2.2
nbclient: 0.10.0
nbconvert: 7.16.4
nbformat: 5.10.4
notebook: 7.2.1
qtconsole: 5.5.2
traitlets: 5.14.3

By the way, `jupyter lab` is very useful, many things are available out of the box, if you have no other reason, please use this instead of `jupyter notebook`

lewis

unread,
Nov 2, 2024, 5:44:44 AM11/2/24
to leo-editor
On Saturday, November 2, 2024 at 7:08:28 AM UTC+11 Edward K. Ream wrote:

With the latest code, refresh-from-disk should break the file into nodes.  Do you see that behaviour?

Yes with latest build, refresh-from-disk does break the file into nodes. 
@tbpassin - thanks for the tip on using File/Import Files/Import  Any File menu.  It is easy and automatic. Hopefully using  File/Import Files/Import  Any File  will make it into @jupytext document.

Edward K. Ream

unread,
Nov 2, 2024, 6:29:27 AM11/2/24
to leo-e...@googlegroups.com
On Sat, Nov 2, 2024 at 4:44 AM lewis <lewi...@fastmail.com.au> wrote:

On Saturday, November 2, 2024 at 7:08:28 AM UTC+11 Edward K. Ream wrote:

With the latest code, refresh-from-disk should break the file into nodes.  Do you see that behaviour?

Yes with latest build, refresh-from-disk does break the file into nodes. 

Good. Am I correct that you are happy with how @jupytext works?

Reply all
Reply to author
Forward
0 new messages