在 Jupyter Notebook 中使用更多语言
本文最后更新于 2023年5月15日 凌晨
使用过 Jupyter Notebook 的开发者想必都十分熟悉这种基于 Web 的交互式计算环境,并且喜爱这种可以同时包含代码、文本、公式、图表和富媒体的文件格式吧!Jupyter Notebook 中最流行的程序语言是 Python,但是这不代表其他语言不能在 Jupyter Notebook 中运行,毕竟,这么好的格式怎么能让 Python 独占呢!事实上,只要能安装运行相应的核(Kernel),那么对应语言的代码就能在 Jupyter Notebook 中运行。你可以在这里找到第三方核和它们支持的语言的完整列表,或者在文末查看。
本文主要介绍 R 和 MATLAB 核的安装与使用。
R
在安装任何 R 内核之前,请确保你已经安装了 Jupyter Notebook 和 R,在虚拟环境里安装的请确保在同一虚拟环境里进行后续操作,否则相关程序可能无法正常运行。下面给出了在一台全新的机器上安装内核的步骤,读者若已经部分安装了相关程序可以按需阅读。
安装 Python
在全局 Python 环境下安装
1 | |
在虚拟 Python 环境下安装
1 | |
安装 R 和 R 内核
1 | |
安装完毕后,进入 R 环境,安装 IRkernel。
1 | |
注意启动 R 需要大写,小写的 r 是 shell 内置命令,表示运行上一条命令。
1 | |
第一条命令会下载并编译很多包,因此过程可能比较缓慢,耐心等待即可。网络上有些教程可能会用 IRkernel::installspec(user = FALSE) 代替上述第二条命令,不过在我的机器上提示权限不够,个人感觉并不会影响使用。
安装完毕后,就可以在 Jupyter Notebook 中找到 R 的核了。
MATLAB
MATLAB 需要 Python 与之通信,因此机器上除了完整的 MATLAB 以外,还需要安装 Python。截止 R2023a,MATLAB 仍不支持 Python 3.11,所以我们建立一个 Python 3.10 的虚拟环境。这里我们使用 conda 管理虚拟环境,如果读者有其他等效方法也可使用。
1 | |
建立好虚拟环境后,我们激活并安装 MATLAB Engine for Python(由 MathWorks 提供)和 MATLAB Kernel。
1 | |
安装完毕后,就可以在 Jupyter Notebook 中找到 MATLAB 的核了。
Jupyter Kernels
以下是一份完整的 Jupyter 内核和支持语言的列表。相应的使用方法点击链接即可访问。
| Name | Jupyter/IPython Version | Language(s) Version | 3rd party dependencies | Example Notebooks | Notes |
|---|---|---|---|---|---|
| GoNB | Jupyter >= 5 | Go >= 1.19 | Tutorial | ||
| Micronaut | Python>=3.7.5, Groovy>3 | Micronaut | https://github.com/stainlessai/micronaut-jupyter/blob/master/examples/basic-service/notebooks/use-library.ipynb | Compatible with BeakerX | |
| Agda kernel | 2.6.0 | https://mybinder.org/v2/gh/lclem/agda-kernel/master?filepath=example/LabImp.ipynb | |||
| Dyalog Jupyter Kernel | APL (Dyalog) | Dyalog >= 15.0 | Notebooks | Can also be run on TryAPL’s Learn tab | |
| Coarray-Fortran | Jupyter 4.0 | Fortran 2008/2015 | GFortran >= 7.1, OpenCoarrays, MPICH >= 3.2 | Demo, Binder demo | Docker image |
| LFortran | Binder demo | Main repository at GitLab | |||
| Ansible Jupyter Kernel | Jupyter 5.6.0.dev0 | Ansible 2.x | Hello World | ||
| sparkmagic | Jupyter >=4.0 | Pyspark (Python 2 & 3), Spark (Scala), SparkR ® | Livy | Notebooks, Docker Images | This kernels are implemented via the magics machinery of the ipython kernel to use Spark via Livy |
| sas_kernel | Jupyter 4.0 | python >= 3.3 | SAS 9.4 or higher | ||
| IPyKernel | Jupyter 4.0 | python 2.7, >= 3.3 | pyzmq | ||
| IJulia | julia >= 0.3 | ||||
| IHaskell | ghc >= 7.6 | Demo | |||
| IRuby | ruby >= 2.3 | ||||
| tslab | Typescript 3.7.2, JavaScript ESNext | Node.js | Example notebooks | Jupyter kernel for JavaScript and TypeScript - npm | |
| IJavascript | nodejs >= 0.10 | ||||
| ITypeScript | Typescript >= 2.0 | Node.js >= 0.10.0 | |||
| jpCoffeescript | coffeescript >= 1.7 | ||||
| jp-LiveScript | livescript >= 1.5 | Based on IJavascript and jpCoffeescript | |||
| Juka | Jupyter 4 | Any | Juka | Example | Wrapper. Requires Juka executable in PATH |
| ICSharp | Jupyter 4.0 | C# 4.0+ | scriptcs | ||
| IRKernel | IPython 3.0 | R 3.2 | rzmq | ||
| SageMath | Jupyter 4 | Any | many | ||
| pari_jupyter | Jupyter 4 | PARI/GP >= 2.9 | |||
| IFSharp | Jupyter 4 | F# | Features | ||
| lgo | Jupyter >= 4, JupyterLab | Go >= 1.8 | ZeroMQ (4.x) | Example | Docker image |
| iGalileo | Jupyter >= 4, JupyterLab | Galileo >= 0.1.3 | Docker image | ||
| gopherlab | Jupyter 4.1, JupyterLab | Go >= 1.6 | ZeroMQ (4.x) | examples | Deprecated, use gophernotes |
| Gophernotes | Jupyter 4, JupyterLab, nteract | Go >= 1.9 | ZeroMQ 4.x.x | examples | docker image |
| IGo | Go >= 1.4 | Unmaintained, use gophernotes | |||
| IScala | Scala | ||||
| almond (old name: Jupyter-scala) | IPython>=3.0 | Scala>=2.10 | examples | Docs | |
| IErlang | IPython 2.3 | Erlang | rebar | ||
| ITorch | IPython >= 2.2 and <= 5.x | Torch 7 (LuaJIT) | |||
| IElixir | Jupyter >= 4.0 | Elixir >= 1.5 | Erlang OTP >= 19.3, Rebar | example, Boyle package manager examples, Boyle examples with usage of Matrex library | IElixir Docker image, IElixir Notebook in Docker |
| ierl | Jupyter >= 4.0 | Erlang >= 19, Elixir >= 1.4, LFE 1.2 | Erlang, (optional) Elixir | ||
| IAldor | IPython >= 1 | Aldor | |||
| IOCaml | IPython >= 1.1 | OCaml >= 4.01 | opam | ||
| OCaml-Jupyter | Jupyter >= 4.0 | OCaml >= 4.02 | opam | Example | Docker image |
| IForth | IPython >= 3 | Forth | |||
| peforth | IPython 6/Jupyter 5 | Forth | Example | python debugger in FORTH syntax | |
| IPerl | Perl 5 | ||||
| Perl6 | Jupyter >= 4 | Perl 6.c | zeromq 4 | ||
| IPerl6 | Perl 6 | ||||
| Jupyter-Perl6 | Jupyter | Perl 6.C | Rakudo Perl 6 | ||
| IPHP | IPython >= 2 | PHP >= 5.4 | composer | DEPRECATED, use Jupyter-PHP | |
| Jupyter-PHP | Jupyter 4.0 | PHP >= 7.0.0 | composer, php-zmq | ||
| IOctave | Jupyter | Octave | Example | MetaKernel | |
| IScilab | Jupyter | Scilab | Example | MetaKernel | |
| MATLAB Kernel | Jupyter | Matlab | pymatbridge | Example | MetaKernel |
| Bash | IPython >= 3 | bash | Wrapper | ||
| Z shell | IPython >= 3 | zsh >= 5.3 | Example | ||
| Pharo Smalltalk | IPython >= 3 | Mac Os X | Binder demo | Paro 64 bits native kernel, zeromq | |
| PowerShell | IPython >= 3 | Windows | Wrapper, Based on Bash Kernel | ||
| CloJupyter | Jupyter | Clojure >= 1.7 | |||
| CLJ-Jupyter | Jupyter | Clojure | Abandoned as of 2017-02-12 | ||
| jupyter-kernel-jsr223 | Jupyter>=4.0 | Clojure 1.8 | clojure-jrs223, Java>=7 | Java based JSR223 compliant | |
| Hy Kernel | Jupyter | Hy | Tutorial | treats Hy as Python pre-processor | |
| Calysto Hy | Jupyter | Hy | Tutorial | based on MetaKernel (magics, shell, parallel, etc.) | |
| Redis Kernel | IPython >= 3 | redis | Wrapper | ||
| jove | io.js | ||||
| jp-babel | Jupyter | Babel | |||
| ICalico | IPython >= 2 | multiple | Index | ||
| IMathics | Mathics | ||||
| IWolfram | Wolfram Mathematica | Wolfram Mathematica®, Metakernel | MetaKernel | ||
| Lua Kernel | Lua | ||||
| IPurescript | Purescript | ||||
| IPyLua | Lua | Fork of Lua Kernel | |||
| ILua | Lua | ||||
| Calysto Scheme | Scheme | Reference Guide | MetaKernel | ||
| Calysto Processing | Processing.js >= 2 | MetaKernel | |||
| idl_kernel | IDL | IDL seem to have a built-in kernel starting with version 8.5 | |||
| Mochi Kernel | Mochi | ||||
| Lua (used in Splash) | Lua | ||||
| Apache Toree (formerly Spark Kernel) | Jupyter | Scala, Python, R | Spark >= 1.5 | Example | |
| Skulpt Python Kernel | Skulpt Python | Examples | MetaKernel | ||
| Calysto Bash | bash | MetaKernel | |||
| MetaKernel Python | python | MetaKernel | |||
| IVisual | VPython | Ball-in-Box | |||
| IBrainfuck | Brainfuck | Example | Wrapper | ||
| JupyterQ (KX Official Kernel) | Jupyter | kdb+ ≥ v3.5 64-bit, Python ≥ 3.6, embedPy | Notebook Examples | ||
| KDB+/Q Kernel (IKdbQ) | IPython >= 3.1 | Q | qzmq, qcrypt | ||
| KDB+/Q Kernel (KdbQ Kernel) | Jupyter | Q | |||
| PyQ Kernel | Jupyter | Q | Python for kdb+ | ||
| ICryptol | Cryptol | CVC4 | |||
| cling | Jupyter 4 | C++ | Example | ||
| xeus-cling | Jupyter >= 5.1 | C++ | Example | Supports Jupyter widgets | |
| Xonsh | Xonsh | Example | MetaKernel | ||
| Prolog | Prolog | MetaKernel | |||
| SWI-Prolog | Jupyter >=4.0 | SWI-Prolog | https://hub.docker.com/r/jm1337/jupyter-prolog-notebook/ | ||
| common-lisp-jupyter | Jupyter | Common Lisp | Quicklisp | About | |
| Maxima-Jupyter | Jupyter | Maxima | Quicklisp | ||
| ielisp | Jupyter | Emacs Lisp | emacs-zmq | ||
| Calysto LC3 | Assembly Language for the Little Computer 3 | ||||
| Yacas | YACAS | ||||
| IJython | Jython 2.7 | ||||
| ROOT | Jupyter | C++/python | ROOT >= 6.05 | ||
| Gnuplot Kernel | Gnuplot | Example | MetaKernel | ||
| Tcl | Jupyter | Tcl 8.5 | Based on Bash Kernel | ||
| Tcl | Jupyter | Tcl 8.6 | Binder demo | Written from scratch with a patched Tcl zmq binding | |
| J | Jupyter Notebook/Lab | J 805-807 (J901beta) | Examples | ||
| Jython | Jupyter>=4.0 | Jython>=2.7.0 | Java>=7 | Java based JSR223 compliant | |
| C | Jupyter | C | gcc |
||
| jupyterC | Jupyter | C | gcc>=3.0 |
Supports C89 to C17, Built for teaching C |
|
| TaQL | Jupyter | TaQL | python-casacore | TaQL tutorial | |
| Coconut | Jupyter | Coconut | |||
| SPARQL | Jupyter 4 | Python 2.7 or >=3.4 | rdflib, SPARQLWrapper | Examples | Optional GraphViz dependency |
| AIML chatbot | Jupyter 4 | Python 2.7 | pyAIML | Examples | |
| IArm | Jupyter 4 | ARMv6 THUMB | Examples | Based off of the ARM Cortex M0+ CPU | |
| SoS | Jupyter 4 | Python >=3.4 | Support kernels for bash, python2/3, matlab/octabe, javascript, julia, R, Stata, SAS, and more | Examples | Workflow system, Multi-Kernel support |
| jupyter-nodejs | Jupyter, iPython 3.x | NodeJS, Babel, Clojurescript | Examples | ||
| Pike | IPython >= 3 | Pike >= 7.8 | Wrapper, Based on Bash Kernel | ||
| imatlab | ipykernel >= 4.1 | MATLAB >= 2016b | |||
| jupyter-kotlin | Jupyter | Kotlin 1.4.20-dev-*** DEV | Java >= 8 | Samples | |
| jupyter_kernel_singular | Jupyter | Singular 4.1.0 | Demo | Optional PySingular for better performance, surf for images, details | |
| spylon-kernel | ipykernel >=4.5 | python >= 3.5, scala >= 2.11 | Apache Spark >=2.0 | Example | MetaKernel |
| mit-scheme-kernel | Jupyter 4.0 | MIT Scheme 9.2 | |||
| elm-kernel | Jupyter | Examples | |||
| Isbt | Jupyter 4.3.0 | sbt >= 1.0.0 | sbt | example | |
| BeakerX | Groovy, Java, Scala, Clojure, Kotlin, SQL | example | docker image | ||
| MicroPython | Jupyter | ESP8266/ESP32 | USB or Webrepl | developer notebooks | relies on the micro-controller’s paste-mode |
| IJava | Jupyter | Java 9 | Java JDK >= 9 | Binder online demo | Based on the new JShell tool |
| Guile | Jupyter 5.2 | Guile 2.0.12 | guile-json, openssl | ||
| circuitpython_kernel | Jupyter | CircuitPython | USB | Examples | |
| stata_kernel | Jupyter >=5 | Stata | Stata >=14 | Communicates natively with Stata | |
| iPyStata | Jupyter | Stata | Stata | Example Notebook | Implemented using magics machinery of ipython. |
| pystata-kernel | Stata >= 17 | Communicates with Stata through the pystata official Python bindings | |||
| nbstata | Jupyter >= 5.2 | Stata >= 17 | pystata, ipydatagrid | stata_kernel example | |
| IRacket | IPython >= 3 | Racket >= 6.10 | Racket, ZeroMQ | Example | |
| jupyter-dot-kernel | Jupyter >= 4.0 | dot/graphviz | graphviz version 2.40.1 | ||
| Teradata SQL kernel and extensions | JupyterLab >= 3.0 | SQL | Example Notebooks | ||
| HiveQL Kernel | Jupyter >= 5 | HiveQL | pyhive | Display HiveQL queries in HTML tables | |
| EvCxR Jupyter Kernel | Jupyter 4, JupyterLab, nteract | Rust >= 1.29.2 | ZeroMQ 4.x.x | Examples, Binder online demo | |
| StuPyd Kernel | Jupyter >= 4 | StuPyd Programming Language | Python3, antlr4-python3-runtime >= 4.7.1 | nbviewer demo | |
| coq_jupyter | Jupyter 5 | Coq | coq | Binder online demo | |
| Cadabra2 | Jupyter 5 | Cadabra2 | Example notebook | ||
| iMongo | MongoDB | ||||
| jupyter_kernel_chapel | Jupyter | Chapel | |||
| A Jupyter kernel for Vim script | Jupyter | Vim script | |||
| SSH Kernel | Jupyter | Bash | paramiko, metakernel | Examples | A Jupyter kernel specialized in executing commands remotely with paramiko SSH client. |
| GAP Kernel | Jupyter | GAP >= 4.10 | Binder demo | A Jupyter kernel for the computational algebra system GAP. | |
| Wolfram Language for Jupyter | Wolfram Engine, i.e., a Wolfram Desktop or Mathematica installation; wolframscript is optional but recommended |
A Jupyter kernel for the Wolfram Language (Mathematica). | |||
| GrADS kernel | GrADS >= 2.0 | ||||
| Bacatá | Jupyter | Java & Rascal | ZeroMQ & Rascal | Example | A Jupyter kernel generator for domain-specific languages. |
| nelu-kernelu | Jupyter | NodeJs 12 | NodeJs 12.3+ | Examples | An advanced NodeJs Jupyter kernel supporting comms and displays among other things. |
| IPolyglot | Jupyter | JavaScript, Ruby, Python, R, and more | GraalVM | Example Polyglot Notebook | Dockerfile |
| Emu86 Kernel | Jupyter | Intel Assembly Language | Introduction to Intel Assembly Language Tutorial | ||
| Common Workflow Language (CWL) Kernel | v1.1 | examples directory | |||
| MIPS Jupyter Kernel | Jupyter | MIPS32 Assembly Language | Python3, SPIM | Example | Driven by Python3 and Pexpect |
| iTTS | Natural languages | speech-dispatcher | Example | Currently cannot output sound into files or blobs because of limitations of speech-dispatcher | |
| xeus-clickhouse | ClickHouse SQL | xeus | Example | ||
| IQSharp | Jupyter 4 | Q# | QuantumKatas | ||
| .Net Interactive | Jupyter 4 | C#, F#, Powershell | .Net Core SDK | Binder Examples | |
| mariadb_kernel | Jupyter Notebook/Lab | SQL | Internal Dependencies, MariaDB Server | Binder notebook | A Jupyter kernel for the MariaDB Open Source database |
| ISetlX | Jupyter | SetlX | Example | ||
| Ganymede | Jupyter >= 4.0 | Java 11+, Groovy, Javascript, Kotlin, Scala, Apache Spark, and more | JShell, Apache Maven Resolver | Examples | |
| cqljupyter | Jupyter | Cassandra CQL | CQL Examples | ||
| iclingo | IPython 8 | clingo | clingo | Basic Examples | |
| ICrystal | Jupyter | Crystal | IRC | ||
| crystal_kernel | Jupyter | Crystal | Python wrapper kernel Crystal interpreter | ||
| idg | Jupyter | Dg (Doge) | Example Notebooks | ||
| Whitenote | Jupyter>=5 | Whitespace 0.1 | Go>=1.19 | example.ipynb | Docker image |