bokeh / bokeh-scala

Scala bindings for Bokeh plotting library
MIT License
135 stars 16 forks source link

Minimal example for Jupyter notebooks? #24

Open mattwg opened 8 years ago

mattwg commented 8 years ago

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?

mattpap commented 8 years ago

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).

praveen-srinivasan commented 8 years ago

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)
}
rgbkrk commented 7 years ago

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

spmp commented 7 years ago

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

letalvoj commented 5 years ago
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?