8
8
import json
9
9
import os
10
10
import uuid
11
-
12
- import requests
11
+ import warnings
12
+ from pkg_resources import resource_string
13
13
14
14
from plotly import session , tools , utils
15
15
from plotly .exceptions import PlotlyError
16
16
17
- PLOTLY_OFFLINE_DIRECTORY = plotlyjs_path = os .path .expanduser (
18
- os .path .join (* '~/.plotly/plotlyjs' .split ('/' )))
19
- PLOTLY_OFFLINE_BUNDLE = os .path .join (PLOTLY_OFFLINE_DIRECTORY ,
20
- 'plotly-ipython-offline-bundle.js' )
21
-
22
17
23
18
__PLOTLY_OFFLINE_INITIALIZED = False
24
19
25
20
26
21
def download_plotlyjs (download_url ):
27
- if not os .path .exists (PLOTLY_OFFLINE_DIRECTORY ):
28
- os .makedirs (PLOTLY_OFFLINE_DIRECTORY )
29
-
30
- res = requests .get (download_url )
31
- res .raise_for_status ()
22
+ warnings .warn ('''
23
+ `download_plotlyjs` is deprecated and will be removed in the
24
+ next release. plotly.js is shipped with this module, it is no
25
+ longer necessary to download this bundle separately.
26
+ ''' , DeprecationWarning )
27
+ pass
32
28
33
- with open (PLOTLY_OFFLINE_BUNDLE , 'wb' ) as f :
34
- f .write (res .content )
35
29
36
- print ('\n ' .join ([
37
- 'Success! Now start an IPython notebook and run the following ' +
38
- 'code to make your first offline graph:' ,
39
- '' ,
40
- 'import plotly' ,
41
- 'plotly.offline.init_notebook_mode() '
42
- '# run at the start of every ipython notebook' ,
43
- 'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])'
44
- ]))
30
+ def get_plotlyjs ():
31
+ path = os .path .join ('offline' , 'plotly.min.js' )
32
+ plotlyjs = resource_string ('plotly' , path ).decode ('utf-8' )
33
+ return plotlyjs
45
34
46
35
47
36
def init_notebook_mode ():
@@ -55,24 +44,20 @@ def init_notebook_mode():
55
44
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
56
45
from IPython .display import HTML , display
57
46
58
- if not os .path .exists (PLOTLY_OFFLINE_BUNDLE ):
59
- raise PlotlyError ('Plotly Offline source file at {source_path} '
60
- 'is not found.\n '
61
- 'If you have a Plotly Offline license, then try '
62
- 'running plotly.offline.download_plotlyjs(url) '
63
- 'with a licensed download url.\n '
64
- "Don't have a Plotly Offline license? "
65
- 'Contact [email protected] learn more about licensing.\n '
66
-
67
- .format (source_path = PLOTLY_OFFLINE_BUNDLE ))
68
-
69
47
global __PLOTLY_OFFLINE_INITIALIZED
70
48
__PLOTLY_OFFLINE_INITIALIZED = True
71
49
display (HTML ('<script type="text/javascript">' +
72
- open (PLOTLY_OFFLINE_BUNDLE ).read () + '</script>' ))
50
+ # ipython's includes `require` as a global, which
51
+ # conflicts with plotly.js. so, unrequire it.
52
+ 'require=requirejs=define=undefined;' +
53
+ '</script>' +
54
+ '<script type="text/javascript">' +
55
+ get_plotlyjs () +
56
+ '</script>' ))
73
57
74
58
75
- def iplot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ):
59
+ def iplot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ,
60
+ validate = True ):
76
61
"""
77
62
Draw plotly graphs inside an IPython notebook without
78
63
connecting to an external server.
@@ -90,6 +75,11 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
90
75
of the chart that will export the chart to
91
76
Plotly Cloud or Plotly Enterprise
92
77
link_text (default='Export to plot.ly') -- the text of export link
78
+ validate (default=True) -- validate that all of the keys in the figure
79
+ are valid? omit if your version of plotly.js
80
+ has become outdated with your version of
81
+ graph_reference.json or if you need to include
82
+ extra, unnecessary keys in your figure.
93
83
94
84
Example:
95
85
```
@@ -112,15 +102,10 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
112
102
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
113
103
114
104
from IPython .display import HTML , display
115
- if isinstance (figure_or_data , dict ):
116
- data = figure_or_data ['data' ]
117
- layout = figure_or_data .get ('layout' , {})
118
- else :
119
- data = figure_or_data
120
- layout = {}
105
+ figure = tools .return_figure_from_figure_or_data (figure_or_data , validate )
121
106
122
- width = layout .get ('width' , '100%' )
123
- height = layout .get ('height' , 525 )
107
+ width = figure . get ( ' layout' , {}) .get ('width' , '100%' )
108
+ height = figure . get ( ' layout' , {}) .get ('height' , 525 )
124
109
try :
125
110
float (width )
126
111
except (ValueError , TypeError ):
@@ -136,11 +121,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
136
121
width = str (width ) + 'px'
137
122
138
123
plotdivid = uuid .uuid4 ()
139
- jdata = json .dumps (data , cls = utils .PlotlyJSONEncoder )
140
- jlayout = json .dumps (layout , cls = utils .PlotlyJSONEncoder )
124
+ jdata = json .dumps (figure . get ( ' data' , []) , cls = utils .PlotlyJSONEncoder )
125
+ jlayout = json .dumps (figure . get ( ' layout' , {}) , cls = utils .PlotlyJSONEncoder )
141
126
142
- if show_link is False :
143
- link_text = ''
127
+ config = {}
128
+ config ['showLink' ] = show_link
129
+ config ['linkText' ] = link_text
130
+ jconfig = json .dumps (config )
144
131
145
132
plotly_platform_url = session .get_session_config ().get ('plotly_domain' ,
146
133
'https://plot.ly' )
@@ -156,18 +143,17 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
156
143
'<script type="text/javascript">'
157
144
'window.PLOTLYENV=window.PLOTLYENV || {};'
158
145
'window.PLOTLYENV.BASE_URL="' + plotly_platform_url + '";'
159
- 'Plotly.LINKTEXT = "' + link_text + '";'
160
146
'</script>'
161
147
))
162
148
163
149
script = '\n ' .join ([
164
- 'Plotly.plot("{id}", {data}, {layout}).then(function() {{' ,
150
+ 'Plotly.plot("{id}", {data}, {layout}, {config} ).then(function() {{' ,
165
151
' $(".{id}.loading").remove();' ,
166
152
'}})'
167
153
]).format (id = plotdivid ,
168
154
data = jdata ,
169
155
layout = jlayout ,
170
- link_text = link_text )
156
+ config = jconfig )
171
157
172
158
display (HTML (''
173
159
'<div class="{id} loading" style="color: rgb(50,50,50);">'
0 commit comments