Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Minimal example for Jupyter notebooks? #24

Open
mattwg opened this issue Jan 19, 2016 · 5 comments
Open

Minimal example for Jupyter notebooks? #24

mattwg opened this issue Jan 19, 2016 · 5 comments
Milestone

Comments

@mattwg
Copy link

mattwg commented Jan 19, 2016

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
Copy link
Contributor

mattpap commented Jan 19, 2016

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
Copy link

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

@mattpap mattpap added this to the 0.8 milestone Feb 11, 2016
@rgbkrk
Copy link

rgbkrk commented Jul 10, 2017

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

@spmp
Copy link

spmp commented Aug 13, 2017

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

@letalvoj
Copy link

letalvoj commented Jan 3, 2019

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants