|
44 | 44 |
|
45 | 45 | _plot_options = dict()
|
46 | 46 |
|
| 47 | +_config = dict() |
| 48 | + |
47 | 49 | ### test file permissions and make sure nothing is corrupted ###
|
48 | 50 | tools.ensure_local_plotly_files()
|
49 | 51 |
|
50 | 52 | ### _credentials stuff ###
|
51 | 53 |
|
52 | 54 |
|
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 | + """ |
56 | 58 | _credentials['username'], _credentials['api_key'] = username, api_key
|
57 | 59 | # TODO: verify these _credentials with plotly
|
58 | 60 |
|
| 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 | + |
59 | 65 |
|
60 | 66 | ### plot options stuff ###
|
61 | 67 |
|
62 | 68 | def update_plot_options(**kwargs):
|
63 | 69 | """ Update the module-level _plot_options
|
64 | 70 | """
|
65 |
| - global _plot_options |
66 | 71 | _plot_options.update(kwargs)
|
67 | 72 |
|
68 | 73 |
|
69 | 74 | def get_plot_options():
|
70 | 75 | """ Returns a copy of the user supplied plot options.
|
71 | 76 | Use `update_plot_options()` to change.
|
72 | 77 | """
|
73 |
| - global _plot_options |
74 | 78 | return copy.copy(_plot_options)
|
75 | 79 |
|
76 | 80 |
|
77 | 81 | 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 |
85 | 88 |
|
86 | 89 |
|
87 | 90 | ### plot stuff ###
|
@@ -127,6 +130,15 @@ def _plot_option_logic(plot_options):
|
127 | 130 | return options
|
128 | 131 |
|
129 | 132 |
|
| 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 | + |
130 | 142 | def plot(figure_or_data, validate=True, **plot_options):
|
131 | 143 | """Create a unique url for this plot in Plotly and optionally open url.
|
132 | 144 |
|
@@ -307,7 +319,7 @@ def get_figure(file_owner_or_url, file_id=None, raw=False):
|
307 | 319 | `graph objects`.
|
308 | 320 |
|
309 | 321 | """
|
310 |
| - plotly_rest_url = tools.get_config_file()['plotly_domain'] |
| 322 | + plotly_rest_url = get_config()['plotly_domain'] |
311 | 323 | if file_id is None: # assume we're using a url
|
312 | 324 | url = file_owner_or_url
|
313 | 325 | if url[:len(plotly_rest_url)] != plotly_rest_url:
|
@@ -413,7 +425,7 @@ def open(self):
|
413 | 425 | http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb
|
414 | 426 | """
|
415 | 427 |
|
416 |
| - streaming_url = tools.get_config_file()['plotly_streaming_domain'] |
| 428 | + streaming_url = get_config()['plotly_streaming_domain'] |
417 | 429 | self._stream = chunked_requests.Stream(streaming_url,
|
418 | 430 | 80,
|
419 | 431 | {'Host': streaming_url,
|
@@ -562,7 +574,7 @@ def get(figure_or_data, format='png', width=None, height=None):
|
562 | 574 | if height is not None:
|
563 | 575 | payload['height'] = height
|
564 | 576 |
|
565 |
| - url = tools.get_config_file()['plotly_domain'] + "/apigenimage/" |
| 577 | + url = get_config()['plotly_domain'] + "/apigenimage/" |
566 | 578 | res = requests.post(url,
|
567 | 579 | data=json.dumps(payload,
|
568 | 580 | cls=utils._plotlyJSONEncoder),
|
@@ -667,7 +679,7 @@ def _send_to_plotly(figure, **plot_options):
|
667 | 679 | origin='plot',
|
668 | 680 | kwargs=kwargs)
|
669 | 681 |
|
670 |
| - url = tools.get_config_file()['plotly_domain'] + "/clientresp" |
| 682 | + url = get_config()['plotly_domain'] + "/clientresp" |
671 | 683 |
|
672 | 684 | r = requests.post(url, data=payload)
|
673 | 685 | r.raise_for_status()
|
|
0 commit comments