Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
ijulia working, output more sane
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jun 26, 2014
1 parent 93509d8 commit 6d2f0a6
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 85 deletions.
314 changes: 314 additions & 0 deletions doc/juliabokeh.ipynb

Large diffs are not rendered by default.

21 changes: 4 additions & 17 deletions src/Bokeh.jl
@@ -1,9 +1,9 @@
module Bokeh
include("bokehjs.jl")
include("objects.jl")
include("display.jl")
include("generate.jl")
include("plot.jl")
include("browser.jl")
import Base: display

# displays indented JSON, uses unminified js and saves the raw JSON to "bokeh_models.json" if true
Expand Down Expand Up @@ -40,7 +40,7 @@ module Bokeh

# default filename
FILENAME = "bokeh_plot.html"
warn_overwrite() = ispath(FILENAME) && isinteractive() && warn(
warn_overwrite() = ispath(FILENAME) && isinteractive() && !isdefined(Main, :IJulia) && warn(
"$FILENAME already exists, it will be overwritten when a plot is generated.\nChange the output file with plotfile(<new file name>)")
warn_overwrite()
function plotfile(fn::String)
Expand Down Expand Up @@ -73,19 +73,6 @@ module Bokeh
glyphs,
glyphsize,
plotfile,
title

# type BokehDisplay <: Display
# figs::Dict{Int,Plot}
# figs::Vector{Int}
# current_fig::Int
# next_fig::Int
# BokehDisplay() = new(Dict{Int,Plot}(), Int[], 0, 1)
# end

# # function Base.writemime(io::IO, ::MIME"image/png", plot::Plot)
# # openhtml(plot.filename)
# # end
# display(p::Plot) = openhtml(plot.filename)
# pushdisplay(BokehDisplay())
title,
setupnotebook
end
8 changes: 0 additions & 8 deletions src/browser.jl

This file was deleted.

45 changes: 45 additions & 0 deletions src/display.jl
@@ -0,0 +1,45 @@
function openhtmldoc(filepath::String)
os = Sys.OS_NAME
open = os == :Linux ? "xdg-open" :
os == :Darwin ? "open" :
warn("Not Implemented: No idea how to open a browser in Windows, perhaps you could work it out and submit a pull request?")
open == nothing && return
run(`$open $filepath`)
end

_basic(p::Plot) = "Plot(\"$(p.title)\" with $(length(p.datacolumns)) datacolumns)"

function Base.writemime(io::IO, ::MIME"text/html", p::Plot)
print(io, renderplot(p, true))
print(io, "<p>", _basic(p), "</p>")
end

function Base.writemime(io::IO, ::MIME"text/plain", p::Plot)
print(io, _basic(p))
end

if !isdefined(Main, :IJulia)
type BokehDisplay <: Display
end

function Base.display(d::BokehDisplay, p::Plot)
display("text/plain", p)
html = renderplot(p, false)
if ispath(p.filename)
println()
warn("$(p.filename) already exists, overwriting")
end
open(p.filename, "w") do f
print(f, html)
end
openhtmldoc(p.filename)
end
pushdisplay(BokehDisplay())
end

function setupnotebook()
jspath, csspath = _bokehjs_paths(!DEBUG)
jscss = _render_jscss(jspath, csspath, true)
display("text/html", jscss)
display("text/html", "<p>BokehJS successfully loaded.</p>")
end
57 changes: 37 additions & 20 deletions src/generate.jl
Expand Up @@ -24,7 +24,7 @@ function Bokehjs.Glyph(glyph::Glyph,
Bokehjs.Glyph(coldata, xrange, yrange, glyphspec)
end

function genmodels(plot::Plot)
function _genmodels(plot::Plot)
bkplot = Bokehjs.Plot()
doc = Bokehjs.uuid4()
obs = Dict{String, BkAny}[]
Expand Down Expand Up @@ -92,7 +92,7 @@ function genmodels(plot::Plot)
(json(obs), plotcontext)
end

function obdict(ob::Bokehjs.PlotObject, doc::Bokehjs.UUID)
function _obdict(ob::Bokehjs.PlotObject, doc::Bokehjs.UUID)
d = Dict{String, BkAny}()
d["id"] = ob.uuid
extra_attrs = typeof(ob).names
Expand All @@ -111,27 +111,40 @@ function obdict(ob::Bokehjs.PlotObject, doc::Bokehjs.UUID)
return d
end

pushdict!(obs::Any, ob::Bokehjs.PlotObject, doc::Bokehjs.UUID) = push!(obs, obdict(ob, doc))
pushdict!(obs::Any, ob::Bokehjs.PlotObject, doc::Bokehjs.UUID) = push!(obs, _obdict(ob, doc))

get_resources_dir() = Pkg.dir("Bokeh", "deps", "resources")
_get_resources_dir() = Pkg.dir("Bokeh", "templates")

# DEFAULT_PATH = "_default _resources _directory"
function gettemplate(template::String="basic.html", path::Union(String, Nothing)=nothing)
path = path == nothing ? get_resources_dir() : path
function _gettemplate(template::String, path::Union(String, Nothing)=nothing)
path = path == nothing ? _get_resources_dir() : path
fname = joinpath(path, template)
open(readall, fname, "r")
end

function bokehjs_paths(minified::Bool=true)
function _bokehjs_paths(minified::Bool=true)
dir = Pkg.dir("Bokeh", "deps", "bokehjs")
jspath = joinpath(dir, "js", minified ? "bokeh.min.js" : "bokeh.js")
csspath = joinpath(dir, "css", minified ? "bokeh.min.css" : "bokeh.css")
(jspath, csspath)
end

function rendertemplate(models::String, plotcon::Bokehjs.PlotContext, fname::String)
template = gettemplate()
jspath, csspath = bokehjs_paths(!DEBUG)
function _render_jscss(jspath::String, csspath::String, buildin::Bool)
if !buildin
return "<link rel=\"stylesheet\" href=\"$csspath\" type=\"text/css\" />\n"*
"<script type=\"text/javascript\" src=\"$jspath\"></script>\n"
else
css = outfile_content = open(readall, csspath, "r")
js = outfile_content = open(readall, jspath, "r")
return "<style>\n$css\n</style><script type=\"text/javascript\">\n$js\n</script>\n"
end
end

function _rendertemplate(models::String, plotcon::Bokehjs.PlotContext, isijulia::Bool)
base = isijulia ? _gettemplate("ijulia.html") : _gettemplate("standalone.html")
main = _gettemplate("main.html")
body = _gettemplate("body.html")
jspath, csspath = _bokehjs_paths(!DEBUG)
jscss = _render_jscss(jspath, csspath, isijulia)
if DEBUG
open("bokeh_models.json", "w") do f
print(f, models)
Expand All @@ -140,18 +153,22 @@ function rendertemplate(models::String, plotcon::Bokehjs.PlotContext, fname::Str
context = Dict{String, String}([
"model_id" => string(plotcon.uuid),
"all_models" => models,
"div_id" => string(Bokehjs.uuid4()),
"js_path" => jspath,
"css_path" => csspath
"div_id" => string(Bokehjs.uuid4())
])
result = render(template, context)
ispath(fname) && warn("$fname already exists, overwriting")
open(fname, "w") do f
print(f, result)
end
main = render(main, context)
body = render(body, context)
maincontext = Dict{String, String}([
"jscss" => jscss,
"main" => main,
"body" => body,
])
result = render(base, maincontext)
end


function renderplot(plot::Plot, isijulia::Bool)
modelsjson, plotcontext = _genmodels(plot)
_rendertemplate(modelsjson, plotcontext, isijulia)
end



8 changes: 0 additions & 8 deletions src/objects.jl
Expand Up @@ -71,14 +71,6 @@ type Plot
height::Int
end

function Base.show(io::IO, p::Plot)
print(io, "Plot(\"$(p.title)\" with $(length(p.datacolumns)) datacolumns)")
end

function showplot(plot::Plot)
openhtml(plot.filename)
end

# heavily borrowed from Winston, thanks Winston!

const chartokens = [
Expand Down
6 changes: 1 addition & 5 deletions src/plot.jl
Expand Up @@ -50,10 +50,6 @@ function plot(columns::Array{DataColumn, 1};
title::String=TITLE, width::Int=WIDTH, height::Int=HEIGHT,
filename::String=FILENAME, autoopen::Bool=AUTOOPEN)
plt = Plot(columns, filename, title, width, height)
if autoopen
models, plotcon = genmodels(plt)
rendertemplate(models, plotcon, plt.filename)
showplot(plt)
end
!isinteractive() && autoopen && display(plt)
return plt
end
27 changes: 0 additions & 27 deletions templates/basic.html

This file was deleted.

1 change: 1 addition & 0 deletions templates/body.html
@@ -0,0 +1 @@
<div class="plotdiv" id="{{ div_id }}"></div>
2 changes: 2 additions & 0 deletions templates/ijulia.html
@@ -0,0 +1,2 @@
{{{ main }}}
{{{ body }}}
14 changes: 14 additions & 0 deletions templates/main.html
@@ -0,0 +1,14 @@
<script type="text/javascript">
$(function() {
var modelid = "{{ model_id }}";
var modeltype = "PlotContext";
var elementid = "{{ div_id }}";
console.log(" modeltype:", modeltype);
console.log(" modelid:", modelid);
console.log(" elementid:", elementid);
var all_models = {{{ all_models }}};
Bokeh.load_models(all_models);
var model = Bokeh.Collections(modeltype).get(modelid);
var view = new model.default_view({model: model, el: "#{{ div_id }}"});
});
</script>
11 changes: 11 additions & 0 deletions templates/standalone.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{{{ jscss }}}
{{{ main }}}
</head>
<body>
{{{ body }}}
</body>
</html>

0 comments on commit 6d2f0a6

Please sign in to comment.