mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 11:45:28 -05:00
Add REPL plugin hooks; Add output, output-mode, stderr attributes (#1106)
* Add before, after REPL hooks * Re-introduce 'output-mode' attribute for py-repl * Add plugin execution tests * Documentation * Changelog --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: mariana <marianameireles@protonmail.com>
This commit is contained in:
@@ -109,6 +109,27 @@ class TestPyRepl(PyScriptTest):
|
||||
out_div = py_repl.locator("div.py-repl-output")
|
||||
assert out_div.all_inner_texts()[0] == "42"
|
||||
|
||||
def test_show_last_expression_with_output(self):
|
||||
"""
|
||||
Test that we display() the value of the last expression, as you would
|
||||
expect by a REPL
|
||||
"""
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="repl-target"></div>
|
||||
<py-repl output="repl-target">
|
||||
42
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
out_div = py_repl.locator("div.py-repl-output")
|
||||
assert out_div.all_inner_texts()[0] == ""
|
||||
|
||||
out_div = self.page.locator("#repl-target")
|
||||
assert out_div.all_inner_texts()[0] == "42"
|
||||
|
||||
def test_run_clears_previous_output(self):
|
||||
"""
|
||||
Check that we clear the previous output of the cell before executing it
|
||||
@@ -219,26 +240,6 @@ class TestPyRepl(PyScriptTest):
|
||||
self.page.keyboard.press("Shift+Enter")
|
||||
assert out_div.all_inner_texts()[0] == "hello"
|
||||
|
||||
def test_output_attribute(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<py-repl output="mydiv">
|
||||
display('hello world')
|
||||
</py-repl>
|
||||
<hr>
|
||||
<div id="mydiv"></div>
|
||||
"""
|
||||
)
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
#
|
||||
# check that we did NOT write to py-repl-output
|
||||
out_div = py_repl.locator("div.py-repl-output")
|
||||
assert out_div.inner_text() == ""
|
||||
# check that we are using mydiv instead
|
||||
mydiv = self.page.locator("#mydiv")
|
||||
assert mydiv.all_inner_texts()[0] == "hello world"
|
||||
|
||||
def test_output_attribute_does_not_exist(self):
|
||||
"""
|
||||
If we try to use an attribute which doesn't exist, we display an error
|
||||
@@ -253,11 +254,15 @@ class TestPyRepl(PyScriptTest):
|
||||
)
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
#
|
||||
out_div = py_repl.locator("div.py-repl-output")
|
||||
msg = "py-repl ERROR: cannot find the output element #I-dont-exist in the DOM"
|
||||
assert out_div.all_inner_texts()[0] == msg
|
||||
assert "I will not be executed" not in self.console.log.text
|
||||
|
||||
banner = self.page.query_selector_all(".py-warning")
|
||||
assert len(banner) == 1
|
||||
|
||||
banner_content = banner[0].inner_text()
|
||||
expected = (
|
||||
'output = "I-dont-exist" does not match the id of any element on the page.'
|
||||
)
|
||||
assert banner_content == expected
|
||||
|
||||
def test_auto_generate(self):
|
||||
self.pyscript_run(
|
||||
@@ -315,3 +320,249 @@ class TestPyRepl(PyScriptTest):
|
||||
|
||||
assert self.page.inner_text("#py-internal-1-1-repl-output") == "second children"
|
||||
assert self.page.inner_text("#py-internal-0-1-repl-output") == "first children"
|
||||
|
||||
def test_repl_output_attribute(self):
|
||||
# Test that output attribute sends stdout to the element
|
||||
# with the given ID, but not display()
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="repl-target"></div>
|
||||
<py-repl output="repl-target">
|
||||
print('print from py-repl')
|
||||
display('display from py-repl')
|
||||
</py-repl>
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
target = self.page.locator("#repl-target")
|
||||
assert "print from py-repl" in target.text_content()
|
||||
|
||||
out_div = py_repl.locator("div.py-repl-output")
|
||||
assert out_div.all_inner_texts()[0] == "display from py-repl"
|
||||
|
||||
self.assert_no_banners()
|
||||
|
||||
def test_repl_output_display_async(self):
|
||||
# py-repls running async code are not expected to
|
||||
# send display to element element
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="repl-target"></div>
|
||||
<py-script>
|
||||
import asyncio
|
||||
import js
|
||||
|
||||
async def print_it():
|
||||
await asyncio.sleep(1)
|
||||
print('print from py-repl')
|
||||
|
||||
|
||||
async def display_it():
|
||||
display('display from py-repl')
|
||||
await asyncio.sleep(2)
|
||||
|
||||
async def done():
|
||||
await asyncio.sleep(3)
|
||||
js.console.log("DONE")
|
||||
</py-script>
|
||||
|
||||
<py-repl output="repl-target">
|
||||
asyncio.ensure_future(print_it());
|
||||
asyncio.ensure_future(display_it());
|
||||
asyncio.ensure_future(done());
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
self.wait_for_console("DONE")
|
||||
|
||||
assert self.page.locator("#repl-target").text_content() == ""
|
||||
self.assert_no_banners()
|
||||
|
||||
def test_repl_stdio_dynamic_tags(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="first"></div>
|
||||
<div id="second"></div>
|
||||
<py-repl output="first">
|
||||
import js
|
||||
|
||||
print("first.")
|
||||
|
||||
# Using string, since no clean way to write to the
|
||||
# code contents of the CodeMirror in a PyRepl
|
||||
newTag = '<py-repl id="second-repl" output="second">print("second.")</py-repl>'
|
||||
js.document.body.innerHTML += newTag
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
assert self.page.locator("#first").text_content() == "first."
|
||||
|
||||
second_repl = self.page.locator("py-repl#second-repl")
|
||||
second_repl.locator("button").click()
|
||||
assert self.page.locator("#second").text_content() == "second."
|
||||
|
||||
def test_repl_output_id_errors(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<py-repl output="not-on-page">
|
||||
print("bad.")
|
||||
print("bad.")
|
||||
</py-repl>
|
||||
|
||||
<py-repl output="not-on-page">
|
||||
print("bad.")
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
py_repls = self.page.query_selector_all("py-repl")
|
||||
for repl in py_repls:
|
||||
repl.query_selector_all("button")[0].click()
|
||||
|
||||
banner = self.page.query_selector_all(".py-warning")
|
||||
assert len(banner) == 1
|
||||
|
||||
banner_content = banner[0].inner_text()
|
||||
expected = (
|
||||
'output = "not-on-page" does not match the id of any element on the page.'
|
||||
)
|
||||
|
||||
assert banner_content == expected
|
||||
|
||||
def test_repl_stderr_id_errors(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<py-repl stderr="not-on-page">
|
||||
import sys
|
||||
print("bad.", file=sys.stderr)
|
||||
print("bad.", file=sys.stderr)
|
||||
</py-repl>
|
||||
|
||||
<py-repl stderr="not-on-page">
|
||||
print("bad.", file=sys.stderr)
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
py_repls = self.page.query_selector_all("py-repl")
|
||||
for repl in py_repls:
|
||||
repl.query_selector_all("button")[0].click()
|
||||
|
||||
banner = self.page.query_selector_all(".py-warning")
|
||||
assert len(banner) == 1
|
||||
|
||||
banner_content = banner[0].inner_text()
|
||||
expected = (
|
||||
'stderr = "not-on-page" does not match the id of any element on the page.'
|
||||
)
|
||||
|
||||
assert banner_content == expected
|
||||
|
||||
def test_repl_output_stderr(self):
|
||||
# Test that stderr works, and routes to the same location as stdout
|
||||
# Also, repls with the stderr attribute route to an additional location
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="stdout-div"></div>
|
||||
<div id="stderr-div"></div>
|
||||
<py-repl output="stdout-div" stderr="stderr-div">
|
||||
import sys
|
||||
print("one.", file=sys.stderr)
|
||||
print("two.")
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
assert self.page.locator("#stdout-div").text_content() == "one.two."
|
||||
assert self.page.locator("#stderr-div").text_content() == "one."
|
||||
self.assert_no_banners()
|
||||
|
||||
def test_repl_output_attribute_change(self):
|
||||
# If the user changes the 'output' attribute of a <py-repl> tag mid-execution,
|
||||
# Output should no longer go to the selected div and a warning should appear
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="first"></div>
|
||||
<div id="second"></div>
|
||||
<!-- There is no tag with id "third" -->
|
||||
<py-repl id="repl-tag" output="first">
|
||||
print("one.")
|
||||
|
||||
# Change the 'output' attribute of this tag
|
||||
import js
|
||||
this_tag = js.document.getElementById("repl-tag")
|
||||
|
||||
this_tag.setAttribute("output", "second")
|
||||
print("two.")
|
||||
|
||||
this_tag.setAttribute("output", "third")
|
||||
print("three.")
|
||||
</py-script>
|
||||
"""
|
||||
)
|
||||
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
assert self.page.locator("#first").text_content() == "one."
|
||||
assert self.page.locator("#second").text_content() == "two."
|
||||
|
||||
expected_alert_banner_msg = (
|
||||
'output = "third" does not match the id of any element on the page.'
|
||||
)
|
||||
|
||||
alert_banner = self.page.locator(".alert-banner")
|
||||
assert expected_alert_banner_msg in alert_banner.inner_text()
|
||||
|
||||
def test_repl_output_element_id_change(self):
|
||||
# If the user changes the ID of the targeted DOM element mid-execution,
|
||||
# Output should no longer go to the selected element and a warning should appear
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<div id="first"></div>
|
||||
<div id="second"></div>
|
||||
<!-- There is no tag with id "third" -->
|
||||
<py-repl id="pyscript-tag" output="first">
|
||||
print("one.")
|
||||
|
||||
# Change the ID of the targeted DIV to something else
|
||||
import js
|
||||
target_tag = js.document.getElementById("first")
|
||||
|
||||
# should fail and show banner
|
||||
target_tag.setAttribute("id", "second")
|
||||
print("two.")
|
||||
|
||||
# But changing both the 'output' attribute and the id of the target
|
||||
# should work
|
||||
target_tag.setAttribute("id", "third")
|
||||
js.document.getElementById("pyscript-tag").setAttribute("output", "third")
|
||||
print("three.")
|
||||
</py-repl>
|
||||
"""
|
||||
)
|
||||
|
||||
py_repl = self.page.locator("py-repl")
|
||||
py_repl.locator("button").click()
|
||||
|
||||
# Note the ID of the div has changed by the time of this assert
|
||||
assert self.page.locator("#third").text_content() == "one.three."
|
||||
|
||||
expected_alert_banner_msg = (
|
||||
'output = "first" does not match the id of any element on the page.'
|
||||
)
|
||||
alert_banner = self.page.locator(".alert-banner")
|
||||
assert expected_alert_banner_msg in alert_banner.inner_text()
|
||||
|
||||
Reference in New Issue
Block a user