diff --git a/CHANGELOG.md b/CHANGELOG.md index 68ba931d02f..354fe258504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/packages/python/plotly/plotly/io/_html.py b/packages/python/plotly/plotly/io/_html.py index ac7a642b755..1e2fe2db6d6 100644 --- a/packages/python/plotly/plotly/io/_html.py +++ b/packages/python/plotly/plotly/io/_html.py @@ -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. @@ -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", [])) @@ -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 @@ -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 diff --git a/packages/python/plotly/plotly/tests/test_io/test_html.py b/packages/python/plotly/plotly/tests/test_io/test_html.py index e2b2b976a7e..a056fd8f872 100644 --- a/packages/python/plotly/plotly/tests/test_io/test_html.py +++ b/packages/python/plotly/plotly/tests/test_io/test_html.py @@ -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 + )