Configuration | Usage for Library Authors | Usage for Kernel Authors
Standardizing object representation and inspection across JVM kernels (Scala, Clojure, Groovy, ...).
JVM languages use various conventions to convert REPL outputs to forms that can be displayed. This project is an attempt to standardize by providing an API that libraries can use to register display methods for any jupyter frontend, whether console, notebook, or dashboard.
This API has two main uses:
- For library authors to provide a way to convert from a library's JVM objects to useful representations by MIME type
- For kernel authors to convert any JVM object to useful representations by MIME type
As it is with IPython, this is a contract between the libraries within the ecosystem, the kernel that inspects the objects, and the frontends that display them.
See instructions on JitPack for gradle, maven, sbt, or leiningen.
Library authors can register conversion code either
- by implementing a
Displayer
and registering it withDisplayers
, or - by having classes extend
AsDisplayData
, whosedisplay
method returns how an instance should be displayed.
For example, the following will register a displayer for Vegas graphs:
import java.util.Map
import jupyter.Displayer
import jupyter.Displayers
import scala.collection.JavaConverters._
import vegas.DSL.ExtendedUnitSpecBuilder
...
Displayers.register(classOf[ExtendedUnitSpecBuilder],
new Displayer[ExtendedUnitSpecBuilder] {
override def display(plot: ExtendedUnitSpecBuilder): Map[String, String] = {
val plotAsJson = plot.toJson
Map(
"text/plain" -> plotAsJson,
"application/json" -> plotAsJson,
"text/html" -> new StaticHTMLRenderer(plotAsJson).frameHTML()
).asJava
}
})
The following has Thing
be represented as simple text in Jupyter front-ends,
like Thing(2)
for new Thing(2)
:
import java.util.HashMap;
import java.util.Map;
class Thing implements AsDisplayData {
private int n;
public Thing(int n) {
this.n = n;
}
public Map<String, String> display() {
Map<String, String> result = new HashMap<>();
result.put(MIMETypes.TEXT, "Thing(" + n + ")");
return result;
}
}
Any kernel implementation can use the method to display Vegas graphs for the DSL objects.
Library authors can optionally implement setMimeTypes(String...)
to receive
hints for the MIME types that the kernel or front-end supports. It is
recommended that library authors use these hints to avoid expensive conversions.
Kernel authors can use this API to display registered objects:
import java.util.Map
// ...
Object result = interpreter.eval(code);
Map<String, String> resultByMIME = Displayers.display(result);
Kernel.this.display(resultByMIME);
Kernel authors can optionally call Displayers.setMimeTypes(String...)
to send
hints to display implementations with the set of MIME types that can be used by
the kernel or front-end.