Skip to content

write_html fails with latest plotly.min.js version 2.17.1 #4019

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

Closed
johannesloibl opened this issue Jan 16, 2023 · 5 comments
Closed

write_html fails with latest plotly.min.js version 2.17.1 #4019

johannesloibl opened this issue Jan 16, 2023 · 5 comments

Comments

@johannesloibl
Copy link
Contributor

The latest plotly.min.js version 2.17.1 contains special characters which are not supported by the Windows cp1252 default codec.
E.g. \u25c4 (https://www.fileformat.info/info/unicode/char/25c4/index.htm).

Reproducible example (Win 10, 64bit, plotly 5.12):

import plotly
print(plotly.__version__)

import plotly.express as px

fig =px.scatter(x=range(10), y=range(10))
fig.write_html("file.html", include_plotlyjs="directory")

This produces following error:

Traceback (most recent call last):
  File "C:\Users\...\scratches\scratch_2.py", line 7, in <module>
    fig.write_html("file.html", include_plotlyjs="directory")
  File "C:\git\...\.tox\codecov\lib\site-packages\plotly\basedatatypes.py", line 3700, in write_html
    return pio.write_html(self, *args, **kwargs)
  File "C:\git\...\.tox\codecov\lib\site-packages\plotly\io\_html.py", line 545, in write_html
    bundle_path.write_text(get_plotlyjs())
  File "C:\Python39\lib\pathlib.py", line 1286, in write_text
    return f.write(data)
  File "C:\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u25c4' in position 276182: character maps to <undefined>
@alexcjohnson
Copy link
Collaborator

alexcjohnson commented Jan 17, 2023

Thanks @johannesloibl! This must be an artifact of the switch from browserify to webpack.

@archmoj let's tweak the webpack config so that its output is ASCII. See https://gist.github.com/hkhc/e5961e4df1cb8f5b0d7a8e9c6730ebd0 (edit: see comment below)

@alexcjohnson
Copy link
Collaborator

That said, I wonder if #4022 does this one better by fixing even more problems?

@rxxg
Copy link
Contributor

rxxg commented Jan 17, 2023

Author of #4022 here, since HTML pages generated by plotly.py have

<html>
<head><meta charset="utf-8" /></head>
<body>

by default (see _html.py), for me it makes sense to support the full palette of Unicode by exporting files in utf-8 (as claimed).

@alexcjohnson
Copy link
Collaborator

Thanks @rxxg - looking at this in more detail, seems like #4022 and #4021 each address part of the issue: #4021 handles the new part about the plotly.js change to including unicode in the bundle, while #4022 is about other unicode content in the plot. So we need both of these.

And while it could be considered a breaking change that the plotly.js bundle is no longer pure ASCII, we never made that an explicit contract, it was an accidental requirement of the usage here. UTF-8 is more efficient and the generally accepted as the standard at this point so on second thought I'd prefer we fix these cases rather than enforce an ASCII-only bundle.

@nicolaskruchten
Copy link
Contributor

Thanks folks! I've merged both the PRs and I'm just waiting on another fix related to webpack to release 5.12.1 ... hopefully this week.

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

No branches or pull requests

4 participants