Skip to content

Commit

Permalink
feat: modifying code generation to reduce bundle size
Browse files Browse the repository at this point in the history
1.  Add `bin/get_size.py` so that `python bin/get_size.py plotly build`
    reports the number of files and total size in bytes of the `plotly`
    directory (where generated code is put) and the `build` directory
    that is populated by `python setup.py build`.

1.  Modify `codegen/__init__.py` and `./setup.py` so that
    `python setup.py --reformat=false` disables reformatting.

1.  Alias name of base validator during import in `codegen/validators.py`.

1.  Remove the long list of CSS colors from help strings for color
    properties.

1.  Assign an empty string to the `data_docs` field of generated validators.

1.  Introduce a method `_init_provided` for `BaseFigure` and
    `BasePlotlyType` that calls a helper function `_initialize_provided`
    to replace repetitions of:

```
_v = arg.pop("something", None)
_v = something if something is not None else _v
if _v is not None:
    self["something"] = _v
```

Original size of plotly/**/*.py: 50365842 bytes
Current size of plotly/**/*.py:  38256842 bytes
Change: -26%
  • Loading branch information
gvwilson committed Jan 23, 2025
1 parent 3d36f14 commit 2b3d47b
Show file tree
Hide file tree
Showing 14,849 changed files with 245,607 additions and 460,318 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
27 changes: 4 additions & 23 deletions packages/python/plotly/_plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,25 +1328,14 @@ def numbers_allowed(self):
return self.colorscale_path is not None

def description(self):

named_clrs_str = "\n".join(
textwrap.wrap(
", ".join(self.named_colors),
width=79 - 16,
initial_indent=" " * 12,
subsequent_indent=" " * 12,
)
)

