mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
remove pys-on* and py-on* attributes (#1361)
* remove pys-on* and py-on* attributes * update changelog * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix eslint --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -72,11 +72,6 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp)
|
||||
|
||||
/** Defines all possible py-on* and their corresponding event types */
|
||||
const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
|
||||
// Leaving pys-onClick and pys-onKeyDown for backward compatibility
|
||||
['pys-onClick', 'click'],
|
||||
['pys-onKeyDown', 'keydown'],
|
||||
['py-onClick', 'click'],
|
||||
['py-onKeyDown', 'keydown'],
|
||||
// Window Events
|
||||
['py-afterprint', 'afterprint'],
|
||||
['py-beforeprint', 'beforeprint'],
|
||||
@@ -166,15 +161,15 @@ const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
|
||||
]);
|
||||
|
||||
/** Initialize all elements with py-* handlers attributes */
|
||||
export async function initHandlers(interpreter: InterpreterClient) {
|
||||
export function initHandlers(interpreter: InterpreterClient) {
|
||||
logger.debug('Initializing py-* event handlers...');
|
||||
for (const pyAttribute of pyAttributeToEvent.keys()) {
|
||||
await createElementsWithEventListeners(interpreter, pyAttribute);
|
||||
createElementsWithEventListeners(interpreter, pyAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
/** Initializes an element with the given py-on* attribute and its handler */
|
||||
async function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) {
|
||||
function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) {
|
||||
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pyAttribute}]`);
|
||||
for (const el of matches) {
|
||||
// If the element doesn't have an id, let's add one automatically!
|
||||
@@ -183,37 +178,16 @@ async function createElementsWithEventListeners(interpreter: InterpreterClient,
|
||||
}
|
||||
const handlerCode = el.getAttribute(pyAttribute);
|
||||
const event = pyAttributeToEvent.get(pyAttribute);
|
||||
|
||||
if (pyAttribute === 'pys-onClick' || pyAttribute === 'pys-onKeyDown') {
|
||||
const msg =
|
||||
`The attribute 'pys-onClick' and 'pys-onKeyDown' are deprecated. Please 'py-click="myFunction()"' ` +
|
||||
` or 'py-keydown="myFunction()"' instead.`;
|
||||
createDeprecationWarning(msg, msg);
|
||||
const source = `
|
||||
from pyodide.ffi import create_proxy
|
||||
Element("${el.id}").element.addEventListener("${event}", create_proxy(${handlerCode}))
|
||||
`;
|
||||
|
||||
// We meed to run the source code in a try/catch block, because
|
||||
// the source code may contain a syntax error, which will cause
|
||||
// the splashscreen to not be removed.
|
||||
try {
|
||||
await interpreter.run(source);
|
||||
} catch (e) {
|
||||
logger.error((e as Error).message);
|
||||
}
|
||||
} else {
|
||||
el.addEventListener(event, () => {
|
||||
void (async () => {
|
||||
try {
|
||||
await interpreter.run(handlerCode);
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
displayPyException(err, el.parentElement);
|
||||
}
|
||||
})();
|
||||
});
|
||||
}
|
||||
el.addEventListener(event, () => {
|
||||
void (async () => {
|
||||
try {
|
||||
await interpreter.run(handlerCode);
|
||||
} catch (e) {
|
||||
const err = e as Error;
|
||||
displayPyException(err, el.parentElement);
|
||||
}
|
||||
})();
|
||||
});
|
||||
// TODO: Should we actually map handlers in JS instead of Python?
|
||||
// el.onclick = (evt: any) => {
|
||||
// console.log("click");
|
||||
@@ -224,7 +198,7 @@ async function createElementsWithEventListeners(interpreter: InterpreterClient,
|
||||
// }).then(() => {
|
||||
// console.log("resolved")
|
||||
// });
|
||||
// // let handlerCode = el.getAttribute('py-onClick');
|
||||
// // let handlerCode = el.getAttribute('py-click');
|
||||
// // pyodide.runPython(handlerCode);
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ export class PyScriptApp {
|
||||
|
||||
//Takes a runtime and a reference to the PyScriptApp (to access plugins)
|
||||
createCustomElements(interpreter, this);
|
||||
await initHandlers(interpreter);
|
||||
initHandlers(interpreter);
|
||||
|
||||
// NOTE: interpreter message is used by integration tests to know that
|
||||
// pyscript initialization has complete. If you change it, you need to
|
||||
|
||||
@@ -301,26 +301,6 @@ class TestBasic(PyScriptTest):
|
||||
== 'print("hello world!")\n'
|
||||
)
|
||||
|
||||
@pytest.mark.skip(reason="pys-onClick is broken, we should kill it, see #1213")
|
||||
def test_pys_onClick_shows_deprecation_warning(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<button id="1" pys-onClick="myfunc()">Click me</button>
|
||||
<py-script>
|
||||
def myfunc():
|
||||
print("hello world")
|
||||
|
||||
</py-script>
|
||||
"""
|
||||
)
|
||||
banner = self.page.locator(".alert-banner")
|
||||
expected_message = (
|
||||
"The attribute 'pys-onClick' and 'pys-onKeyDown' are "
|
||||
"deprecated. Please 'py-click=\"myFunction()\"' or "
|
||||
"'py-keydown=\"myFunction()\"' instead."
|
||||
)
|
||||
assert banner.inner_text() == expected_message
|
||||
|
||||
def test_py_attribute_without_id(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
|
||||
@@ -113,7 +113,7 @@ class TestOutput(PyScriptTest):
|
||||
# from event handlers
|
||||
display('hello world')
|
||||
</py-script>
|
||||
<button id="my-button" py-onClick="display_hello()">Click me</button>
|
||||
<button id="my-button" py-click="display_hello()">Click me</button>
|
||||
"""
|
||||
)
|
||||
self.page.locator("text=Click me").click()
|
||||
@@ -151,7 +151,7 @@ class TestOutput(PyScriptTest):
|
||||
def display_hello():
|
||||
display('hello', target='my-button')
|
||||
</py-script>
|
||||
<button id="my-button" py-onClick="display_hello()">Click me</button>
|
||||
<button id="my-button" py-click="display_hello()">Click me</button>
|
||||
"""
|
||||
)
|
||||
self.page.locator("text=Click me").click()
|
||||
|
||||
@@ -69,7 +69,7 @@ class TestPyTerminal(PyScriptTest):
|
||||
"""
|
||||
<py-terminal auto></py-terminal>
|
||||
|
||||
<button id="my-button" py-onClick="print('hello world')">Click me</button>
|
||||
<button id="my-button" py-click="print('hello world')">Click me</button>
|
||||
"""
|
||||
)
|
||||
term = self.page.locator("py-terminal")
|
||||
@@ -85,7 +85,7 @@ class TestPyTerminal(PyScriptTest):
|
||||
"""
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<button id="my-button" py-onClick="print('hello world')">Click me</button>
|
||||
<button id="my-button" py-click="print('hello world')">Click me</button>
|
||||
"""
|
||||
)
|
||||
term = self.page.locator("py-terminal")
|
||||
@@ -136,7 +136,7 @@ class TestPyTerminal(PyScriptTest):
|
||||
"""
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<button id="my-button" py-onClick="print('hello world')">Click me</button>
|
||||
<button id="my-button" py-click="print('hello world')">Click me</button>
|
||||
"""
|
||||
)
|
||||
term = self.page.locator("py-terminal")
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import pytest
|
||||
from playwright.sync_api import expect
|
||||
|
||||
from .support import PyScriptTest
|
||||
@@ -77,26 +76,6 @@ class TestSplashscreen(PyScriptTest):
|
||||
expect(div).to_contain_text("Startup complete")
|
||||
assert "hello pyscript" in self.console.log.lines
|
||||
|
||||
@pytest.mark.skip(reason="pys-onClick is broken, we should kill it, see #1213")
|
||||
def test_splashscreen_closes_on_error_with_pys_onClick(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<button id="submit-button" type="submit" pys-onClick="myFunc">OK</button>
|
||||
|
||||
<py-script>
|
||||
from js import console
|
||||
|
||||
def myFunc(*args, **kwargs):
|
||||
text = Element('test-input').element.value
|
||||
Element('test-output').element.innerText = text
|
||||
|
||||
</py-script>
|
||||
""",
|
||||
)
|
||||
|
||||
assert self.page.locator("py-splashscreen").count() == 0
|
||||
assert "Python exception" in self.console.error.text
|
||||
|
||||
def test_splashscreen_disabled_option(self):
|
||||
self.pyscript_run(
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user