Skip to content

Commit 54e129a

Browse files
committed
adapt to new graph ref structure
1 parent efdd187 commit 54e129a

File tree

1 file changed

+91
-138
lines changed

1 file changed

+91
-138
lines changed

Diff for: plotly/graph_objs/graph_objs_tools.py

+91-138
Original file line numberDiff line numberDiff line change
@@ -10,99 +10,25 @@
1010
import json
1111
import six
1212

13-
1413
from pkg_resources import resource_string
15-
s = resource_string('plotly',
16-
'graph_reference/graph_objs_meta.json').decode('utf-8')
17-
INFO = json.loads(s, object_pairs_hook=OrderedDict)
18-
19-
INFO = utils.decode_unicode(INFO)
2014

21-
OBJ_MAP = dict(
22-
PlotlyList=dict(
23-
base_name='list', info_key='plotlylist'),
24-
PlotlyDict=dict(
25-
base_name='dict', info_key='plotlydict'),
26-
PlotlyTrace=dict(
27-
base_name='PlotlyDict', info_key='plotlytrace'),
28-
Trace=dict(
29-
base_name='PlotlyTrace', info_key='trace'),
30-
Data=dict(
31-
base_name='PlotlyList', info_key='data'),
32-
Annotations=dict(
33-
base_name='PlotlyList', info_key='annotations'),
34-
AngularAxis=dict(
35-
base_name='PlotlyDict', info_key='angularaxis'),
36-
Annotation=dict(
37-
base_name='PlotlyDict', info_key='annotation'),
38-
ColorBar=dict(
39-
base_name='PlotlyDict', info_key='colorbar'),
40-
Contours=dict(
41-
base_name='PlotlyDict', info_key='contours'),
42-
ErrorX=dict(
43-
base_name='PlotlyDict', info_key='error_x'),
44-
ErrorY=dict(
45-
base_name='PlotlyDict', info_key='error_y'),
46-
Figure=dict(
47-
base_name='PlotlyDict', info_key='figure'),
48-
Font=dict(
49-
base_name='PlotlyDict', info_key='font'),
50-
# TitleFont=dict(
51-
# base_name='PlotlyDict', info_key='titlefont'),
52-
# TextFont=dict(
53-
# base_name='PlotlyDict', info_key='textfont'),
54-
Layout=dict(
55-
base_name='PlotlyDict', info_key='layout'),
56-
Legend=dict(
57-
base_name='PlotlyDict', info_key='legend'),
58-
Line=dict(
59-
base_name='PlotlyDict', info_key='line'),
60-
Margin=dict(
61-
base_name='PlotlyDict', info_key='margin'),
62-
Marker=dict(
63-
base_name='PlotlyDict', info_key='marker'),
64-
RadialAxis=dict(
65-
base_name='PlotlyDict', info_key='radialaxis'),
66-
Stream=dict(
67-
base_name='PlotlyDict', info_key='stream'),
68-
XAxis=dict(
69-
base_name='PlotlyDict', info_key='xaxis'),
70-
XBins=dict(
71-
base_name='PlotlyDict', info_key='xbins'),
72-
YAxis=dict(
73-
base_name='PlotlyDict', info_key='yaxis'),
74-
YBins=dict(
75-
base_name='PlotlyDict', info_key='ybins'),
76-
Area=dict(
77-
base_name='PlotlyTrace', info_key='area'),
78-
Bar=dict(
79-
base_name='PlotlyTrace', info_key='bar'),
80-
Box=dict(
81-
base_name='PlotlyTrace', info_key='box'),
82-
Contour=dict(
83-
base_name='PlotlyTrace', info_key='contour'),
84-
Heatmap=dict(
85-
base_name='PlotlyTrace', info_key='heatmap'),
86-
Histogram=dict(
87-
base_name='PlotlyTrace', info_key='histogram'),
88-
Histogram2d=dict(
89-
base_name='PlotlyTrace', info_key='histogram2d'),
90-
Histogram2dContour=dict(
91-
base_name='PlotlyTrace', info_key='histogram2dcontour'),
92-
Scatter=dict(
93-
base_name='PlotlyTrace', info_key='scatter')
94-
)
9515

96-
NAME_TO_KEY = dict()
97-
for _name, _obj_dict in OBJ_MAP.items():
98-
NAME_TO_KEY[_name] = _obj_dict['info_key']
16+
# Define graph reference loader
17+
def _load_graph_ref():
18+
json_files = ['graph_objs_meta', 'OBJ_MAP',
19+
'NAME_TO_KEY', 'KEY_TO_NAME']
20+
out = []
21+
for json_file in json_files:
22+
s = resource_string('plotly',
23+
'graph_reference/' +
24+
json_file+'.json').decode('utf-8')
25+
tmp = json.loads(s, object_pairs_hook=OrderedDict)
26+
tmp = utils.decode_unicode(tmp)
27+
out += [tmp]
28+
return tuple(out)
9929