valid_color_description = """\
The '{plotly_name}' property is a color and may be specified as:
- A hex string (e.g. '#ff0000')
- An rgb/rgba string (e.g. 'rgb(255,0,0)')
- An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
- An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
- A named CSS color:
{clrs}""".format(
plotly_name=self.plotly_name, clrs=named_clrs_str
- A named CSS color""".format(
plotly_name=self.plotly_name
)

if self.colorscale_path:
Expand Down Expand Up @@ -2483,15 +2472,11 @@ def description(self):
that may be specified as:
- An instance of :class:`{module_str}.{class_str}`
- A dict of string/value properties that will be passed
to the {class_str} constructor
Supported dict properties:
{constructor_params_str}"""
to the {class_str} constructor"""
).format(
plotly_name=self.plotly_name,
class_str=self.data_class_str,
module_str=self.module_str,
constructor_params_str=self.data_docs,
)

return desc
Expand Down Expand Up @@ -2560,15 +2545,11 @@ def description(self):
{class_str} that may be specified as:
- A list or tuple of instances of {module_str}.{class_str}
- A list or tuple of dicts of string/value properties that
will be passed to the {class_str} constructor
Supported dict properties:
{constructor_params_str}"""
will be passed to the {class_str} constructor"""
).format(
plotly_name=self.plotly_name,
class_str=self.data_class_str,
module_str=self.module_str,
constructor_params_str=self.data_docs,
)

return desc
Expand Down
29 changes: 29 additions & 0 deletions packages/python/plotly/bin/get_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Calculate total size and total number of files of package."""

from pathlib import Path
import sys


def main():
"""Main driver."""
assert len(sys.argv) == 3, "Usage: get_size.py src_dir build_dir"
src_files, src_bytes = get_size(sys.argv[1])
build_files, build_bytes = get_size(sys.argv[2])
print(f"src,files,{src_files}")
print(f"src,bytes,{src_bytes}")
print(f"build,files,{build_files}")
print(f"build,bytes,{build_bytes}")


def get_size(root_dir):
"""Count files and size in bytes."""
num_files, num_bytes = 0, 0
for f in Path(root_dir).glob("**/*.*"):
if "__pycache__" not in str(f):
num_files += 1
num_bytes += f.stat().st_size
return num_files, num_bytes


if __name__ == "__main__":
main()
11 changes: 7 additions & 4 deletions packages/python/plotly/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def preprocess_schema(plotly_schema):
items["colorscale"] = items.pop("concentrationscales")


def perform_codegen():
def perform_codegen(reformat=True):
# Set root codegen output directory
# ---------------------------------
# (relative to project root)
Expand Down Expand Up @@ -337,9 +337,12 @@ def __getattr__(import_name):
f.write(graph_objects_init_source)

# ### Run black code formatter on output directories ###
subprocess.call(["black", "--target-version=py36", validators_pkgdir])
subprocess.call(["black", "--target-version=py36", graph_objs_pkgdir])
subprocess.call(["black", "--target-version=py36", graph_objects_path])
if reformat:
subprocess.call(["black", "--target-version=py36", validators_pkgdir])
subprocess.call(["black", "--target-version=py36", graph_objs_pkgdir])
subprocess.call(["black", "--target-version=py36", graph_objects_path])
else:
print("skipping reformatting")


if __name__ == "__main__":
Expand Down
5 changes: 1 addition & 4 deletions packages/python/plotly/codegen/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,7 @@ def __init__(self"""
name_prop = subtype_node.name_property
buffer.write(
f"""
_v = arg.pop('{name_prop}', None)
_v = {name_prop} if {name_prop} is not None else _v
if _v is not None:
self['{name_prop}'] = _v"""
self._init_provided('{name_prop}', arg, {name_prop})"""
)

# ### Literals ###
Expand Down
4 changes: 1 addition & 3 deletions packages/python/plotly/codegen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,7 @@ def get_validator_params(self):

if self.is_compound:
params["data_class_str"] = repr(self.name_datatype_class)
params["data_docs"] = (
'"""' + self.get_constructor_params_docstring() + '\n"""'
)
params["data_docs"] = '"""\n"""'
else:
assert self.is_simple

Expand Down
10 changes: 5 additions & 5 deletions packages/python/plotly/codegen/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def build_validator_py(node: PlotlyNode):
# ---------------
assert node.is_datatype

# Initialize source code buffer
# -----------------------------
# Initialize
buffer = StringIO()
import_alias = "_bv"

# Imports
# -------
# ### Import package of the validator's superclass ###
import_str = ".".join(node.name_base_validator.split(".")[:-1])
buffer.write(f"import {import_str }\n")
buffer.write(f"import {import_str} as {import_alias}\n")

# Build Validator
# ---------------
Expand All @@ -41,11 +41,11 @@ def build_validator_py(node: PlotlyNode):

# ### Write class definition ###
class_name = node.name_validator_class
superclass_name = node.name_base_validator
superclass_name = node.name_base_validator.split(".")[-1]
buffer.write(
f"""
class {class_name}({superclass_name}):
class {class_name}({import_alias}.{superclass_name}):
def __init__(self, plotly_name={params['plotly_name']},
parent_name={params['parent_name']},
**kwargs):"""
Expand Down
28 changes: 28 additions & 0 deletions packages/python/plotly/plotly/basedatatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ def _generator(i):
yield x


def _initialize_provided(obj, name, arg, provided):
"""
Initialize a property of this object using the provided value
or a value popped from the arguments dictionary. If neither
is available, do not set the property.
"""
val = arg.pop(name, None)
val = provided if provided is not None else val
if val is not None:
obj[name] = val


class BaseFigure(object):
"""
Base class for all figure types (both widget and non-widget)
Expand Down Expand Up @@ -834,6 +846,14 @@ def _ipython_display_(self):
else:
print(repr(self))

def _init_provided(self, name, arg, provided):
"""
Initialize a property of this object using the provided value
or a value popped from the arguments dictionary. If neither
is available, do not set the property.
"""
_initialize_provided(self, name, arg, provided)

def update(self, dict1=None, overwrite=False, **kwargs):
"""
Update the properties of the figure with a dict and/or with
Expand Down Expand Up @@ -4329,6 +4349,14 @@ def _get_validator(self, prop):

return ValidatorCache.get_validator(self._path_str, prop)

def _init_provided(self, name, arg, provided):
"""
Initialize a property of this object using the provided value
or a value popped from the arguments dictionary. If neither
is available, do not set the property.
"""
_initialize_provided(self, name, arg, provided)

@property
def _validators(self):
"""
Expand Down
139 changes: 3 additions & 136 deletions packages/python/plotly/plotly/graph_objects/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
from typing import TYPE_CHECKING

if sys.version_info < (3, 7) or TYPE_CHECKING:
from ..graph_objs import Waterfall
from ..graph_objs import Volume
Expand Down Expand Up @@ -131,148 +130,17 @@
from ..graph_objs import layout
else:
from _plotly_utils.importers import relative_import

__all__, __getattr__, __dir__ = relative_import(
__name__,
[
"..graph_objs.waterfall",
"..graph_objs.volume",
"..graph_objs.violin",
"..graph_objs.treemap",
"..graph_objs.table",
"..graph_objs.surface",
"..graph_objs.sunburst",
"..graph_objs.streamtube",
"..graph_objs.splom",
"..graph_objs.scatterternary",
"..graph_objs.scattersmith",
"..graph_objs.scatterpolargl",
"..graph_objs.scatterpolar",
"..graph_objs.scattermapbox",
"..graph_objs.scattermap",
"..graph_objs.scattergl",
"..graph_objs.scattergeo",
"..graph_objs.scattercarpet",
"..graph_objs.scatter3d",
"..graph_objs.scatter",
"..graph_objs.sankey",
"..graph_objs.pie",
"..graph_objs.parcoords",
"..graph_objs.parcats",
"..graph_objs.ohlc",
"..graph_objs.mesh3d",
"..graph_objs.isosurface",
"..graph_objs.indicator",
"..graph_objs.image",
"..graph_objs.icicle",
"..graph_objs.histogram2dcontour",
"..graph_objs.histogram2d",
"..graph_objs.histogram",
"..graph_objs.heatmap",
"..graph_objs.funnelarea",
"..graph_objs.funnel",
"..graph_objs.densitymapbox",
"..graph_objs.densitymap",
"..graph_objs.contourcarpet",
"..graph_objs.contour",
"..graph_objs.cone",
"..graph_objs.choroplethmapbox",
"..graph_objs.choroplethmap",
"..graph_objs.choropleth",
"..graph_objs.carpet",
"..graph_objs.candlestick",
"..graph_objs.box",
"..graph_objs.barpolar",
"..graph_objs.bar",
"..graph_objs.layout",
],
[
"..graph_objs.Waterfall",
"..graph_objs.Volume",
"..graph_objs.Violin",
"..graph_objs.Treemap",
"..graph_objs.Table",
"..graph_objs.Surface",
"..graph_objs.Sunburst",
"..graph_objs.Streamtube",
"..graph_objs.Splom",
"..graph_objs.Scatterternary",
"..graph_objs.Scattersmith",
"..graph_objs.Scatterpolargl",
"..graph_objs.Scatterpolar",
"..graph_objs.Scattermapbox",
"..graph_objs.Scattermap",
"..graph_objs.Scattergl",
"..graph_objs.Scattergeo",
"..graph_objs.Scattercarpet",
"..graph_objs.Scatter3d",
"..graph_objs.Scatter",
"..graph_objs.Sankey",
"..graph_objs.Pie",
"..graph_objs.Parcoords",
"..graph_objs.Parcats",
"..graph_objs.Ohlc",
"..graph_objs.Mesh3d",
"..graph_objs.Isosurface",
"..graph_objs.Indicator",
"..graph_objs.Image",
"..graph_objs.Icicle",
"..graph_objs.Histogram2dContour",
"..graph_objs.Histogram2d",
"..graph_objs.Histogram",
"..graph_objs.Heatmap",
"..graph_objs.Funnelarea",
"..graph_objs.Funnel",
"..graph_objs.Densitymapbox",
"..graph_objs.Densitymap",
"..graph_objs.Contourcarpet",
"..graph_objs.Contour",
"..graph_objs.Cone",
"..graph_objs.Choroplethmapbox",
"..graph_objs.Choroplethmap",
"..graph_objs.Choropleth",
"..graph_objs.Carpet",
"..graph_objs.Candlestick",
"..graph_objs.Box",
"..graph_objs.Barpolar",
"..graph_objs.Bar",
"..graph_objs.Layout",
"..graph_objs.Frame",
"..graph_objs.Figure",
"..graph_objs.Data",
"..graph_objs.Annotations",
"..graph_objs.Frames",
"..graph_objs.AngularAxis",
"..graph_objs.Annotation",
"..graph_objs.ColorBar",
"..graph_objs.Contours",
"..graph_objs.ErrorX",
"..graph_objs.ErrorY",
"..graph_objs.ErrorZ",
"..graph_objs.Font",
"..graph_objs.Legend",
"..graph_objs.Line",
"..graph_objs.Margin",
"..graph_objs.Marker",
"..graph_objs.RadialAxis",
"..graph_objs.Scene",
"..graph_objs.Stream",
"..graph_objs.XAxis",
"..graph_objs.YAxis",
"..graph_objs.ZAxis",
"..graph_objs.XBins",
"..graph_objs.YBins",
"..graph_objs.Trace",
"..graph_objs.Histogram2dcontour",
],
['..graph_objs.waterfall', '..graph_objs.volume', '..graph_objs.violin', '..graph_objs.treemap', '..graph_objs.table', '..graph_objs.surface', '..graph_objs.sunburst', '..graph_objs.streamtube', '..graph_objs.splom', '..graph_objs.scatterternary', '..graph_objs.scattersmith', '..graph_objs.scatterpolargl', '..graph_objs.scatterpolar', '..graph_objs.scattermapbox', '..graph_objs.scattermap', '..graph_objs.scattergl', '..graph_objs.scattergeo', '..graph_objs.scattercarpet', '..graph_objs.scatter3d', '..graph_objs.scatter', '..graph_objs.sankey', '..graph_objs.pie', '..graph_objs.parcoords', '..graph_objs.parcats', '..graph_objs.ohlc', '..graph_objs.mesh3d', '..graph_objs.isosurface', '..graph_objs.indicator', '..graph_objs.image', '..graph_objs.icicle', '..graph_objs.histogram2dcontour', '..graph_objs.histogram2d', '..graph_objs.histogram', '..graph_objs.heatmap', '..graph_objs.funnelarea', '..graph_objs.funnel', '..graph_objs.densitymapbox', '..graph_objs.densitymap', '..graph_objs.contourcarpet', '..graph_objs.contour', '..graph_objs.cone', '..graph_objs.choroplethmapbox', '..graph_objs.choroplethmap', '..graph_objs.choropleth', '..graph_objs.carpet', '..graph_objs.candlestick', '..graph_objs.box', '..graph_objs.barpolar', '..graph_objs.bar', '..graph_objs.layout'],
['..graph_objs.Waterfall', '..graph_objs.Volume', '..graph_objs.Violin', '..graph_objs.Treemap', '..graph_objs.Table', '..graph_objs.Surface', '..graph_objs.Sunburst', '..graph_objs.Streamtube', '..graph_objs.Splom', '..graph_objs.Scatterternary', '..graph_objs.Scattersmith', '..graph_objs.Scatterpolargl', '..graph_objs.Scatterpolar', '..graph_objs.Scattermapbox', '..graph_objs.Scattermap', '..graph_objs.Scattergl', '..graph_objs.Scattergeo', '..graph_objs.Scattercarpet', '..graph_objs.Scatter3d', '..graph_objs.Scatter', '..graph_objs.Sankey', '..graph_objs.Pie', '..graph_objs.Parcoords', '..graph_objs.Parcats', '..graph_objs.Ohlc', '..graph_objs.Mesh3d', '..graph_objs.Isosurface', '..graph_objs.Indicator', '..graph_objs.Image', '..graph_objs.Icicle', '..graph_objs.Histogram2dContour', '..graph_objs.Histogram2d', '..graph_objs.Histogram', '..graph_objs.Heatmap', '..graph_objs.Funnelarea', '..graph_objs.Funnel', '..graph_objs.Densitymapbox', '..graph_objs.Densitymap', '..graph_objs.Contourcarpet', '..graph_objs.Contour', '..graph_objs.Cone', '..graph_objs.Choroplethmapbox', '..graph_objs.Choroplethmap', '..graph_objs.Choropleth', '..graph_objs.Carpet', '..graph_objs.Candlestick', '..graph_objs.Box', '..graph_objs.Barpolar', '..graph_objs.Bar', '..graph_objs.Layout', '..graph_objs.Frame', '..graph_objs.Figure', '..graph_objs.Data', '..graph_objs.Annotations', '..graph_objs.Frames', '..graph_objs.AngularAxis', '..graph_objs.Annotation', '..graph_objs.ColorBar', '..graph_objs.Contours', '..graph_objs.ErrorX', '..graph_objs.ErrorY', '..graph_objs.ErrorZ', '..graph_objs.Font', '..graph_objs.Legend', '..graph_objs.Line', '..graph_objs.Margin', '..graph_objs.Marker', '..graph_objs.RadialAxis', '..graph_objs.Scene', '..graph_objs.Stream', '..graph_objs.XAxis', '..graph_objs.YAxis', '..graph_objs.ZAxis', '..graph_objs.XBins', '..graph_objs.YBins', '..graph_objs.Trace', '..graph_objs.Histogram2dcontour']
)


if sys.version_info < (3, 7) or TYPE_CHECKING:
try:
import ipywidgets as _ipywidgets
from packaging.version import Version as _Version

if _Version(_ipywidgets.__version__) >= _Version("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
else:
Expand All @@ -282,7 +150,6 @@
else:
__all__.append("FigureWidget")
orig_getattr = __getattr__

def __getattr__(import_name):
if import_name == "FigureWidget":
try:
Expand All @@ -297,7 +164,7 @@ def __getattr__(import_name):
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget

return FigureWidget

return orig_getattr(import_name)

Loading

0 comments on commit 2b3d47b

Please sign in to comment.