On Thursday, October 31, 2024 at 10:29:31 PM UTC-4 [email protected] wrote:

Thank you, Thomas. The script works.
 

The command will create a new node below your @jupytext tree and will 
select it.  


But I don't quite understand why you need to do this? Why do we need a new 
node? Is it because you don't want to put the `#` logic in the jupytext 
node into vr3?


Ask and you shall receive. Here is a modified version of the script that 
renders the notebook on the fly - without creating a new node.  I suggest 
that you open VR3 before running it since I have seen once in a while that 
the splitter doesn't open up the VR3 pane.  It's there, and it even has a 
splitter handle to drag its pane larger, but that's hard to see.  I think 
this only happens right after startup but I can' reproduce the behavior 
yet. After 6.8.3 ...

Note that after rendering the notebook, VR3 will be left in its "freeze" 
state.  That means it won't render other nodes when they get selected.  
This is intentional because otherwise you would lose the rendered notebook 
by mistake all the time.  The top left-hand menu in VR3 has a checkable 
item "Freeze".  Click it and the freeze state will be removed.

Oh, and you can export the rendered view to the system browser by clicking 
on VR3's "Export" button.

"""Convert an @jupytext node tree for rendering with VR3.

   Select any node of the tree before running this script.
   Note that VR3 will be set into its "freeze" mode.
"""
import leo.plugins.viewrendered3 as v3

MD_MARKER = '# %% [markdown]'
CODE_MARKER = '# %%'
PREFIX = '<< prefix >>'

AT_MD = '@language md'
AT_CODE = '@language python'
MD_LITERAL_FENCE = "```"

pc = g.app.pluginsController
vr3_enabled = pc.isLoaded('viewrendered3.py')

class MdNode:
    def __init__(self, h:str = '', md:str = ''):
        self.h = h  # headline
        self.b = md  # body

def process_first_line(line:str) -> tuple(str, bool):
    """Convert the first line of a cell for VR3."""
    is_code = False
    if line.startswith(MD_MARKER):
        line = AT_MD
    elif line.startswith(CODE_MARKER):
        line = AT_CODE
        is_code = True
    if line.startswith('# '):
        line = line[2:]
    return (line, is_code)

if not vr3_enabled:
    g.es('viewrendered3.py must be enabled in settings to render this 
@jupytext file.')
else:
    found_head = False
    count = 0
    for p0 in c.p.self_and_parents():
        count += 1
        if count > 2:  # Only look one step up the tree
            break
        jupy_file = p0.h.split('@jupytext')
        if len(jupy_file) > 1:
            found_head = True
            JUPYTER_POSITION = p0
            break

    if not found_head:
        g.es('Cannot find an @jupytext node')
    else:
        cells = [AT_MD]
        for p in JUPYTER_POSITION.subtree():
            is_code = False
            headline = p.h
            is_prefix = headline == PREFIX
            if is_prefix:
                cells.append(MD_LITERAL_FENCE)
            lines = p.b.split('\n')
            firstline = lines[0]
            firstline, is_code = process_first_line(firstline)
            cells.append(firstline)
            for line in lines[1:]:
                i = 0 if is_code else 2
                cells.append(line[i:])
            if is_prefix:
                cells.append(MD_LITERAL_FENCE)

        body = '\n'.join(cells)

        h = c.hash()
        vr3 = v3.controllers.get(h)
        c.selectPosition(c.p)
        c.redraw()

        headline = 'Rendering of ' + jupy_file[1].split('/')[-1]
        md_node = MdNode(headline, body)
        vr3.set_freeze()
        vr3.update_md([md_node], {})


-- 
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/5ca9054c-b7c2-47af-8537-efd206152e45n%40googlegroups.com.

Reply via email to