Skip to content

feat: modifying code generation to reduce bundle size #4978

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 11 commits into from
Apr 17, 2025
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ jobs:
command: |
python -m venv venv
. venv/bin/activate
pip install black==22.3.0
pip install black==25.1.0
- run:
name: Check formatting with black
command: |
Expand Down
27 changes: 4 additions & 23 deletions _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: see https://plotly.com/python/css-colors/ for a list""".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
1 change: 1 addition & 0 deletions _plotly_utils/colors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
Be careful! If you have a lot of unique numbers in your color column you will
end up with a colormap that is massive and may slow down graphing performance.
"""

import decimal
from numbers import Number

Expand Down
62 changes: 30 additions & 32 deletions codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
get_data_validator_instance,
)

# Target Python version for code formatting with Black.
# Must be one of the values listed in pyproject.toml.
BLACK_TARGET_VERSIONS = "py38 py39 py310 py311 py312"


# Import notes
# ------------
Expand Down Expand Up @@ -85,7 +89,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 @@ -267,36 +271,24 @@ def perform_codegen():
root_datatype_imports.append(f"._deprecations.{dep_clas}")

optional_figure_widget_import = f"""
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:
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget
else:
__all__.append("FigureWidget")
orig_getattr = __getattr__
def __getattr__(import_name):
if import_name == "FigureWidget":
try:
import ipywidgets
from packaging.version import Version

if Version(ipywidgets.__version__) >= Version("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget

return FigureWidget
else:
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget
__all__.append("FigureWidget")
orig_getattr = __getattr__
def __getattr__(import_name):
if import_name == "FigureWidget":
try:
import ipywidgets
from packaging.version import Version

if Version(ipywidgets.__version__) >= Version("7.0.0"):
from ..graph_objs._figurewidget import FigureWidget
return FigureWidget
else:
raise ImportError()
except Exception:
from ..missing_anywidget import FigureWidget
return FigureWidget

return orig_getattr(import_name)
return orig_getattr(import_name)
"""
# ### __all__ ###
for path_parts, class_names in alls.items():
Expand Down Expand Up @@ -337,9 +329,15 @@ 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:
target_version = [
f"--target-version={v}" for v in BLACK_TARGET_VERSIONS.split()
]
subprocess.call(["black", *target_version, validators_pkgdir])
subprocess.call(["black", *target_version, graph_objs_pkgdir])
subprocess.call(["black", *target_version, graph_objects_path])
else:
print("skipping reformatting")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion codegen/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, *args, **kwargs):
{depr_msg}
\"\"\"
warnings.warn(\"\"\"{depr_msg}\"\"\", DeprecationWarning)
super({class_name}, self).__init__(*args, **kwargs)\n\n\n"""
super().__init__(*args, **kwargs)\n\n\n"""
)

# Return source string
Expand Down
Loading