Class: Charty::Backends::PlotlyHelpers::NotebookRenderer
- Inherits:
-
HtmlRenderer
- Object
- HtmlRenderer
- Charty::Backends::PlotlyHelpers::NotebookRenderer
- Defined in:
- lib/charty/backends/plotly_helpers/notebook_renderer.rb
Constant Summary
Constants inherited from HtmlRenderer
HtmlRenderer::DEFAULT_HEIGHT, HtmlRenderer::DEFAULT_WIDTH, HtmlRenderer::MATHJAX_CDN_URL, HtmlRenderer::PLOTLY_LATEST_CDN_URL, HtmlRenderer::PLOTLY_URL
Instance Method Summary collapse
- #activate ⇒ Object
-
#initialize(use_cdn: false) ⇒ NotebookRenderer
constructor
A new instance of NotebookRenderer.
- #render(figure, element_id: nil, post_script: nil) ⇒ Object
Constructor Details
#initialize(use_cdn: false) ⇒ NotebookRenderer
Returns a new instance of NotebookRenderer.
5 6 7 8 |
# File 'lib/charty/backends/plotly_helpers/notebook_renderer.rb', line 5 def initialize(use_cdn: false) super(use_cdn: use_cdn, full_html: false, requirejs: true) @initialized = false end |
Instance Method Details
#activate ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/charty/backends/plotly_helpers/notebook_renderer.rb', line 10 def activate return if @initialized unless IRubyHelper.iruby_notebook? raise "IRuby is unavailable" end if @use_cdn script = <<~END_SCRIPT % {win_config: window_plotly_config, mathjax_config: mathjax_config} <script type="text/javascript"> %{win_config} %{mathjax_config} if (typeof require !== 'undefined') { require.undef("plotly"); requirejs.config({ paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min'] } }); require(['plotly'], function (Plotly) { window._Plotly = Plotly; }); } </script> END_SCRIPT else script = <<~END_SCRIPT % {script: get_plotlyjs, win_config: window_plotly_config, mathjax_config: mathjax_config} <script type="text/javascript"> %{win_config} %{mathjax_config} if (typeof require !== 'undefined') { require.undef("plotly"); define('plotly', function (require, exports, module) { %{script} }); require(['plotly'], function (Plotly) { window._Plotly = Plotly; }); } </script> END_SCRIPT end IRuby.display(script, mime: "text/html") @initialized = true nil end |
#render(figure, element_id: nil, post_script: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/charty/backends/plotly_helpers/notebook_renderer.rb', line 57 def render(figure, element_id: nil, post_script: nil) ary = Array.try_convert(post_script) post_script = ary || [post_script] post_script.unshift(<<~END_POST_SCRIPT) var gd = document.getElementById('%{plot_id}'); var x = new MutationObserver(function (mutations, observer) { var display = window.getComputedStyle(gd).display; if (!display || display === 'none') { console.log([gd, 'removed']); Plotly.purge(gd); observer.disconnect(); } }); // Listen for the removal of the full notebook cell var notebookContainer = gd.closest('#notebook-container'); if (notebookContainer) { x.observe(notebookContainer, {childList: true}); } // Listen for the clearing of the current output cell var outputEl = gd.closest('.output'); if (outputEl) { x.observe(outputEl, {childList: true}); } END_POST_SCRIPT super(figure, element_id: element_id, post_script: post_script) end |