forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly.py
More file actions
336 lines (267 loc) · 11.2 KB
/
plotly.py
File metadata and controls
336 lines (267 loc) · 11.2 KB
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
"""
plotly
======
A module that contains the plotly class, a liaison between the user
and ploty's servers.
1. get DEFAULT_PLOT_OPTIONS for options
2. update plot_options with .plotly/ dir
3. update plot_options with _plot_options
4. update plot_options with kwargs!
"""
import requests
import json
import warnings
import httplib
from copy import copy
from . import utils
import tools
from . import exceptions
from .version import __version__
_credentials = dict()
_plot_options = dict()
_plotly_url = 'https://plot.ly/clientresp'
### _credentials stuff ###
def sign_in(username, api_key):
"""Set module-scoped _credentials for session. Verify with plotly."""
global _credentials
_credentials['username'], _credentials['api_key'] = username, api_key
# TODO: verify these _credentials with plotly
### _plot_options stuff ###
def load_plot_options():
""" Import the plot_options from file into the module-level _plot_options.
"""
global _plot_options
_plot_options = _plot_options.update(tools.get_plot_options_file())
def save_plot_options(**kwargs):
""" Save the module-level _plot_options to file for later access
"""
global _plot_options
update_plot_options(**kwargs)
tools.save_plot_options_file(**_plot_options)
def update_plot_options(**kwargs):
""" Update the module-level _plot_options
"""
global _plot_options
_plot_options.update(kwargs)
def get_plot_options():
""" Returns a copy of the user supplied plot options.
Use `update_plot_options()` to change.
"""
global _plot_options
return copy(_plot_options)
### plot stuff ###
def iplot(figure_or_data, **plot_options):
"""for use in ipython notebooks"""
if 'auto_open' not in plot_options:
plot_options['auto_open'] = False
res = plot(figure_or_data, **plot_options)
urlsplit = res['url'].split('/')
username, plot_id = urlsplit[-2][1:], urlsplit[-1] # TODO: HACKY!
embed_options = dict()
if 'width' in plot_options:
embed_options['width'] = plot_options['width']
if 'height' in plot_options:
embed_options['height'] = plot_options['height']
return tools.embed(username, plot_id, **embed_options)
def plot(figure_or_data, **plot_options):
"""returns a url with the graph
opens the graph in the browser if plot_options['auto_open'] is True
"""
if isinstance(figure_or_data, dict):
figure = figure_or_data
elif isinstance(figure_or_data, list):
figure = {'data': figure_or_data}
else:
raise exceptions.PlotlyError("The `figure_or_data` positional argument "
"must be either `dict`-like or "
"`list`-like.")
res = _send_to_plotly(figure, **plot_options)
if ('error' in res) and ('auto_open' in plot_options): # TODO: OK?
if (res['error'] == '') and plot_options['auto_open']:
try:
from webbrowser import open as wbopen
wbopen(res['url'])
except: # TODO: what should we except here? this is dangerous
pass
return res
def iplot_mpl(fig, resize=True, **plot_options):
fig = tools.mpl_to_plotly(fig, resize=resize)
return iplot(fig, **plot_options)
def plot_mpl(fig, resize=True, **plot_options):
fig = tools.mpl_to_plotly(fig, resize=resize)
return plot(fig, **plot_options)
def get_figure(file_owner, file_id):
# server = "http://ec2-54-196-84-85.compute-1.amazonaws.com"
server = "https://plot.ly"
resource = "/apigetfile/{username}/{file_id}".format(username=file_owner,
file_id=file_id)
(username, api_key) = _validation_key_logic()
headers = {'plotly-username': username,
'plotly-apikey': api_key,
'plotly-version': '2.0',
'plotly-platform': 'pythonz'}
response = requests.get(server + resource, headers=headers)
if response.status_code == 200:
content = json.loads(response.content)
response_payload = content['payload']
figure = response_payload['figure']
utils.decode_unicode(figure)
return tools.get_valid_graph_obj(figure, obj_type='figure')
else:
try:
content = json.loads(response.content)
print content['error']
except:
raise("There was an error retrieving this file")
class Stream:
""" Plotly's real-time graphing interface. Initialize
a Stream object with a stream_id found in https://plot.ly/settings.
Real-time graphs are initialized with a call to `plot` that embeds
your unique `stream_id`s in each of the graph's traces. The `Stream`
interface plots data to these traces, as identified with the unique
stream_id, in real-time. Every viewer of the graph sees the same
data at the same time.
View examples here: nbviewer.ipython.org/github/plotly/Streaming-Demos
Stream example:
# Initialize a streaming graph
# by embedding stream_id's in the graph's traces
>>> stream_id = "your_stream_id" # See https://plot.ly/settings
>>> py.plot(Data([Scatter(x=[],
y=[],
stream=dict(token=stream_id, maxpoints=100))])
# Stream data to the import trace
>>> stream = Stream(stream_id) # Initialize a stream object
>>> stream.open() # Open the stream
>>> stream.write(dict(x=1, y=1)) # Plot (1, 1) in your graph
"""
def __init__(self, stream_id):
""" Initialize a Stream object with your unique stream_id.
Find your stream_id at https://plot.ly/settings.
For more help, see: `help(plotly.plotly.Stream)`
or see examples here:
http://nbviewer.ipython.org/github/plotly/Streaming-Demos
"""
self.stream_id = stream_id
self.connected = False
def open(self):
"""Open a streaming connection to plotly.
For more help, see: `help(plotly.plotly.Stream)`
or see examples here:
http://nbviewer.ipython.org/github/plotly/Streaming-Demos
"""
self.conn = httplib.HTTPConnection('stream.plot.ly', 80)
self.conn.putrequest('POST', '/')
self.conn.putheader('Host', 'stream.plot.ly')
self.conn.putheader('User-Agent', 'Python-Plotly')
self.conn.putheader('Transfer-Encoding', 'chunked')
self.conn.putheader('Connection', 'close')
self.conn.putheader('plotly-streamtoken', self.stream_id)
self.conn.endheaders()
self.connected = True
def reopen(self):
""" Not Implemented
For more help, see: `help(plotly.plotly.Stream)`
or see examples here:
http://nbviewer.ipython.org/github/plotly/Streaming-Demos
"""
raise NotImplementedError
def write(self, data):
""" Write `data` to your stream. This will plot the
`data` in your graph in real-time.
`data` is a plotly formatted dict.
Valid keys:
'x', 'y', 'text', 'z', 'marker', 'line'
Examples:
>>> write(dict(x = 1, y = 2))
>>> write(dict(x = [1, 2, 3], y = [10, 20, 30]))
>>> write(dict(x = 1, y = 2, text = 'scatter text'))
>>> write(dict(x = 1, y = 3, marker = dict(color = 'blue')))
>>> write(dict(z = [[1,2,3], [4,5,6]]))
For more help, see: `help(plotly.plotly.Stream)`
or see examples here:
http://nbviewer.ipython.org/github/plotly/Streaming-Demos
"""
if not self.connected:
self.init()
# plotly's streaming API takes new-line separated json objects
msg = json.dumps(data, cls=tools._plotlyJSONEncoder) + '\n'
msglen = format(len(msg), 'x')
# chunked encoding requests contain the messege length in hex,
# \r\n, and then the message
print '{msglen}\r\n{msg}\r\n'.format(msglen=msglen, msg=msg)
self.conn.send('{msglen}\r\n{msg}\r\n'.format(msglen=msglen, msg=msg))
def close(self):
""" Close the stream connection to plotly's streaming servers.
For more help, see: `help(plotly.plotly.Stream)`
or see examples here:
http://nbviewer.ipython.org/github/plotly/Streaming-Demos
"""
self.conn.send('0\r\n\r\n')
self.conn.close()
self.connected = False
def is_connected(self):
""" Not Implemented
For more help, see: `help(plotly.plotly.Stream)`
or see examples here:
http://nbviewer.ipython.org/github/plotly/Streaming-Demos
"""
raise NotImplementedError
def _send_to_plotly(figure, **supplied_plot_options):
"""
"""
plot_options = dict()
plot_options.update(tools._DEFAULT_PLOT_OPTIONS)
plot_options.update(_plot_options)
plot_options.update(supplied_plot_options)
data = json.dumps(figure['data'] if 'data' in figure else [],
cls=utils._plotlyJSONEncoder)
file_credentials = tools.get_credentials_file()
if ('username' in _credentials) and ('api_key' in _credentials):
username, api_key = _credentials['username'], _credentials['api_key']
elif ('username' in file_credentials) and ('api_key' in file_credentials):
(username, api_key) = (file_credentials['username'],
file_credentials['api_key'])
else:
raise exceptions.PlotlyAccountError("Couldn't find a username, "
"api_key pair.")
kwargs = json.dumps(dict(filename=plot_options['filename'],
fileopt=plot_options['fileopt'],
world_readable=plot_options['world_readable'],
layout=figure['layout'] if 'layout' in figure
else {}),
cls=utils._plotlyJSONEncoder)
payload = dict(platform='python', # TODO: It'd be cool to expose the platform for RaspPi and others
version=__version__,
args=data,
un=username,
key=api_key,
origin='plot',
kwargs=kwargs)
url = _plotly_url
r = requests.post(url, data=payload)
r.raise_for_status()
r = json.loads(r.text)
if 'error' in r and r['error'] != '':
print(r['error'])
if 'warning' in r and r['warning'] != '':
warnings.warn(r['warning'])
if 'message' in r and r['message'] != '':
print(r['message'])
return r
def _validation_key_logic():
creds_on_file = tools.get_credentials_file()
if 'username' in _credentials:
username = _credentials['username']
elif 'username' in creds_on_file:
username = creds_on_file['username']
else:
raise exceptions.PlotlyAccountError("Not signed in or no username "
"saved in config file") # TODO: a message that doesn't suck
if 'api_key' in _credentials:
api_key = _credentials['api_key']
elif 'api_key' in creds_on_file:
api_key = creds_on_file['api_key']
else:
raise exceptions.PlotlyAccountError("Not signed in or no api_key saved "
"in config file") # TODO: a message that doesn't suck
return (username, api_key)