ENB: About @jupytext and @language jupytext

36 views
Skip to first unread message

Edward K. Ream

unread,
Nov 2, 2024, 9:55:59 AM11/2/24
to leo-editor

This Engineering Notebook post discusses how I created @jupytext and its companion, @language jupytext. As always, please feel free to ignore this post.


@jupytext


I created @jupytext to solve a specific problem: synchronizing .ipynb files with their corresponding .py pseudo files. Long experience has taught me that synchronizing files is difficult or impossible.


@jupytext was no stroke of genius. It is a natural extension of @clean. The new code practically wrote itself. I added only the following:


- A wrapper class for the jupytext api.

- Two new methods: at.read/writeOneAtJupytextNode.

  These methods merely extend Leo's existing @<file> machinery.

- A one-line hook in the code that detects changes to external files.


No wonder the entire project went smoothly.


@language jupytext


In contrast, @language jupytext required arduous work. The inspiration came late and required a complete re-imaging of:


- the colorizer's api.

- how the colorizer delegates work.


I set myself an absolute requirement. The work could only add methods to the JEditColorizer class. No substantial changes to existing methods were permissible.


Two Ahas appeared only after many doomed attempts:


Aha: The new pattern matcher should use a predicate instead of a long list of kwargs. Doh!


Aha: The new pattern pattern matcher must use a secondary colorizer main loop instead of delegation. The colorizer's delegation machinery can not possibly work in a line-oriented context.


This second Aha immediately lead me to this code in jedit.restart_match_span_delegated_line:

n = self.currentState()

self.init()

self.language = self.delegated_lines_language

self.mainLoop(n, s)


The final code looks so simple. You cannot imagine how complex my early attempts were.


Summary


@jupytext is a straightforward extension of Leo's @<file> machinery. The inspiration came first. I needed no other invention. The code practically wrote itself.


In contrast, @language jupytext required arduous work. The inspiration came late. Significant invention was required. Perhaps nobody else could have done what I did. I'm proud of this work.


Edward


P.S. Another ENB, coming soon, will discuss the implications of @language jupytext. It's possible to re-imagine Leo's syntax coloring safely. There will be minimal changes to Leo's colorizer and no changes to any file in the leo/modes directory.


EKR

Viktor Ransmayr

unread,
Nov 5, 2024, 5:00:12 AM11/5/24
to leo-editor
Hello Edward, hello Thomas,

Edward K. Ream schrieb am Samstag, 2. November 2024 um 14:55:59 UTC+1:

This Engineering Notebook post discusses how I created @jupytext and its companion, @language jupytext

.
...


FYI: I have been able to import  all notebooks from this series [1] into a test outline w/o issues - and - render them successfully using the script [2] published by you (Thomas).

Now it's time for me to study Jupyter & Jupytext (notebooks) in more detail !

With kind regards,

Viktor

---


Thomas Passin

unread,
Nov 5, 2024, 8:11:43 AM11/5/24
to leo-editor
Thanks for testing!

Edward K. Ream

unread,
Nov 5, 2024, 8:37:23 AM11/5/24
to leo-e...@googlegroups.com
On Tue, Nov 5, 2024 at 4:00 AM Viktor Ransmayr wrote:

> I have been able to import  all notebooks from this series [1] into a test outline w/o issues - and - render them successfully using the script [2] published by you (Thomas).

Viktor, I greatly appreciate your ongoing, thorough, testing.

> Now it's time for me to study Jupyter & Jupytext (notebooks) in more detail !

Please keep us devs informed of your experiences.

Edward

P.S. A new Engineering Notebook post, coming soon, will discuss a small glitch in @language jupytext.

The glitch affects only Leo nodes that contain multiple Jupyter Notebook cells. For such nodes, Leo's colorizer will mishandle the '# %%' lines that separate markdown sections from python sections. Sometimes these lines are colored blue, and sometimes red.

I have spent days attempting a fix. The problem seems intractable.

The workaround is to put each cell in its own node.

EKR
Reply all
Reply to author
Forward
0 new messages