benlubas / molten-nvim

A neovim plugin for interactively running code with the jupyter kernel. Fork of magma-nvim with improvements in image rendering, performance, and more
GNU General Public License v3.0
426 stars 23 forks source link

[Bug] pyenv causes issues with image rendering #92

Closed bpnordin closed 6 months ago

bpnordin commented 7 months ago

Description

There is no image in the output, even though everything else is correctly displaying.

image

I believe I have everything properly installed

image

and image.nvim works just fine with the test that they provide:

image

The issue is a lot like #7 on image.nvim repo. However, the fix/troubleshooting steps on that issue did not pertain to my issue with molten-nvim.

I am using a minimal test config file for neovim so that variables can be isolated (I didn't use a color scheme in the screenshots because I thought that might be the issue, but I added it back because it looks better)

-- Example for configuring Neovim to load user-installed installed Lua rocks:
package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?/init.lua;"
package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?.lua;"

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

-- install plugins
local plugins = {
    {  "folke/tokyonight.nvim",},
    {
        "benlubas/molten-nvim",
        version = "^1.0.0", -- use version <2.0.0 to avoid breaking changes
        dependencies = { "3rd/image.nvim" },
        build = ":UpdateRemotePlugins",
        init = function()
            -- these are examples, not defaults. Please see the readme
            vim.g.molten_image_provider = "image.nvim"
            vim.g.molten_output_win_max_height = 20
        end,
    },
    { "3rd/image.nvim",
    opts = {
        backend = "kitty", -- Kitty will provide the best experience, but you need a compatible terminal
        integrations = {}, -- do whatever you want with image.nvim's integrations
        max_width = 100, -- tweak to preference
        max_height = 12, -- ^
        max_height_window_percentage = math.huge, -- this is necessary for a good experience
        max_width_window_percentage = math.huge,
        window_overlap_clear_enabled = true,
        window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs", "" },
    },
    version = "1.1.0", },
    {
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        config = function()
            require("nvim-treesitter.configs").setup({
                ensure_installed = {
                    "markdown",
                    "markdown_inline",
                },
            })
        end,
    },
}
require("lazy").setup(plugins)

vim.cmd.colorscheme("tokyonight")

Reproduction Steps

I am running python 3.12.1 currently. But I have also tried this with 3.11. Here are the python packages I have installed

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
plotly = "*"
numpy = "*"
matplotlib = "*"
pynvim = "*"
jupyter-client = "*"
pillow = "*"
cairosvg = "*"
pnglatex = "*"
pyperclip = "*"
kaleido = "*"
notebook = "*"
nbformat = "*"

[requires]
python_version = "3.12"
python_full_version = "3.12.1"

For these current screenshots open up NeoVim with the slim config file

nvim -u minimal_config.lua .

run the python file to test the plotting behavior

import matplotlib.pyplot as plt
import numpy as np

# Create some data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create the plot
plt.plot(x, y)

Expected Behavior

I expect that the plot would be drawn in the output box. I have tried changing settings in Molten but nothing I have tried has fixed the issue. If I am doing anything wrong let me know. I guess it seems like the issue would be Molten if image.nvim is able to output images with the exact same config.

benlubas commented 7 months ago

I'm on a train currently, so no Internet and answering from my phone, but I have a few ideas:

You probably need to update to the master branch of image.nvim and I probably need to update my docs. If that doesn't fix it could you try these things:

bpnordin commented 7 months ago

Updated on master branch, and no luck :

image
  1. extra lines on the bottom don't help
  2. If I enter the floating window I still can not see the image
  3. When inside the floating window, I can scroll <C-e> until it looks like this image

basically I can only scroll one line to where Out[1]: Done is no longer visible.

When outside the floating window, the floating window will move up the screen like it is pinned to the spot just underneath the code. Until I scroll too far, the cursor is pushed down, and it disappears. Nothing crazy, all seems normal.

benlubas commented 7 months ago

Yup all that behavior seems normal. I've got time to dig into this. Thank you for providing a minimal config, i'll take a look.

benlubas commented 7 months ago

@bpnordin I can see the image when the output opens the first time, but then if I close the window, the image doesn't show up again. This seems to be related to #84. I'll fix that or revert it if it's too complex (that change didn't make much of a perf impact iirc).

bpnordin commented 7 months ago

