On Wednesday, October 23, 2024 at 11:39:56 AM UTC-5 Edward K. Ream wrote:

> This Engineering Notebook post describes a breakthrough that will 
radically simplify #4117 
<https://github.com/leo-editor/leo-editor/issues/4117>.

Today has been a golden day of programming. Pr  #4119 
<https://github.com/leo-editor/leo-editor/pull/4119>, renamed "Support 
@jupytext," now demonstrates *all* facets of the project.


Here are the highlights:


*Dead easy collaboration*


The present code demonstrates round-tripping of .ipynb files between Leo 
and Jupyter.


Everything "just works" *without using jupytext in Jupyter!*


*Dead easy class design*


- leoJupytext.py defines the *JupytextManager *(jtm) class. The LeoApp 
class instantiates a singleton instance of the jtm in the usual way.

- The AtFile class supports reading and writing @jupytext nodes with 
prosaic code, delegating all significant operations to the jtm.

- The ExternalFilesController class supports updating @jupytext nodes when 
external editors (*including Jupyter*) write .ipynb files.


*Dead easy code*


The JupytextManager class is remarkably concise. For example, here is* 
jtm.read*:


def read(self, c: Cmdr, p: Position) -> str: # pragma: no cover

    """

    p must be an @jupytext node describing an .ipynb file.

    Convert x.ipynb to a string and return that string.

    """

    path = self.full_path(c, p)

    if not path:

        return '' # full_path gives any errors.

    # Read .ipynb file into contents.

    notebook = jupytext.read(path, fmt='py:percent')

    contents = jupytext.writes(notebook, fmt="py:percent")

    return contents


Nothing could be simpler! And here is the client, 
*AtFile.readOneAtJupytextNode*:


def readOneAtJupytextNode(self, p: Position) -> None:

    """

    p must be an @jupytext node.

    - Convert the .ipynb file to a string s.

    - Update p.b's tree using s. 

    """

    c = self.c

    contents = g.app.jupytextManager.read(c, p)

    if contents:

        p.b = '@language json\n\n' + contents


This code is a prototype. Instead of setting p.b, this method should use 
the @clean update code to set p's entire tree.


*Summary*


The present code demonstrates the round-tripping of .ipynb files between 
Leo and Jupyter. Everything "just works" *without using jupytext in 
Jupyter!*


The *JupytextManager* and AtFile classes work together to support reading 
and writing @jupytext nodes. The jtm and the ExternalFilesController 
classes collaborate to update @jupytext nodes when .ipynb files change 
externally.


The only remaining task is to have @jupytext nodes work like @clean nodes. 
Perhaps only a few more lines of code will suffice!


It's probably been five years since I have had such a significant and 
productive day of programming. None of this progress would have been 
possible without jupytext's spectacularly simple API!


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 view this discussion visit 
https://groups.google.com/d/msgid/leo-editor/f07dbc9d-1ec6-44e6-98ee-8545cea07fa1n%40googlegroups.com.

Reply via email to