|
45 | 45 |
|
46 | 46 | _plot_options = dict() |
47 | 47 |
|
| 48 | +_config = dict() |
| 49 | + |
48 | 50 | ### test file permissions and make sure nothing is corrupted ### |
49 | 51 | tools.ensure_local_plotly_files() |
50 | 52 |
|
51 | 53 | ### _credentials stuff ### |
52 | 54 |
|
53 | 55 |
|
54 | | -def sign_in(username, api_key): |
55 | | - """Set module-scoped _credentials for session. Verify with plotly.""" |
56 | | - global _credentials |
| 56 | +def sign_in(username, api_key, **kwargs): |
| 57 | + """Set module-scoped _credentials for session. Optionally, set config info. |
| 58 | + """ |
57 | 59 | _credentials['username'], _credentials['api_key'] = username, api_key |
58 | 60 | # TODO: verify these _credentials with plotly |
59 | 61 |
|
| 62 | + _config['plotly_domain'] = kwargs.get('plotly_domain') |
| 63 | + _config['plotly_streaming_domain'] = kwargs.get('plotly_streaming_domain') |
| 64 | + _config['plotly_api_domain'] = kwargs.get('plotly_api_domain') |
| 65 | + # TODO: verify format of config options |
| 66 | + |
60 | 67 |
|
61 | 68 | ### plot options stuff ### |
62 | 69 |
|
63 | 70 | def update_plot_options(**kwargs): |
64 | 71 | """ Update the module-level _plot_options |
65 | 72 | """ |
66 | | - global _plot_options |
67 | 73 | _plot_options.update(kwargs) |
68 | 74 |
|
69 | 75 |
|
70 | 76 | def get_plot_options(): |
71 | 77 | """ Returns a copy of the user supplied plot options. |
72 | 78 | Use `update_plot_options()` to change. |
73 | 79 | """ |
74 | | - global _plot_options |
75 | 80 | return copy.copy(_plot_options) |
76 | 81 |
|
77 | 82 |
|
78 | 83 | def get_credentials(): |
79 | | - """ Returns a copy of the user supplied credentials. |
80 | | - """ |
81 | | - global _credentials |
82 | | - if ('username' in _credentials) and ('api_key' in _credentials): |
83 | | - return copy.copy(_credentials) |
84 | | - else: |
85 | | - return tools.get_credentials_file() |
| 84 | + """Returns the credentials that will be sent to plotly.""" |
| 85 | + credentials = tools.get_credentials_file() |
| 86 | + for credentials_key in credentials: |
| 87 | + if _credentials.get(credentials_key): |
| 88 | + credentials[credentials_key] = _credentials[credentials_key] |
| 89 | + return credentials |
86 | 90 |
|
87 | 91 |
|
88 | 92 | ### plot stuff ### |
@@ -128,6 +132,15 @@ def _plot_option_logic(plot_options): |
128 | 132 | return options |
129 | 133 |
|
130 | 134 |
|
| 135 | +def get_config(): |
| 136 | + """Returns either module config or file config.""" |
| 137 | + config = tools.get_config_file() |
| 138 | + for config_key in config: |
| 139 | + if _config.get(config_key): |
| 140 | + config[config_key] = _config[config_key] |
| 141 | + return config |
| 142 | + |
| 143 | + |
131 | 144 | def plot(figure_or_data, validate=True, **plot_options): |
132 | 145 | """Create a unique url for this plot in Plotly and optionally open url. |
133 | 146 |
|
@@ -165,6 +178,12 @@ def plot(figure_or_data, validate=True, **plot_options): |
165 | 178 | "plot option.\nHere's why you're " |
166 | 179 | "seeing this error:\n\n{0}" |
167 | 180 | "".format(err)) |
| 181 | + if not figure['data']: |
| 182 | + raise exceptions.PlotlyEmptyDataError( |
| 183 | + "Empty data list found. Make sure that you populated the " |
| 184 | + "list of data objects you're sending and try again.\n" |
| 185 | + "Questions? support@plot.ly" |
| 186 | + ) |
168 | 187 | for entry in figure['data']: |
169 | 188 | for key, val in list(entry.items()): |
170 | 189 | try: |
@@ -299,7 +318,7 @@ def get_figure(file_owner_or_url, file_id=None, raw=False): |
299 | 318 | `graph objects`. |
300 | 319 |
|
301 | 320 | """ |
302 | | - plotly_rest_url = tools.get_config_file()['plotly_domain'] |
| 321 | + plotly_rest_url = get_config()['plotly_domain'] |
303 | 322 | if file_id is None: # assume we're using a url |
304 | 323 | url = file_owner_or_url |
305 | 324 | if url[:len(plotly_rest_url)] != plotly_rest_url: |
@@ -405,7 +424,7 @@ def open(self): |
405 | 424 | http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb |
406 | 425 | """ |
407 | 426 |
|
408 | | - streaming_url = tools.get_config_file()['plotly_streaming_domain'] |
| 427 | + streaming_url = get_config()['plotly_streaming_domain'] |
409 | 428 | self._stream = chunked_requests.Stream(streaming_url, |
410 | 429 | 80, |
411 | 430 | {'Host': streaming_url, |
@@ -554,7 +573,7 @@ def get(figure_or_data, format='png', width=None, height=None): |
554 | 573 | if height is not None: |
555 | 574 | payload['height'] = height |
556 | 575 |
|
557 | | - url = tools.get_config_file()['plotly_domain'] + "/apigenimage/" |
| 576 | + url = get_config()['plotly_domain'] + "/apigenimage/" |
558 | 577 | res = requests.post(url, |
559 | 578 | data=json.dumps(payload, |
560 | 579 | cls=utils._plotlyJSONEncoder), |
@@ -721,7 +740,7 @@ def upload(cls, grid, filename, |
721 | 740 |
|
722 | 741 | grid.id = grid_id |
723 | 742 |
|
724 | | - plotly_domain = tools.get_config_file()['plotly_domain'] |
| 743 | + plotly_domain = get_config()['plotly_domain'] |
725 | 744 | grid_url = '{}/~{}'.format(plotly_domain, grid_id.replace(':', '/')) |
726 | 745 |
|
727 | 746 | if meta is not None: |
@@ -889,7 +908,7 @@ def response_handler(cls, response): |
889 | 908 |
|
890 | 909 | @classmethod |
891 | 910 | def api_url(cls, resource): |
892 | | - return ('{}/v2/{}'.format(tools.get_config_file()['plotly_api_domain'], |
| 911 | + return ('{}/v2/{}'.format(get_config()['plotly_api_domain'], |
893 | 912 | resource)) |
894 | 913 |
|
895 | 914 | @classmethod |
@@ -937,7 +956,7 @@ def _send_to_plotly(figure, **plot_options): |
937 | 956 | origin='plot', |
938 | 957 | kwargs=kwargs) |
939 | 958 |
|
940 | | - url = tools.get_config_file()['plotly_domain'] + "/clientresp" |
| 959 | + url = get_config()['plotly_domain'] + "/clientresp" |
941 | 960 |
|
942 | 961 | r = requests.post(url, data=payload) |
943 | 962 | r.raise_for_status() |
|
0 commit comments