imbue-ai / jupyter_ascending.vim

Vim plugin to interact with jupyter_ascending
MIT License
166 stars 16 forks source link

<Plug>JupyterExecute is executing the next cell instead of the current one #8

Open IndianBoy42 opened 2 years ago

IndianBoy42 commented 2 years ago
vim.api.nvim_buf_set_keymap(0, "n", "<localleader>j", "<Plug>JupyterExecute", {})
# %%
import numpy as np
import scipy as sp

# %%
print("Hello World")

Putting my cursor on import and hitting <localleader>j will cause the second cell to execute. What could be the problem?

fecet commented 2 years ago

same issue

fecet commented 2 years ago

A rough workaround is evoke function of vim-ipython-cell:

function! PrevExecNextCell()
    call IPythonCellPrevCell()
    call jupyter_ascending#execute()
    call IPythonCellNextCell()
endfunction

nmap <leader><CR> :call PrevExecNextCell()<CR>

hope someone have a more elegant solution

jvivian commented 2 years ago

hope someone have a more elegant solution

@fecet @IndianBoy42

After taking a look at the code, it appears to be an issue with the assumptions for how the file starts: https://github.com/untitled-ai/jupyter_ascending/blob/main/jupyter_ascending/requests/execute.py#L20

I don't know enough about jupytext as this is my first time trying to use it, but I assume you need the header at the top for it to work correctly, so you just need to remove the very first # %% in order to trigger the second specified case.

This worked for me, hopefully it does for you as well.

Cheers

ViRu-ThE-ViRuS commented 2 years ago

@jvivian this solutions works! Thanks for the this workaround :)

IndianBoy42 commented 2 years ago

So if i'm understanding right, not having the first cell marker on the first line should make it work?

But my notebook files are generally created by jupytext which does add a leading cell marker, so could it be fixed in jupyter_ascending?

jvivian commented 2 years ago

So if i'm understanding right, not having the first cell marker on the first line should make it work?

I used the jupyter_ascending.scripts.make_pairs function to generate the .py/ipynb pairs, which puts a jupytext header at the top. If you remove the first # %% below the header (---), it should work. Example below

(base) jvivian@DESKTOP-8U6S9I6:~/Dropbox/Learning/Statistics/Bayesian-modeling-and-computation$ head Notebooks/03_Linear_models.sync.py -n 20
# ---
# jupyter:
#   jupytext:
#     text_representation:   
#       extension: .py       
#       format_name: percent 
#       format_version: '1.3'
#       jupytext_version: 1.3.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd

# %%
print('hello world')
zplizzi commented 1 year ago

This was maybe hopefully fixed in the most recent update of the jupyter_ascending plugin - there was a numbering bug. If anyone happens to be able to confirm or deny, lmk!