Skip to content

Commit

Permalink
Merge pull request #3487 from Skn0tt/3449-deterministic-html
Browse files Browse the repository at this point in the history
feat: make `to_html` deterministic
  • Loading branch information
nicolaskruchten authored Dec 20, 2021
2 parents 35cbe11 + 832a98a commit e735ba8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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
)

0 comments on commit e735ba8

Please sign in to comment.