Skip to content

Commit 246fbc4

Browse files
committed
Update plotly.py to use new session.py module.
1 parent 94cd9b3 commit 246fbc4

File tree

1 file changed

+71
-85
lines changed

1 file changed

+71
-85
lines changed

plotly/plotly/plotly.py

Lines changed: 71 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
from plotly import tools
3636
from plotly import exceptions
3737
from plotly import version
38+
from plotly.session import (sign_in, update_session_plot_options,
39+
get_session_plot_options, get_session_credentials,
40+
get_session_config)
3841

3942

4043
__all__ = None
@@ -46,56 +49,70 @@
4649
auto_open=True,
4750
validate=True)
4851

49-
_credentials = dict()
5052

51-
_plot_options = dict()
53+
# don't break backwards compatibility
54+
sign_in = sign_in
55+
update_plot_options = update_session_plot_options
5256

53-
_config = dict()
5457

5558
### test file permissions and make sure nothing is corrupted ###
5659
tools.ensure_local_plotly_files()
60+
def get_credentials():
61+
"""Returns the credentials that will be sent to plotly."""
62+
credentials = tools.get_credentials_file()
63+
session_credentials = get_session_credentials()
64+
for credentials_key in credentials:
5765

58-
### _credentials stuff ###
66+
# checking for not false, but truthy value here is the desired behavior
67+
session_value = session_credentials.get(credentials_key)
68+
if session_value is False or session_value:
69+
credentials[credentials_key] = session_value
70+
return credentials
5971

6072

61-
def sign_in(username, api_key, **kwargs):
62-
"""Set module-scoped _credentials for session. Optionally, set config info.
63-
"""
64-
_credentials['username'], _credentials['api_key'] = username, api_key
65-
# TODO: verify these _credentials with plotly
66-
67-
_config['plotly_domain'] = kwargs.get('plotly_domain')
68-
_config['plotly_streaming_domain'] = kwargs.get('plotly_streaming_domain')
69-
_config['plotly_api_domain'] = kwargs.get('plotly_api_domain')
70-
_config['plotly_ssl_verification'] = kwargs.get('plotly_ssl_verification')
71-
# TODO: verify format of config options
73+
def get_config():
74+
"""Returns either module config or file config."""
75+
config = tools.get_config_file()
76+
session_config = get_session_config()
77+
for config_key in config:
7278

79+
# checking for not false, but truthy value here is the desired behavior
80+
session_value = session_config.get(config_key)
81+
if session_value is False or session_value:
82+
config[config_key] = session_value
83+
return config
7384

74-
### plot options stuff ###
7585

76-
def update_plot_options(**kwargs):
77-
""" Update the module-level _plot_options
86+
def get_plot_options():
7887
"""
79-
_plot_options.update(kwargs)
80-
88+
Merge default and user-defined plot options.
8189
82-
def get_plot_options():
83-
""" Returns a copy of the user supplied plot options.
84-
Use `update_plot_options()` to change.
8590
"""
86-
return copy.copy(_plot_options)
91+
plot_options = copy.deepcopy(DEFAULT_PLOT_OPTIONS)
92+
session_plot_options = get_session_plot_options()
93+
for plot_option_key in plot_options:
8794

95+
# checking for not false, but truthy value here is the desired behavior
96+
session_value = session_plot_options.get(plot_option_key)
97+
if session_value is False or session_value:
98+
plot_options[plot_option_key] = session_value
99+
return plot_options
88100

89-
def get_credentials():
90-
"""Returns the credentials that will be sent to plotly."""
91-
credentials = tools.get_credentials_file()
92-
for credentials_key in credentials:
93-
if _credentials.get(credentials_key):
94-
credentials[credentials_key] = _credentials[credentials_key]
95-
return credentials
96101

102+
def _plot_option_logic(plot_options):
103+
"""
104+
Given some plot_options as part of a plot call, decide on final options
105+
106+
"""
107+
session_plot_options = get_session_plot_options()
108+
current_plot_options = get_plot_options()
109+
current_plot_options.update(plot_options)
110+
if (('filename' in plot_options or 'filename' in session_plot_options) and
111+
'fileopt' not in session_plot_options and
112+
'fileopt' not in plot_options):
113+
current_plot_options['fileopt'] = 'overwrite'
114+
return current_plot_options
97115

98-
### plot stuff ###
99116

100117
def iplot(figure_or_data, **plot_options):
101118
"""Create a unique url for this plot in Plotly and open in IPython.
@@ -125,28 +142,6 @@ def iplot(figure_or_data, **plot_options):
125142
return tools.embed(username, plot_id, **embed_options)
126143

127144

128-
def _plot_option_logic(plot_options):
129-
"""Sets plot_options via a precedence hierarchy."""
130-
options = dict()
131-
options.update(_DEFAULT_PLOT_OPTIONS)
132-
options.update(_plot_options)
133-
options.update(plot_options)
134-
if ('filename' in plot_options
135-
and 'fileopt' not in _plot_options
136-
and 'fileopt' not in plot_options):
137-
options['fileopt'] = 'overwrite'
138-
return options
139-
140-
141-
def get_config():
142-
"""Returns either module config or file config."""
143-
config = tools.get_config_file()
144-
for config_key in config:
145-
if _config.get(config_key) is not None:
146-
config[config_key] = _config[config_key]
147-
return config
148-
149-
150145
def plot(figure_or_data, validate=True, **plot_options):
151146
"""Create a unique url for this plot in Plotly and optionally open url.
152147
@@ -346,7 +341,9 @@ def get_figure(file_owner_or_url, file_id=None, raw=False):
346341
file_owner = file_owner_or_url
347342
resource = "/apigetfile/{username}/{file_id}".format(username=file_owner,
348343
file_id=file_id)
349-
(username, api_key) = _validation_key_logic()
344+
credentials = get_credentials()
345+
validate_credentials(credentials)
346+
username, api_key = credentials['username'], credentials['api_key']
350347
headers = {'plotly-username': username,
351348
'plotly-apikey': api_key,
352349
'plotly-version': version.__version__,
@@ -585,7 +582,9 @@ def get(figure_or_data, format='png', width=None, height=None):
585582
"types here: "
586583
"https://plot.ly/python/static-image-export/")
587584

588-
(username, api_key) = _validation_key_logic()
585+
credentials = get_credentials()
586+
validate_credentials(credentials)
587+
username, api_key = credentials['username'], credentials['api_key']
589588
headers = {'plotly-username': username,
590589
'plotly-apikey': api_key,
591590
'plotly-version': version.__version__,
@@ -1179,7 +1178,9 @@ def api_url(cls, resource):
11791178

11801179
@classmethod
11811180
def headers(cls):
1182-
un, api_key = _get_session_username_and_key()
1181+
credentials = get_credentials()
1182+
# todo, validate here?
1183+
un, api_key = credentials['username'], credentials['api_key']
11831184
encoded_un_key_pair = base64.b64encode(
11841185
six.b('{0}:{1}'.format(un, api_key))
11851186
).decode('utf8')
@@ -1190,25 +1191,28 @@ def headers(cls):
11901191
}
11911192

11921193

1193-
def _get_session_username_and_key():
1194-
file_credentials = tools.get_credentials_file()
1195-
if ('username' in _credentials) and ('api_key' in _credentials):
1196-
username, api_key = _credentials['username'], _credentials['api_key']
1197-
elif ('username' in file_credentials) and ('api_key' in file_credentials):
1198-
(username, api_key) = (file_credentials['username'],
1199-
file_credentials['api_key'])
1200-
else:
1194+
def validate_credentials(credentials):
1195+
"""
1196+
Currently only checks for truthy username and api_key
1197+
1198+
"""
1199+
username = credentials.get('username')
1200+
api_key = credentials.get('api_key')
1201+
if not username or not api_key:
12011202
raise exceptions.PlotlyLocalCredentialsError()
1202-
return username, api_key
12031203

12041204

12051205
def _send_to_plotly(figure, **plot_options):
12061206
"""
1207+
12071208
"""
12081209
fig = tools._replace_newline(figure) # does not mutate figure
12091210
data = json.dumps(fig['data'] if 'data' in fig else [],
12101211
cls=utils.PlotlyJSONEncoder)
1211-
username, api_key = _get_session_username_and_key()
1212+
credentials = get_credentials()
1213+
validate_credentials(credentials)
1214+
username = credentials['username']
1215+
api_key = credentials['api_key']
12121216
kwargs = json.dumps(dict(filename=plot_options['filename'],
12131217
fileopt=plot_options['fileopt'],
12141218
world_readable=plot_options['world_readable'],
@@ -1241,24 +1245,6 @@ def _send_to_plotly(figure, **plot_options):
12411245
return r
12421246

12431247

1244-
def _validation_key_logic():
1245-
creds_on_file = tools.get_credentials_file()
1246-
if 'username' in _credentials:
1247-
username = _credentials['username']
1248-
elif 'username' in creds_on_file:
1249-
username = creds_on_file['username']
1250-
else:
1251-
username = None
1252-
if 'api_key' in _credentials:
1253-
api_key = _credentials['api_key']
1254-
elif 'api_key' in creds_on_file:
1255-
api_key = creds_on_file['api_key']
1256-
else:
1257-
api_key = None
1258-
if username is None or api_key is None:
1259-
raise exceptions.PlotlyLocalCredentialsError()
1260-
return (username, api_key)
1261-
12621248
def _open_url(url):
12631249
try:
12641250
from webbrowser import open as wbopen

0 commit comments

Comments
 (0)