@@ -12,7 +12,7 @@ def signup(un, email):
12
12
:un: <string> username
13
13
:email: <string> email address
14
14
'''
15
- payload = {'version' : '0.4 ' , 'un' : un , 'email' : email , 'platform' :'Python' }
15
+ payload = {'version' : '0.5.2 ' , 'un' : un , 'email' : email , 'platform' :'Python' }
16
16
r = requests .post ('https://plot.ly/apimkacct' , data = payload )
17
17
r = json .loads (r .text )
18
18
if 'error' in r .keys ():
@@ -34,10 +34,11 @@ def __init__(self, username=None, key=None,verbose=True):
34
34
self .__filename = None
35
35
self .__fileopt = None
36
36
self .verbose = verbose
37
+ self .open = False
37
38
38
39
def iplot (self , * args , ** kwargs ):
39
40
''' for use in ipython notebooks '''
40
- res = self .plot (* args , ** kwargs )
41
+ res = self .__callplot (* args , ** kwargs )
41
42
width = kwargs .get ('width' , 600 )
42
43
height = kwargs .get ('height' , 600 )
43
44
s = '<iframe height="' + str (height + 50 )+ '" id="igraph" scrolling="no" seamless="seamless" src="' + res ['url' ]+ '/' + str (width )+ '/' + str (height )+ '" width="' + str (width + 50 )+ '"></iframe>'
@@ -48,6 +49,13 @@ def iplot(self, *args, **kwargs):
48
49
return s
49
50
50
51
def plot (self , * args , ** kwargs ):
52
+ res = self .__callplot (* args , ** kwargs )
53
+ if res ['error' ] == '' and self .open :
54
+ from webbrowser import open as wbopen
55
+ wbopen (res ['url' ])
56
+ return res
57
+
58
+ def __callplot (self , * args , ** kwargs ):
51
59
''' Make a plot in plotly.
52
60
Two interfaces:
53
61
1 - ploty.plot(x1, y1[,x2,y2,...],**kwargs)
@@ -131,27 +139,58 @@ def style(self, *args, **kwargs):
131
139
r = self .__makecall (args , un , key , origin , kwargs )
132
140
return r
133
141
142
+ class __plotlyJSONEncoder (json .JSONEncoder ):
143
+ def numpyJSONEncoder (self , obj ):
144
+ try :
145
+ import numpy
146
+ if type (obj ).__module__ == numpy .__name__ :
147
+ l = obj .tolist ()
148
+ d = self .datetimeJSONEncoder (l )
149
+ return d if d else l
150
+ except :
151
+ pass
152
+ return None
153
+ def datetimeJSONEncoder (self , obj ):
154
+ # if datetime or iterable of datetimes, convert to a string that plotly understands
155
+ import datetime
156
+ try :
157
+ if isinstance (obj ,(datetime .datetime , datetime .date )):
158
+ return obj .strftime ('%Y-%m-%d %H:%M:%S' )
159
+ elif isinstance (obj [0 ],(datetime .datetime , datetime .date )):
160
+ return [o .strftime ('%Y-%m-%d %H:%M:%S' ) for o in obj ]
161
+ except :
162
+ pass
163
+ return None
164
+ def pandasJSONEncoder (self , obj ):
165
+ try :
166
+ import pandas
167
+ if isinstance (obj , pandas .DataFrame ):
168
+ return obj .to_json ()
169
+ except :
170
+ pass
171
+ return None
172
+ def default (self , obj ):
173
+ try :
174
+ return json .dumps (obj )
175
+ except TypeError as e :
176
+ encoders = (self .datetimeJSONEncoder , self .numpyJSONEncoder , self .pandasJSONEncoder )
177
+ for encoder in encoders :
178
+ s = encoder (obj )
179
+ if s :
180
+ return s
181
+ raise e
182
+ return json .JSONEncoder .default (self ,obj )
183
+
134
184
def __makecall (self , args , un , key , origin , kwargs ):
135
- version = '0.5'
185
+ version = '0.5.2 '
136
186
platform = 'Python'
137
-
138
- class NumpyAwareJSONEncoder (json .JSONEncoder ):
139
- def default (self , obj ):
140
- try :
141
- import numpy
142
- if isinstance (obj , numpy .ndarray ) and obj .ndim == 1 :
143
- return [x for x in obj ]
144
- return json .JSONEncoder .default (self , obj )
145
- except :
146
- return json .JSONEncoder .default (self , obj )
147
-
148
- args = json .dumps (args , cls = NumpyAwareJSONEncoder )
149
- kwargs = json .dumps (kwargs , cls = NumpyAwareJSONEncoder )
187
+
188
+ args = json .dumps (args , cls = self .__plotlyJSONEncoder )
189
+ kwargs = json .dumps (kwargs , cls = self .__plotlyJSONEncoder )
150
190
url = 'https://plot.ly/clientresp'
151
191
payload = {'platform' : platform , 'version' : version , 'args' : args , 'un' : un , 'key' : key , 'origin' : origin , 'kwargs' : kwargs }
152
192
r = requests .post (url , data = payload )
153
193
r = json .loads (r .text )
154
-
155
194
if 'error' in r .keys ():
156
195
print (r ['error' ])
157
196
if 'warning' in r .keys ():
0 commit comments