Skip to content

feat: make to_html deterministic #3487

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 5 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).


## UNRELEASED

### Fixed
Expand All @@ -11,7 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- `text_auto` argument to `px.bar`, `px.histogram`, `px.density_heatmap`, `px.imshow` [#3518](https://github.com/plotly/plotly.py/issues/3518)
- Deprecated `ff.create_annotated_heatmap`, `ff.create_county_choropleth`, `ff.create_gantt` [#3518](https://github.com/plotly/plotly.py/issues/3518)

- `div_id` argument to `pio.to_html` to optionally make its IDs deterministic [#3487](https://github.com/plotly/plotly.py/issues/3487)

### Updated
- Updated Plotly.js to from version 2.6.3 to version 2.8.1. See the [plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#280----2021-12-10) for more information. Notable changes include:
- Horizontal color bars
Expand Down
5 changes: 4 additions & 1 deletion packages/python/plotly/plotly/io/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def to_html(
default_width="100%",
default_height="100%",
validate=True,
div_id=None,
):
"""
Convert a figure to an HTML string representation.
Expand Down Expand Up @@ -135,7 +136,7 @@ def to_html(
fig_dict = validate_coerce_fig_to_dict(fig, validate)

# ## Generate div id ##
plotdivid = str(uuid.uuid4())
plotdivid = div_id or str(uuid.uuid4())

# ## Serialize figure ##
jdata = to_json_plotly(fig_dict.get("data", []))
Expand Down Expand Up @@ -391,6 +392,7 @@ def write_html(
default_width="100%",
default_height="100%",
auto_open=False,
div_id=None,
):
"""
Write a figure to an HTML file representation
Expand Down Expand Up @@ -512,6 +514,7 @@ def write_html(
default_width=default_width,
default_height=default_height,
validate=validate,
div_id=div_id,
)

# Check if file is a string
Expand Down
7 changes: 7 additions & 0 deletions packages/python/plotly/plotly/tests/test_io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ def fig1(request):

def test_versioned_cdn_included(fig1):
assert plotly_cdn_url() in pio.to_html(fig1, include_plotlyjs="cdn")


def test_html_deterministic(fig1):
div_id = "plotly-root"
assert pio.to_html(fig1, include_plotlyjs="cdn", div_id=div_id) == pio.to_html(
fig1, include_plotlyjs="cdn", div_id=div_id
)