Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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`).

Expand All @@ -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.
}
```

Expand Down Expand Up @@ -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.
60 changes: 59 additions & 1 deletion lua/jupytext/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down