100-
KEY_TO_NAME = dict()
101-
for _name, _key in NAME_TO_KEY.items():
102-
KEY_TO_NAME[_key] = _name
103-
KEY_TO_NAME['textfont'] = 'Font'
104-
KEY_TO_NAME['titlefont'] = 'Font'
105-
KEY_TO_NAME['tickfont'] = 'Font'
30+
# Load graph reference
31+
INFO, OBJ_MAP, NAME_TO_KEY, KEY_TO_NAME = _load_graph_ref()
10632

10733

10834
def update_keys(keys):
@@ -120,77 +46,105 @@ def update_keys(keys):
12046
reversescl="reversescale"
12147
)
12248

49+
# Define line and tab size for help text!
50+
LINE_SIZE = 76
51+
TAB_SIZE = 4
52+
12353

54+
# Doc make function for list-like objects
12455
def make_list_doc(name):
125-
doc = ("A list-like object representing a {0} object in a "
126-
"figure.\n\n".format(name)) # initial doc here?
127-
tab_size = 4
128-
min_indent = min([len(a) - len(b)
129-
for a, b in zip(doc.splitlines(),
130-
[l.lstrip()
131-
for l in doc.splitlines()])])
132-
doc = "".join([line[min_indent:] + '\n' for line in doc.splitlines()])
133-
# Add section header for method list...
56+
# get info for this graph obj
57+
info = INFO[NAME_TO_KEY[name]]
58+
# add docstring to doc
59+
doc = info['docstring']
60+
doc = "\t" + "\n\t".join(textwrap.wrap(doc, width=LINE_SIZE)) + "\n"
61+
# Add examples to doc
62+
examples = info['examples']
63+
if len(examples):
64+
doc += "\nExample:\n\n >>> " + "\n >>> ".join(examples) + "\n"
65+
# Add links to online examples to doc
66+
links = info['links']
67+
if len(links) == 1:
68+
doc += "\nOnline example:\n\n " + "\n ".join(links) + "\n"
69+
elif len(links) > 1:
70+
doc += "\nOnline examples:\n\n " + "\n ".join(links) + "\n"
71+
# Add parents keys to doc
72+
parent_keys = info['parent_keys']
73+
if len(parent_keys) == 1:
74+
doc += "\nParent key:\n\n " + "\n ".join(parent_keys) + "\n"
75+
elif len(parent_keys) > 1:
76+
doc += "\nParent keys:\n\n " + "\n ".join(parent_keys) + "\n"
77+
# Add method list to doc
13478
doc += "Quick method reference:\n\n"
13579
doc += "\t{0}.".format(name) + "\n\t{0}.".format(name).join(
13680
["update(changes)", "strip_style()", "get_data()",
13781
"to_graph_objs()", "validate()", "to_string()",
13882
"force_clean()"]) + "\n\n"
139-
return doc.expandtabs(tab_size)
83+
return doc.expandtabs(TAB_SIZE)
14084

14185

