-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: modifying code generation to reduce bundle size #4978
base: main
Are you sure you want to change the base?
Conversation
0c7ea5e
to
2b3d47b
Compare
Removing the check for Python < 3.7 using `sys.version_info` and as a backup checking `typing.TYPE_CHECKING`; this saves us a little space and also cleans up the code. Proposing this as an enhancement beyond what's in the `codegen2` branch / PR #4978.
000a5e9
to
dbbbaa8
Compare
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. Assign an empty string to the `data_docs` field of generated validators. (This has a major impact because those docs are duplicated many times.) 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. Replace `super(Parent, self)` with `super()` in generated code. 1. Drop use of sys.version_info and TYPE_CHECKING. Removed the check for Python < 3.7 using `sys.version_info` and as a backup checking `typing.TYPE_CHECKING`; this saves a little space and also cleans up the code. 1. Remove mention of Chart Studio and explicit enumeration of system font names from plotly.js / plot-schema.json so that this text isn't copied dozens of times into the plotly.py bundle. 1. Introduce `_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: 42283582 bytes Current size of plotly/**/*.py: 31931739 bytes Change: -25%
1. Modify `commands.py` to run code generation. 1. Remove comments from generated code. 1. Replaced named arguments in constructors with positional arguments. 1. Regenerate all code. Notes: The generated code is reformatted once again: this slightly increases size but makes it more readable. There is one flaky test: `tests/test_plotly_utils/validators/test_colorscale_validator.py::test_acceptance_named[Inferno_r]` It fails when the entire test suite is run but does *not* fail when only `test_colorscale_validator.py` is run (using Python 3.11.9). | branch | format | bytes | %age | | -------- | ------- | -------- | ---- | | master | .whl | 14803768 | | | codegen2 | .whl | 12215667 | -18% | | master | .tar.gz | 8100014 | | | codegen2 | .tar.gz | 6114458 | -24% |
@@ -267,36 +267,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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexcjohnson commented on this in a Slack thread saying "I would have thought we’d still want the TYPE_CHECKING
case (just not the sys.version_info < (3, 7)
case)?" Our code doesn't make mypy happy right now, and I suspect that fixing that will involve (a) adding type annotations (started in #5008) and (b) re-thinking and updating code like this. My thought was therefore to take this out for now and fix it properly as part of type annotation work, but if @emilykl or @marthacryan thinks we should keep the if TYPE_CHECKING
code, easy to add it back into this PR.
feat: modify code generation to reduce bundle size
Assign an empty string to the
data_docs
field of generatedvalidators. (This has a major impact because those docs are
duplicated many times.)
Alias name of base validator during import in
codegen/validators.py
.Remove the long list of CSS colors from help strings for color
properties.
Replace
super(Parent, self)
withsuper()
in generated code.Modify
commands.py
to run code generation.Remove comments from generated code.
Replaced named arguments in constructors with positional arguments.
Drop use of sys.version_info and TYPE_CHECKING. Removed the check
for Python < 3.7 using
sys.version_info
and as a backup checkingtyping.TYPE_CHECKING
; this saves a little space and also cleansup the code.
Introduce
_init_provided()
forBaseFigure
andBasePlotlyType
that calls a helper function
_initialize_provided()
to replacerepetitions of:
Used in #5008.