-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Repr html #2421
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
Repr html #2421
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b97041
added _repr_html_ to BaseFigure
emmanuelle 33bc2bd
added test for _repr_html_
emmanuelle f5d6758
black
emmanuelle 16c63fd
use mimebundle for _repr_html_ and introduce new renderer for sphinx-…
emmanuelle 55dd832
added to_mimebundle to remove postscript
emmanuelle 5841808
added tests
emmanuelle fa703f9
more tests
emmanuelle 44abde9
removed orca renderers from test
emmanuelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,3 +287,90 @@ def test_reject_invalid_renderer(renderer): | |
) | ||
def test_accept_valid_renderer(renderer): | ||
pio.renderers.default = renderer | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"renderer", | ||
plotly_mimetype_renderers | ||
+ ["notebook", "notebook_connected", "browser", "notebook+plotly_mimetype"], | ||
) | ||
def test_repr_html(renderer): | ||
pio.renderers.default = renderer | ||
fig = go.Figure() | ||
fig.update_layout(template=None) | ||
str_html = fig._repr_html_() | ||
bundle = fig._repr_mimebundle_() | ||
# id number of figure | ||
id_html = str_html.split('document.getElementById("')[1].split('")')[0] | ||
id_pattern = "cd462b94-79ce-42a2-887f-2650a761a144" | ||
template = ( | ||
'<div>\n \n <script type="text/javascript">' | ||
"window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n " | ||
'<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> \n ' | ||
'<div id="cd462b94-79ce-42a2-887f-2650a761a144" class="plotly-graph-div" ' | ||
'style="height:100%; width:100%;"></div>\n <script type="text/javascript">' | ||
"\n \n window.PLOTLYENV=window.PLOTLYENV || {};" | ||
'\n \n if (document.getElementById("cd462b94-79ce-42a2-887f-2650a761a144"))' | ||
" {\n Plotly.newPlot(\n 'cd462b94-79ce-42a2-887f-2650a761a144'," | ||
'\n [],\n {"template": {}},' | ||
'\n {"responsive": true}\n )\n };' | ||
"\n \n </script>\n </div>" | ||
) | ||
if "text/html" in bundle: | ||
str_bundle = bundle["text/html"] | ||
id_bundle = str_bundle.split('document.getElementById("')[1].split('")')[0] | ||
assert str_html.replace(id_html, "") == str_bundle.replace(id_bundle, "") | ||
else: | ||
assert str_html.replace(id_html, "") == template.replace(id_pattern, "") | ||
|
||
|
||
all_renderers_without_orca = [ | ||
"plotly_mimetype", | ||
"jupyterlab", | ||
"nteract", | ||
"vscode", | ||
"notebook", | ||
"notebook_connected", | ||
"kaggle", | ||
"azure", | ||
"colab", | ||
"cocalc", | ||
"databricks", | ||
"json", | ||
"browser", | ||
"firefox", | ||
"chrome", | ||
"chromium", | ||
"iframe", | ||
"iframe_connected", | ||
"sphinx_gallery", | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("renderer_str", all_renderers_without_orca) | ||
def test_repr_mimebundle(renderer_str): | ||
pio.renderers.default = renderer_str | ||
fig = go.Figure() | ||
fig.update_layout(template=None) | ||
bundle = fig._repr_mimebundle_() | ||
renderer = pio.renderers[renderer_str] | ||
from plotly.io._renderers import MimetypeRenderer | ||
|
||
if isinstance(renderer, MimetypeRenderer): | ||
ref_bundle = renderer.to_mimebundle(fig.to_dict()) | ||
for key in bundle: | ||
if "getElementById" in bundle[key]: | ||
id1 = bundle[key].split('document.getElementById("')[1].split('")')[0] | ||
id2 = ( | ||
ref_bundle[key].split('document.getElementById("')[1].split('")')[0] | ||
) | ||
assert bundle[key].replace(id1, "") == ref_bundle[key].replace(id2, "") | ||
else: | ||
assert bundle == {} | ||
|
||
|
||
def test_repr_mimebundle_mixed_renderer(fig1): | ||
pio.renderers.default = "notebook+plotly_mimetype" | ||
assert set(fig1._repr_mimebundle_().keys()) == set( | ||
{"application/vnd.plotly.v1+json", "text/html"} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, thanks for the tests! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I like the fallback here.