From be05b7f5b7ab6ee06dd8bbb19f8583c54c40bd52 Mon Sep 17 00:00:00 2001 From: Diego Ortiz Date: Mon, 20 Jan 2025 11:37:11 -0500 Subject: [PATCH 1/2] Empty notebook generator --- lua/jupytext/init.lua | 60 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/lua/jupytext/init.lua b/lua/jupytext/init.lua index 1129208..c95628f 100644 --- a/lua/jupytext/init.lua +++ b/lua/jupytext/init.lua @@ -3,11 +3,50 @@ local utils = require "jupytext.utils" local M = {} +local function empty_notebook() + return [[{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "initial_id", + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}]] +end + M.config = { style = "hydrogen", output_extension = "auto", force_ft = nil, custom_language_formatting = {}, + empty_notebook_generator = empty_notebook, } local write_to_ipynb = function(event, output_extension) @@ -66,9 +105,28 @@ local cleanup = function(ipynb_filename, delete) end end +local function check_new_file(filename) + local file_content = "" + if vim.fn.filereadable(filename) == 1 then + file_content = io.open(filename, "r"):read "a" + end + if M.config.empty_notebook_generator and file_content == "" then + file_content = M.config.empty_notebook_generator() + end + local file = io.open(filename, "w") + if not file then + vim.notify "Failed to create empty Jupyter Notebook" + return + end + -- Write the content to the file + file:write(file_content) + file:close() +end + local read_from_ipynb = function(ipynb_filename) + check_new_file(ipynb_filename) local metadata = utils.get_ipynb_metadata(ipynb_filename) - local ipynb_filename = vim.fn.resolve(vim.fn.expand(ipynb_filename)) + ipynb_filename = vim.fn.resolve(vim.fn.expand(ipynb_filename)) -- Decide output extension and style local custom_formatting, output_extension, to_extension_and_style = style_and_extension(metadata) From c8e4dd1ef866399890271fc58c2e84a2832c19af Mon Sep 17 00:00:00 2001 From: Diego Ortiz Date: Mon, 20 Jan 2025 11:48:01 -0500 Subject: [PATCH 2/2] Updated documentation --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index db0ac44..3f5ca1c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ plain text alternatives. Powered by [jupytext](https://www.github.com/mwouts/jup features and a simpler configuration. ## Installation + `lazy.nvim` example: + ```lua return { "GCBallesteros/jupytext.nvim", @@ -18,7 +20,6 @@ return { } ``` - For `jupytext.nvim` to run correctly you will also need to make sure that you have the `jupytext` CLI installed (`pip install jupytext`). @@ -39,6 +40,7 @@ representation you want jupytext to output. The default configuration is: output_extension = "auto", -- Default extension. Don't change unless you know what you are doing force_ft = nil, -- Default filetype. Don't change unless you know what you are doing custom_language_formatting = {}, + empty_notebook_generator = empty_notebook, -- Where empty_notebook is a function that returns the json representation of an empty notebook. } ``` @@ -95,6 +97,6 @@ custom_language_formatting = { Setting force_ft is important to get other plugins like [otter.nvim](https://github.com/jmbuhr/otter.nvim) working correctly. - ## Acknowledgements + This plugin is a lua port of [goerz/jupytext.vim](https://www.github.com/goerz/jupytext.vim) and it wouldn't have existed without it.