Skip to content
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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gvwilson
Copy link
Contributor

@gvwilson gvwilson commented Jan 22, 2025

feat: modify code generation to reduce bundle size

  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.

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

  3. Assign an empty string to the data_docs field of generated
    validators. (This has a major impact because those docs are
    duplicated many times.)

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

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

  6. Replace super(Parent, self) with super() in generated code.

  7. 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.

  8. 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 (bytes): 47086264
New size (bytes): 37142340
Change: -27%

@gvwilson gvwilson added P2 considered for next cycle feature something new labels Jan 22, 2025
@gvwilson gvwilson self-assigned this Jan 22, 2025
@gvwilson gvwilson force-pushed the codegen2 branch 2 times, most recently from 0c7ea5e to 2b3d47b Compare January 23, 2025 17:56
gvwilson added a commit that referenced this pull request Jan 24, 2025
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.
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.  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: 50365842 bytes
Current size of plotly/**/*.py:  38256842 bytes
Change: -26%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P2 considered for next cycle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant