88 add_constructor_params , add_docstring )
99from codegen .utils import PlotlyNode , format_and_write_source_py
1010
11+ import inflect
12+
1113
1214def build_figure_py (trace_node , base_package , base_classname , fig_classname ,
13- data_validator , layout_validator , frame_validator ):
15+ data_validator , layout_validator , frame_validator ,
16+ subplot_nodes ):
1417 """
1518
1619 Parameters
@@ -30,7 +33,8 @@ def build_figure_py(trace_node, base_package, base_classname, fig_classname,
3033 LayoutValidator instance
3134 frame_validator : CompoundArrayValidator
3235 FrameValidator instance
33-
36+ subplot_nodes: list of str
37+ List of names of all of the layout subplot properties
3438 Returns
3539 -------
3640 str
@@ -53,6 +57,7 @@ def build_figure_py(trace_node, base_package, base_classname, fig_classname,
5357 # ### Import trace graph_obj classes ###
5458 trace_types_csv = ', ' .join ([n .name_datatype_class for n in trace_nodes ])
5559 buffer .write (f'from plotly.graph_objs import ({ trace_types_csv } )\n ' )
60+ buffer .write ("from plotly.subplots import _validate_v4_subplots\n " )
5661
5762 # Write class definition
5863 # ----------------------
@@ -141,6 +146,111 @@ def add_{trace_node.plotly_name}(self""")
141146 buffer .write (f"""
142147 return self.add_trace(new_trace, row=row, col=col)""" )
143148
149+ # update layout subplots
150+ # ----------------------
151+ inflect_eng = inflect .engine ()
152+ for subplot_node in subplot_nodes :
153+ singular_name = subplot_node .name_property
154+ plural_name = inflect_eng .plural_noun (singular_name )
155+ buffer .write (f"""
156+
157+ def select_{ plural_name } (self, selector=None, row=None, col=None):
158+ \" \" \"
159+ Select { singular_name } subplot objects from a particular subplot cell
160+ and/or { singular_name } subplot objects that satisfy custom selection
161+ criteria.
162+
163+ Parameters
164+ ----------
165+ selector: dict or None (default None)
166+ Dict to use as selection criteria.
167+ { singular_name } objects will be selected if they contain
168+ properties corresponding to all of the dictionary's keys, with
169+ values that exactly match the supplied values. If None
170+ (the default), all { singular_name } objects are selected.
171+ row, col: int or None (default None)
172+ Subplot row and column index of { singular_name } objects to select.
173+ To select { singular_name } objects by row and column, the Figure
174+ must have been created using plotly.subplots.make_subplots.
175+ If None (the default), all { singular_name } objects are selected.
176+
177+ Returns
178+ -------
179+ generator
180+ Generator that iterates through all of the { singular_name }
181+ objects that satisfy all of the specified selection criteria
182+ \" \" \"
183+ if row is not None or col is not None:
184+ _validate_v4_subplots('select_{ plural_name } ')
185+
186+ return self._select_layout_subplots_by_prefix(
187+ '{ singular_name } ', selector, row, col)
188+
189+ def for_each_{ singular_name } (self, fn, selector=None, row=None, col=None):
190+ \" \" \"
191+ Apply a function to all { singular_name } objects that satisfy the
192+ specified selection criteria
193+
194+ Parameters
195+ ----------
196+ fn:
197+ Function that inputs a single { singular_name } object.
198+ selector: dict or None (default None)
199+ Dict to use as selection criteria.
200+ { singular_name } objects will be selected if they contain
201+ properties corresponding to all of the dictionary's keys, with
202+ values that exactly match the supplied values. If None
203+ (the default), all { singular_name } objects are selected.
204+ row, col: int or None (default None)
205+ Subplot row and column index of { singular_name } objects to select.
206+ To select { singular_name } objects by row and column, the Figure
207+ must have been created using plotly.subplots.make_subplots.
208+ If None (the default), all { singular_name } objects are selected.
209+
210+ Returns
211+ -------
212+ self
213+ Returns the Figure object that the method was called on
214+ \" \" \"
215+ for obj in self.select_{ plural_name } (
216+ selector=selector, row=row, col=col):
217+ fn(obj)
218+
219+ return self
220+
221+ def update_{ plural_name } (self, patch, selector=None, row=None, col=None):
222+ \" \" \"
223+ Perform a property update operation on all { singular_name } objects
224+ that satisfy the specified selection criteria
225+
226+ Parameters
227+ ----------
228+ patch: dict
229+ Dictionary of property updates to be applied to all
230+ { singular_name } objects that satisfy the selection criteria.
231+ selector: dict or None (default None)
232+ Dict to use as selection criteria.
233+ { singular_name } objects will be selected if they contain
234+ properties corresponding to all of the dictionary's keys, with
235+ values that exactly match the supplied values. If None
236+ (the default), all { singular_name } objects are selected.
237+ row, col: int or None (default None)
238+ Subplot row and column index of { singular_name } objects to select.
239+ To select { singular_name } objects by row and column, the Figure
240+ must have been created using plotly.subplots.make_subplots.
241+ If None (the default), all { singular_name } objects are selected.
242+
243+ Returns
244+ -------
245+ self
246+ Returns the Figure object that the method was called on
247+ \" \" \"
248+ for obj in self.select_{ plural_name } (
249+ selector=selector, row=row, col=col):
250+ obj.update(patch)
251+
252+ return self""" )
253+
144254 # Return source string
145255 # --------------------
146256 buffer .write ('\n ' )
@@ -150,7 +260,8 @@ def add_{trace_node.plotly_name}(self""")
150260def write_figure_classes (outdir , trace_node ,
151261 data_validator ,
152262 layout_validator ,
153- frame_validator ):
263+ frame_validator ,
264+ subplot_nodes ):
154265 """
155266 Construct source code for the Figure and FigureWidget classes and
156267 write to graph_objs/_figure.py and graph_objs/_figurewidget.py
@@ -169,6 +280,8 @@ def write_figure_classes(outdir, trace_node,
169280 LayoutValidator instance
170281 frame_validator : CompoundArrayValidator
171282 FrameValidator instance
283+ subplot_nodes: list of str
284+ List of names of all of the layout subplot properties
172285
173286 Returns
174287 -------
@@ -195,7 +308,8 @@ def write_figure_classes(outdir, trace_node,
195308 fig_classname ,
196309 data_validator ,
197310 layout_validator ,
198- frame_validator )
311+ frame_validator ,
312+ subplot_nodes )
199313
200314 # ### Format and write to file###
201315 filepath = opath .join (outdir , 'graph_objs' ,
0 commit comments