From c8a4df096148aeb2b23d32f0783c2787d354c8bd Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Fri, 1 Aug 2014 05:46:27 +0200 Subject: [PATCH 1/2] automatically call init_iruby requires IRuby master for now --- lib/nyaplot/core.rb | 20 +++++++++---------- .../templates/{init.html.erb => init.js.erb} | 2 -- 2 files changed, 10 insertions(+), 12 deletions(-) rename lib/nyaplot/templates/{init.html.erb => init.js.erb} (98%) diff --git a/lib/nyaplot/core.rb b/lib/nyaplot/core.rb index 7d5b4ce..a5b1530 100644 --- a/lib/nyaplot/core.rb +++ b/lib/nyaplot/core.rb @@ -11,16 +11,6 @@ def self.extension_lists @@extension_lists end - def self.init_iruby - raise "IRuby notebook is not loaded." unless defined? IRuby - - path = File.expand_path("../templates/init.html.erb", __FILE__) - template = File.read(path) - dep_libraries = @@dep_libraries - html = ERB.new(template).result(binding) - return IRuby.html(html) - end - def self.add_extension(name) @@extension_lists.push(name) end @@ -28,4 +18,14 @@ def self.add_extension(name) def self.add_dependency(name, url) @@dep_libraries[name]=url; end + + def self.init_iruby + path = File.expand_path("../templates/init.js.erb", __FILE__) + template = File.read(path) + dep_libraries = @@dep_libraries + js = ERB.new(template).result(binding) + IRuby.display(IRuby.javascript(js)) + end + + init_iruby if defined? IRuby end diff --git a/lib/nyaplot/templates/init.html.erb b/lib/nyaplot/templates/init.js.erb similarity index 98% rename from lib/nyaplot/templates/init.html.erb rename to lib/nyaplot/templates/init.js.erb index f7142cd..9ec8343 100644 --- a/lib/nyaplot/templates/init.html.erb +++ b/lib/nyaplot/templates/init.js.erb @@ -1,4 +1,3 @@ - From 2c16e613d572879bf9088335df9c3a9c31257d25 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Fri, 1 Aug 2014 05:58:49 +0200 Subject: [PATCH 2/2] Add Frame#to_iruby, Plot#to_iruby - This allows automatic plotting by just returning the Plot/Frame object - Plot#show and Frame#show calls IRuby#display --- lib/nyaplot/frame.rb | 11 +++++++---- lib/nyaplot/plot.rb | 8 +++++--- lib/nyaplot/templates/iruby.erb | 3 ++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/nyaplot/frame.rb b/lib/nyaplot/frame.rb index 508ccbe..6fdb3cb 100644 --- a/lib/nyaplot/frame.rb +++ b/lib/nyaplot/frame.rb @@ -27,16 +27,19 @@ def export_html template = File.read(path) model = self.to_json html = ERB.new(template).result(binding) - return html + html end - def show + def to_iruby path = File.expand_path("../templates/iruby.erb", __FILE__) template = File.read(path) id = SecureRandom.uuid() model = self.to_json - html = ERB.new(template).result(binding) - return IRuby.html(html) + ['text/html', ERB.new(template).result(binding)] + end + + def show + IRuby.display(self) end def configure(&block) diff --git a/lib/nyaplot/plot.rb b/lib/nyaplot/plot.rb index fe2a5ab..0d7048d 100644 --- a/lib/nyaplot/plot.rb +++ b/lib/nyaplot/plot.rb @@ -27,10 +27,12 @@ def add_with_df(df, type, *labels) return diagram end + def to_iruby + Frame.new.tap {|f| f.add(self) }.to_iruby + end + def show - frame = Frame.new - frame.add(self) - frame.show + Frame.new.tap {|f| f.add(self) }.show end def df_list diff --git a/lib/nyaplot/templates/iruby.erb b/lib/nyaplot/templates/iruby.erb index fa30412..582bec7 100644 --- a/lib/nyaplot/templates/iruby.erb +++ b/lib/nyaplot/templates/iruby.erb @@ -8,7 +8,8 @@ if(window['Nyaplot']==undefined){ window.addEventListener('load_nyaplot', render, false); return; + } else { + render(); } - render(); })();