在Jupyter上加入JavaScript内核
在Jupyter上加入JavaScript内核
Jupyter Lab是一个轻量化的代码编辑器,其小巧便携的体积以及原生支持MarkDown使其深受广大代码编辑者的喜爱。Jupyter Lab的原生内核仅支持语言为Python语言,通过插件,也可以将R语言JavaScript语言等部署在Jupyter上。
下面,将介绍如何将JavaScript内核添加到Jupyter Lab 中
背景
先前有许多部署JS内核的教程需要先安装Jupyter和Node.js,其安装过程较为繁琐复杂,并且安装时容易报错。如:在Jupyter Lab(Notebook)上安装运行JavaScript应用程序和在Jupyter Lab(Notebook)上安装运行JavaScript应用程序_jupyter 前端js。
在尝试以上方法安装时,安装ijavascript
库时容易出现以下报错:
npm ERR! code 1
npm ERR! path 你的文件夹\npm\node_modules\ijavascript\node_modules\zeromq
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@14.17.6 | win32 | x64
npm ERR! gyp info find Python using Python version 3.6.2 found at "你的文件夹\Python\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version was set from command line or npm config
npm ERR! gyp ERR! find VS - looking for Visual Studio version 2017
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS valid versions for msvs_version:
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack at VisualStudioFinder.fail (你的文件夹\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:121:47)
npm ERR! gyp ERR! stack at 你的文件夹\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:74:16
npm ERR! gyp ERR! stack at VisualStudioFinder.findVisualStudio2013 (你的文件夹\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
npm ERR! gyp ERR! stack at 你的文件夹\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:70:14
npm ERR! gyp ERR! stack at 你的文件夹\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:372:16
npm ERR! gyp ERR! stack at 你的文件夹\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack at 你的文件夹\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack at ChildProcess.exithandler (child_process.js:397:5)
npm ERR! gyp ERR! stack at ChildProcess.emit (events.js:400:28)
npm ERR! gyp ERR! stack at maybeClose (internal/child_process.js:1055:16)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "F:\\Js\\node.exe" "你的文件夹\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd 你的文件夹\Roaming\npm\node_modules\ijavascript\node_modules\zeromq
npm ERR! gyp ERR! node -v v14.17.6
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok
npm ERR! A complete log of this run can be found in:
npm ERR! 你的文件夹AppData\Local\npm-cache\_logs\2021-09-16T07_42_09_653Z-debug.log
详细可以参考:记录安装Jupyter以及加入Javascript
上面这篇文章给出的方法是安装Visual Studio 2017,并且通过输入npm install ijavascript -g
来解决,我在尝试后发现效果并不理想。
随后尝试了几种其他方法,发现在Anaconda上进行安装最为简便快捷。
安装Anaconda和jupyter lab
Anaconda 包括了python和很多常见的软件库, 和一个包管理器conda。通过Anaconda可以非常便捷地管理和安装各种包。
Anaconda的详细安装教程可以参考这篇文章Anaconda安装教程,超详细超简单(2024最新版)
安装完Anaconda后还需要在anaconda中安装Jupyter Lab,详细可以 参考:Anaconda安装jupyter notebook、jupyterlab及体验
通过Anaconda安装Node.js
安装完Anaconda后,在windows搜索框中搜索Anaconda Prompt并以管理员启动。
在anaconda prompt中输入以下代码安装Node.js
conda install nodejs
安装完成后可以通过代码查看是否成果安装npm
npm -version
安装ijavascript库
在anaconda prompt 中输入以下代码安装ijavascript库
npm install -g ijavascript
等待安装完成后输入以下代码安装ijavascript
ijsinstall
启动Jupyter Lab
在anaconda prompt中输入代码启动jupyter lab
conda activate 你创建的虚拟环境
jupyter lab
随后会在浏览器中启动jupyter lab
新建一个ipynb文件
在左上角切换Kernel
选择JavaScript即可
在Vscode中启动
如果你有在Vscode中使用jupyter的习惯,你也可以在vscode中选择JavaScript内核,这样即可在vscode中使用jupyter编写js。