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

Skip to content

Commit 223a0b7

Browse files
committed
variable domain in docstring
1 parent 9e568b6 commit 223a0b7

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

plotly/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . version import __version__
22
import graph_objs
33
import plotly
4-
import tools
4+
import tools
5+
import utils

plotly/plotly/plotly.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,12 @@ def get_figure(file_owner, file_id, raw=False):
316316
"There was an error retrieving this file")
317317

318318

319+
@utils.template_doc(plotly_domain=tools._get_plotly_urls(forgiving=True)[0])
319320
class Stream:
320321
""" Interface to Plotly's real-time graphing API.
321322
322323
Initialize a Stream object with a stream_id
323-
found in https://plot.ly/settings.
324+
found in {plotly_domain}/settings.
324325
Real-time graphs are initialized with a call to `plot` that embeds
325326
your unique `stream_id`s in each of the graph's traces. The `Stream`
326327
interface plots data to these traces, as identified with the unique
@@ -333,7 +334,7 @@ class Stream:
333334
Stream example:
334335
# Initialize a streaming graph
335336
# by embedding stream_id's in the graph's traces
336-
>>> stream_id = "your_stream_id" # See https://plot.ly/settings
337+
>>> stream_id = "your_stream_id" # See {plotly_domain}/settings
337338
>>> py.plot(Data([Scatter(x=[],
338339
y=[],
339340
stream=dict(token=stream_id, maxpoints=100))])
@@ -343,9 +344,10 @@ class Stream:
343344
>>> stream.write(dict(x=1, y=1)) # Plot (1, 1) in your graph
344345
"""
345346

347+
@utils.template_doc(plotly_domain=tools._get_plotly_urls(forgiving=True)[0])
346348
def __init__(self, stream_id):
347349
""" Initialize a Stream object with your unique stream_id.
348-
Find your stream_id at https://plot.ly/settings.
350+
Find your stream_id at {plotly_domain}/settings.
349351
350352
For more help, see: `help(plotly.plotly.Stream)`
351353
or see examples and tutorials here:
@@ -362,7 +364,7 @@ def open(self):
362364
http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb
363365
"""
364366

365-
plotly_streaming_url = tools._get_plotly_urls()[1]
367+
plotly_streaming_url = tools._get_plotly_urls(forgiving=True)[1]
366368
self._stream = chunked_requests.Stream(plotly_streaming_url,
367369
80,
368370
{'Host': plotly_streaming_url,

plotly/tools.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,38 @@ def embed(username, plot_id, width="100%", height=525):
186186
pass
187187

188188

189-
### mpl-related tools ###
189+
def _get_plotly_urls(forgiving=False):
190+
''' Return url endpoints for Plotly services.
191+
These endpoints are configurable, and are
192+
retrieved from ~/.plotly/.credentials as:
193+
{
194+
'plotly_rest_url': '...',
195+
'plotly_streaming_url': '...'
196+
}
197+
'''
198+
if forgiving:
199+
try:
200+
config_on_file = get_credentials_file()
201+
except:
202+
config_on_file = {}
203+
else:
204+
config_on_file = get_credentials_file()
205+
206+
if 'plotly_rest_url' in config_on_file:
207+
plotly_rest_url = config_on_file['plotly_rest_url']
208+
else:
209+
plotly_rest_url = 'https://plot.ly'
210+
211+
if 'plotly_streaming_url' in config_on_file:
212+
plotly_streaming_url = config_on_file['plotly_streaming_url']
213+
else:
214+
plotly_streaming_url = 'stream.plot.ly'
215+
216+
return (plotly_rest_url, plotly_streaming_url)
190217

218+
219+
### mpl-related tools ###
220+
@utils.template_doc(plotly_domain=_get_plotly_urls(forgiving=True)[0])
191221
def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
192222
"""Convert a matplotlib figure to plotly dictionary and send.
193223
@@ -230,10 +260,10 @@ def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
230260
notebook -- an option for use with an IPython notebook
231261
232262
** Don't have a username/api_key? Try looking here:
233-
https://plot.ly/plot
263+
{plotly_domain}/plot
234264
235265
** Forgot your api_key? Try signing in and looking here:
236-
https://plot.ly/api/python/getting-started
266+
{plotly_domain}/api/python/getting-started
237267
238268
"""
239269
if _matplotlylib_imported:
@@ -401,26 +431,3 @@ def validate_stream(obj, obj_type):
401431
validate_stream(val, sub_obj_type)
402432
except KeyError:
403433
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)

plotly/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def default(self, obj):
103103

104104

105105
### unicode stuff ###
106-
107106
def decode_unicode(coll):
108107
if isinstance(coll, list):
109108
for no, entry in enumerate(coll):
@@ -126,4 +125,13 @@ def decode_unicode(coll):
126125
except UnicodeEncodeError:
127126
pass
128127
coll[str(key)] = coll.pop(key)
129-
return coll
128+
return coll
129+
130+
131+
### docstring templating ###
132+
def template_doc(**kwargs):
133+
def _decorator(func):
134+
if func.__doc__ is not None:
135+
func.__doc__ = func.__doc__.format(**kwargs)
136+
return func
137+
return _decorator

0 commit comments

Comments
 (0)