8000 remove pys-on* and py-on* attributes (#1361) · patrickloeber/pyscript@e3602f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e3602f4

Browse files
remove pys-on* and py-on* attributes (pyscript#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>
1 parent f3db6a3 commit e3602f4

13 files changed

+29
-97
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ Deprecations and Removals
7777
- The py-config `runtimes` to specify an interpreter has been deprecated. The `interpreters` config should be used instead. ([#1082](https://github.com/pyscript/pyscript/pull/1082))
7878
- The attributes `pys-onClick` and `pys-onKeyDown` have been deprecated, but the warning was only shown in the console. An alert banner will now be shown on the page if the attributes are used. They will be removed in the next release. ([#1084](https://github.com/pyscript/pyscript/pull/1084))
7979
- The pyscript elements `py-button`, `py-inputbox`, `py-box` and `py-title` have now completed their deprecation cycle and have been removed. ([#1084](https://github.com/pyscript/pyscript/pull/1084))
80+
- The attributes `pys-onClick` and `pys-onKeyDown` have been removed. Use `py-click` and `py-keydown` instead ([#1361](https://github.com/pyscript/pyscript/pull/1361))

docs/reference/API/attr_to_event.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ PyScript provides a convenient syntax for mapping JavaScript events to PyScript
55
For example, you can use the following code to connect the click event to a button:
66

77
```
8-
<button id="py-click" py-onClick="foo()">Click me</button>
8+
<button id="py-click" py-click="foo()">Click me</button>
99
```
1010

1111
Here is a list of all the available event mappings:
1212

1313
| PyScript Event Name | DOM Event Name |
1414
|-------------------|----------------|
15-
| py-onClick | click |
16-
| py-onKeyDown | keydown |
1715
| py-afterprint | afterprint |
1816
| py-beforeprint | beforeprint |
1917
| py-beforeunload | beforeunload |

docs/reference/API/display.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Display will throw an exception if the target is not clear. E.g. the following c
3535
# from event handlers
3636
display('hello')
3737
</py-script>
38-
<button id="my-button" py-onClick="display_hello()">Click me</button>
38+
<button id="my-button" py-click="display_hello()">Click me</button>
3939
```
4040

4141
Because it's considered unclear if the `hello` string should be displayed underneath the `<py-script>` tag or the `<button>` tag.
@@ -49,7 +49,7 @@ To write compliant code, make sure to specify the target using the `target` para
4949
display('hello', target="helloDiv")
5050
</py-script>
5151
<div id="helloDiv"></div>
52-
<button id="my-button" py-onClick="display_hello()">Click me</button>
52+
<button id="my-button" py-click="display_hello()">Click me</button>
5353
```
5454

5555
#### Using matplotlib with display

examples/handtrack/say_hello.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
id="trackbutton"
116116
class="bx--btn bx--btn--secondary"
117117
type="button"
118-
py-onClick="toggle_video()"
118+
py-click="toggle_video()"
119119
>
120120
Toggle Video
121121
</button>

examples/mario/play_mario.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
id="trackbutton"
144144
class="bx--btn bx--btn--secondary"
145145
type="button"
146-
py-onClick="toggle_video()"
146+
py-click="toggle_video()"
147147
>
148148
Start Video
149149
</button>

examples/micrograd_ai.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ <h1>Micrograd - A tiny Autograd engine (with a bite! :))</h1>
8383
id="run-all-button"
8484
class="btn btn-primary"
8585
type="submit"
86-
py-onClick="run_all_micrograd_demo()"
86+
py-click="run_all_micrograd_demo()"
8787
>
8888
Run All</button
8989
><br />

examples/simple_bioinformatics_tool.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@
7878
id="run"
7979
type="button"
8080
class="button is-primary"
81-
py-onClick="run()"
81+
py-click="run()"
8282
>
8383
Run!
8484
</button>
8585
<button
8686
id="clear"
8787
type="button"
8888
class="button is-danger"
89-
py-onClick="clear()"
89+
py-click="clear()"
9090
>
9191
Clear
9292
</button>

pyscriptjs/src/components/pyscript.ts

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ export function make_PyScript(interpreter: InterpreterClient, app: PyScriptApp)
7272

7373
/** Defines all possible py-on* and their corresponding event types */
7474
const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
75-
// Leaving pys-onClick and pys-onKeyDown for backward compatibility
76-
['pys-onClick', 'click'],
77-
['pys-onKeyDown', 'keydown'],
78-
['py-onClick', 'click'],
79-
['py-onKeyDown', 'keydown'],
8075
// Window Events
8176
['py-afterprint', 'afterprint'],
8277
['py-beforeprint', 'beforeprint'],
@@ -166,15 +161,15 @@ const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
166161
]);
167162

168163
/** Initialize all elements with py-* handlers attributes */
169-
export async function initHandlers(interpreter: InterpreterClient) {
164+
export function initHandlers(interpreter: InterpreterClient) {
170165
logger.debug('Initializing py-* event handlers...');
171166
for (const pyAttribute of pyAttributeToEvent.keys()) {
172-
await createElementsWithEventListeners(interpreter, pyAttribute);
167+
createElementsWithEventListeners(interpreter, pyAttribute);
173168
}
174169
}
175170

176171
/** Initializes an element with the given py-on* attribute and its handler */
177-
async function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) {
172+
function createElementsWithEventListeners(interpreter: InterpreterClient, pyAttribute: string) {
178173
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pyAttribute}]`);
179174
for (const el of matches) {
180175
// If the element doesn't have an id, let's add one automatically!
@@ -183,37 +178,16 @@ async function createElementsWithEventListeners(interpreter: InterpreterClient,
183178
}
184179
const handlerCode = el.getAttribute(pyAttribute);
185180
const event = pyAttributeToEvent.get(pyAttribute);
186-
187-
if (pyAttribute === 'pys-onClick' || pyAttribute === 'pys-onKeyDown') {
188-
const msg =
189-
`The attribute 'pys-onClick' and 'pys-onKeyDown' are deprecated. Please 'py-click="myFunction()"' ` +
190-
` or 'py-keydown="myFunction()"' instead.`;
191-
createDeprecationWarning(msg, msg);
192-
const source = `
193-
from pyodide.ffi import create_proxy
194-
Element("${el.id}").element.addEventListener("${event}", create_proxy(${handlerCode}))
195-
`;
196-
197-
// We meed to run the source code in a try/catch block, because
198-
// the source code may contain a syntax error, which will cause
199-
// the splashscreen to not be removed.
200-
try {
201-
await interpreter.run(source);
202-
} catch (e) {
203-
logger.error((e as Error).message);
204-
}
205-
} else {
206-
el.addEventListener(event, () => {
207-
void (async () => {
208-
try {
209-
await interpreter.run(handlerCode);
210-
} catch (e) {
211-
const err = e as Error;
212-
displayPyException(err, el.parentElement);
213-
}
214-
})();
215-
});
216-
}
181+
el.addEventListener(event, () => {
182+
void (async () => {
183+
try {
184+
await interpreter.run(handlerCode);
185+
} catch (e) {
186+
const err = e as Error;
187+
displayPyException(err, el.parentElement);
188+
}
189+
})();
190+
});
217191
// TODO: Should we actually map handlers in JS instead of Python?
218192
// el.onclick = (evt: any) => {
219193
// console.log("click");
@@ -224,7 +198,7 @@ async function createElementsWithEventListeners(interpreter: InterpreterClient,
224198
// }).then(() => {
225199
// console.log("resolved")
226200
// });
227-
// F438 // let handlerCode = el.getAttribute('py-onClick');
201+
// // let handlerCode = el.getAttribute('py-click');
228202
// // pyodide.runPython(handlerCode);
229203
// }
230204
}

pyscriptjs/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export class PyScriptApp {
246246

247247
//Takes a runtime and a reference to the PyScriptApp (to access plugins)
248248
createCustomElements(interpreter, this);
249-
await initHandlers(interpreter);
249+
initHandlers(interpreter);
250250

251251
// NOTE: interpreter message is used by integration tests to know that
252252
// pyscript initialization has complete. If you change it, you need to

pyscriptjs/tests/integration/test_01_basic.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -301,26 +301,6 @@ def test_getPySrc_returns_source_code(self):
301301
== 'print("hello world!")\n'
302302
)
303303

304-
@pytest.mark.skip(reason="pys-onClick is broken, we should kill it, see #1213")
305-
def test_pys_onClick_shows_deprecation_warning(self):
306-
self.pyscript_run(
307-
"""
308-
<button id="1" pys-onClick="myfunc()">Click me</button>
309-
<py-script>
310-
def myfunc():
311-
print("hello world")
312-
313-
</py-script>
314-
"""
315-
)
316-
banner = self.page.locator(".alert-banner")
317-
expected_message = (
318-
"The attribute 'pys-onClick' and 'pys-onKeyDown' are "
319-
"deprecated. Please 'py-click=\"myFunction()\"' or "
320-
"'py-keydown=\"myFunction()\"' instead."
321-
)
322-
assert banner.inner_text() == expected_message
323-
324304
def test_py_attribute_without_id(self):
325305
self.pyscript_run(
326306
"""

pyscriptjs/tests/integration/test_02_display.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def display_hello():
113113
# from event handlers
114114
display('hello world')
115115
</py-script>
116-
<button id="my-button" py-onClick="display_hello()">Click me</button>
116+
<button id="my-button" py-click="display_hello()">Click me</button>
117117
"""
118118
)
119119
self.page.locator("text=Click me").click()
@@ -151,7 +151,7 @@ def test_explicit_target_on_button_tag(self):
151151
def display_hello():
152152
display('hello', target='my-button')
153153
</py-script>
154-
<button id="my-button" py-onClick="display_hello()">Click me</button>
154+
<button id="my-button" py-click="display_hello()">Click me</button>
155155
"""
156156
)
157157
self.page.locator("text=Click me").click()

pyscriptjs/tests/integration/test_py_terminal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_auto_attribute(self):
6969
"""
7070
<py-terminal auto></py-terminal>
7171
72-
<button id="my-button" py-onClick="print('hello world')">Click me</button>
72+
<button id="my-button" py-click="print('hello world')">Click me</button>
7373
"""
7474
)
7575
term = self.page.locator("py-terminal")
@@ -85,7 +85,7 @@ def test_config_auto(self):
8585
"""
8686
self.pyscript_run(
8787
"""
88-
<button id="my-button" py-onClick="print('hello world')">Click me</button>
88+
<button id="my-button" py-click="print('hello world')">Click me</button>
8989
"""
9090
)
9191
term = self.page.locator("py-terminal")
@@ -136,7 +136,7 @@ def test_config_docked(self):
136136
"""
137137
self.pyscript_run(
138138
"""
139-
<button id="my-button" py-onClick="print('hello world')">Click me</button>
139+
<button id="my-button" py-click="print('hello world')">Click me</button>
140140
"""
141141
)
142142
term = self.page.locator("py-terminal")

pyscriptjs/tests/integration/test_splashscreen.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21
from playwright.sync_api import expect
32

43
from .support import PyScriptTest
@@ -77,26 +76,6 @@ def test_autoclose_loader_deprecated(self):
7776
expect(div).to_contain_text("Startup complete")
7877
assert "hello pyscript" in self.console.log.lines
7978

80-
@pytest.mark.skip(reason="pys-onClick is broken, we should kill it, see #1213")
81-
def test_splashscreen_closes_on_error_with_pys_onClick(self):
82-
self.pyscript_run(
83-
"""
84-
<button id="submit-button" type="submit" pys-onClick="myFunc">OK</button>
85-
86-
<py-script>
87-
from js import console
88-
89-
def myFunc(*args, **kwargs):
90-
text = Element('test-input').element.value
91-
Element('test-output').element.innerText = text
92-
93-
</py-script> 6AE9 ;
94-
""",
95-
)
96-
97-
assert self.page.locator("py-splashscreen").count() == 0
98-
assert "Python exception" in self.console.error.text
99-
10079
def test_splashscreen_disabled_option(self):
10180
self.pyscript_run(
10281
"""

0 commit comments

Comments
 (0)
0