Class: Charty::Backends::PlotlyHelpers::NotebookRenderer
- Inherits:
-
HtmlRenderer
- Object
- HtmlRenderer
- Charty::Backends::PlotlyHelpers::NotebookRenderer
show all
- 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
Constructor Details
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 = " <script type=\"text/javascript\">\n %{win_config}\n %{mathjax_config}\n if (typeof require !== 'undefined') {\n require.undef(\"plotly\");\n requirejs.config({\n paths: {\n 'plotly': ['https://cdn.plot.ly/plotly-latest.min']\n }\n });\n require(['plotly'], function (Plotly) {\n window._Plotly = Plotly;\n });\n }\n </script>\n END_SCRIPT\n else\n script = <<~END_SCRIPT % {script: get_plotlyjs, win_config: window_plotly_config, mathjax_config: mathjax_config}\n <script type=\"text/javascript\">\n %{win_config}\n %{mathjax_config}\n if (typeof require !== 'undefined') {\n require.undef(\"plotly\");\n define('plotly', function (require, exports, module) {\n %{script}\n });\n require(['plotly'], function (Plotly) {\n window._Plotly = Plotly;\n });\n }\n </script>\n END_SCRIPT\n end\n IRuby.display(script, mime: \"text/html\")\n @initialized = true\n nil\nend\n" % {win_config: window_plotly_config, mathjax_config: mathjax_config}
|
#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(" var gd = document.getElementById('%{plot_id}');\n var x = new MutationObserver(function (mutations, observer) {\n var display = window.getComputedStyle(gd).display;\n if (!display || display === 'none') {\n console.log([gd, 'removed']);\n Plotly.purge(gd);\n observer.disconnect();\n }\n });\n\n // Listen for the removal of the full notebook cell\n var notebookContainer = gd.closest('#notebook-container');\n if (notebookContainer) {\n x.observe(notebookContainer, {childList: true});\n }\n\n // Listen for the clearing of the current output cell\n var outputEl = gd.closest('.output');\n if (outputEl) {\n x.observe(outputEl, {childList: true});\n }\n END_POST_SCRIPT\n\n super(figure, element_id: element_id, post_script: post_script)\nend\n")
|