86+
# Doc make function for dict-like objects
14287
def make_dict_doc(name):
143-
# remove min indentation...
144-
doc = ("A dictionary-like object representing a {0} object in a "
145-
"figure.\n\n".format(name)) # initial doc here?
146-
obj_info = INFO[NAME_TO_KEY[name]]
147-
line_size = 76
148-
tab_size = 4
149-
min_indent = min([len(a) - len(b)
150-
for a, b in zip(doc.splitlines(),
151-
[l.lstrip()
152-
for l in doc.splitlines()])])
153-
doc = "".join([line[min_indent:] + '\n' for line in doc.splitlines()])
154-
# Add section header for method list...
155-
doc += "Quick method reference:\n\n"
88+
# get info for this graph obj
89+
info = INFO[NAME_TO_KEY[name]]
90+
# add docstring to doc
91+
doc = info['docstring']
92+
doc = "\t" + "\n\t".join(textwrap.wrap(doc, width=LINE_SIZE)) + "\n"
93+
# Add examples to doc
94+
examples = info['examples']
95+
if len(examples):
96+
doc += "\nExample:\n\n >>> " + "\n >>> ".join(examples) + "\n"
97+
# Add links to online examples to doc
98+
links = info['links']
99+
if len(links) == 1:
100+
doc += "\nOnline example:\n\n " + "\n ".join(links) + "\n"
101+
elif len(links) > 1:
102+
doc += "\nOnline examples:\n\n " + "\n ".join(links) + "\n"
103+
# Add parents keys to doc
104+
parent_keys = info['parent_keys']
105+
if len(parent_keys) == 1:
106+
doc += "\nParent key:\n\n " + "\n ".join(parent_keys) + "\n"
107+
elif len(parent_keys) > 1:
108+
doc += "\nParent keys:\n\n " + "\n ".join(parent_keys) + "\n"
109+
# Add method list to doc
110+
doc += "\nQuick method reference:\n\n"
156111
doc += "\t{0}.".format(name) + "\n\t{0}.".format(name).join(
157112
["update(changes)", "strip_style()", "get_data()",
158113
"to_graph_objs()", "validate()", "to_string()",
159114
"force_clean()"]) + "\n\n"
160-
# Add section header
161-
if len(obj_info):
115+
# Add key meta to doc
116+
keymeta = info['keymeta']
117+
if len(keymeta):
162118
doc += "Valid keys:\n\n"
163119
# Add each key one-by-one and format
164-
width1 = line_size-tab_size
165-
width2 = line_size-2*tab_size
166-
width3 = line_size-3*tab_size
120+
width1 = LINE_SIZE-TAB_SIZE
121+
width2 = LINE_SIZE-2*TAB_SIZE
122+
width3 = LINE_SIZE-3*TAB_SIZE
167123
undocumented = "Aw, snap! Undocumented!"
168-
for key in obj_info:
124+
for key in keymeta:
169125
# main portion of documentation
170126
try:
171-
required = str(obj_info[key]['required'])
127+
required = str(keymeta[key]['required'])
172128
except KeyError:
173129
required = undocumented
174-
175130
try:
176-
typ = str(obj_info[key]['type'])
131+
typ = str(keymeta[key]['key_type'])
177132
except KeyError:
178133
typ = undocumented
179-
180134
try:
181-
val_types = str(obj_info[key]['val_types'])
135+
val_types = str(keymeta[key]['val_types'])
182136
if typ == 'object':
183137
val_types = ("{0} object | ".format(KEY_TO_NAME[key]) +
184138
val_types)
185139
except KeyError:
186140
val_types = undocumented
187141
try:
188-
descr = str(obj_info[key]['description'])
142+
descr = str(keymeta[key]['description'])
189143
except KeyError:
190144
descr = undocumented
191145
str_1 = "{0} [required={1}] (value={2})".format(
192146
key, required, val_types)
193-
if "streamable" in obj_info[key] and obj_info[key]["streamable"]:
147+
if "streamable" in keymeta[key] and keymeta[key]["streamable"]:
194148
str_1 += " (streamable)"
195149
str_1 += ":\n"
196150
str_1 = "\t" + "\n\t".join(textwrap.wrap(str_1,
@@ -203,19 +157,17 @@ def make_dict_doc(name):
203157
doc += "\n\t\tFor more, run `help(plotly.graph_objs.{0" \
204158
"})`\n".format(KEY_TO_NAME[key])
205159
# if example usage exists, tell them!
206-
if 'examples' in obj_info[key]:
207-
ex = "\n\t\tExamples:\n" + "\t\t\t"
208-
ex += "\n\t\t\t".join(
209-
textwrap.wrap(str(obj_info[key]['examples']),
210-
width=width3)) + "\n"
211-
doc += ex
212-
if 'code' in obj_info[key]:
213-
code = "\n\t\tCode snippet:"
214-
code += "\n\t\t\t>>>".join(
215-
str(obj_info[key]['code']).split('>>>')) + "\n"
216-
doc += code
160+
try:
161+
if len(keymeta[key]['examples']):
162+
ex = "\n\t\tExamples:\n" + "\t\t\t"
163+
ex += "\n\t\t\t".join(
164+
textwrap.wrap(' | '.join(keymeta[key]['examples']),
165+
width=width3)) + "\n"
166+
doc += ex
167+
except:
168+
pass
217169
doc += '\n'
218-
return doc.expandtabs(tab_size)
170+
return doc.expandtabs(TAB_SIZE)
219171

220172

221173
def curtail_val_repr(val, max_chars, add_delim=False):
@@ -238,7 +190,8 @@ def curtail_val_repr(val, max_chars, add_delim=False):
238190
if isinstance(val, six.string_types):
239191
# TODO: can we assume this ends in "'"
240192
r = r[:max_chars - len(end + "'")] + end + "'"
241-
elif isinstance(val, list) and max_chars >= len("[{end}]".format(end)):
193+
elif (isinstance(val, list) and
194+
max_chars >= len("[{end}]".format(end))):
242195
r = r[:max_chars - len(end + ']')] + end + ']'
243196
else:
244197
r = r[:max_chars - len(end)] + end

0 commit comments

Comments
 (0)