Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f173839

Browse files
cparmercparmer
cparmer
authored and
cparmer
committed
first
0 parents  commit f173839

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

plotly/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .plotly import signup
2+
from .plotly import plotly

plotly/__init__.pyc

186 Bytes
Binary file not shown.
6.55 KB
Binary file not shown.

plotly/plotly.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import requests
2+
import json
3+
4+
5+
def signup(un, email):
6+
''' Remote signup to plot.ly and plot.ly API
7+
Returns:
8+
:param r with r['tmp_pw']: Temporary password to access your plot.ly acount
9+
:param r['api_key']: A key to use the API with
10+
11+
Full docs and examples at https://plot.ly/API
12+
:un: <string> username
13+
:email: <string> email address
14+
'''
15+
payload = {'version': '0.4', 'un': un, 'email': email, 'platform':'Python'}
16+
r = requests.post('https://plot.ly/apimkacct', data=payload)
17+
r = json.loads(r.text)
18+
if 'error' in r.keys():
19+
if not r['error'] == '':
20+
print(r['error'])
21+
if 'warning' in r.keys():
22+
print(r['warning'])
23+
if 'message' in r.keys():
24+
print(r['message'])
25+
26+
return r
27+
28+
class plotly:
29+
def __init__(self, username=None, key=None,verbose=True):
30+
''' plotly constructor. Supply username and api key.
31+
'''
32+
self.un = username
33+
self.key = key
34+
self.__filename = None
35+
self.__fileopt = None
36+
self.verbose = verbose
37+
38+
def iplot(self, *args, **kwargs):
39+
''' for use in ipython notebooks '''
40+
res = self.plot(*args, **kwargs)
41+
width = kwargs.get('width', 600)
42+
height = kwargs.get('height', 600)
43+
s = '<iframe height="'+str(height+50)+'" id="igraph" scrolling="no" seamless="seamless" src="'+res['url']+'/'+str(width)+'/'+str(height)+'" width="'+str(width+50)+'"></iframe>'
44+
try:
45+
from IPython.display import HTML
46+
return HTML(s)
47+
except:
48+
return s
49+
50+
def plot(self, *args, **kwargs):
51+
''' Make a plot in plotly.
52+
Two interfaces:
53+
1 - ploty.plot(x1, y1[,x2,y2,...],**kwargs)
54+
where x1, y1, .... are lists, numpy arrays
55+
2 - plot.plot([data1, ...], **kwargs)
56+
where data1 is a dict that is at least
57+
{'x': x1, 'y': y1} but can contain more styling and sharing options.
58+
kwargs accepts:
59+
filename
60+
fileopt
61+
style
62+
layout
63+
See https://plot.ly/API for details.
64+
Returns:
65+
:param r with r['url']: A URL that displays the generated plot
66+
:param r['filename']: The filename of the plot in your plotly account.
67+
'''
68+
69+
un = kwargs['un'] if 'un' in kwargs.keys() else self.un
70+
key = kwargs['key'] if 'key' in kwargs.keys() else self.key
71+
if not un or not key:
72+
raise Exception('Not Signed in')
73+
74+
if not 'filename' in kwargs.keys():
75+
kwargs['filename'] = self.__filename
76+
if not 'fileopt' in kwargs.keys():
77+
kwargs['fileopt'] = self.__fileopt
78+
79+
origin = 'plot'
80+
r = self.__makecall(args, un, key, origin, kwargs)
81+
return r
82+
83+
def layout(self, *args, **kwargs):
84+
''' Style the layout of a Plotly plot.
85+
ploty.layout(layout,**kwargs)
86+
:param layout - a dict that customizes the style of the layout,
87+
the axes, and the legend.
88+
:param kwargs - accepts:
89+
filename
90+
See https://plot.ly/API for details.
91+
Returns:
92+
:param r with r['url']: A URL that displays the generated plot
93+
:param r['filename']: The filename of the plot in your plotly account.
94+
'''
95+
96+
un = kwargs['un'] if 'un' in kwargs.keys() else self.un
97+
key = kwargs['un'] if 'key' in kwargs.keys() else self.key
98+
if not un or not key:
99+
raise Exception('Not Signed in')
100+
if not 'filename' in kwargs.keys():
101+
kwargs['filename'] = self.__filename
102+
if not 'fileopt' in kwargs.keys():
103+
kwargs['fileopt'] = self.__fileopt
104+
105+
origin = 'layout'
106+
r = self.__makecall(args, un, key, origin, kwargs)
107+
return r
108+
109+
def style(self, *args, **kwargs):
110+
''' Style the data traces of a Plotly plot.
111+
ploty.style([data1,[,data2,...],**kwargs)
112+
:param data1 - a dict that customizes the style of the i'th trace
113+
:param kwargs - accepts:
114+
filename
115+
See https://plot.ly/API for details.
116+
Returns:
117+
:param r with r['url']: A URL that displays the generated plot
118+
:param r['filename']: The filename of the plot in your plotly account.
119+
'''
120+
121+
un = kwargs['un'] if 'un' in kwargs.keys() else self.un
122+
key = kwargs['un'] if 'key' in kwargs.keys() else self.key
123+
if not un or not key:
124+
raise Exception('Not Signed in')
125+
if not 'filename' in kwargs.keys():
126+
kwargs['filename'] = self.__filename
127+
if not 'fileopt' in kwargs.keys():
128+
kwargs['fileopt'] = self.__fileopt
129+
130+
origin = 'style'
131+
r = self.__makecall(args, un, key, origin, kwargs)
132+
return r
133+
134+
def __makecall(self, args, un, key, origin, kwargs):
135+
version = '0.5'
136+
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)
150+
url = 'https://plot.ly/clientresp'
151+
payload = {'platform': platform, 'version': version, 'args': args, 'un': un, 'key': key, 'origin': origin, 'kwargs': kwargs}
152+
r = requests.post(url, data=payload)
153+
r = json.loads(r.text)
154+
155+
if 'error' in r.keys():
156+
print(r['error'])
157+
if 'warning' in r.keys():
158+
print(r['warning'])
159+
if 'message' in r.keys():
160+
if self.verbose:
161+
print(r['message'])
162+
163+
if 'filename' in r.keys():
164+
self.__filename = r['filename']
165+
return r
166+
167+
168+

plotly/plotly.pyc

5.79 KB
Binary file not shown.

0 commit comments

Comments
 (0)