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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
|
from io import StringIO
from os import path as opath
from _plotly_utils.basevalidators import (
BaseDataValidator,
CompoundValidator,
CompoundArrayValidator,
)
from codegen.datatypes import (
reindent_validator_description,
add_constructor_params,
add_docstring,
)
from codegen.utils import PlotlyNode, write_source_py
import inflect
def build_figure_py(
trace_node,
base_package,
base_classname,
fig_classname,
data_validator,
layout_validator,
frame_validator,
subplot_nodes,
layout_array_nodes,
):
"""
Parameters
----------
trace_node : PlotlyNode
Root trace node (the node that is the parent of all of the
individual trace nodes like bar, scatter, etc.)
base_package : str
Package that the figure's superclass resides in
base_classname : str
Name of the figure's superclass
fig_classname : str
Name of the Figure class to be generated
data_validator : BaseDataValidator
DataValidator instance
layout_validator : CompoundValidator
LayoutValidator instance
frame_validator : CompoundArrayValidator
FrameValidator instance
subplot_nodes: list of str
List of names of all of the layout subplot properties
layout_array_nodes: list of PlotlyNode
List of array nodes under layout that can be positioned using xref/yref
Returns
-------
str
Source code for figure class definition
"""
# Initialize source code buffer
# -----------------------------
buffer = StringIO()
# Get list of trace type nodes
# ----------------------------
trace_nodes = trace_node.child_compound_datatypes
# Write imports
# -------------
# ### Import base class ###
buffer.write(f"from plotly.{base_package} import {base_classname}\n")
# Write class definition
# ----------------------
buffer.write(
f"""
class {fig_classname}({base_classname}):\n"""
)
# ### Constructor ###
# Build constructor description strings
data_description = reindent_validator_description(data_validator, 8)
layout_description = reindent_validator_description(layout_validator, 8)
frames_description = reindent_validator_description(frame_validator, 8)
buffer.write(
f"""
def __init__(self, data=None, layout=None,
frames=None, skip_invalid=False, **kwargs):
\"\"\"
Create a new :class:{fig_classname} instance
Parameters
----------
data
{data_description}
layout
{layout_description}
frames
{frames_description}
skip_invalid: bool
If True, invalid properties in the figure specification will be
skipped silently. If False (default) invalid properties in the
figure specification will result in a ValueError
Raises
------
ValueError
if a property in the specification of data, layout, or frames
is invalid AND skip_invalid is False
\"\"\"
super({fig_classname} ,self).__init__(data, layout,
frames, skip_invalid,
**kwargs)
"""
)
# ### add_trace methods for each trace type ###
for trace_node in trace_nodes:
include_secondary_y = bool(
[d for d in trace_node.child_datatypes if d.name_property == "yaxis"]
)
# #### Function signature ####
buffer.write(
f"""
def add_{trace_node.plotly_name}(self"""
)
# #### Function params####
param_extras = ["row", "col"]
if include_secondary_y:
param_extras.append("secondary_y")
add_constructor_params(
buffer, trace_node.child_datatypes, append_extras=param_extras
)
# #### Docstring ####
header = f"Add a new {trace_node.name_datatype_class} trace"
doc_extras = [
(
"row : 'all', int or None (default)",
"Subplot row index (starting from 1) for the trace to be "
"added. Only valid if figure was created using "
"`plotly.tools.make_subplots`."
"If 'all', addresses all rows in the specified column(s).",
),
(
"col : 'all', int or None (default)",
"Subplot col index (starting from 1) for the trace to be "
"added. Only valid if figure was created using "
"`plotly.tools.make_subplots`."
"If 'all', addresses all columns in the specified row(s).",
),
]
if include_secondary_y:
doc_extras.append(
(
"secondary_y: boolean or None (default None)",
"""\
If True, associate this trace with the secondary y-axis of the
subplot at the specified row and col. Only valid if all of the
following conditions are satisfied:
* The figure was created using `plotly.subplots.make_subplots`.
* The row and col arguments are not None
* The subplot at the specified row and col has type xy
(which is the default) and secondary_y True. These
properties are specified in the specs argument to
make_subplots. See the make_subplots docstring for more info.\
""",
)
)
add_docstring(
buffer,
trace_node,
header,
append_extras=doc_extras,
return_type=fig_classname,
)
# #### Function body ####
buffer.write(
f"""
from plotly.graph_objs import {trace_node.name_datatype_class}
new_trace = {trace_node.name_datatype_class}(
"""
)
for i, subtype_node in enumerate(trace_node.child_datatypes):
subtype_prop_name = subtype_node.name_property
buffer.write(
f"""
{subtype_prop_name}={subtype_prop_name},"""
)
buffer.write(
f"""
**kwargs)"""
)
if include_secondary_y:
secondary_y_kwarg = ", secondary_y=secondary_y"
else:
secondary_y_kwarg = ""
buffer.write(
f"""
return self.add_trace(
new_trace, row=row, col=col{secondary_y_kwarg})"""
)
# update layout subplots
# ----------------------
inflect_eng = inflect.engine()
for subplot_node in subplot_nodes:
singular_name = subplot_node.name_property
plural_name = inflect_eng.plural_noun(singular_name)
if singular_name == "yaxis":
secondary_y_1 = ", secondary_y=None"
secondary_y_2 = ", secondary_y=secondary_y"
secondary_y_docstring = f"""
secondary_y: boolean or None (default None)
* If True, only select yaxis objects associated with the secondary
y-axis of the subplot.
* If False, only select yaxis objects associated with the primary
y-axis of the subplot.
* If None (the default), do not filter yaxis objects based on
a secondary y-axis condition.
To select yaxis objects by secondary y-axis, the Figure must
have been created using plotly.subplots.make_subplots. See
the docstring for the specs argument to make_subplots for more
info on creating subplots with secondary y-axes."""
else:
secondary_y_1 = ""
secondary_y_2 = ""
secondary_y_docstring = ""
buffer.write(
f"""
def select_{plural_name}(
self, selector=None, row=None, col=None{secondary_y_1}):
\"\"\"
Select {singular_name} subplot objects from a particular subplot cell
and/or {singular_name} subplot objects that satisfy custom selection
criteria.
Parameters
----------
selector: dict, function, or None (default None)
Dict to use as selection criteria.
{singular_name} objects will be selected if they contain
properties corresponding to all of the dictionary's keys, with
values that exactly match the supplied values. If None
(the default), all {singular_name} objects are selected. If a
function, it must be a function accepting a single argument and
returning a boolean. The function will be called on each
{singular_name} and those for which the function returned True will
be in the selection.
row, col: int or None (default None)
Subplot row and column index of {singular_name} objects to select.
To select {singular_name} objects by row and column, the Figure
must have been created using plotly.subplots.make_subplots.
If None (the default), all {singular_name} objects are selected.\
{secondary_y_docstring}
Returns
-------
generator
Generator that iterates through all of the {singular_name}
objects that satisfy all of the specified selection criteria
\"\"\"
return self._select_layout_subplots_by_prefix(
'{singular_name}', selector, row, col{secondary_y_2})
def for_each_{singular_name}(
self, fn, selector=None, row=None, col=None{secondary_y_1}):
\"\"\"
Apply a function to all {singular_name} objects that satisfy the
specified selection criteria
Parameters
----------
fn:
Function that inputs a single {singular_name} object.
selector: dict, function, or None (default None)
Dict to use as selection criteria.
{singular_name} objects will be selected if they contain
properties corresponding to all of the dictionary's keys, with
values that exactly match the supplied values. If None
(the default), all {singular_name} objects are selected. If a
function, it must be a function accepting a single argument and
returning a boolean. The function will be called on each
{singular_name} and those for which the function returned True will
be in the selection.
row, col: int or None (default None)
Subplot row and column index of {singular_name} objects to select.
To select {singular_name} objects by row and column, the Figure
must have been created using plotly.subplots.make_subplots.
If None (the default), all {singular_name} objects are selected.\
{secondary_y_docstring}
Returns
-------
self
Returns the Figure object that the method was called on
\"\"\"
for obj in self.select_{plural_name}(
selector=selector, row=row, col=col{secondary_y_2}):
fn(obj)
return self
def update_{plural_name}(
self,
patch=None,
selector=None,
overwrite=False,
row=None, col=None{secondary_y_1},
**kwargs):
\"\"\"
Perform a property update operation on all {singular_name} objects
that satisfy the specified selection criteria
Parameters
----------
patch: dict
Dictionary of property updates to be applied to all
{singular_name} objects that satisfy the selection criteria.
selector: dict, function, or None (default None)
Dict to use as selection criteria.
{singular_name} objects will be selected if they contain
properties corresponding to all of the dictionary's keys, with
values that exactly match the supplied values. If None
(the default), all {singular_name} objects are selected. If a
function, it must be a function accepting a single argument and
returning a boolean. The function will be called on each
{singular_name} and those for which the function returned True will
be in the selection.
overwrite: bool
If True, overwrite existing properties. If False, apply updates
to existing properties recursively, preserving existing
properties that are not specified in the update operation.
row, col: int or None (default None)
Subplot row and column index of {singular_name} objects to select.
To select {singular_name} objects by row and column, the Figure
must have been created using plotly.subplots.make_subplots.
If None (the default), all {singular_name} objects are selected.\
{secondary_y_docstring}
**kwargs
Additional property updates to apply to each selected
{singular_name} object. If a property is specified in
both patch and in **kwargs then the one in **kwargs
takes precedence.
Returns
-------
self
Returns the Figure object that the method was called on
\"\"\"
for obj in self.select_{plural_name}(
selector=selector, row=row, col=col{secondary_y_2}):
obj.update(patch, overwrite=overwrite, **kwargs)
return self"""
)
# update annotations/shapes/images
# --------------------------------
for node in layout_array_nodes:
singular_name = node.plotly_name
plural_name = node.name_property
if singular_name == "image":
# Rename image to layout_image to avoid conflict with an image trace
method_prefix = "layout_"
else:
method_prefix = ""
buffer.write(
f"""
def select_{method_prefix}{plural_name}(
self, selector=None, row=None, col=None, secondary_y=None
):
\"\"\"
Select {plural_name} from a particular subplot cell and/or {plural_name}
that satisfy custom selection criteria.
Parameters
----------
selector: dict, function, int, str, or None (default None)
Dict to use as selection criteria.
Annotations will be selected if they contain properties corresponding
to all of the dictionary's keys, with values that exactly match
the supplied values. If None (the default), all {plural_name} are
selected. If a function, it must be a function accepting a single
argument and returning a boolean. The function will be called on
each {singular_name} and those for which the function returned True
will be in the selection. If an int N, the Nth {singular_name} matching row
and col will be selected (N can be negative). If a string S, the selector
is equivalent to dict(type=S).
row, col: int or None (default None)
Subplot row and column index of {plural_name} to select.
To select {plural_name} by row and column, the Figure must have been
created using plotly.subplots.make_subplots. To select only those
{singular_name} that are in paper coordinates, set row and col to the
string 'paper'. If None (the default), all {plural_name} are selected.
secondary_y: boolean or None (default None)
* If True, only select {plural_name} associated with the secondary
y-axis of the subplot.
* If False, only select {plural_name} associated with the primary
y-axis of the subplot.
* If None (the default), do not filter {plural_name} based on secondary
y-axis.
To select {plural_name} by secondary y-axis, the Figure must have been
created using plotly.subplots.make_subplots. See the docstring
for the specs argument to make_subplots for more info on
creating subplots with secondary y-axes.
Returns
-------
generator
Generator that iterates through all of the {plural_name} that satisfy
all of the specified selection criteria
\"\"\"
return self._select_annotations_like(
"{plural_name}", selector=selector, row=row, col=col, secondary_y=secondary_y
)
def for_each_{method_prefix}{singular_name}(
self, fn, selector=None, row=None, col=None, secondary_y=None
):
\"\"\"
Apply a function to all {plural_name} that satisfy the specified selection
criteria
Parameters
----------
fn:
Function that inputs a single {singular_name} object.
selector: dict, function, int, str or None (default None)
Dict to use as selection criteria.
Traces will be selected if they contain properties corresponding
to all of the dictionary's keys, with values that exactly match
the supplied values. If None (the default), all {plural_name} are
selected. If a function, it must be a function accepting a single
argument and returning a boolean. The function will be called on
each {singular_name} and those for which the function returned True
will be in the selection. If an int N, the Nth {singular_name} matching row
and col will be selected (N can be negative). If a string S, the selector
is equivalent to dict(type=S).
row, col: int or None (default None)
Subplot row and column index of {plural_name} to select.
To select {plural_name} by row and column, the Figure must have been
created using plotly.subplots.make_subplots. To select only those
{plural_name} that are in paper coordinates, set row and col to the
string 'paper'. If None (the default), all {plural_name} are selected.
secondary_y: boolean or None (default None)
* If True, only select {plural_name} associated with the secondary
y-axis of the subplot.
* If False, only select {plural_name} associated with the primary
y-axis of the subplot.
* If None (the default), do not filter {plural_name} based on secondary
y-axis.
To select {plural_name} by secondary y-axis, the Figure must have been
created using plotly.subplots.make_subplots. See the docstring
for the specs argument to make_subplots for more info on
creating subplots with secondary y-axes.
Returns
-------
self
Returns the Figure object that the method was called on
\"\"\"
for obj in self._select_annotations_like(
prop='{plural_name}',
selector=selector,
row=row,
col=col,
secondary_y=secondary_y,
):
fn(obj)
return self
def update_{method_prefix}{plural_name}(
self,
patch=None,
selector=None,
row=None,
col=None,
secondary_y=None,
**kwargs
):
\"\"\"
Perform a property update operation on all {plural_name} that satisfy the
specified selection criteria
Parameters
----------
patch: dict or None (default None)
Dictionary of property updates to be applied to all {plural_name} that
satisfy the selection criteria.
selector: dict, function, int, str or None (default None)
Dict to use as selection criteria.
Traces will be selected if they contain properties corresponding
to all of the dictionary's keys, with values that exactly match
the supplied values. If None (the default), all {plural_name} are
selected. If a function, it must be a function accepting a single
argument and returning a boolean. The function will be called on
each {singular_name} and those for which the function returned True
will be in the selection. If an int N, the Nth {singular_name} matching row
and col will be selected (N can be negative). If a string S, the selector
is equivalent to dict(type=S).
row, col: int or None (default None)
Subplot row and column index of {plural_name} to select.
To select {plural_name} by row and column, the Figure must have been
created using plotly.subplots.make_subplots. To select only those
{singular_name} that are in paper coordinates, set row and col to the
string 'paper'. If None (the default), all {plural_name} are selected.
secondary_y: boolean or None (default None)
* If True, only select {plural_name} associated with the secondary
y-axis of the subplot.
* If False, only select {plural_name} associated with the primary
y-axis of the subplot.
* If None (the default), do not filter {plural_name} based on secondary
y-axis.
To select {plural_name} by secondary y-axis, the Figure must have been
created using plotly.subplots.make_subplots. See the docstring
for the specs argument to make_subplots for more info on
creating subplots with secondary y-axes.
**kwargs
Additional property updates to apply to each selected {singular_name}. If
a property is specified in both patch and in **kwargs then the
one in **kwargs takes precedence.
Returns
-------
self
Returns the Figure object that the method was called on
\"\"\"
for obj in self._select_annotations_like(
prop='{plural_name}',
selector=selector,
row=row,
col=col,
secondary_y=secondary_y,
):
obj.update(patch, **kwargs)
return self
"""
)
# Add layout array items
buffer.write(
f"""
def add_{method_prefix}{singular_name}(self"""
)
add_constructor_params(
buffer,
node.child_datatypes,
prepend_extras=["arg"],
append_extras=["row", "col", "secondary_y", "exclude_empty_subplots"],
)
prepend_extras = [
(
"arg",
f"instance of {node.name_datatype_class} or dict with "
"compatible properties",
)
]
append_extras = [
(
"row",
f"Subplot row for {singular_name}. If 'all', addresses all rows in the specified column(s).",
),
(
"col",
f"Subplot column for {singular_name}. If 'all', addresses all columns in the specified row(s).",
),
("secondary_y", f"Whether to add {singular_name} to secondary y-axis"),
(
"exclude_empty_subplots",
f"If True, {singular_name} will not be added to subplots without traces.",
),
]
add_docstring(
buffer,
node,
header=f"Create and add a new {singular_name} to the figure's layout",
prepend_extras=prepend_extras,
append_extras=append_extras,
return_type=fig_classname,
)
# #### Function body ####
buffer.write(
f"""
from plotly.graph_objs import layout as _layout
new_obj = _layout.{node.name_datatype_class}(arg,
"""
)
for i, subtype_node in enumerate(node.child_datatypes):
subtype_prop_name = subtype_node.name_property
buffer.write(
f"""
{subtype_prop_name}={subtype_prop_name},"""
)
buffer.write("""**kwargs)""")
buffer.write(
f"""
return self._add_annotation_like(
'{singular_name}',
'{plural_name}',
new_obj,
row=row,
col=col,
secondary_y=secondary_y,
exclude_empty_subplots=exclude_empty_subplots,
)"""
)
# Return source string
# --------------------
buffer.write("\n")
return buffer.getvalue()
def write_figure_classes(
outdir,
trace_node,
data_validator,
layout_validator,
frame_validator,
subplot_nodes,
layout_array_nodes,
):
"""
Construct source code for the Figure and FigureWidget classes and
write to graph_objs/_figure.py and graph_objs/_figurewidget.py
respectively
Parameters
----------
outdir : str
Root outdir in which the graph_objs package should reside
trace_node : PlotlyNode
Root trace node (the node that is the parent of all of the
individual trace nodes like bar, scatter, etc.)
data_validator : BaseDataValidator
DataValidator instance
layout_validator : CompoundValidator
LayoutValidator instance
frame_validator : CompoundArrayValidator
FrameValidator instance
subplot_nodes: list of PlotlyNode
List of names of all of the layout subplot properties
layout_array_nodes: list of PlotlyNode
List of array nodes under layout that can be positioned using xref/yref
Returns
-------
None
"""
# Validate inputs
# ---------------
if trace_node.node_path:
raise ValueError(
f"Expected root trace node.\n"
f'Received node with path "{trace_node.path_str}"'
)
# Loop over figure types
# ----------------------
base_figures = [
("basewidget", "BaseFigureWidget", "FigureWidget"),
("basedatatypes", "BaseFigure", "Figure"),
]
for base_package, base_classname, fig_classname in base_figures:
# ### Build figure source code string ###
figure_source = build_figure_py(
trace_node,
base_package,
base_classname,
fig_classname,
data_validator,
layout_validator,
frame_validator,
subplot_nodes,
layout_array_nodes,
)
# ### Format and write to file###
filepath = opath.join(outdir, "graph_objs", f"_{fig_classname.lower()}.py")
write_source_py(figure_source, filepath)
|