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

Skip to content

Commit 94cd9b3

Browse files
committed
Add session.py file for creds/config bookkeeping
1 parent a283d83 commit 94cd9b3

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

plotly/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828

2929
from __future__ import absolute_import
3030

31-
from plotly import plotly, graph_objs, grid_objs, tools, utils
31+
from plotly import plotly, graph_objs, grid_objs, tools, utils, session
3232
from plotly.version import __version__

plotly/session.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""
2+
The session module handles the user's current credentials, config and plot opts
3+
4+
This allows users to dynamically change which plotly domain they're using,
5+
which user they're signed in as, and plotting defaults.
6+
7+
"""
8+
9+
import copy
10+
import six
11+
from . import exceptions
12+
13+
_session = {
14+
'credentials': {},
15+
'config': {},
16+
'plot_options': {}
17+
}
18+
19+
CREDENTIALS_KEYS = {
20+
'username': six.string_types,
21+
'api_key': six.string_types,
22+
'stream_ids': []
23+
}
24+
25+
CONFIG_KEYS = {
26+
'plotly_domain': six.string_types,
27+
'plotly_streaming_domain': six.string_types,
28+
'plotly_api_domain': six.string_types,
29+
'plotly_ssl_verification': bool
30+
}
31+
32+
PLOT_OPTIONS = {
33+
'filename': six.string_types,
34+
'fileopt': six.string_types,
35+
'world_readable': bool,
36+
'auto_open': bool,
37+
'validate': bool
38+
}
39+
40+
41+
def sign_in(username, api_key, **kwargs):
42+
"""
43+
Set set session credentials and config (not saved to file).
44+
45+
If unspecified, credentials and config are searched for in `.plotly` dir.
46+
47+
:param (str) username: The username you'd use to sign into Plotly
48+
:param (str) api_key: The api key associated with above username
49+
:param (list|optional) stream_ids: Stream tokens for above credentials
50+
51+
:param (str|optional) plotly_domain:
52+
:param (str|optional) plotly_streaming_domain:
53+
:param (str|optional) plotly_api_domain:
54+
:param (str|optional) plotly_ssl_verification:
55+
56+
"""
57+
# TODO: verify these _credentials with plotly
58+
59+
# kwargs will contain all our info
60+
kwargs.update(username=username, api_key=api_key)
61+
62+
# raise error if key isn't valid anywhere
63+
for key in kwargs:
64+
if key not in CREDENTIALS_KEYS and key not in CONFIG_KEYS:
65+
raise exceptions.PlotlyError(
66+
"{} is not a valid config or credentials key".format(key)
67+
)
68+
69+
# add credentials, raise error if type is wrong.
70+
for key in CREDENTIALS_KEYS:
71+
if key in kwargs:
72+
if not isinstance(kwargs[key], CREDENTIALS_KEYS[key]):
73+
raise exceptions.PlotlyError(
74+
"{} must be of type '{}'"
75+
.format(key, CREDENTIALS_KEYS[key])
76+
)
77+
_session['credentials'][key] = kwargs[key]
78+
79+
# add config, raise error if type is wrong.
80+
for key in CONFIG_KEYS:
81+
if key in kwargs:
82+
if not isinstance(kwargs[key], CONFIG_KEYS[key]):
83+
raise exceptions.PlotlyError("{} must be of type '{}'"
84+
.format(key, CONFIG_KEYS[key]))
85+
_session['config'][key] = kwargs.get(key)
86+
87+
88+
def update_session_plot_options(**kwargs):
89+
"""
90+
Update the _session plot_options
91+
92+
:param (str|optional) filename: What the file will be named in Plotly
93+
:param (str|optional) fileopt: 'overwrite', 'append', 'new', or 'extend'
94+
:param (bool|optional) world_readable: Make public or private.
95+
:param (bool|optional) auto_open: For `plot`, open in new browser tab?
96+
:param (bool|optional) validate: Error locally if data doesn't pass?
97+
98+
"""
99+
# raise exception if key is invalid or value is the wrong type
100+
for key in kwargs:
101+
if key not in PLOT_OPTIONS:
102+
raise exceptions.PlotlyError(
103+
"{} is not a valid config or plot option key".format(key)
104+
)
105+
if not isinstance(kwargs[key], PLOT_OPTIONS[key]):
106+
raise exceptions.PlotlyError("{} must be of type '{}'"
107+
.format(key, PLOT_OPTIONS[key]))
108+
109+
# update local _session dict with new plot options
110+
_session['plot_options'].update(kwargs)
111+
112+
113+
def get_session_plot_options():
114+
""" Returns a copy of the user supplied plot options.
115+
Use `update_plot_options()` to change.
116+
"""
117+
return copy.deepcopy(_session['plot_options'])
118+
119+
120+
def get_session_config():
121+
"""Returns either module config or file config."""
122+
return copy.deepcopy(_session['config'])
123+
124+
125+
def get_session_credentials():
126+
"""Returns the credentials that will be sent to plotly."""
127+
return copy.deepcopy(_session['credentials'])

0 commit comments

Comments
 (0)