Skip to content

Commit 72cc18f

Browse files
committed
Enable to render png image on GitHub viewer
1 parent 4c27606 commit 72cc18f

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

lib/nyaplot/exportable.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ module Nyaplot
33
# !! Classes to include PlotBase should have #to_json. !!
44
#
55
module Exportable
6+
PATH_NYAPLOT = "https://cdn.rawgit.com/domitry/Nyaplotjs/bb48705474f7878c0a83f267675e019658421b52/release/nyaplot"
7+
68
def raise_display_failed
79
raise "This method works only on IRuby. Use #export_html or install IRuby."
810
end
911

1012
# generate static html file
1113
# @return [String] generated html
12-
def generate_html(to_png=false)
13-
path = File.expand_path("../templates/iruby.erb", __FILE__)
14+
def generate_html(temp_path)
15+
path = File.expand_path(temp_path, __FILE__)
1416
id = SecureRandom.uuid
1517
model = to_json
1618
template = File.read(path)
@@ -20,8 +22,7 @@ def generate_html(to_png=false)
2022
# export static html file
2123
def export_html(path="./plot.html", to_png=false)
2224
path = File.expand_path(path, Dir::pwd)
23-
body = generate_html(to_png)
24-
25+
body = generate_html("../templates/iruby.erb")
2526
temp_path = File.expand_path("../templates/static_html.erb", __FILE__)
2627
template = File.read(temp_path)
2728
num = File.write(path, ERB.new(template).result(binding))
@@ -30,14 +31,14 @@ def export_html(path="./plot.html", to_png=false)
3031

3132
def to_png
3233
raise_display_failed unless defined? IRuby
33-
html = generate_html(true)
34+
html = generate_html("../templates/to_png.erb")
3435
IRuby.display html, mime: 'text/html'
3536
end
3637

3738
# show plot automatically on IRuby notebook
3839
def to_iruby
3940
raise_display_failed unless defined? IRuby
40-
html = generate_html
41+
html = generate_html("../templates/iruby.erb")
4142
['text/html', html]
4243
end
4344

lib/nyaplot/templates/iruby.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<div id='vis-<%= id %>'></div>
22
<script>
33
require.config({paths: {
4-
Nyaplot: "https://cdn.rawgit.com/domitry/Nyaplotjs/a885e2d919adeb6919bf256fd078ea36b052f2e7/release/nyaplot"
4+
Nyaplot: "<%= PATH_NYAPLOT %>"
55
}});
66

77
require(["Nyaplot"], function(Nyaplot){
8-
<% if to_png == true %>
9-
Nyaplot.core.to_png('#vis-<%= id %>', <%= model %>);
10-
<% else %>
118
Nyaplot.core.parse('#vis-<%= id %>', <%= model %>);
12-
<% end %>
139
});
1410
</script>

lib/nyaplot/templates/to_png.erb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<div id='vis-<%= id %>'></div>
2+
<script>
3+
require.config({paths: {
4+
Nyaplot: "<%= PATH_NYAPLOT %>"
5+
}});
6+
7+
require(["Nyaplot"], function(Nyaplot){
8+
var vis_id = "vis-<%= id %>";
9+
var pr = Nyaplot.core.to_png('#'+vis_id, <%= model %>);
10+
11+
function searchCell(uuid){
12+
var n = IPython.notebook.ncells();
13+
for(var i=0; i<n; i++){
14+
var cell = IPython.notebook.get_cell(i);
15+
var outputs = cell.output_area.outputs.filter(function(out){
16+
var html = out.data["text/html"];
17+
if(typeof html == "undefined")return false;
18+
if(html.includes(uuid))return true;
19+
return false;
20+
});
21+
if(outputs.length>0)return cell;
22+
}
23+
return null;
24+
}
25+
26+
if(typeof window.IPython != "undefined"){
27+
pr.then(function(selection){
28+
try{
29+
var html = selection.node().outerHTML;
30+
var cell = searchCell(vis_id);
31+
if(cell == null)throw new Error("The cell whose id is " + vis_id + " not found.");
32+
cell.output_area.outputs[0].data["text/html"] = html;
33+
}
34+
catch(e){
35+
console.warn("Maybe the front-end API of Jupyter has changed.\nmessage:" + e.message);
36+
}
37+
});
38+
}
39+
});
40+
</script>

0 commit comments

Comments
 (0)