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
> 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
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'
With the latest code, refresh-from-disk should break the file into nodes. Do you see that behaviour?
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.