10
10
import json
11
11
import six
12
12
13
-
14
13
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 )
20
14
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
- )
95
15
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 )
99
29
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 ()
106
32
107
33
108
34
def update_keys (keys ):
@@ -120,77 +46,105 @@ def update_keys(keys):
120
46
reversescl = "reversescale"
121
47
)
122
48
49
+ # Define line and tab size for help text!
50
+ LINE_SIZE = 76
51
+ TAB_SIZE = 4
52
+
123
53
54
+ # Doc make function for list-like objects
124
55
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 += "\n Example:\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 += "\n Online example:\n \n " + "\n " .join (links ) + "\n "
69
+ elif len (links ) > 1 :
70
+ doc += "\n Online 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 += "\n Parent key:\n \n " + "\n " .join (parent_keys ) + "\n "
75
+ elif len (parent_keys ) > 1 :
76
+ doc += "\n Parent keys:\n \n " + "\n " .join (parent_keys ) + "\n "
77
+ # Add method list to doc
134
78
doc += "Quick method reference:\n \n "
135
79
doc += "\t {0}." .format (name ) + "\n \t {0}." .format (name ).join (
136
80
["update(changes)" , "strip_style()" , "get_data()" ,
137
81
"to_graph_objs()" , "validate()" , "to_string()" ,
138
82
"force_clean()" ]) + "\n \n "
139
- return doc .expandtabs (tab_size )
83
+ return doc .expandtabs (TAB_SIZE )
140
84
141
85
86
+ # Doc make function for dict-like objects
142
87
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 += "\n Example:\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 += "\n Online example:\n \n " + "\n " .join (links ) + "\n "
101
+ elif len (links ) > 1 :
102
+ doc += "\n Online 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 += "\n Parent key:\n \n " + "\n " .join (parent_keys ) + "\n "
107
+ elif len (parent_keys ) > 1 :
108
+ doc += "\n Parent keys:\n \n " + "\n " .join (parent_keys ) + "\n "
109
+ # Add method list to doc
110
+ doc += "\n Quick method reference:\n \n "
156
111
doc += "\t {0}." .format (name ) + "\n \t {0}." .format (name ).join (
157
112
["update(changes)" , "strip_style()" , "get_data()" ,
158
113
"to_graph_objs()" , "validate()" , "to_string()" ,
159
114
"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 ):
162
118
doc += "Valid keys:\n \n "
163
119
# 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
167
123
undocumented = "Aw, snap! Undocumented!"
168
- for key in obj_info :
124
+ for key in keymeta :
169
125
# main portion of documentation
170
126
try :
171
- required = str (obj_info [key ]['required' ])
127
+ required = str (keymeta [key ]['required' ])
172
128
except KeyError :
173
129
required = undocumented
174
-
175
130
try :
176
- typ = str (obj_info [key ]['type ' ])
131
+ typ = str (keymeta [key ]['key_type ' ])
177
132
except KeyError :
178
133
typ = undocumented
179
-
180
134
try :
181
- val_types = str (obj_info [key ]['val_types' ])
135
+ val_types = str (keymeta [key ]['val_types' ])
182
136
if typ == 'object' :
183
137
val_types = ("{0} object | " .format (KEY_TO_NAME [key ]) +
184
138
val_types )
185
139
except KeyError :
186
140
val_types = undocumented
187
141
try :
188
- descr = str (obj_info [key ]['description' ])
142
+ descr = str (keymeta [key ]['description' ])
189
143
except KeyError :
190
144
descr = undocumented
191
145
str_1 = "{0} [required={1}] (value={2})" .format (
192
146
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" ]:
194
148
str_1 += " (streamable)"
195
149
str_1 += ":\n "
196
150
str_1 = "\t " + "\n \t " .join (textwrap .wrap (str_1 ,
@@ -203,19 +157,17 @@ def make_dict_doc(name):
203
157
doc += "\n \t \t For more, run `help(plotly.graph_objs.{0" \
204
158
"})`\n " .format (KEY_TO_NAME [key ])
205
159
# if example usage exists, tell them!
206
- if 'examples' in obj_info [key ]:
207
- ex = "\n \t \t Examples:\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 \t Code 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 \t Examples:\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
217
169
doc += '\n '
218
- return doc .expandtabs (tab_size )
170
+ return doc .expandtabs (TAB_SIZE )
219
171
220
172
221
173
def curtail_val_repr (val , max_chars , add_delim = False ):
@@ -238,7 +190,8 @@ def curtail_val_repr(val, max_chars, add_delim=False):
238
190
if isinstance (val , six .string_types ):
239
191
# TODO: can we assume this ends in "'"
240
192
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 ))):
242
195
r = r [:max_chars - len (end + ']' )] + end + ']'
243
196
else :
244
197
r = r [:max_chars - len (end )] + end
0 commit comments