forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
320 lines (254 loc) · 10.5 KB
/
tools.py
File metadata and controls
320 lines (254 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- coding: utf-8 -*-
"""
tools
=====
Functions that USERS will possibly want access to.
"""
import os.path
from . import graph_objs
from . import matplotlylib
from . import utils
from . import exceptions
PLOTLY_DIR = os.path.join(os.path.expanduser("~"), ".plotly")
CREDENTIALS_FILE = os.path.join(PLOTLY_DIR, ".credentials")
PLOT_OPTIONS_FILE = os.path.join(PLOTLY_DIR, ".plot_options")
THEMES_FILE = os.path.join(PLOTLY_DIR, ".themes")
_DEFAULT_PLOT_OPTIONS = dict(
filename="plot from API",
fileopt="new",
world_readable=True,
auto_open=True)
def ensure_local_plotly_files_exist():
if not os.path.isdir(PLOTLY_DIR):
os.mkdir(PLOTLY_DIR)
for filename in [CREDENTIALS_FILE, PLOT_OPTIONS_FILE, THEMES_FILE]:
if not os.path.exists(filename):
f = open(filename, "w")
f.close()
### config tools ###
def save_plot_options_file(filename="", fileopt="",
world_readable=None, auto_open=None):
"""Set the keyword-value pairs in `~/.plotly_plot_options`.
TODO: the kwarg defaults are confusing - maybe should be left as a kwargs
TODO: should this be hiddenz?
"""
ensure_local_plotly_files_exist()
plot_options = get_plot_options_file()
if (not plot_options and
(filename or fileopt or world_readable is not None or
auto_open is not None)):
plot_options = {}
if filename:
plot_options['filename'] = filename
if fileopt:
plot_options['fileopt'] = fileopt
if world_readable is not None:
plot_options['world_readable'] = world_readable
if auto_open is not None:
plot_options['auto_open'] = auto_open
utils.save_json(PLOT_OPTIONS_FILE, plot_options)
def get_plot_options_file(*args):
"""Return specified args from `~/.plotly_plot_options`. as dict.
Returns all if no arguments are specified.
Example:
get_plot_options_file('username', 'api_key')
"""
ensure_local_plotly_files_exist()
options = utils.load_json(PLOT_OPTIONS_FILE, *args)
if len(options):
return {str(key): val for key, val in options.items()}
else:
return {}
def show_plot_options_file(*args):
"""Print specified kwargs from `~/.plotly_plot_options`.
Prints all if no keyword arguments are specified.
"""
ensure_local_plotly_files_exist()
plot_options = get_plot_options_file(*args)
if len(args):
print "The specified keys from your plot options file:\n"
else:
print "Your plot options file:\n"
for key, val in plot_options.items():
print "\t{}: {}".format(key, val).expandtabs()
### credentials tools ###
def set_credentials_file(username="", api_key="", stream_ids=()):
"""Set the keyword-value pairs in `~/.plotly_credentials`.
"""
ensure_local_plotly_files_exist()
credentials = get_credentials_file()
if not credentials and (username or api_key or stream_ids):
credentials = {}
if username:
credentials['username'] = username
if api_key:
credentials['api_key'] = api_key
if stream_ids:
credentials['stream_ids'] = stream_ids
utils.save_json(CREDENTIALS_FILE, credentials)
def get_credentials_file(*args):
"""Return specified args from `~/.plotly_credentials`. as dict.
Returns all if no arguments are specified.
Example:
get_credentials_file('username')
"""
ensure_local_plotly_files_exist()
return utils.load_json(CREDENTIALS_FILE, *args)
def show_credentials_file(*args):
"""Print specified kwargs from `~/.plotly_credentials`.
Prints all if no keyword arguments are specified.
"""
ensure_local_plotly_files_exist()
credentials = get_credentials_file(*args)
if len(args):
print "The specified keys from your credentials file:\n"
else:
print "Your credentials file:\n"
for key, val in credentials.items():
print "\t{}: {}".format(key, val).expandtabs()
### embed tools ###
def get_embed(username, plot_id, width="100%", height=525):
padding = 25
if isinstance(width, (int, long)):
s = ("<iframe id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" "
"src=\"https://plot.ly/"
"~{username}/{plot_id}/{plot_width}/{plot_height}\" "
"height=\"{iframe_height}\" width=\"{iframe_width}\">"
"</iframe>").format(
username=username, plot_id=plot_id,
plot_width=width-padding, plot_height=height-padding,
iframe_height=height, iframe_width=width)
else:
s = ("<iframe id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" "
"src=\"https://plot.ly/"
"~{username}/{plot_id}\" "
"height=\"{iframe_height}\" width=\"{iframe_width}\">"
"</iframe>").format(
username=username, plot_id=plot_id,
iframe_height=height, iframe_width=width)
return s
def embed(username, plot_id, width="100%", height=525):
s = get_embed(username, plot_id, width, height)
try:
# see if we are in the SageMath Cloud
from sage_salvus import html
return html(s, hide=False)
except:
pass
try:
from IPython.display import HTML
return HTML(s)
except:
return s
### mpl-related tools ###
def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
"""Convert a matplotlib figure to plotly dictionary and send.
All available information about matplotlib visualizations are stored
within a matplotlib.figure.Figure object. You can create a plot in python
using matplotlib, store the figure object, and then pass this object to
the fig_to_plotly function. In the background, mplexporter is used to
crawl through the mpl figure object for appropriate information. This
information is then systematically sent to the PlotlyRenderer which
creates the JSON structure used to make plotly visualizations. Finally,
these dictionaries are sent to plotly and your browser should open up a
new tab for viewing! Optionally, if you're working in IPython, you can
set notebook=True and the PlotlyRenderer will call plotly.iplot instead
of plotly.plot to have the graph appear directly in the IPython notebook.
Note, this function gives the user access to a simple, one-line way to
render an mpl figure in plotly. If you need to trouble shoot, you can do
this step manually by NOT running this fuction and entereing the following:
============================================================================
from mplexporter import Exporter
from mplexporter.renderers import PlotlyRenderer
# create an mpl figure and store it under a varialble 'fig'
renderer = PlotlyRenderer()
exporter = Exporter(renderer)
exporter.run(fig)
============================================================================
You can then inspect the JSON structures by accessing these:
renderer.layout -- a plotly layout dictionary
renderer.data -- a list of plotly data dictionaries
Positional arguments:
fig -- a matplotlib figure object
username -- a valid plotly username **
api_key -- a valid api_key for the above username **
notebook -- an option for use with an IPython notebook
** Don't have a username/api_key? Try looking here:
https://plot.ly/plot
** Forgot your api_key? Try signing in and looking here:
https://plot.ly/api/python/getting-started
"""
renderer = matplotlylib.PlotlyRenderer()
matplotlylib.Exporter(renderer).run(fig)
if resize:
renderer.resize()
if strip_style:
renderer.strip_style()
if verbose:
print renderer.msg
return renderer.plotly_fig
### graph_objs related tools ###
# TODO: Scale spacing based on number of plots and figure size
def get_subplots(rows=1, columns=1, horizontal_spacing=0.1,
vertical_spacing=0.15, print_grid=False):
"""Return a dictionary instance with the subplots set in 'layout'.
key (types, default=default):
description.
rows (int, default=1):
Number of rows, evenly spaced vertically on the figure.
columns (int, default=1):
Number of columns, evenly spaced horizontally on the figure.
horizontal_spacing (float in [0,1], default=0.1):
Space between subplot columns. Applied to all columns.
vertical_spacing (float in [0,1], default=0.05):
Space between subplot rows. Applied to all rows.
print_grid (True | False, default=False):
If True, prints a tab-delimited string representation of your plot grid.
"""
fig = dict(layout=graph_objs.Layout()) # will return this at the end
plot_width = (1 - horizontal_spacing * (columns - 1)) / columns
plot_height = (1 - vertical_spacing * (rows - 1)) / rows
plot_num = 0
for rrr in range(rows):
for ccc in range(columns):
xaxis_name = 'xaxis{}'.format(plot_num + 1)
x_anchor = 'y{}'.format(plot_num + 1)
x_start = (plot_width + horizontal_spacing) * ccc
x_end = x_start + plot_width
yaxis_name = 'yaxis{}'.format(plot_num + 1)
y_anchor = 'x{}'.format(plot_num + 1)
y_start = (plot_height + vertical_spacing) * rrr
y_end = y_start + plot_height
xaxis = graph_objs.XAxis(domain=[x_start, x_end], anchor=x_anchor)
fig['layout'][xaxis_name] = xaxis
yaxis = graph_objs.YAxis(domain=[y_start, y_end], anchor=y_anchor)
fig['layout'][yaxis_name] = yaxis
plot_num += 1
if print_grid:
print "This is the format of your plot grid!"
grid_string = ""
plot = 1
for rrr in range(rows):
grid_line = ""
for ccc in range(columns):
grid_line += "[{}]\t".format(plot)
plot += 1
grid_string = grid_line + '\n' + grid_string
print grid_string
return graph_objs.Figure(fig) # forces us to validate what we just did...
def get_valid_graph_obj(obj, obj_type=None):
try:
new_obj = graph_objs.STRING_TO_CLASS[obj.__class__.__name__.lower]()
except KeyError:
try:
new_obj = graph_objs.STRING_TO_CLASS[obj_type]()
except KeyError:
raise exceptions.PlotlyError("'obj' nor 'obj_type' are "
"recognizable graph_objs.") # TODO
if isinstance(new_obj, list):
new_obj += obj
else:
for key, val in obj.items():
new_obj[key] = val
new_obj.force_clean()
return new_obj