Skip to content

Commit 5761bcf

Browse files
committed
Merge pull request plotly#134 from plotly/andrew-flexible-sign-in
Andrew flexible sign in
2 parents ba52ba3 + 980cbb1 commit 5761bcf

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

plotly/plotly/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"""
1010
from . plotly import (
1111
sign_in, update_plot_options, get_plot_options, get_credentials, iplot,
12-
plot, iplot_mpl, plot_mpl, get_figure, Stream, image
12+
plot, iplot_mpl, plot_mpl, get_figure, Stream, image, get_config
1313
)

plotly/plotly/plotly.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,47 @@
4444

4545
_plot_options = dict()
4646

47+
_config = dict()
48+
4749
### test file permissions and make sure nothing is corrupted ###
4850
tools.ensure_local_plotly_files()
4951

5052
### _credentials stuff ###
5153

5254

53-
def sign_in(username, api_key):
54-
"""Set module-scoped _credentials for session. Verify with plotly."""
55-
global _credentials
55+
def sign_in(username, api_key, **kwargs):
56+
"""Set module-scoped _credentials for session. Optionally, set config info.
57+
"""
5658
_credentials['username'], _credentials['api_key'] = username, api_key
5759
# TODO: verify these _credentials with plotly
5860

61+
_config['plotly_domain'] = kwargs.get('plotly_domain')
62+
_config['plotly_streaming_domain'] = kwargs.get('plotly_streaming_domain')
63+
# TODO: verify format of config options
64+
5965

6066
### plot options stuff ###
6167

6268
def update_plot_options(**kwargs):
6369
""" Update the module-level _plot_options
6470
"""
65-
global _plot_options
6671
_plot_options.update(kwargs)
6772

6873

6974
def get_plot_options():
7075
""" Returns a copy of the user supplied plot options.
7176
Use `update_plot_options()` to change.
7277
"""
73-
global _plot_options
7478
return copy.copy(_plot_options)
7579

7680

7781
def get_credentials():
78-
""" Returns a copy of the user supplied credentials.
79-
"""
80-
global _credentials
81-
if ('username' in _credentials) and ('api_key' in _credentials):
82-
return copy.copy(_credentials)
83-
else:
84-
return tools.get_credentials_file()
82+
"""Returns the credentials that will be sent to plotly."""
83+
credentials = tools.get_credentials_file()
84+
for credentials_key in credentials:
85+
if _credentials.get(credentials_key):
86+
credentials[credentials_key] = _credentials[credentials_key]
87+
return credentials
8588

8689

8790
### plot stuff ###
@@ -127,6 +130,15 @@ def _plot_option_logic(plot_options):
127130
return options
128131

129132

133+
def get_config():
134+
"""Returns either module config or file config."""
135+
config = tools.get_config_file()
136+
for config_key in config:
137+
if _config.get(config_key):
138+
config[config_key] = _config[config_key]
139+
return config
140+
141+
130142
def plot(figure_or_data, validate=True, **plot_options):
131143
"""Create a unique url for this plot in Plotly and optionally open url.
132144
@@ -307,7 +319,7 @@ def get_figure(file_owner_or_url, file_id=None, raw=False):
307319
`graph objects`.
308320
309321
"""
310-
plotly_rest_url = tools.get_config_file()['plotly_domain']
322+
plotly_rest_url = get_config()['plotly_domain']
311323
if file_id is None: # assume we're using a url
312324
url = file_owner_or_url
313325
if url[:len(plotly_rest_url)] != plotly_rest_url:
@@ -413,7 +425,7 @@ def open(self):
413425
http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb
414426
"""
415427

416-
streaming_url = tools.get_config_file()['plotly_streaming_domain']
428+
streaming_url = get_config()['plotly_streaming_domain']
417429
self._stream = chunked_requests.Stream(streaming_url,
418430
80,
419431
{'Host': streaming_url,
@@ -562,7 +574,7 @@ def get(figure_or_data, format='png', width=None, height=None):
562574
if height is not None:
563575
payload['height'] = height
564576

565-
url = tools.get_config_file()['plotly_domain'] + "/apigenimage/"
577+
url = get_config()['plotly_domain'] + "/apigenimage/"
566578
res = requests.post(url,
567579
data=json.dumps(payload,
568580
cls=utils._plotlyJSONEncoder),
@@ -667,7 +679,7 @@ def _send_to_plotly(figure, **plot_options):
667679
origin='plot',
668680
kwargs=kwargs)
669681

670-
url = tools.get_config_file()['plotly_domain'] + "/clientresp"
682+
url = get_config()['plotly_domain'] + "/clientresp"
671683

672684
r = requests.post(url, data=payload)
673685
r.raise_for_status()

plotly/tests/test_core/test_plotly/test_credentials.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest import TestCase
12
import plotly.plotly.plotly as py
23
import plotly.tools as tls
34

@@ -24,4 +25,40 @@ def test_sign_in():
2425
assert creds['username'] == un
2526
assert creds['api_key'] == ak
2627
# TODO, and check it!
27-
# assert creds['stream_ids'] == si
28+
# assert creds['stream_ids'] == si
29+
30+
31+
class TestSignIn(TestCase):
32+
33+
def test_get_config(self):
34+
plotly_domain = 'test ___domain'
35+
plotly_streaming_domain = 'test streaming ___domain'
36+
config1 = py.get_config()
37+
py._config['plotly_domain'] = plotly_domain
38+
config2 = py.get_config()
39+
py._config['plotly_streaming_domain'] = plotly_streaming_domain
40+
config3 = py.get_config()
41+
self.assertEqual(config2['plotly_domain'], plotly_domain)
42+
self.assertNotEqual(
43+
config2['plotly_streaming_domain'], plotly_streaming_domain
44+
)
45+
self.assertEqual(
46+
config3['plotly_streaming_domain'], plotly_streaming_domain
47+
)
48+
49+
def test_sign_in_with_config(self):
50+
username = 'place holder'
51+
api_key = 'place holder'
52+
plotly_domain = 'test ___domain'
53+
plotly_streaming_domain = 'test streaming ___domain'
54+
py.sign_in(
55+
username,
56+
api_key,
57+
plotly_domain=plotly_domain,
58+
plotly_streaming_domain=plotly_streaming_domain
59+
)
60+
config = py.get_config()
61+
self.assertEqual(config['plotly_domain'], plotly_domain)
62+
self.assertEqual(
63+
config['plotly_streaming_domain'], plotly_streaming_domain
64+
)

0 commit comments

Comments
 (0)