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

Skip to content

Commit 9e568b6

Browse files
committed
Variable server urls
1 parent 3668850 commit 9e568b6

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

plotly/plotly/plotly.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242

4343
_plot_options = dict()
4444

45-
_plotly_url = "https://plot.ly" # do not append final '/' here for url!
46-
4745
### _credentials stuff ###
4846

4947
def sign_in(username, api_key):
@@ -273,7 +271,8 @@ def get_figure(file_owner, file_id, raw=False):
273271
`graph objects`.
274272
275273
"""
276-
server = _plotly_url
274+
275+
plotly_rest_url = tools._get_plotly_urls()[0]
277276
resource = "/apigetfile/{username}/{file_id}".format(username=file_owner,
278277
file_id=file_id)
279278
(username, api_key) = _validation_key_logic()
@@ -298,7 +297,7 @@ def get_figure(file_owner, file_id, raw=False):
298297
"The 'file_id' argument must be a non-negative number."
299298
)
300299

301-
response = requests.get(server + resource, headers=headers)
300+
response = requests.get(plotly_rest_url + resource, headers=headers)
302301
if response.status_code == 200:
303302
content = json.loads(response.content)
304303
response_payload = content['payload']
@@ -362,9 +361,11 @@ def open(self):
362361
or see examples and tutorials here:
363362
http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb
364363
"""
365-
self._stream = chunked_requests.Stream('stream.plot.ly',
364+
365+
plotly_streaming_url = tools._get_plotly_urls()[1]
366+
self._stream = chunked_requests.Stream(plotly_streaming_url,
366367
80,
367-
{'Host': 'stream.plot.ly',
368+
{'Host': plotly_streaming_url,
368369
'plotly-streamtoken': self.stream_id})
369370

370371

@@ -470,8 +471,8 @@ def get(figure):
470471
'plotly-version': '2.0',
471472
'plotly-platform': 'python'}
472473

473-
server = "https://plot.ly/apigenimage/"
474-
res = requests.post(server,
474+
url = tools._get_plotly_urls()[0] + "/apigenimage/"
475+
res = requests.post(url,
475476
data=json.dumps(figure,
476477
cls=utils._plotlyJSONEncoder),
477478
headers=headers)
@@ -543,9 +544,7 @@ def _send_to_plotly(figure, **plot_options):
543544
origin='plot',
544545
kwargs=kwargs)
545546

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"
547+
url = tools._get_plotly_urls()[0] + "/clientresp"
549548

550549
r = requests.post(url, data=payload)
551550
r.raise_for_status()
@@ -577,4 +576,3 @@ def _validation_key_logic():
577576
if username is None or api_key is None:
578577
raise exceptions.PlotlyLocalCredentialsError()
579578
return (username, api_key)
580-

plotly/tools.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def ensure_local_plotly_files_exist():
9494

9595
### credentials tools ###
9696

97-
def set_credentials_file(username="", api_key="", stream_ids=()):
97+
def set_credentials_file(username="", api_key="", stream_ids=(), **extra):
9898
"""Set the keyword-value pairs in `~/.plotly_credentials`.
9999
100100
"""
@@ -108,6 +108,7 @@ def set_credentials_file(username="", api_key="", stream_ids=()):
108108
credentials['api_key'] = api_key
109109
if stream_ids:
110110
credentials['stream_ids'] = stream_ids
111+
credentials.update(extra)
111112
utils.save_json(CREDENTIALS_FILE, credentials)
112113

113114

@@ -144,23 +145,26 @@ def show_credentials_file(*args):
144145

145146
def get_embed(username, plot_id, width="100%", height=525):
146147
padding = 25
148+
plotly_rest_url = _get_plotly_urls()[0]
147149
if isinstance(width, (int, long)):
148150
s = ("<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\""
149151
"seamless=\"seamless\" "
150-
"src=\"https://plot.ly/"
152+
"src=\"{plotly_rest_url}/"
151153
"~{username}/{plot_id}/{plot_width}/{plot_height}\" "
152154
"height=\"{iframe_height}\" width=\"{iframe_width}\">"
153155
"</iframe>").format(
156+
plotly_rest_url=plotly_rest_url,
154157
username=username, plot_id=plot_id,
155158
plot_width=width-padding, plot_height=height-padding,
156159
iframe_height=height, iframe_width=width)
157160
else:
158161
s = ("<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\""
159162
"seamless=\"seamless\" "
160-
"src=\"https://plot.ly/"
163+
"src=\"{plotly_rest_url}/"
161164
"~{username}/{plot_id}\" "
162165
"height=\"{iframe_height}\" width=\"{iframe_width}\">"
163166
"</iframe>").format(
167+
plotly_rest_url=plotly_rest_url,
164168
username=username, plot_id=plot_id,
165169
iframe_height=height, iframe_width=width)
166170

@@ -369,7 +373,7 @@ def validate_stream(obj, obj_type):
369373
"""Validate a data dictionary (only) for use with streaming.
370374
371375
An error is raised if a key within (or nested within) is not streamable.
372-
376+
373377
"""
374378
try:
375379
obj_type = graph_objs.KEY_TO_NAME[obj_type]
@@ -396,4 +400,27 @@ def validate_stream(obj, obj_type):
396400
sub_obj_type = graph_objs.KEY_TO_NAME[key]
397401
validate_stream(val, sub_obj_type)
398402
except KeyError:
399-
pass
403+
pass
404+
405+
def _get_plotly_urls():
406+
''' Return url endpoints for Plotly services.
407+
These endpoints are configurable, and are
408+
retrieved from ~/.plotly/.credentials as:
409+
{
410+
'plotly_rest_url': '...',
411+
'plotly_streaming_url': '...'
412+
}
413+
'''
414+
config_on_file = get_credentials_file()
415+
416+
if 'plotly_rest_url' in config_on_file:
417+
plotly_rest_url = config_on_file['plotly_rest_url']
418+
else:
419+
plotly_rest_url = 'https://plot.ly'
420+
421+
if 'plotly_streaming_url' in config_on_file:
422+
plotly_streaming_url = config_on_file['plotly_streaming_url']
423+
else:
424+
plotly_streaming_url = 'stream.plot.ly'
425+
426+
return (plotly_rest_url, plotly_streaming_url)

0 commit comments

Comments
 (0)