bokeh-scala icon indicating copy to clipboard operation
bokeh-scala copied to clipboard

Minimal example for Jupyter notebooks?

Open mattwg opened this issue 8 years ago • 5 comments

I would love to combine scala-bokeh with jupyter notebooks running a scala/spark kernel. I can import scala-bokeh dependencies into my notebook - any tips as to how I can create a minimal chart and display it in the notebook?

mattwg avatar Jan 19 '16 07:01 mattwg

bokeh-scala generates HTML with plots as instances of NodeSeq. For example:

import io.continuum.bokeh._
val plot = new Plot() # etc.
val doc = new Document(plot)
val frag = doc.fragment(Resources.InlineMin)
frag.preamble // this is <script>, <style>, <link> with bokehjs and some <div>s with logo
frag.html // this is <script>, <div>s and <canvas> with the plot

The remainder is on the specific Scala kernel. In IScala (a variation of my own) there was build-in support for NodeSeq, so you didn't have to do much on your own, just make sure frag.preamble and frag.html were last expressions in two cells (or you could call display() to be explicit). I don't know how this works in other kernels, but I presume it isn't much different.

Note this works with bokeh-scala 0.7 and bokehjs pre-0.11. bokehjs 0.11 changed a lot, so this API may change as well. I will the notebook case in mind (I'm currently working on a new release).

mattpap avatar Jan 19 '16 20:01 mattpap

I did this recently w/ a Jupyter notebook and a Scala kernel. I didn't need to modify bokeh-scala. Here's a couple of helper functions I wrote (which make use of a method repl.showHtml that tells the kernel to send a message to the notebook to display the passed in HTML):

def initBokeh() = {

    import io.continuum.bokeh.{Resources, HTMLFragment}
    val resources = io.continuum.bokeh.Resources.default

    val fragment = new HTMLFragment(scala.xml.NodeSeq.Empty, resources.styles, resources.scripts)
    val writer = new java.io.StringWriter()
    scala.xml.XML.write(writer, <div> {  fragment.preamble } </div>, "UTF-8", xmlDecl=false, doctype=null)
    repl.showHtml(writer.toString)
}

def showBokehPlot(plot: io.continuum.bokeh.Plot) = {
    val writer = new java.io.StringWriter()
    val document = new io.continuum.bokeh.Document(plot)
    scala.xml.XML.write(writer, document.fragment.html(0), "UTF-8", xmlDecl=false, doctype=null)

    val result = writer.toString
    repl.showHtml(result)
}

praveen-srinivasan avatar Jan 26 '16 05:01 praveen-srinivasan

Which kernel are people recommending at this point for Bokeh Scala?

rgbkrk avatar Jul 10 '17 17:07 rgbkrk

Do you think you could impliment support for Toree and others using jvm-repr (https://github.com/jupyter/jvm-repr)?

spmp avatar Aug 13 '17 23:08 spmp

screenshot 2019-01-03 at 14 12 30 Any attempt to show the plot, including the plot above, produces a broken plot for me. :( Any had a similar problem?

letalvoj avatar Jan 03 '19 13:01 letalvoj