import re from .support import PyScriptTest class TestOutuput(PyScriptTest): def test_simple_display(self): self.pyscript_run( """ display('hello world') """ ) inner_html = self.page.content() pattern = r'
hello world
' assert re.search(pattern, inner_html) def test_consecutive_display(self): self.pyscript_run( """ display('hello 1') display('hello 2') """ ) # need to improve this to get the first/second input # instead of just searching for it in the page inner_html = self.page.content() first_pattern = r'
hello 1
' assert re.search(first_pattern, inner_html) second_pattern = r'
hello 2
' assert re.search(second_pattern, inner_html) assert first_pattern is not second_pattern def test_multiple_display_calls_same_tag(self): self.pyscript_run( """ display('hello') display('world') """ ) inner_html = self.page.content() pattern = r'
hello
' assert re.search(pattern, inner_html) pattern = r'
world
' assert re.search(pattern, inner_html) def test_no_implicit_target(self): self.pyscript_run( """ def display_hello(): # this fails because we don't have any implicit target # from event handlers display('hello') """ ) self.page.locator("text=Click me").click() text = self.page.text_content("body") assert "hello" not in text # currently the test infrastructure doesn't allow to easily assert that # js exceptions were raised this is a workaround but we need a better fix. # Antonio promised to write it assert len(self._page_errors) == 1 console_text = self._page_errors assert ( "Implicit target not allowed here. Please use display(..., target=...)" in console_text[0].message ) self._page_errors = [] def test_explicit_target_pyscript_tag(self): self.pyscript_run( """ def display_hello(): display('hello', target='second-pyscript-tag') display_hello() """ ) text = self.page.locator("id=second-pyscript-tag-2").inner_text() assert "hello" in text def test_explicit_target_on_button_tag(self): self.pyscript_run( """ def display_hello(): display('hello', target='my-button') """ ) self.page.locator("text=Click me").click() text = self.page.locator("id=my-button").inner_text() assert "hello" in text def test_explicit_different_target_from_call(self): self.pyscript_run( """ def display_hello(): display('hello', target='second-pyscript-tag') print('nothing to see here') display_hello() """ ) text = self.page.locator("id=second-pyscript-tag").all_inner_texts() assert "hello" in text def test_append_true(self): self.pyscript_run( """ display('hello world', append=True) """ ) inner_html = self.page.content() pattern = r'
hello world
' assert re.search(pattern, inner_html) def test_append_false(self): self.pyscript_run( """ display('hello world', append=False) """ ) inner_html = self.page.content() pattern = r'hello world' assert re.search(pattern, inner_html) def test_display_multiple_values(self): self.pyscript_run( """ hello = 'hello' world = 'world' display(hello, world) """ ) inner_text = self.page.inner_text("html") assert inner_text == "hello\nworld" def test_display_list_dict_tuple(self): self.pyscript_run( """ l = ['A', 1, '!'] d = {'B': 2, 'List': l} t = ('C', 3, '!') display(l, d, t) """ ) inner_text = self.page.inner_text("html") print(inner_text) assert ( inner_text == "['A', 1, '!']\n{'B': 2, 'List': ['A', 1, '!']}\n('C', 3, '!')" ) def test_image_display(self): self.pyscript_run( """ packages = [ "matplotlib"] import matplotlib.pyplot as plt xpoints = [3, 6, 9] ypoints = [1, 2, 3] plt.plot(xpoints, ypoints) plt.show() """ ) inner_html = self.page.content() pattern = r'