22
22
import copy
23
23
import base64
24
24
import os
25
- from .. import utils
25
+ from .. import utils # TODO make non-relative
26
26
from .. import tools
27
27
from .. import exceptions
28
28
from .. import version
42
42
43
43
_plot_options = dict ()
44
44
45
- _plotly_url = "https://plot.ly" # do not append final '/' here for url!
45
+ ### test file permissions and make sure nothing is corrupted ###
46
+ tools .ensure_local_plotly_files ()
46
47
47
48
### _credentials stuff ###
48
49
50
+
49
51
def sign_in (username , api_key ):
50
52
"""Set module-scoped _credentials for session. Verify with plotly."""
51
53
global _credentials
52
54
_credentials ['username' ], _credentials ['api_key' ] = username , api_key
53
55
# TODO: verify these _credentials with plotly
54
56
55
57
56
- ### _plot_options stuff ###
57
-
58
- # def load_plot_options():
59
- # """ Import the plot_options from file into the module-level _plot_options.
60
- # """
61
- # global _plot_options
62
- # _plot_options = _plot_options.update(tools.get_plot_options_file())
63
- #
64
- #
65
- # def save_plot_options(**kwargs):
66
- # """ Save the module-level _plot_options to file for later access
67
- # """
68
- # global _plot_options
69
- # update_plot_options(**kwargs)
70
- # tools.save_plot_options_file(**_plot_options)
71
-
58
+ ### plot options stuff ###
72
59
73
60
def update_plot_options (** kwargs ):
74
61
""" Update the module-level _plot_options
@@ -273,7 +260,8 @@ def get_figure(file_owner, file_id, raw=False):
273
260
`graph objects`.
274
261
275
262
"""
276
- server = _plotly_url
263
+
264
+ plotly_rest_url = tools .get_config_file ()['plotly_domain' ]
277
265
resource = "/apigetfile/{username}/{file_id}" .format (username = file_owner ,
278
266
file_id = file_id )
279
267
(username , api_key ) = _validation_key_logic ()
@@ -298,7 +286,7 @@ def get_figure(file_owner, file_id, raw=False):
298
286
"The 'file_id' argument must be a non-negative number."
299
287
)
300
288
301
- response = requests .get (server + resource , headers = headers )
289
+ response = requests .get (plotly_rest_url + resource , headers = headers )
302
290
if response .status_code == 200 :
303
291
content = json .loads (response .content )
304
292
response_payload = content ['payload' ]
@@ -317,11 +305,12 @@ def get_figure(file_owner, file_id, raw=False):
317
305
"There was an error retrieving this file" )
318
306
319
307
308
+ @utils .template_doc (** tools .get_config_file ())
320
309
class Stream :
321
310
""" Interface to Plotly's real-time graphing API.
322
311
323
312
Initialize a Stream object with a stream_id
324
- found in https://plot.ly /settings.
313
+ found in {plotly_domain} /settings.
325
314
Real-time graphs are initialized with a call to `plot` that embeds
326
315
your unique `stream_id`s in each of the graph's traces. The `Stream`
327
316
interface plots data to these traces, as identified with the unique
@@ -334,7 +323,7 @@ class Stream:
334
323
Stream example:
335
324
# Initialize a streaming graph
336
325
# by embedding stream_id's in the graph's traces
337
- >>> stream_id = "your_stream_id" # See https://plot.ly /settings
326
+ >>> stream_id = "your_stream_id" # See {plotly_domain} /settings
338
327
>>> py.plot(Data([Scatter(x=[],
339
328
y=[],
340
329
stream=dict(token=stream_id, maxpoints=100))])
@@ -344,9 +333,10 @@ class Stream:
344
333
>>> stream.write(dict(x=1, y=1)) # Plot (1, 1) in your graph
345
334
"""
346
335
336
+ @utils .template_doc (** tools .get_config_file ())
347
337
def __init__ (self , stream_id ):
348
338
""" Initialize a Stream object with your unique stream_id.
349
- Find your stream_id at https://plot.ly /settings.
339
+ Find your stream_id at {plotly_domain} /settings.
350
340
351
341
For more help, see: `help(plotly.plotly.Stream)`
352
342
or see examples and tutorials here:
@@ -362,12 +352,13 @@ def open(self):
362
352
or see examples and tutorials here:
363
353
http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb
364
354
"""
365
- self ._stream = chunked_requests .Stream ('stream.plot.ly' ,
355
+
356
+ streaming_url = tools .get_config_file ()['plotly_streaming_domain' ]
357
+ self ._stream = chunked_requests .Stream (streaming_url ,
366
358
80 ,
367
- {'Host' : 'stream.plot.ly' ,
359
+ {'Host' : streaming_url ,
368
360
'plotly-streamtoken' : self .stream_id })
369
361
370
-
371
362
def write (self , data , layout = None , validate = True ,
372
363
reconnect_on = (200 , '' , 408 )):
373
364
""" Write `data` to your stream. This will plot the
@@ -470,8 +461,8 @@ def get(figure):
470
461
'plotly-version' : '2.0' ,
471
462
'plotly-platform' : 'python' }
472
463
473
- server = "https://plot.ly /apigenimage/"
474
- res = requests .post (server ,
464
+ url = tools . get_config_file ()[ 'plotly_domain' ] + " /apigenimage/"
465
+ res = requests .post (url ,
475
466
data = json .dumps (figure ,
476
467
cls = utils ._plotlyJSONEncoder ),
477
468
headers = headers )
@@ -543,9 +534,7 @@ def _send_to_plotly(figure, **plot_options):
543
534
origin = 'plot' ,
544
535
kwargs = kwargs )
545
536
546
- # TODO: this doesn't work yet for ppl's individual servers for testing...
547
- # url = _plotly_url + "/clientresp"
548
- url = "https://plot.ly/clientresp"
537
+ url = tools .get_config_file ()['plotly_domain' ] + "/clientresp"
549
538
550
539
r = requests .post (url , data = payload )
551
540
r .raise_for_status ()
@@ -577,4 +566,3 @@ def _validation_key_logic():
577
566
if username is None or api_key is None :
578
567
raise exceptions .PlotlyLocalCredentialsError ()
579
568
return (username , api_key )
580
-
0 commit comments