forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffline.py
542 lines (459 loc) · 19.7 KB
/
offline.py
1
2
3
4
5
6
7
8
9
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
56
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
""" Plotly Offline
A module to use Plotly's graphing library with Python
without connecting to a public or private plotly enterprise
server.
"""
from __future__ import absolute_import
import json
import os
import shutil
import uuid
import warnings
from pkg_resources import resource_string
import webbrowser
import plotly
from plotly import tools, utils
from plotly.exceptions import PlotlyError
try:
import IPython
from IPython.display import HTML, display
_ipython_imported = True
except ImportError:
_ipython_imported = False
try:
import matplotlib
_matplotlib_imported = True
except ImportError:
_matplotlib_imported = False
__PLOTLY_OFFLINE_INITIALIZED = False
__SERVE_PLOTLYJS_FROM_DIRECTORY = False
def download_plotlyjs(download_url):
warnings.warn('''
`download_plotlyjs` is deprecated and will be removed in the
next release. plotly.js is shipped with this module, it is no
longer necessary to download this bundle separately.
''', DeprecationWarning)
pass
def get_plotlyjs():
global __SERVE_PLOTLYJS_FROM_DIRECTORY
if __SERVE_PLOTLYJS_FROM_DIRECTORY:
plotlyjs = '</script><script src="plotly.min.js">'
else:
path = os.path.join('offline', 'plotly.min.js')
plotlyjs = resource_string('plotly', path).decode('utf-8')
return plotlyjs
def serve_plotlyjs_from_directory():
"""
Sets global plotly.offline.__SERVE_PLOTLYJS_FROM_DIRECTORY to True,
which will trigger the separation of the html and the plotly.min.js
into two files. This will reduce the overall space required
if muliple plots are created in the same directory, since only one
copy of plotly.min.js will be stored.
"""
global __SERVE_PLOTLYJS_FROM_DIRECTORY
__SERVE_PLOTLYJS_FROM_DIRECTORY = True
return
def init_notebook_mode(connected=False):
"""
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
yet. This is an idempotent method and can and should be called from any
offline methods that require plotly.js to be loaded into the notebook dom.
Keyword arguments:
connected (default=False) -- If True, the plotly.js library will be loaded
from an online CDN. If False, the plotly.js library will be loaded locally
from the plotly python package
Use `connected=True` if you want your notebooks to have smaller file sizes.
In the case where `connected=False`, the entirety of the plotly.js library
will be loaded into the notebook, which will result in a file-size increase
of a couple megabytes. Additionally, because the library will be downloaded
from the web, you and your viewers must be connected to the internet to be
able to view charts within this notebook.
Use `connected=False` if you want you and your collaborators to be able to
create and view these charts regardless of the availability of an internet
connection. This is the default option since it is the most predictable.
Note that under this setting the library will be included inline inside
your notebook, resulting in much larger notebook sizes compared to the case
where `connected=True`.
"""
if not _ipython_imported:
raise ImportError('`iplot` can only run inside an IPython Notebook.')
global __PLOTLY_OFFLINE_INITIALIZED
if connected:
# Inject plotly.js into the output cell
script_inject = (
''
'<script>'
'requirejs.config({'
'paths: { '
# Note we omit the extension .js because require will include it.
'\'plotly\': [\'https://cdn.plot.ly/plotly-latest.min\']},'
'});'
'if(!window.Plotly) {{'
'require([\'plotly\'],'
'function(plotly) {window.Plotly=plotly;});'
'}}'
'</script>'
)
else:
# Inject plotly.js into the output cell
script_inject = (
''
'<script type=\'text/javascript\'>'
'if(!window.Plotly){{'
'define(\'plotly\', function(require, exports, module) {{'
'{script}'
'}});'
'require([\'plotly\'], function(Plotly) {{'
'window.Plotly = Plotly;'
'}});'
'}}'
'</script>'
'').format(script=get_plotlyjs())
display(HTML(script_inject))
__PLOTLY_OFFLINE_INITIALIZED = True
def _plot_html(figure_or_data, show_link, link_text,
validate, default_width, default_height, global_requirejs):
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
width = figure.get('layout', {}).get('width', default_width)
height = figure.get('layout', {}).get('height', default_height)
try:
float(width)
except (ValueError, TypeError):
pass
else:
width = str(width) + 'px'
try:
float(width)
except (ValueError, TypeError):
pass
else:
width = str(width) + 'px'
plotdivid = uuid.uuid4()
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)
config = {}
config['showLink'] = show_link
config['linkText'] = link_text
jconfig = json.dumps(config)
# TODO: The get_config 'source of truth' should
# really be somewhere other than plotly.plotly
plotly_platform_url = plotly.plotly.get_config().get('plotly_domain',
'https://plot.ly')
if (plotly_platform_url != 'https://plot.ly' and
link_text == 'Export to plot.ly'):
link_domain = plotly_platform_url\
.replace('https://', '')\
.replace('http://', '')
link_text = link_text.replace('plot.ly', link_domain)
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format(
id=plotdivid,
data=jdata,
layout=jlayout,
config=jconfig)
optional_line1 = ('require(["plotly"], function(Plotly) {{ '
if global_requirejs else '')
optional_line2 = ('}});' if global_requirejs else '')
plotly_html_div = (
''
'<div id="{id}" style="height: {height}; width: {width};" '
'class="plotly-graph-div">'
'</div>'
'<script type="text/javascript">' +
optional_line1 +
'window.PLOTLYENV=window.PLOTLYENV || {{}};'
'window.PLOTLYENV.BASE_URL="' + plotly_platform_url + '";'
'{script}' +
optional_line2 +
'</script>'
'').format(
id=plotdivid, script=script,
height=height, width=width)
return plotly_html_div, plotdivid, width, height
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
validate=True):
"""
Draw plotly graphs inside an IPython notebook without
connecting to an external server.
To save the chart to Plotly Cloud or Plotly Enterprise, use
`plotly.plotly.iplot`.
To embed an image of the chart, use `plotly.image.ishow`.
figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or
dict or list that describes a Plotly graph.
See https://plot.ly/python/ for examples of
graph descriptions.
Keyword arguments:
show_link (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to
Plotly Cloud or Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js
has become outdated with your version of
graph_reference.json or if you need to include
extra, unnecessary keys in your figure.
Example:
```
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
```
"""
if not __PLOTLY_OFFLINE_INITIALIZED:
raise PlotlyError('\n'.join([
'Plotly Offline mode has not been initialized in this notebook. '
'Run: ',
'',
'import plotly',
'plotly.offline.init_notebook_mode() '
'# run at the start of every ipython notebook',
]))
if not tools._ipython_imported:
raise ImportError('`iplot` can only run inside an IPython Notebook.')
plot_html, plotdivid, width, height = _plot_html(
figure_or_data, show_link, link_text, validate,
'100%', 525, global_requirejs=True)
display(HTML(plot_html))
def plot(figure_or_data,
show_link=True, link_text='Export to plot.ly',
validate=True, output_type='file',
include_plotlyjs=True,
filename='temp-plot.html',
auto_open=True,
):
""" Create a plotly graph locally as an HTML document or string.
Example:
```
from plotly.offline import plot
import plotly.graph_objs as go
plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html')
```
More examples below.
figure_or_data -- a plotly.graph_objs.Figure or plotly.graph_objs.Data or
dict or list that describes a Plotly graph.
See https://plot.ly/python/ for examples of
graph descriptions.
Keyword arguments:
show_link (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js has become outdated
with your version of graph_reference.json or if you need to include
extra, unnecessary keys in your figure.
output_type ('file' | 'div' - default 'file') -- if 'file', then
the graph is saved as a standalone HTML file and `plot`
returns None.
If 'div', then `plot` returns a string that just contains the
HTML <div> that contains the graph and the script to generate the
graph.
Use 'file' if you want to save and view a single graph at a time
in a standalone HTML file.
Use 'div' if you are embedding these graphs in an HTML file with
other graphs or HTML markup, like a HTML report or an website.
include_plotlyjs (default=True) -- If True, include the plotly.js
source code in the output file or string.
Set as False if your HTML file already contains a copy of the plotly.js
library.
filename (default='temp-plot.html') -- The local filename to save the
outputted chart to. If the filename already exists, it will be
overwritten. This argument only applies if `output_type` is 'file'.
auto_open (default=True) -- If True, open the saved file in a
web browser after saving.
This argument only applies if `output_type` is 'file'.
"""
if output_type not in ['div', 'file']:
raise ValueError(
"`output_type` argument must be 'div' or 'file'. "
"You supplied `" + output_type + "``")
if not filename.endswith('.html') and output_type == 'file':
warnings.warn(
"Your filename `" + filename + "` didn't end with .html. "
"Adding .html to the end of your file.")
filename += '.html'
plot_html, plotdivid, width, height = _plot_html(
figure_or_data, show_link, link_text, validate,
'100%', '100%', global_requirejs=False)
resize_script = ''
if width == '100%' or height == '100%':
resize_script = (
''
'<script type="text/javascript">'
'window.removeEventListener("resize");'
'window.addEventListener("resize", function(){{'
'Plotly.Plots.resize(document.getElementById("{id}"));}});'
'</script>'
).format(id=plotdivid)
if output_type == 'file':
src_path = os.path.join(
os.path.dirname(__file__), 'plotly.min.js'
)
# src_path = resource_string('plotly', path)
global __SERVE_PLOTLYJS_FROM_DIRECTORY
if __SERVE_PLOTLYJS_FROM_DIRECTORY:
dest_path = os.path.join(
os.path.dirname( filename ), 'plotly.min.js'
)
if os.path.exists( dest_path ) is False:
shutil.copy( src_path, dest_path )
with open(filename, 'w') as f:
if include_plotlyjs:
plotly_js_script = ''.join([
'<script type="text/javascript">',
get_plotlyjs(),
'</script>',
])
else:
plotly_js_script = ''
f.write(''.join([
'<html>',
'<head><meta charset="utf-8" /></head>',
'<body>',
plotly_js_script,
plot_html,
resize_script,
'</body>',
'</html>']))
url = 'file://' + os.path.abspath(filename)
if auto_open:
webbrowser.open(url)
return url
elif output_type == 'div':
if include_plotlyjs:
return ''.join([
'<div>',
'<script type="text/javascript">',
get_plotlyjs(),
'</script>',
plot_html,
'</div>'
])
else:
return plot_html
def plot_mpl(mpl_fig, resize=False, strip_style=False,
verbose=False, show_link=True, link_text='Export to plot.ly',
validate=True, output_type='file', include_plotlyjs=True,
filename='temp-plot.html', auto_open=True):
"""
Convert a matplotlib figure to a Plotly graph stored locally as HTML.
For more information on converting matplotlib visualizations to plotly
graphs, call help(plotly.tools.mpl_to_plotly)
For more information on creating plotly charts locally as an HTML document
or string, call help(plotly.offline.plot)
mpl_fig -- a matplotlib figure object to convert to a plotly graph
Keyword arguments:
resize (default=False) -- allow plotly to choose the figure size.
strip_style (default=False) -- allow plotly to choose style options.
verbose (default=False) -- print message.
show_link (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to Plotly Cloud or
Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js has become outdated
with your version of graph_reference.json or if you need to include
extra, unnecessary keys in your figure.
output_type ('file' | 'div' - default 'file') -- if 'file', then
the graph is saved as a standalone HTML file and `plot`
returns None.
If 'div', then `plot` returns a string that just contains the
HTML <div> that contains the graph and the script to generate the
graph.
Use 'file' if you want to save and view a single graph at a time
in a standalone HTML file.
Use 'div' if you are embedding these graphs in an HTML file with
other graphs or HTML markup, like a HTML report or an website.
include_plotlyjs (default=True) -- If True, include the plotly.js
source code in the output file or string.
Set as False if your HTML file already contains a copy of the plotly.js
library.
filename (default='temp-plot.html') -- The local filename to save the
outputted chart to. If the filename already exists, it will be
overwritten. This argument only applies if `output_type` is 'file'.
auto_open (default=True) -- If True, open the saved file in a
web browser after saving.
This argument only applies if `output_type` is 'file'.
Example:
```
from plotly.offline import init_notebook_mode, plot_mpl
import matplotlib.pyplot as plt
init_notebook_mode()
fig = plt.figure()
x = [10, 15, 20, 25, 30]
y = [100, 250, 200, 150, 300]
plt.plot(x, y, "o")
plot_mpl(fig)
```
"""
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
return plot(plotly_plot, show_link, link_text, validate, output_type,
include_plotlyjs, filename, auto_open)
def iplot_mpl(mpl_fig, resize=False, strip_style=False,
verbose=False, show_link=True,
link_text='Export to plot.ly', validate=True):
"""
Convert a matplotlib figure to a plotly graph and plot inside an IPython
notebook without connecting to an external server.
To save the chart to Plotly Cloud or Plotly Enterprise, use
`plotly.plotly.plot_mpl`.
For more information on converting matplotlib visualizations to plotly
graphs call `help(plotly.tools.mpl_to_plotly)`
For more information on plotting plotly charts offline in an Ipython
notebook call `help(plotly.offline.iplot)`
mpl_fig -- a matplotlib.figure to convert to a plotly graph
Keyword arguments:
resize (default=False) -- allow plotly to choose the figure size.
strip_style (default=False) -- allow plotly to choose style options.
verbose (default=False) -- print message.
show_link (default=True) -- display a link in the bottom-right corner of
of the chart that will export the chart to
Plotly Cloud or Plotly Enterprise
link_text (default='Export to plot.ly') -- the text of export link
validate (default=True) -- validate that all of the keys in the figure
are valid? omit if your version of plotly.js
has become outdated with your version of
graph_reference.json or if you need to include
extra, unnecessary keys in your figure.
Example:
```
from plotly.offline import init_notebook_mode, iplot_mpl
import matplotlib.pyplot as plt
fig = plt.figure()
x = [10, 15, 20, 25, 30]
y = [100, 250, 200, 150, 300]
plt.plot(x, y, "o")
init_notebook_mode()
iplot_mpl(fig)
```
"""
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
return iplot(plotly_plot, show_link, link_text, validate)
def enable_mpl_offline(resize=False, strip_style=False,
verbose=False, show_link=True,
link_text='Export to plot.ly', validate=True):
"""
Convert mpl plots to locally hosted HTML documents.
This function should be used with the inline matplotlib backend
that ships with IPython that can be enabled with `%pylab inline`
or `%matplotlib inline`. This works by adding an HTML formatter
for Figure objects; the existing SVG/PNG formatters will remain
enabled.
(idea taken from `mpld3._display.enable_notebook`)
Example:
```
from plotly.offline import enable_mpl_offline
import matplotlib.pyplot as plt
enable_mpl_offline()
fig = plt.figure()
x = [10, 15, 20, 25, 30]
y = [100, 250, 200, 150, 300]
plt.plot(x, y, "o")
fig
```
"""
init_notebook_mode()
ip = IPython.core.getipython.get_ipython()
formatter = ip.display_formatter.formatters['text/html']
formatter.for_type(matplotlib.figure.Figure,
lambda fig: iplot_mpl(fig, resize, strip_style, verbose,
show_link, link_text, validate))