Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Enhance/Refactor detecting kernel which name is same as venv #243

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
32 changes: 20 additions & 12 deletions docs/Notebook-Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ other plugins that I don't use but will mention at the bottom.

## The promise:

\> your friend sends you a jupyter notebook file
\> `nvim friends_file.ipynb`
\> you see a markdown representation of the notebook, including code outputs and images
\> your friend sends you a jupyter notebook file
\> `nvim friends_file.ipynb`
\> you see a markdown representation of the notebook, including code outputs and images
\> you edit the notebook, with LSP autocomplete, and format the code cells before running
your new code, and all the cells below it, watching each cell output update as they run
\> `:wq`
your new code, and all the cells below it, watching each cell output update as they run
\> `:wq`
\> You send the `.ipynb` file, complete with your changes and the output of the code you
ran, back to your friend

Expand Down Expand Up @@ -104,7 +104,7 @@ The neovim plugin [quarto-nvim](https://github.com/quarto-dev/quarto-nvim) provi

<details>
<summary>Sample configuration for quarto-nvim</summary>

```lua
local quarto = require("quarto")
quarto.setup({
Expand Down Expand Up @@ -277,18 +277,26 @@ We can make importing/exporting outputs seamless with a few autocommands:
local imb = function(e) -- init molten buffer
vim.schedule(function()
local kernels = vim.fn.MoltenAvailableKernels()

-- if kernel runs on the venv or conda env, then use that kernel
local try_env_name = function()
return os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX")
end

local try_kernel_name = function()
local metadata = vim.json.decode(io.open(e.file, "r"):read("a"))["metadata"]
return metadata.kernelspec.name
end
local ok, kernel_name = pcall(try_kernel_name)
if not ok or not vim.tbl_contains(kernels, kernel_name) then
kernel_name = nil
local venv = os.getenv("VIRTUAL_ENV")
if venv ~= nil then
kernel_name = string.match(venv, "/.+/(.+)")

local _, kernel_name = pcall(try_kernel_name)
local env_ok, env_name = pcall(try_env_name)

if env_ok or not vim.tbl_contains(kernels, kernel_name) then
if env_name ~= nil then
kernel_name = string.match(env_name, "([^/\\]+)$")
end
end

if kernel_name ~= nil and vim.tbl_contains(kernels, kernel_name) then
vim.cmd(("MoltenInit %s"):format(kernel_name))
end
Expand Down
5 changes: 4 additions & 1 deletion docs/Virtual-Environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ wrapper](https://gist.github.com/benlubas/5b5e38ae27d9bb8b5c756d8371e238e6). I w
recommend a wrapper script of some kind if you are a python dev. If you're just installing these
deps to use Molten with a non-python kernel, you can skip the wrapper without much worry.

If you are using Anaconda or Miniconda as virtual environment manager, remind that your environment
path is `CONDA_PREFIX` instead of `VIRTUAL_ENV`.

## Create a Virtual Environment

We'll create a virtual environment called `neovim` that will contain all of our Molten (and other
Expand Down Expand Up @@ -72,7 +75,7 @@ initialize the correct kernel.

```lua
vim.keymap.set("n", "<localleader>ip", function()
local venv = os.getenv("VIRTUAL_ENV")
local venv = os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX"):
if venv ~= nil then
-- in the form of /home/benlubas/.virtualenvs/VENV_NAME
venv = string.match(venv, "/.+/(.+)")
Expand Down