Commit 887b5fa1 authored by Romain Casati's avatar Romain Casati

Fix Matplotlib element getter.

parent 6ca9812e
Pipeline #398969 passed with stage
in 3 minutes and 6 seconds
......@@ -13,6 +13,10 @@ def hack_matplotlib():
"""
from matplotlib.backends.wasm_backend import FigureCanvasWasm as wasm_backend
# already hacked?
if hasattr(wasm_backend, "_original_show"):
return
# hacking root node creation
def create_root_element(self):
self.root = document.createElement("div")
......@@ -20,9 +24,19 @@ def hack_matplotlib():
wasm_backend.create_root_element = create_root_element
# hacking element getter carefuly addressing the case where the
# root node is not yet added to the DOM
def get_element(self, name):
if name == "" or not hasattr(self, "root"):
root = document
else:
root = self.root
return root.querySelector('#' + self._id + name)
wasm_backend.get_element = get_element
# hacking show
if not hasattr(wasm_backend, "_original_show"):
wasm_backend._original_show = wasm_backend.show
wasm_backend._original_show = wasm_backend.show
def show(self):
res = self._original_show()
......
<div style="margin: 0 auto; text-align: center;width: 640.0px" id="matplotlib_"><div id="matplotlib_top" style="font-weight: bold; text-align: center">Figure 1</div><div style="position: relative"><canvas id="matplotlib_nvas" width="640" height="480" style="left: 0; top: 0; z-index: 0; outline: 0;width: 640.0px; height: 480.0px"></canvas><canvas id="matplotlib_rubberband" width="640" height="480" style="position: absolute; left: 0; top: 0; z-index: 0; outline: 0; width: 640.0px; height: 480.0px" tabindex="0"></canvas></div><div><span><button class="fa fa-home matplotlib-toolbar-button"></button><button class="fa fa-arrow-left matplotlib-toolbar-button"></button><button class="fa fa-arrow-right matplotlib-toolbar-button"></button><span style="min-width: 16px;">&nbsp;</span><button class="fa fa-arrows matplotlib-toolbar-button"></button><button class="fa fa-search-plus matplotlib-toolbar-button"></button><span style="min-width: 16px;">&nbsp;</span><button class="fa matplotlib-toolbar-button" id="text">pdf</button><button class="fa matplotlib-toolbar-button" id="text">png</button><button class="fa matplotlib-toolbar-button" id="text">svg</button></span><div id="matplotlib_message" style="min-height: 1.5em"></div></div></div>
\ No newline at end of file
<div style="margin: 0 auto; text-align: center;width: 640.0px" id="matplotlib_"><div id="matplotlib_top" style="font-weight: bold; text-align: center">Figure 1</div><div style="position: relative"><canvas id="matplotlib_nvas" width="640" height="480" style="left: 0; top: 0; z-index: 0; outline: 0;width: 640.0px; height: 480.0px"></canvas><canvas id="matplotlib_rubberband" width="640" height="480" style="position: absolute; left: 0px; top: 0px; z-index: 0; outline: 0px; width: 640px; height: 480px; cursor: default;" tabindex="0"></canvas></div><div><span><button class="fa fa-home matplotlib-toolbar-button"></button><button class="fa fa-arrow-left matplotlib-toolbar-button"></button><button class="fa fa-arrow-right matplotlib-toolbar-button"></button><span style="min-width: 16px;">&nbsp;</span><button class="fa fa-arrows matplotlib-toolbar-button"></button><button class="fa fa-search-plus matplotlib-toolbar-button"></button><span style="min-width: 16px;">&nbsp;</span><button class="fa matplotlib-toolbar-button" id="text">pdf</button><button class="fa matplotlib-toolbar-button" id="text">png</button><button class="fa matplotlib-toolbar-button" id="text">svg</button></span><div id="matplotlib_message" style="min-height: 1.5em"></div></div></div>
\ No newline at end of file
<div style="margin: 0 auto; text-align: center;width: 640.0px" id="matplotlib_"><div id="matplotlib_top" style="font-weight: bold; text-align: center">Figure 1</div><div style="position: relative"><canvas id="matplotlib_nvas" width="640" height="480" style="left: 0; top: 0; z-index: 0; outline: 0;width: 640.0px; height: 480.0px"></canvas><canvas id="matplotlib_rubberband" width="640" height="480" style="position: absolute; left: 0px; top: 0px; z-index: 0; outline: currentcolor none 0px; width: 640px; height: 480px; cursor: default;" tabindex="0"></canvas></div><div><span><button class="fa fa-home matplotlib-toolbar-button"></button><button class="fa fa-arrow-left matplotlib-toolbar-button"></button><button class="fa fa-arrow-right matplotlib-toolbar-button"></button><span style="min-width: 16px;">&nbsp;</span><button class="fa fa-arrows matplotlib-toolbar-button"></button><button class="fa fa-search-plus matplotlib-toolbar-button"></button><span style="min-width: 16px;">&nbsp;</span><button class="fa matplotlib-toolbar-button" id="text">pdf</button><button class="fa matplotlib-toolbar-button" id="text">png</button><button class="fa matplotlib-toolbar-button" id="text">svg</button></span><div id="matplotlib_message" style="min-height: 1.5em"></div></div></div>
\ No newline at end of file
from pathlib import Path
import re
import base64
_test_data = Path(__file__).parent / "data"
......@@ -36,6 +37,7 @@ def test_PIL(selenium):
def test_matplotlib(selenium):
browser = selenium.driver.capabilities['browserName']
selenium.run_basthon("""
import matplotlib.pyplot as plt
......@@ -47,12 +49,31 @@ def test_matplotlib(selenium):
# elem = result['content']
# because of selenium's StaleElementReferenceException
# bypassing it via JS
html = selenium.run_js("return window._basthon_eval_data.display.content.outerHTML")
html = selenium.run_js("return window._basthon_eval_data.display.content.outerHTML;")
html = re.sub('matplotlib_[0-9a-f]+', 'matplotlib_', html)
with open(_test_data / 'matplotlib.html') as f:
with open(_test_data / f"matplotlib_{browser}.html") as f:
target = f.read()
assert html == target
png = selenium.run_js("return window._basthon_eval_data.display.content.getElementsByTagName('canvas')[0].toDataURL('image/png');")
png = base64.b64decode(png[len('data:image/png;base64,'):])
with open(_test_data / f"matplotlib_{browser}.png", 'rb') as f:
target = f.read()
assert png == target
# case where plot and show are called separately
selenium.run_basthon("""
plt.figure()
plt.plot([0, 1], [0, 1])
""", return_data=False)
selenium.run_basthon("plt.show()", return_data=False)
png = selenium.run_js("return window._basthon_eval_data.display.content.getElementsByTagName('canvas')[0].toDataURL('image/png');")
png = base64.b64decode(png[len('data:image/png;base64,'):])
with open(_test_data / f"matplotlib_{browser}.png", 'rb') as f:
target = f.read()
assert png == target
def test_folium(selenium):
result = selenium.run_basthon("""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment