35
35
from plotly import tools
36
36
from plotly import exceptions
37
37
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 )
38
41
39
42
40
43
__all__ = None
46
49
auto_open = True ,
47
50
validate = True )
48
51
49
- _credentials = dict ()
50
52
51
- _plot_options = dict ()
53
+ # don't break backwards compatibility
54
+ sign_in = sign_in
55
+ update_plot_options = update_session_plot_options
52
56
53
- _config = dict ()
54
57
55
58
### test file permissions and make sure nothing is corrupted ###
56
59
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 :
57
65
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
59
71
60
72
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 :
72
78
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
73
84
74
- ### plot options stuff ###
75
85
76
- def update_plot_options (** kwargs ):
77
- """ Update the module-level _plot_options
86
+ def get_plot_options ():
78
87
"""
79
- _plot_options .update (kwargs )
80
-
88
+ Merge default and user-defined plot options.
81
89
82
- def get_plot_options ():
83
- """ Returns a copy of the user supplied plot options.
84
- Use `update_plot_options()` to change.
85
90
"""
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 :
87
94
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
88
100
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
96
101
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
97
115
98
- ### plot stuff ###
99
116
100
117
def iplot (figure_or_data , ** plot_options ):
101
118
"""Create a unique url for this plot in Plotly and open in IPython.
@@ -125,28 +142,6 @@ def iplot(figure_or_data, **plot_options):
125
142
return tools .embed (username , plot_id , ** embed_options )
126
143
127
144
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
-
150
145
def plot (figure_or_data , validate = True , ** plot_options ):
151
146
"""Create a unique url for this plot in Plotly and optionally open url.
152
147
@@ -346,7 +341,9 @@ def get_figure(file_owner_or_url, file_id=None, raw=False):
346
341
file_owner = file_owner_or_url
347
342
resource = "/apigetfile/{username}/{file_id}" .format (username = file_owner ,
348
343
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' ]
350
347
headers = {'plotly-username' : username ,
351
348
'plotly-apikey' : api_key ,
352
349
'plotly-version' : version .__version__ ,
@@ -585,7 +582,9 @@ def get(figure_or_data, format='png', width=None, height=None):
585
582
"types here: "
586
583
"https://plot.ly/python/static-image-export/" )
587
584
588
- (username , api_key ) = _validation_key_logic ()
585
+ credentials = get_credentials ()
586
+ validate_credentials (credentials )
587
+ username , api_key = credentials ['username' ], credentials ['api_key' ]
589
588
headers = {'plotly-username' : username ,
590
589
'plotly-apikey' : api_key ,
591
590
'plotly-version' : version .__version__ ,
@@ -1179,7 +1178,9 @@ def api_url(cls, resource):
1179
1178
1180
1179
@classmethod
1181
1180
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' ]
1183
1184
encoded_un_key_pair = base64 .b64encode (
1184
1185
six .b ('{0}:{1}' .format (un , api_key ))
1185
1186
).decode ('utf8' )
@@ -1190,25 +1191,28 @@ def headers(cls):
1190
1191
}
1191
1192
1192
1193
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 :
1201
1202
raise exceptions .PlotlyLocalCredentialsError ()
1202
- return username , api_key
1203
1203
1204
1204
1205
1205
def _send_to_plotly (figure , ** plot_options ):
1206
1206
"""
1207
+
1207
1208
"""
1208
1209
fig = tools ._replace_newline (figure ) # does not mutate figure
1209
1210
data = json .dumps (fig ['data' ] if 'data' in fig else [],
1210
1211
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' ]
1212
1216
kwargs = json .dumps (dict (filename = plot_options ['filename' ],
1213
1217
fileopt = plot_options ['fileopt' ],
1214
1218
world_readable = plot_options ['world_readable' ],
@@ -1241,24 +1245,6 @@ def _send_to_plotly(figure, **plot_options):
1241
1245
return r
1242
1246
1243
1247
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
-
1262
1248
def _open_url (url ):
1263
1249
try :
1264
1250
from webbrowser import open as wbopen
0 commit comments