################################################################################ import base64 import html import io import os import re import numpy as np import pytest from PIL import Image from .support import ( PageErrors, PyScriptTest, filter_inner_text, filter_page_content, only_main, skip_worker, wait_for_render, ) 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 world
" assert node_list[0].inner_html() == pattern assert len(node_list) == 1 def test_consecutive_display(self): self.pyscript_run( """

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" def test_target_parameter_with_sharp(self): self.pyscript_run( """
""" ) mydiv = self.page.locator("#mydiv") assert mydiv.inner_text() == "hello world" def test_non_existing_id_target_raises_value_error(self): self.pyscript_run( """ """ ) error_msg = ( f"Invalid selector with id=non-existing. Cannot be found in the page." ) self.check_py_errors(f"ValueError: {error_msg}") def test_empty_string_target_raises_value_error(self): self.pyscript_run( """ """ ) self.check_py_errors(f"ValueError: Cannot have an empty target") def test_non_string_target_values_raise_typerror(self): self.pyscript_run( """ """ ) error_msg = f"target must be str or None, not bool" self.check_py_errors(f"TypeError: {error_msg}") self.pyscript_run( """ """ ) error_msg = f"target must be str or None, not int" self.check_py_errors(f"TypeError: {error_msg}") @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( """
ONE
THREE
""" ) text = self.page.inner_text("body") assert text == "ONE\nTWO\nTHREE" @skip_worker("NEXT: display target does not work properly") def test_consecutive_display_target(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( """ packages = ["matplotlib"] """, timeout=30 * 1000, ) wait_for_render(self.page, "*", " from pyscript import display import js print('print from python') js.console.log('print from js') js.console.error('error from js'); """ ) inner_html = self.page.content() assert re.search("", inner_html) console_text = self.console.all.lines assert "print from python" in console_text assert "print from js" in console_text assert "error from js" in console_text def test_text_HTML_and_console_output(self): self.pyscript_run( """ """ ) inner_text = self.page.inner_text("script-py") assert inner_text == "this goes to the DOM" assert self.console.log.lines[-2:] == [ "print from python", "print from js", ] print(self.console.error.lines) assert self.console.error.lines[-1] == "error from js" def test_console_line_break(self): self.pyscript_run( """ """ ) console_text = self.console.all.lines assert console_text.index("1print") == (console_text.index("2print") - 1) assert console_text.index("1console") == (console_text.index("2console") - 1) @skip_worker("NEXT: display target does not work properly") def test_image_renders_correctly(self): """ This is just a sanity check to make sure that images are rendered in a reasonable way. """ self.pyscript_run( """ packages = ["pillow"]
""", ) img_src = self.page.locator("img").get_attribute("src") assert img_src.startswith("data:image/png;charset=utf-8;base64")