Skip to content

Commit

Permalink
docs(nb-setup): example NewNotebook command (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSquared committed Jul 2, 2024
1 parent 47b9a56 commit 1a5360c
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/Notebook-Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,71 @@ vim.api.nvim_create_autocmd("BufEnter", {
})
```

#### Creating new notebooks

Since Jupytext needs a valid notebook file to convert, creating a blank new notebook is not as easy as making an empty buffer and loading it up.

To simplify this workflow, you can define a vim user command to create an empty, but valid, notebook file and open it:

```lua
-- Provide a command to create a blank new Python notebook
-- note: the metadata is needed for Jupytext to understand how to parse the notebook.
-- if you use another language than Python, you should change it in the template.
local default_notebook = [[
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython"
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
]]

local function new_notebook(filename)
local path = filename .. ".ipynb"
local file = io.open(path, "w")
if file then
file:write(default_notebook)
file:close()
vim.cmd("edit " .. path)
else
print("Error: Could not open new notebook file for writing.")
end
end

vim.api.nvim_create_user_command('NewNotebook', function(opts)
new_notebook(opts.args)
end, {
nargs = 1,
complete = 'file'
})
```

You can then use `:NewNotebook folder/notebook_name` to start a new notebook from scratch!

## Compromises

Compared to Jupyter-lab:
Expand Down

0 comments on commit 1a5360c

Please sign in to comment.