• Stars
    star
    289
  • Rank 143,419 (Top 3 %)
  • Language
    OCaml
  • License
    MIT License
  • Created over 7 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An OCaml kernel for Jupyter (IPython) notebook

OCaml Jupyter

Jupyter protocol License CI Sponsor

An OCaml kernel for Jupyter notebook.

This provides an OCaml REPL with a great user interface such as markdown/HTML documentation, LaTeX formula by MathJax, and image embedding.

Screenshot

Getting started

OCaml Jupyter requires the libraries zlib, libffi, libgmp, libzmq 5+. Type one of the following commands suitable for your environment.

# For Debian or Ubuntu:
sudo apt-get install -y zlib1g-dev libffi-dev libgmp-dev libzmq5-dev
# For REHL or CentOS:
sudo yum install -y epel-release
sudo yum install -y zlib-devel libffi-dev gmp-devel zeromq-devel
# For Mac OS X:
brew install zlib libffi gmp zeromq

OCaml Jupyter can be installed by OPAM as follows:

pip install jupyter
opam install jupyter
grep topfind ~/.ocamlinit || echo '#use "topfind";;' >> ~/.ocamlinit  # For using '#require' directive
grep Topfind.log ~/.ocamlinit || echo 'Topfind.log:=ignore;;' >> ~/.ocamlinit  # Suppress logging of topfind (recommended but not necessary)
ocaml-jupyter-opam-genspec
jupyter kernelspec install [ --user ] --name "ocaml-jupyter-$(opam var switch)" "$(opam var share)/jupyter"

If the last command fails due to permission, --user option or sudo is required. You can use ocaml-jupyter kernel by launching Jupyter notebook server:

jupyter notebook

Development version

opam pin add jupyter https://github.com/akabe/ocaml-jupyter.git

Usage

Examples

In addition, many examples (e.g, image processing, voice analysis, etc.) are available at docker-ocaml-jupyter-datascience/notebooks.

These examples are placed in the public domain, e.g., you can edit, copy, and re-distribute with no copyright messages.

Code completion

OCaml Jupyter kernel supports merlin-based code completion. Candidates are shown by Tab key like

Code completion

The kernel uses .merlin file at a notebook directory for completion.

Inspection

Inspection in Jupyter is also achieved by merlin. You can see documentation and type of an identifier by Shift+Tab key like

Inspection

API documentation

OCaml Jupyter includes some sub-packages:

  • jupyter is a core library of OCaml Jupyter. This package is internally used. You don't need it directly.
  • jupyter.notebook is a library to control Jupyter from OCaml REPL in notebooks. This provides dynamic generation of HTML/markdown, and image embedding.
  • jupyter.comm is a library for communication between OCaml notebooks and Jupyter/Web frontend.

NBConvert

OCaml notebooks can be converted to HTML, Markdown, LaTeX, .ml files, etc. using the jupyter nbconvert command. For example, a .ipynb file is converted into a .html file as follows:

$ jupyter nbconvert --to html notebooks/introduction.ipynb
[NbConvertApp] Converting notebook notebooks/introduction.ipynb to html
[NbConvertApp] Writing 463004 bytes to notebooks/introduction.html

For exporting .ml files, we recommend Jupyter-NBConvert-OCaml. It outputs .ml files with Markdown cells as comments. After installation of Jupyter-NBConvert-OCaml, you can use --to ocaml option to export a .ml file:

$ jupyter nbconvert --to ocaml notebooks/introduction.ipynb
[NbConvertApp] Converting notebook notebooks/introduction.ipynb to ocaml
[NbConvertApp] Writing 2271 bytes to notebooks/introduction.ml

Customize kernel parameters

ocaml-jupyter-opam-genspec generates a configuration file like:

$ cat "$(opam var share)/jupyter/kernel.json"
{
  "display_name": "OCaml 4.08.1",
  "language": "OCaml",
  "argv": [
    "/bin/sh",
    "-c",
    "eval $(opam env --switch=4.08.1) && /home/xxxx/.opam/4.08.1/bin/ocaml-jupyter-kernel \"$@\"",
    "-init", "/home/xxxx/.ocamlinit",
    "--merlin", "/home/xxxx/.opam/4.08.1/bin/ocamlmerlin",
    "--verbosity", "app",
    "--connection-file", "{connection_file}"
  ]
}

See ocaml-jupyter-kernel --help for details of command-line parameters in argv. After you edit the file, re-register the kernel:

jupyter kernelspec install --name ocaml-jupyter "$(opam var share)/jupyter"

Installation without OPAM

ocaml-jupyter-opam-genspec depends on OPAM. If you use an other package manager, you need to write kernel.json by hand or use provided suitable way for registering a new kernel (e.g., jupyter module on Nix).

Running OCaml Jupyter on other environments

Binder

OCaml Jupyter can be run on Binder. Click the button to get started: Binder

For more information, see this repository.

Google Colab

OCaml Jupyter can be run on Google Colab. In order to do this you first have to run this Python notebook in your Colab instance. This will install the kernel and after that OCaml notebooks can be used on the same instance.

Sponsors

If you like this project, please support by becoming a sponsor via GitHub Sponsors.

Related work

Many Jupyter kernels for functional programming languages are available such as IHaskell, Jupyter Scala, and Jupyter Rust. IOCaml is another practical OCaml kernel that inspires us, but it seems no longer maintained. OCaml Jupyter kernel differs from IOCaml in

OCaml Jupyter IOCaml v0.4.8
Jupyter protocol v5.2 v3.2
OCaml PPX support Yes No
Session key support Yes No
Code completion Yes Yes
Introspection Yes Yes
User-defined messages Yes No
Stdin Yes No

Another OCaml kernel simple_jucaml seems too simple to use in practice. jupyter-kernel is a library to write OCaml kernels (not a kernel), but OCaml Jupyter kernel does not use this library.

More Repositories

1

evilml

A compiler from ML to C++ template language
JavaScript
158
star
2

slap

BLAS and LAPACK binding in OCaml with type-based static size checking for matrix operations
OCaml
88
star
3

ocaml-numerical-analysis

Stand-alone programs for numerical analysis in OCaml
OCaml
47
star
4

docker-ocaml-jupyter-datascience

Dockerfiles for data science in OCaml on Jupyter
Jupyter Notebook
43
star
5

docker-iocaml-datascience

Dockerfile of Jupyter (IPython notebook) and IOCaml (OCaml kernel) with libraries for data science and machine learning
Shell
29
star
6

obs-transcript

Real-time subtitle generation by speech recognition for OBS Studio
JavaScript
22
star
7

ppx_bigarray

A PPX extension for big array literals in OCaml
OCaml
20
star
8

docker-ocaml

Minimum docker images for OCaml+OPAM
Dockerfile
16
star
9

odoc-ltxhtml

OCamldoc custom generator to embed LaTeX equations in HTML documentation.
OCaml
8
star
10

ocaml-crf

A simple library for conditional random field (CRF) in OCaml
OCaml
7
star
11

docker-ocaml-jupyter

Minimum Docker image for OCaml Jupyter
Shell
6
star
12

sgpr

OCaml-GPR with static size checking for matrix operations
OCaml
6
star
13

ppl2016

TeX
6
star
14

ocaml-lwt-async-benchmark

A benchmark of Lwt and Jane Street Async
OCaml
3
star
15

docker-latex

My LaTeX environment on Docker
3
star
16

kuru-kuru-rin

Rotate a hypercube
C++
3
star
17

docker-iocaml

Dockerfile for IOCaml on Python Jupyter
Shell
2
star
18

ppx_deriving_binary_bytes

An OCaml PPX extension for type-driven generation of binary serializers and deserializers
OCaml
2
star
19

ocaml-mecab

MeCab binding for OCaml
OCaml
2
star
20

crf-type-inference

Simple type inference by conditional random field
OCaml
1
star
21

lens-poem

ใ‚ซใƒกใƒฉใƒฌใƒณใ‚บใฎใƒใ‚จใƒ ใ‚’่‡ชๅ‹•็”Ÿๆˆ
Python
1
star
22

docker-mysql-replication

An example of MySQL replication on Docker
Shell
1
star
23

jupyter-archimedes

A Jupyter-friendly 2D plotting library based on OCaml Archimedes
Jupyter Notebook
1
star
24

ansible-aerospike

Ansible playbook for Aerospike
1
star
25

droplet-collection

Collect droplet!
C++
1
star
26

ocaml-fluid-simulator

A simple fluid simulator by lattice gas automaton in OCaml
OCaml
1
star
27

httpd-nginx-bench

Simple benchmark: nginx vs apache (httpd)
HTML
1
star
28

ansible-zabbix

Launch Zabbix server and agent by ansible
PHP
1
star
29

ocaml-js-boilerplate

A template repository for OCaml and JavaScript
HTML
1
star
30

ansible-cowrie

Ansible playbook for SSH honeypot Cowrie (https://github.com/micheloosterhof/cowrie)
1
star
31

gameoflife

A simulator for Conway's game of life
C
1
star
32

ansible-playbooks

My ansible playbooks
Python
1
star
33

ocaml-bucklescript-example

An example project of BuckleScript and Web frontend programming
HTML
1
star