################################################################################ import base64 import io import os import re import html import numpy as np import pytest from PIL import Image from .support import ( PyScriptTest, filter_inner_text, filter_page_content, wait_for_render, skip_worker, only_main, ) DISPLAY_OUTPUT_ID_PATTERN = r'script-py[id^="py-"]' class TestDisplay(PyScriptTest): def test_simple_display(self): self.pyscript_run( """ """, timeout=20000, ) node_list = self.page.query_selector_all(DISPLAY_OUTPUT_ID_PATTERN) pattern = r"
hello 2
""" ) inner_text = self.page.inner_text("body") lines = inner_text.splitlines() lines = [line for line in filter_page_content(lines)] # remove empty lines assert lines == ["hello 1", "hello 2", "hello 3"] def test_target_parameter(self): self.pyscript_run( """ """ ) mydiv = self.page.locator("#mydiv") assert mydiv.inner_text() == "hello world" @skip_worker("NEXT: display(target=...) does not work") def test_tag_target_attribute(self): self.pyscript_run( """ """ ) hello = self.page.locator("#hello") assert hello.inner_text() == "hello\nworld" goodbye = self.page.locator("#goodbye") assert goodbye.inner_text() == "goodbye world" @skip_worker("NEXT: display target does not work properly") def test_target_script_py(self): self.pyscript_run( """hello in between 1 and 2
""" ) inner_text = self.page.inner_text("body") lines = inner_text.splitlines() lines = [line for line in filter_page_content(lines)] # remove empty lines assert lines == ["hello 1", "hello in between 1 and 2", "hello 2", "hello 3"] def test_multiple_display_calls_same_tag(self): self.pyscript_run( """ """ ) tag = self.page.locator("script-py") lines = tag.inner_text().splitlines() assert lines == ["hello", "world"] @only_main # with workers, two tags are two separate interpreters def test_implicit_target_from_a_different_tag(self): self.pyscript_run( """ """ ) elems = self.page.locator("script-py") py0 = elems.nth(0) py1 = elems.nth(1) assert py0.inner_text() == "" assert py1.inner_text() == "hello" @skip_worker("NEXT: py-click doesn't work") def test_no_explicit_target(self): self.pyscript_run( """ """ ) self.page.locator("button").click() text = self.page.locator("script-py").text_content() assert "hello world" in text @skip_worker("NEXT: display target does not work properly") def test_explicit_target_pyscript_tag(self): self.pyscript_run( """ """ ) text = self.page.locator("script-py").nth(1).inner_text() assert text == "hello" @skip_worker("NEXT: display target does not work properly") def test_explicit_target_on_button_tag(self): self.pyscript_run( """ """ ) self.page.locator("text=Click me").click() text = self.page.locator("id=my-button").inner_text() assert "hello" in text def test_append_true(self): self.pyscript_run( """ """ ) output = self.page.locator("script-py") assert output.inner_text() == "AAA\nBBB" def test_append_false(self): self.pyscript_run( """ """ ) output = self.page.locator("script-py") assert output.inner_text() == "BBB" def test_display_multiple_values(self): self.pyscript_run( """ """ ) output = self.page.locator("script-py") assert output.inner_text() == "hello\nworld" def test_display_multiple_append_false(self): self.pyscript_run( """ """ ) output = self.page.locator("script-py") assert output.inner_text() == "world" # TODO: this is a display.py issue to fix when append=False is used # do not use the first element, just clean up and then append # remove the # display comment once that's done def test_display_multiple_append_false_with_target(self): self.pyscript_run( """ """ ) innerhtml = self.page.locator("id=circle-div").inner_html() assert ( innerhtml == '' # noqa: E501 ) assert self.console.error.lines == [] def test_display_list_dict_tuple(self): self.pyscript_run( """ """ ) inner_text = self.page.inner_text("html") filtered_inner_text = filter_inner_text(inner_text) print(filtered_inner_text) assert ( filtered_inner_text == "['A', 1, '!']\n{'B': 2, 'List': ['A', 1, '!']}\n('C', 3, '!')" ) def test_display_should_escape(self): self.pyscript_run( """ """ ) out = self.page.locator("script-py > div") assert out.inner_html() == html.escape("hello world
") assert out.inner_text() == "hello world
" def test_display_HTML(self): self.pyscript_run( """ """ ) out = self.page.locator("script-py > div") assert out.inner_html() == "hello world
" assert out.inner_text() == "hello world" @skip_worker("NEXT: matplotlib-pyodide backend does not work") def test_image_display(self): self.pyscript_run( """