Skip to content

Add 'plotly_noembed' cargo feature #231

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

Merged
merged 2 commits into from
Sep 13, 2024

Conversation

sharkdp
Copy link

@sharkdp sharkdp commented Aug 29, 2024

This adds a new plotly_noembed feature that can be used to reduce the sizes of binaries by not embedding plotly.min.js.

Since askama can not handle compile-time flags, we had to duplicate the plot.html and static_plot.html templates, where the new *_noembed.html version only contains the CDN-variant.

The use_local_plotly option has been feature-gated such that it is not available when using the new feature.

I verified that this works by building an application that depends on plotly.rs (including optimization and LTO in all cases):

no plotly:   4.96 MiB (5,201,920 bytes)
master:      8.96 MiB (9,400,320 bytes)
this branch: 5.44 MiB (5,701,632 bytes)

The difference between this branch and master is 3,698,688 bytes, which is very close to the size of plotly.min.js (3,686,400 bytes). Just as expected.

closes #176

@sharkdp sharkdp force-pushed the add-no_embed-feature branch from 4fa6722 to 6fff5de Compare August 29, 2024 20:17
Comment on lines 41 to 50
#[cfg(feature = "plotly_noembed")]
#[derive(Template)]
#[template(path = "static_plot_noembed.html", escape = "none")]
#[cfg(not(target_family = "wasm"))]
struct StaticPlotTemplate<'a> {
plot: &'a Plot,
format: ImageFormat,
width: usize,
height: usize,
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a way to do this without having to duplicate the whole struct.

@andrei-ng
Copy link
Collaborator

andrei-ng commented Sep 2, 2024

@sharkdp , thank you for submitting the PR!
I need a bit more time to review it.

This adds a new `plotly_noembed` feature that can be used to reduce the
sizes of binaries by not embedding `plotly.min.js`.

Since askama can not handle compile-time flags, we had to duplicate the
`plot.html` and `static_plot.html` templates, where the new
`*_noembed.html` version only contains the CDN-variant.

The `use_local_plotly` option has been feature-gated such that it is
not available when using the new feature.

I verified that this works by building an application that depends on
plotly.rs (including optimization and LTO in all cases):

    no plotly:   4.96 MiB (5,201,920 bytes)
    master:      8.96 MiB (9,400,320 bytes)
    this branch: 5.44 MiB (5,701,632 bytes)

The difference between this branch and master is 3,698,688 bytes, which
is very close to the size of `plotly.min.js` (3,686,400 bytes). Just as
expected.
@andrei-ng
Copy link
Collaborator

Hi @sharkdp,

I had a chance to look into the PR today.

Indeed, duplicating the template files is annoying. I have looked into what alternatives we have. TBH, I thought about it and I think the embedding of the plotly.min.js should be an opt-in feature and not an opt-out since it blows up the library size. Although I might risk upsetting some users, I am going to propose that we make it an opt-in .

Also using the if/else in the template as it was originally implemented causes this issue of not being able to use the Rust compile time configuration as you pointed out in the #176 . We might be able to bypass that now since thanks to @GuillaumeGomez , we switched to rinja in #277, which comes with some really cool features and integrations of templates with Rust code.

I propose for the moment a simpler approach, where we always use CDN and we bake in directly the local version only with the opt-in flag. I am going to push a commit on top of yours with these changes. Would be interested to hear your opinion.

@sharkdp
Copy link
Author

sharkdp commented Sep 13, 2024

I think that is a good idea. CDN as default sounds like a reasonable default to me, given that the local plotly version is so large.

Feel free to amend this PR as you see fit. Thank you

@andrei-ng
Copy link
Collaborator

I think that is a good idea. CDN as default sounds like a reasonable default to me, given that the local plotly version is so large.

Feel free to amend this PR as you see fit. Thank you

Great! Then I will merge with the changes I made on top of yours. If you spot issues when you use the upcoming version, let me know.

@andrei-ng andrei-ng self-assigned this Sep 13, 2024
Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
@andrei-ng andrei-ng merged commit cfa7bc3 into plotly:main Sep 13, 2024
18 of 19 checks passed
@sharkdp sharkdp deleted the add-no_embed-feature branch September 13, 2024 18:20
@sharkdp
Copy link
Author

sharkdp commented Sep 13, 2024

Awesome. I just tested it and it still works as expected. I'll integrate it into Numbat as soon as the release is out. Thank you 👍

@andrei-ng
Copy link
Collaborator

I'll make a new release tomorrow.

@andrei-ng
Copy link
Collaborator

andrei-ng commented Sep 16, 2024

Awesome. I just tested it and it still works as expected. I'll integrate it into Numbat as soon as the release is out. Thank you 👍

@sharkdp FYI, released 0.10.0 version just now.

@sharkdp
Copy link
Author

sharkdp commented Sep 24, 2024

Thank you! It's now integrated into Numbat 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

plotly.min.js bloats binary size
2 participants