Swift 4.1 in a Jupyter Notebook

Ray Yamamoto Hilton
Eliiza-AI
Published in
2 min readApr 16, 2018
Validating that Swift 4.1 is in use

TL;DR: Launch Swift 4.1 in BinderHub

Jupyter Notebooks provide a versatile interactive coding environment (Similar to Xcode Playgrounds) for exploring code and data. While the language of choice for data science tends to be Python or R, I believe Swift has potential outside of Apple ecosystem, and specifically, in data science.

I recently put together a little experiment to demonstrate using Swift 4.1 from within Jupyter Notebooks — you can spawn yourself a demo notebook using BinderHub.

Code-completion

I can’t take credit for the majority of this project, that was done by Jin Wang in his iSwift kernel for Swift 3.0. I took his code and made a few changes:

  • Base the Dockerfile off jupyter/minimal-notebook (so it can be easily used in JupyterHub)
  • Updated Swift from 3.0 to 4.1 — For this I needed to compile Swift from source and used the official Swift 4.1 Dockerfile as a reference
  • Since the kernel uses the Swift REPL, which in turn uses the debugger, the container requires privileged docker permissions to run. This seems to work by default in BinderHub and JupyterHub, but running locally requires the docker --privileged flag — see this github issue
  • I updated Package.swift to use the newer v4 API, but also to update the dependencies themselves to support Swift 4.1
  • I removed some Linux specific #ifs — they no longer seemed to be required in Swift 4.1, and, in some cases actually caused problems when running on Linux.
  • Swift has a bug where it fails to find frameworks, such as Foundation — to fix this, I had to pass in the default include path to the swift REPL:
    swift -I /usr/lib/swift/clang/include/

Each cell’s code is passed through the Swift REPL and echo’d back to the Jupyter UI as an output:

Output from multiple variables

While this isn’t terribly useful on it’s own, I hope to get frameworks like Surge and Nifty working — and also Swift for TensorFlow, once it is open sourced.

The code for the swift kernel is on github, as is the binderhub demo project. There is also a public docker image on docker hub. The project is based off Jin Wang’s iSwift kernel for Swift 3.0.

--

--