在neovim terminal中运行jupyter notebook

将jupyter notebook转换为.py

notebook似乎是json格式, 不便于看. 用jupytext. 这个bash脚本做了封装. 放在PATH环境变量下即可. 用法:

jbnc -f py file.ipynb # 需要-f参数, 否则不知道是哪种脚本
jbnc pp.py # percent格式, 即# %%
send命令到neovim terminal

至少有好几个插件. 试了好几个, 我选了neoterm插件. 最主要的命令就是:Tnew和配置文件中出现的. 并且配合hydrogen使用, 后者提供了一个text object, ahih. 识别的是# %%, 也就是jupytext中的percent格式.
neoterm的配置是:

nmap gxx <Plug>(neoterm-repl-send) " gxxih就执行了一个# %%块
vmap gxx <Plug>(neoterm-repl-send) " 执行选中内容
nnoremap gxc :TREPLSendLine<CR> " 执行当前行
nnoremap gxf :TREPLSendFile<CR> " 执行选中内容, 和前面重复了, 可以去掉
let g:neoterm_default_mod = 'botright' " 通过:Tnew创建的terminal在当前buffer下方
let g:neoterm_bracketed_paste = 1

快捷键可以自行修改. 最后一行g:neoterm_bracketed_paste的设置非常重要, 我鼓捣了3个小时(不得不说我是个傻子), 才发现它的重要性. 比如这个函数:

def sin_expansion(x, n):
    """
    Evaluate the nth order Talyor series expansion
    of sin(x) for the numerical values in the array x.
    """
    return s.lambdify(sym_x, s.sin(sym_x).series(n=n+1).removeO(), 'numpy')(x)

如果没有这个选项, 没法正确执行, 因为缩进逐渐增大. 加上这个选项, 才可以正确执行. 原因可以见这个变量的帮助.

posted @ 2021-12-29 16:43  Tokubara  阅读(328)  评论(0编辑  收藏  举报