Skip to content

Commit e735ba8

Browse files
Merge pull request #3487 from Skn0tt/3449-deterministic-html
feat: make `to_html` deterministic
2 parents 35cbe11 + 832a98a commit e735ba8

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
56
## UNRELEASED
67

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

1213
- `text_auto` argument to `px.bar`, `px.histogram`, `px.density_heatmap`, `px.imshow` [#3518](https://github.com/plotly/plotly.py/issues/3518)
1314
- Deprecated `ff.create_annotated_heatmap`, `ff.create_county_choropleth`, `ff.create_gantt` [#3518](https://github.com/plotly/plotly.py/issues/3518)
14-
15+
- `div_id` argument to `pio.to_html` to optionally make its IDs deterministic [#3487](https://github.com/plotly/plotly.py/issues/3487)
16+
1517
### Updated
1618
- 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:
1719
- Horizontal color bars

packages/python/plotly/plotly/io/_html.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def to_html(
3838
default_width="100%",
3939
default_height="100%",
4040
validate=True,
41+
div_id=None,
4142
):
4243
"""
4344
Convert a figure to an HTML string representation.
@@ -135,7 +136,7 @@ def to_html(
135136
fig_dict = validate_coerce_fig_to_dict(fig, validate)
136137

137138
# ## Generate div id ##
138-
plotdivid = str(uuid.uuid4())
139+
plotdivid = div_id or str(uuid.uuid4())
139140

140141
# ## Serialize figure ##
141142
jdata = to_json_plotly(fig_dict.get("data", []))
@@ -391,6 +392,7 @@ def write_html(
391392
default_width="100%",
392393
default_height="100%",
393394
auto_open=False,
395+
div_id=None,
394396
):
395397
"""
396398
Write a figure to an HTML file representation
@@ -512,6 +514,7 @@ def write_html(
512514
default_width=default_width,
513515
default_height=default_height,
514516
validate=validate,
517+
div_id=div_id,
515518
)
516519

517520
# Check if file is a string

packages/python/plotly/plotly/tests/test_io/test_html.py

+7
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ def fig1(request):
3838

3939
def test_versioned_cdn_included(fig1):
4040
assert plotly_cdn_url() in pio.to_html(fig1, include_plotlyjs="cdn")
41+
42+
43+
def test_html_deterministic(fig1):
44+
div_id = "plotly-root"
45+
assert pio.to_html(fig1, include_plotlyjs="cdn", div_id=div_id) == pio.to_html(
46+
fig1, include_plotlyjs="cdn", div_id=div_id
47+
)

0 commit comments

Comments
 (0)