From 19da915bacc14fb6fa7d0d347f642ab3c2e76597 Mon Sep 17 00:00:00 2001 From: dotpie Date: Thu, 19 Sep 2024 02:15:14 +0900 Subject: [PATCH 1/2] docs: Add env path for conda users --- docs/Notebook-Setup.md | 14 +++++++------- docs/Virtual-Environments.md | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/Notebook-Setup.md b/docs/Notebook-Setup.md index 1e1db5e..05f194b 100644 --- a/docs/Notebook-Setup.md +++ b/docs/Notebook-Setup.md @@ -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 @@ -104,7 +104,7 @@ The neovim plugin [quarto-nvim](https://github.com/quarto-dev/quarto-nvim) provi
Sample configuration for quarto-nvim - + ```lua local quarto = require("quarto") quarto.setup({ @@ -284,7 +284,7 @@ local imb = function(e) -- init molten buffer 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") + local venv = os.getenv("VIRTUAL_ENV") or os.getenv("CONDA_PREFIX") if venv ~= nil then kernel_name = string.match(venv, "/.+/(.+)") end diff --git a/docs/Virtual-Environments.md b/docs/Virtual-Environments.md index cf24b1a..07018ff 100644 --- a/docs/Virtual-Environments.md +++ b/docs/Virtual-Environments.md @@ -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 @@ -72,7 +75,7 @@ initialize the correct kernel. ```lua vim.keymap.set("n", "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, "/.+/(.+)") From fb174303df51efa5b335b763389ded75399bb489 Mon Sep 17 00:00:00 2001 From: dotpie Date: Thu, 19 Sep 2024 02:45:17 +0900 Subject: [PATCH 2/2] docs: Enhance/Refactor detecting kernel which name is same as venv --- docs/Notebook-Setup.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/Notebook-Setup.md b/docs/Notebook-Setup.md index 05f194b..db2af7c 100644 --- a/docs/Notebook-Setup.md +++ b/docs/Notebook-Setup.md @@ -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") or os.getenv("CONDA_PREFIX") - 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