notebookPowerTools / vscode-jupytext

Other
51 stars 16 forks source link

Supporting non-py:percent File Formats #15

Open githubpsyche opened 1 year ago

githubpsyche commented 1 year ago

The extension in its current state only really works for python scripts in the percent format. But jupytext doesn't have this limitation.

Currently, the extension supports opening markdown files as jupyter notebooks. But when a save attempts, this error is returned:

Failed to save 'test.ipynb': Unable to write file 'jupytext:/c:/Users/gunnj/workspace/test.ipynb' (Traceback (most recent call last):
  File "C:\Users\gunnj\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\gunnj\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\__main__.py", line 13, in <module>
    sys.exit(jupytext())
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\cli.py", line 427, in jupytext
    exit_code += jupytext_single_file(nb_file, args, log)
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\cli.py", line 811, in jupytext_single_file
    lazy_write(nb_dest, fmt=dest_fmt, action=action)
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\cli.py", line 722, in lazy_write
    write(notebook, "-", fmt=fmt)
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\jupytext.py", line 524, in write
    content = writes(nb, version=version, fmt=fmt, config=config, **kwargs)
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\jupytext.py", line 508, in writes
    writer = TextNotebookConverter(fmt, config)
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\jupytext.py", line 55, in __init__
    self.implementation = get_format_implementation(
  File "c:\Users\gunnj\.vscode-insiders\extensions\donjayamanne.vscode-jupytext-0.1.0\python-libs\jupytext\formats.py", line 240, in get_format_implementation
    raise JupytextFormatError(
jupytext.formats.JupytextFormatError: Format 'percent' is not associated to extension '.md'. Please choose one of: markdown, pandoc, myst.

I found that the root of this bug is line 92 of src/conversion.ts:

const convertedFile = await runJupytext([
        '-m',
        'jupytext',
        '--to',
        `${extension.substring(1)}:percent`,
        uri.fsPath,
        '--output',
        '-',
    ]);

There is no percent format for the markdown file, so this operation raises the described error. Since I mostly work with markdown source files, I've changed this line to

`${extension.substring(1)}`,

And that works well for me. But then this drop supports for the py:percent format, so it's not the ideal solution. Just wanted to raise this point.