I think I did find the one difference between that image.nvim test and when I try to use molten. It is which python is running. When I just use the system python(nothing installed), I can render. When I can't render, I am using a virtual python envirnment, but everything is installed. I will play around with it some more.

Using this to test rendering:

:lua  require("image").from_file("image.png"):render()
bpnordin commented 7 months ago

I am now getting a different output when just using the system python. I also have another picture rendered to show that image.nvim is working Am I just running the wrong code?

image
benlubas commented 7 months ago

I am now getting a different output when just using the system python

Interesting. I wonder if there is a larger issue here then. Do you normally have python3_host_prog set? I didn't see it in the minimal config and I had to add it to mine as I have all my molten dependencies installed in a virtual environment.

Also I just merged the revert, if you could try using the main branch of molten now that will definitely fix a problem. But perhaps not the exact problem that you're having.

bpnordin commented 7 months ago

I normally start NeoVim with a virtualenv active and then I have all of the dependencies for molten installed in that virtualenv, if I am going to use it.

I set the python3_host_prog to the virtualenv instead of activating it

image

It seems that if I have a virtual environment activated, then rendering in general doesn't work. If I set the host-prog, then it does work. Hmmm

However, even after I updated molten, it still doesn't render the plot.

image

This is the same behavior if I just use the system python (non-virtualized)

benlubas commented 7 months ago

It seems that if I have a virtual environment activated, then rendering in general doesn't work. If I set the host-prog, then it does work. Hmmm

that's odd. I will say that you should be using host-program and a corresponding neovim virtual environment. They way neovim will always launch with the necessary decencies, and you don't have to install all the modules molten needs in every venv you create.

sphyrnidae8 commented 7 months ago

Exactly the same issue here. Also on mac m1. But i dont run a special venv.

benlubas commented 7 months ago

Unfortunately I can't help much without a reproduction, I'm not seeing the issue with the current repro.

I do wonder if pyenv has anything to do with it b/c I personally don't use it.

@sphyrnidae8 are you using pyenv as well?

benlubas commented 7 months ago

Both of you should 100% set vim.g.python3_host_prog so you know for sure what environment neovim is working with. Otherwise it seems to choose venv or system, and it's not exactly the most reproducible thing.

I recommend setting up a neovim venv the way the docs suggest. This way you can install the packages for neovim once, in once place, and they're not interfered with by anything else.

bpnordin commented 7 months ago

I think it does have something to do with pyenv. I was using pyenv for the different versions of python. I just uninstalled it and used a homebrew install of python3.10 and set up the environment like in your docs. Now everything works. Thanks!

bpnordin commented 7 months ago

Also I just merged the revert, if you could try using the main branch of molten now that will definitely fix a problem. But perhaps not the exact problem that you're having.

and yes updating to master has fixed an issue where it will only display the plot the first time in the output, and not if the output hides and then displays again

benlubas commented 7 months ago

Awesome! I'm curious about pyenv, now that I know that it's a problem I can look into it. I'm going to open a separate issue to track that problem. nah I'll just change the name of this issue so there's all this context on it.

Thank you for working with me on this one, environment issues are always tricky.

benlubas commented 7 months ago

I probably won't get around to this until after Christmas though

benlubas commented 6 months ago

I can't reproduce this issue with pyenv. I've installed it, set it up, activated it, installed pynvim and jupyter_client (and ipykernel), tested it with both an existing kernel and the new default python3 kernel, they both work. Images show in virtual text and in floating windows, and they show in multiple floating windows as well.

I did confirm with :checkhealth that the expected pyenv version of python was being used. also I used python 3.11

My only guess is that you missed something with your pyenv install that you didn't miss when following the docs.

Zexius commented 2 months ago

I can't reproduce this issue with pyenv. I've installed it, set it up, activated it, installed pynvim and jupyter_client (and ipykernel), tested it with both an existing kernel and the new default python3 kernel, they both work. Images show in virtual text and in floating windows, and they show in multiple floating windows as well.

I did confirm with :checkhealth that the expected pyenv version of python was being used. also I used python 3.11

My only guess is that you missed something with your pyenv install that you didn't miss when following the docs.

I met a similar issue and I am pretty sure this is not caused by pyenv but poetry, which is commonly used together with pyenv. This is a known issue with image.nvim and there is no fix, but the workaround is to source the venv created by poetry directly instead of using the poetry shell