So, I haven’t actually wanted to be a web developer, but
I’m kind of the data chef, cook, and bottle-washer at work at the
moment, so spinning up some web code it is, then.
The existing code base is written in Bokeh which I hadn’t used
in over a year,
but pyLadies Amsterdam
did a web-training
that I attended to get going again.
This is a mockup of a plot I need for my work website, where
we want to be able to select lines from a plot.
You can use the box select tool in the upper left to select lines, then
click the button on the bottom to change the line color to cyan.
The two sides of the plot are linked (which is the feature I wanted
to play with), so if you box-select
the yellow line on the left, the yellow line on the right
also selects.
The reset tool in the upper left will put the colors back.
In order to use the selection styling functionality built into Bokeh,
I’ve bunched all my lines in each plot into a single MultiLine glyph.
Getting the data into the right format takes up a most of the code
below.
Now that I have my data, let’s plot.
Since I want my plots to be linked, I use the same ColumnDataSource for both the
left and right sides. The two plots also have the same x-axis.
The box select callback finds the data points in the box
boundaries and marks those lines associated with those points as selected.
These last two callbacks
Reset the plot to it’s original state
Change the color of the selected plots when you press the button below the plots
The last trick was to get the plot on my blog so I could show it off.
Bokeh’s autoload_static method creates two outputs: the body of a script to
display your plot, and a <script> tag that loads the script. In order
for the script to load properly, you have to give autoload_static the
location where you are going to store your script so that the html tag
part knows what to put for src=.
When I incorporated the script tag into the blog, I wrapped the html part
in a div to set the size (and included a resize corner in case you need it - the plot is
a bit wider than the default display width of the blog). This
blog is in jekyll, so I put the html tag in the _includes/
directory (where jekyll looks when I use the include directive) and the code body in
scripts/ where I told bokeh it would be when I created the html.
(Since the include is only one line, I could’ve just pasted it into the post as well.)