GCBallesteros / jupytext.nvim

Jupyter notebooks on neovim powered by Jupytext
66 stars 14 forks source link

fails to load notebook with missing `language` metadata field #17

Closed hamzashezad closed 3 months ago

hamzashezad commented 4 months ago

with the following notebook (note missing language tag in kernelspec):

{
    "cells": [
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "cfd2a7c6",
            "metadata": {},
            "outputs": [],
            "source": [
                "print(\"hello world\")"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "name": "python3"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 0
}

i get the following error:

Error detected while processing BufReadCmd Autocommands for "*.ipynb":
Error executing lua callback: ...cal/share/nvim/lazy/jupytext.nvim/lua/jupytext/utils.lua:21: attempt to concatenate local 'extension' (a nil value)
stack traceback:
        ...cal/share/nvim/lazy/jupytext.nvim/lua/jupytext/utils.lua:21: in function 'get_jupytext_file'
        ...ocal/share/nvim/lazy/jupytext.nvim/lua/jupytext/init.lua:76: in function 'read_from_ipynb'
        ...ocal/share/nvim/lazy/jupytext.nvim/lua/jupytext/init.lua:176: in function <...ocal/share/nvim/lazy/jupytext.nvim/lua/jupytext/init.lua:175>

however, i can successfully convert the notebook to py:hydrogen using locally installed jupytext v1.16.1. this is the generated file;

# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: hydrogen
#       format_version: '1.3'
#       jupytext_version: 1.16.1
#   kernelspec:
#     display_name: Python 3
#     language: python              # <-- notice this is added 
#     name: python3
# ---

# %%
print("hello world")
hamzashezad commented 4 months ago

i see in utils.lua that the jupytext command is constructed using kernelspec.language field, which i provide myself when i run jupytext. could there be another way to extract the file extension? perhaps the display_name or name?

GCBallesteros commented 4 months ago

Hi @hamzashezad thanks for posting an issue.

The relevant code that would need changing is under here. That is the function that will parse the notebook metadata and extract the relevant bits. As you already have realized it looks for kernelspec.language. I feel like this is the nominal way in which it should be done but would be happy to accept a PR (*) that adds fallback methods for the language detection. I would not expect such a PR to work for every language but it should provide the backbone to add the support for any language such that when something like this pops up for e.g. R we only need to add some entry to a dictionary.

(*) At the moment my life is very hectic and have minimal time to work on side projects but I'm open to reviewing and testing them.

hamzashezad commented 3 months ago

@GCBallesteros, I will happily issue a PR. Thanks for the pointers :)