All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jove.notebook.JoveNotebook.scala Maven / Gradle / Ivy

The newest version!
package jove
package notebook

import java.io.File
import java.net.{InetAddress, InetSocketAddress}

import jove.notebook.components.Cluster
import jove.notebook.services.Clusters
import jove.notebook.util.WebSocketBlazeBuilder
import org.http4s.dsl.{Path, Root}
import org.http4s.server.HttpService

import caseapp._

import scala.concurrent.duration.Duration
import scalaz.concurrent.Strategy

case class ExtraKernelOptions(
  connFile: String,
  id: String,
  name: String,
  extensions: List[String],
  meta: Boolean
)

case class JoveNotebook(
  host: String = "",
  port: Int = 9000,
  @ExtraName("D") notebookDir: String,
  extraKernelOptions: ExtraKernelOptions,
  prefix: String
) extends App with LazyLogging {
  val _prefix = prefix.split("/").filter(_.nonEmpty)
  val state = new ServerState(new File(Some(notebookDir).filter(_.nonEmpty) getOrElse "notebooks"))
  val cluster = new Cluster

  state.kernel loadKernelsFromConfig "jove"

  if (extraKernelOptions.connFile.nonEmpty) {
    val f = new File(extraKernelOptions.connFile)

    if (f.isFile) {
      if (extraKernelOptions.meta)
        state.kernel.addZMQMetaKernel(
          Some(extraKernelOptions.id).filter(_.nonEmpty) getOrElse "zmq",
          Some(extraKernelOptions.name).filter(_.nonEmpty) getOrElse "ZMQ",
          Some(extraKernelOptions.extensions).filter(_.nonEmpty) getOrElse List("snb"),
          f
        )
      else
        state.kernel.addZMQSharedKernel(
          Some(extraKernelOptions.id).filter(_.nonEmpty) getOrElse "zmq",
          Some(extraKernelOptions.name).filter(_.nonEmpty) getOrElse "ZMQ",
          Some(extraKernelOptions.extensions).filter(_.nonEmpty) getOrElse List("snb"),
          f
        )
    } else
      Console.err println s"Warning: ignoring not found connection file ${extraKernelOptions.connFile}"
  }

  val socketAddress = InetSocketAddress.createUnresolved(if (host.isEmpty) "0.0.0.0" else host, port)

  Console.err println
    s"Open http://${if (host.isEmpty || host == "0.0.0.0") InetAddress.getLocalHost.getHostAddress else host}${if (port == 80) "" else ":" + port}/${_prefix mkString "/"} in your browser"

  val builder = new WebSocketBlazeBuilder(
    socketAddress = socketAddress,
    serviceExecutor = Strategy.DefaultExecutorService,
    idleTimeout = Duration.Inf,
    isNio2 = true,
    sslBits = None,
    serviceMounts = Vector.empty
  )

  val frontEnd =
    Frontend.default() getOrElse {
      logger debug s"No frontend found"
      Console.err println s"No frontend found, is jove-jupyter-frontend to the class path?"
      sys exit 1
    }

  builder
    .mountService(
      HttpService(
        frontEnd(state, ((Root: Path) /: _prefix)(_ / _)) orElse Server.apiPlan(state) orElse Clusters(cluster)
      ),
      _prefix mkString "/"
    )
    .run
    .awaitShutdown()
}

object JoveNotebook extends AppOf[JoveNotebook] {
  val parser = default
}

class NotebookConscriptLaunch extends xsbti.AppMain {
  def run(config: xsbti.AppConfiguration) =
    try {
      JoveNotebook.main(config.arguments)
      Exit(0)
    } catch {
      case _: Exception =>
        Exit(1)
    }

  case class Exit(code: Int) extends xsbti.Exit
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy