Ammonite API
The Ammonite API consists in instances of several classes, that are created by Ammonite or almond, and allow you to interact with the REPL. These instances are accessible either by their name, from the REPL, like interp
, or via implicits, like implicitly[ammonite.interp.api.InterpAPI]
.
The following instances are available:
interp
, which is aammonite.interp.api.InterpAPI
,repl
, which is aammonite.repl.api.ReplAPI
.
InterpAPI
InterpAPI
allows to
- load new dependencies or compiler plugins,
- add repositories for dependencies,
- add exit hooks,
- configure compiler options.
Load dependencies
interp.load.ivy
accepts one or several coursier.Dependency
.
Load simple dependencies
interp.load.ivy("org.platanios" %% "tensorflow-data" % "0.4.1")
Load dependencies while adjusting some parameters
interp.load.ivy(
// replace with linux-gpu-x86_64 on linux with nvidia gpu or with darwin-cpu-x86_64 on macOS
("org.platanios" %% "tensorflow" % "0.4.1").withClassifier("linux-cpu-x86_64")
)
The dependencies can then be used in the cell right after the one calling interp.load.ivy
.
Note that in the case of simple dependencies, when directly entering code in a notebook, the following syntax is preferred and allows to use the dependency in the current cell rather than the next one:
import $ivy.`org.platanios::tensorflow-data:0.4.1`
Load compiler plugins
interp.load.plugin.ivy
accepts one or several coursier.Dependency
.
interp.load.plugin.ivy("org.spire-math" %% "kind-projector" % "0.9.9")
The plugins can then be used in the cell right after the one calling interp.load.plugin.ivy
.
Note that when directly entering code in a notebook, the following syntax is more concise, and allows to use the compiler plugin in the current cell:
import $plugin.$ivy.`org.spire-math::kind-projector:0.9.9`
// example of use
trait T[F[_]]
type T2 = T[Either[String, ?]]
Add repositories
One can add extra coursier.Repository
via
import coursierapi._
interp.repositories() ++= Seq(
MavenRepository.of("https://nexus.corp.com/content/repositories/releases")
.withCredentials(Credentials.of("user", "pass"))
)
Add exit hooks
interp.beforeExitHooks += { _ =>
// called before the kernel exits
}
Configure compiler options
// enable warnings
interp.configureCompiler(_.settings.nowarn.value = false)
ReplAPI
ReplAPI
allows to
- access the pretty-printer and customize its behavior,
- access the latest thrown exception,
- access the command history,
- request the compiler instance to be re-created,
- get the current compiler instance,
- get the current imports,
- evaluate code from strings or files, and lastly
- get the class names and byte code of the code entered during the current session.
Pretty-printer
class Foo(val x: Int)
repl.pprinter() = {
val p = repl.pprinter()
p.copy(
additionalHandlers = p.additionalHandlers.orElse {
case f: Foo =>
pprint.Tree.Lazy(_ => Iterator(fansi.Color.Yellow(s"foo: ${f.x}").render))
}
)
}
Latest thrown exception
repl.lastException // last thrown exception, or null if none were thrown
Command history
repl.history // current session history
repl.fullHistory // shared history
Refresh compiler instance
repl.newCompiler()
Get compiler instance
repl.compiler // has type scala.tools.nsc.Global
Get current imports
repl.imports
Evaluate code
repl.load("val a = 2")
Byte code of REPL inputs
repl.sess.frames.flatMap(_.classloader.newFileDict).toMap
// Map[String, Array[Byte]], keys: class names, values: byte code