@@ -98,6 +98,34 @@ def get_plotlyjs():
98
98
return plotlyjs
99
99
100
100
101
+ def _build_resize_script (plotdivid ):
102
+ resize_script = (
103
+ '<script type="text/javascript">'
104
+ 'window.addEventListener("resize", function(){{'
105
+ 'Plotly.Plots.resize(document.getElementById("{id}"));}});'
106
+ '</script>'
107
+ ).format (id = plotdivid )
108
+ return resize_script
109
+
110
+
111
+ def _build_mathjax_script (url ):
112
+ return ('<script src="{url}?config=TeX-AMS-MML_SVG"></script>'
113
+ .format (url = url ))
114
+
115
+
116
+ # Build script to set global PlotlyConfig object. This must execute before
117
+ # plotly.js is loaded.
118
+ _window_plotly_config = """\
119
+ <script type="text/javascript">\
120
+ window.PlotlyConfig = {MathJaxConfig: 'local'};\
121
+ </script>"""
122
+
123
+ _mathjax_config = """\
124
+ <script type="text/javascript">\
125
+ if (window.MathJax) {MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}\
126
+ </script>"""
127
+
128
+
101
129
def get_image_download_script (caller ):
102
130
"""
103
131
This function will return a script that will download an image of a Plotly
@@ -168,23 +196,26 @@ def init_notebook_mode(connected=False):
168
196
if connected :
169
197
# Inject plotly.js into the output cell
170
198
script_inject = (
171
- ''
199
+ '{win_config}'
200
+ '{mathjax_config}'
172
201
'<script>'
173
- 'requirejs.config({'
174
- 'paths: { '
202
+ 'requirejs.config({{ '
203
+ 'paths: {{ '
175
204
# Note we omit the extension .js because require will include it.
176
- '\' plotly\' : [\' https://cdn.plot.ly/plotly-latest.min\' ]},'
177
- '});'
205
+ '\' plotly\' : [\' https://cdn.plot.ly/plotly-latest.min\' ]}} ,'
206
+ '}} );'
178
207
'if(!window.Plotly) {{'
179
208
'require([\' plotly\' ],'
180
- 'function(plotly) {window.Plotly=plotly;});'
209
+ 'function(plotly) {{ window.Plotly=plotly;} });'
181
210
'}}'
182
211
'</script>'
183
- )
212
+ ).format (win_config = _window_plotly_config ,
213
+ mathjax_config = _mathjax_config )
184
214
else :
185
215
# Inject plotly.js into the output cell
186
216
script_inject = (
187
- ''
217
+ '{win_config}'
218
+ '{mathjax_config}'
188
219
'<script type=\' text/javascript\' >'
189
220
'if(!window.Plotly){{'
190
221
'define(\' plotly\' , function(require, exports, module) {{'
@@ -195,7 +226,9 @@ def init_notebook_mode(connected=False):
195
226
'}});'
196
227
'}}'
197
228
'</script>'
198
- '' ).format (script = get_plotlyjs ())
229
+ '' ).format (script = get_plotlyjs (),
230
+ win_config = _window_plotly_config ,
231
+ mathjax_config = _mathjax_config )
199
232
200
233
display_bundle = {
201
234
'text/html' : script_inject ,
@@ -450,21 +483,11 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
450
483
ipython_display .display (ipython_display .HTML (script ))
451
484
452
485
453
- def _build_resize_script (plotdivid ):
454
- resize_script = (
455
- '<script type="text/javascript">'
456
- 'window.addEventListener("resize", function(){{'
457
- 'Plotly.Plots.resize(document.getElementById("{id}"));}});'
458
- '</script>'
459
- ).format (id = plotdivid )
460
- return resize_script
461
-
462
-
463
486
def plot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ,
464
487
validate = True , output_type = 'file' , include_plotlyjs = True ,
465
488
filename = 'temp-plot.html' , auto_open = True , image = None ,
466
489
image_filename = 'plot_image' , image_width = 800 , image_height = 600 ,
467
- config = None ):
490
+ config = None , include_mathjax = False ):
468
491
""" Create a plotly graph locally as an HTML document or string.
469
492
470
493
Example:
@@ -558,6 +581,22 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
558
581
config (default=None) -- Plot view options dictionary. Keyword arguments
559
582
`show_link` and `link_text` set the associated options in this
560
583
dictionary if it doesn't contain them already.
584
+ include_mathjax (False | 'cdn' | path - default=False) --
585
+ Specifies how the MathJax.js library is included in the output html
586
+ file or div string. MathJax is required in order to display labels
587
+ with LaTeX typesetting.
588
+
589
+ If False, no script tag referencing MathJax.js will be included in the
590
+ output. HTML files generated with this option will not be able to
591
+ display LaTeX typesetting.
592
+
593
+ If 'cdn', a script tag that references a MathJax CDN location will be
594
+ included in the output. HTML files generated with this option will be
595
+ able to display LaTeX typesetting as long as they have internet access.
596
+
597
+ If a string that ends in '.js', a script tag is included that
598
+ references the specified path. This approach can be used to point the
599
+ resulting HTML file to an alternative CDN.
561
600
"""
562
601
if output_type not in ['div' , 'file' ]:
563
602
raise ValueError (
@@ -577,31 +616,60 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
577
616
figure_or_data , config , validate ,
578
617
'100%' , '100%' , global_requirejs = False )
579
618
619
+ # Build resize_script
580
620
resize_script = ''
581
621
if width == '100%' or height == '100%' :
582
622
resize_script = _build_resize_script (plotdivid )
583
623
624
+ # Process include_plotlyjs and build plotly_js_script
625
+ include_plotlyjs_orig = include_plotlyjs
584
626
if isinstance (include_plotlyjs , six .string_types ):
585
627
include_plotlyjs = include_plotlyjs .lower ()
586
628
587
629
if include_plotlyjs == 'cdn' :
588
- plotly_js_script = """\
630
+ plotly_js_script = _window_plotly_config + """\
589
631
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>"""
590
632
elif include_plotlyjs == 'directory' :
591
- plotly_js_script = '<script src="plotly.min.js"></script>'
633
+ plotly_js_script = (_window_plotly_config +
634
+ '<script src="plotly.min.js"></script>' )
592
635
elif (isinstance (include_plotlyjs , six .string_types ) and
593
636
include_plotlyjs .endswith ('.js' )):
594
- plotly_js_script = '<script src="{url}"></script>' .format (
595
- url = include_plotlyjs )
637
+ plotly_js_script = (_window_plotly_config +
638
+ '<script src="{url}"></script>' .format (
639
+ url = include_plotlyjs_orig ))
596
640
elif include_plotlyjs :
597
641
plotly_js_script = '' .join ([
642
+ _window_plotly_config ,
598
643
'<script type="text/javascript">' ,
599
644
get_plotlyjs (),
600
645
'</script>' ,
601
646
])
602
647
else :
603
648
plotly_js_script = ''
604
649
650
+ # Process include_mathjax and build mathjax_script
651
+ include_mathjax_orig = include_mathjax
652
+ if isinstance (include_mathjax , six .string_types ):
653
+ include_mathjax = include_mathjax .lower ()
654
+
655
+ if include_mathjax == 'cdn' :
656
+ mathjax_script = _build_mathjax_script (
657
+ url = ('https://cdnjs.cloudflare.com'
658
+ '/ajax/libs/mathjax/2.7.5/MathJax.js' )) + _mathjax_config
659
+ elif (isinstance (include_mathjax , six .string_types ) and
660
+ include_mathjax .endswith ('.js' )):
661
+ mathjax_script = _build_mathjax_script (
662
+ url = include_mathjax_orig ) + _mathjax_config
663
+ elif not include_mathjax :
664
+ mathjax_script = ''
665
+ else :
666
+ raise ValueError ("""\
667
+ Invalid value of type {typ} received as the include_mathjax argument
668
+ Received value: {val}
669
+
670
+ include_mathjax may be specified as False, 'cdn', or a string ending with '.js'
671
+ """ .format (typ = type (include_mathjax ), val = repr (include_mathjax )))
672
+
605
673
if output_type == 'file' :
606
674
with open (filename , 'w' ) as f :
607
675
if image :
@@ -624,6 +692,7 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
624
692
'<html>' ,
625
693
'<head><meta charset="utf-8" /></head>' ,
626
694
'<body>' ,
695
+ mathjax_script ,
627
696
plotly_js_script ,
628
697
plot_html ,
629
698
resize_script ,
@@ -650,6 +719,7 @@ def plot(figure_or_data, show_link=True, link_text='Export to plot.ly',
650
719
651
720
return '' .join ([
652
721
'<div>' ,
722
+ mathjax_script ,
653
723
plotly_js_script ,
654
724
plot_html ,
655
725
resize_script ,
0 commit comments