-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[BUG] pys-onClick attribute does not work, when element has no id #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I guess this:
Element() and pass it to pyodide.runPythonAsync(source) . However, there should be another possibility than the Id to create this Element
|
Looks right. Not sure by what other mechanism you can reference the element though other than maybe 'self' if no element id is specified? |
I'm actually getting this with hello_world.html from a checkout. The checkout worked a few days ago. I can't spot any changes in the commits that might have caused it. Still pointing to 0.20 of Pyodide. |
As it turns out, I get the exception in Chrome, not in Firefox or Safari:
As it turns out, I believe that is indeed an invalid selector. A dash can't be followed by a number. |
I'm seeing as well: in Chrome with the basic hello world from Getting Started. I only get it intermittently - I guess that when i'm refreshing and that GUID type Id happens to have a dash followed by a number (as @pauleveritt mentions) then it fails and other times when I'm lucky enough not to get that it succeeds |
(Sorry for the noise.) A little bit more info. I was using the examples from a |
@nmstoker It appears Am I looking at it correctly? |
But I think this is a different error than mine, here '-509153c5-29df-0d1d-f9fd-c7c688f46911' appears to be the element's id. In my case there is no id at all |
I can also reproduce the issue using firefox 100.0 (64-bit) |
I can also reproduce with a locally built pyscript.js |
One fix would be to actually pass the element to python in the const handlerCode = el.getAttribute('pys-onClick');
const elementGlobals: Map<string, any> = pyodide.globals.toJs()
elementGlobals.set("element", el)
const options = {globals: pyodide.toPy(elementGlobals)};
source = `Element("${el.id}", element).element.onclick = ${handlerCode}`;
output = await pyodide.runPythonAsync(source, options); This way, @property
def element(self):
"""Return the dom element"""
if not self._element:
self._element = document.querySelector(f"#{self._id}")
return self._element doesn't fail, because the python |
I am seeing this even without a button or onClick method - should I open a separate ticket? Reproduce by viewing this page: http://ptmcg.com/dev/pyscript/pyparsing_railroad_diagram.html |
@yannickfunk can you try now? #395 should have temporarily fixed it (although we need to investigate to actually come with a better fix) |
Ok, let me test it locally. |
I also tested it with a local unminified build on chrome and firefox |
Ah! Yeah.. Not the same issue. I think problem here is that we are not properly mapping the TY! |
@fpliger I think the problem is the mapping which happens there. It uses the id of the dom Element, but the in this case has no Id.
With this fix, it works, let me know if this might be an appropriate solution |
@fpliger @yannickfunk Fixed it for me. |
Nice! @yannickfunk that's exactly what's happening. The initially easy "fix" about this is to add an In the long term, I think it'd make sense for us to enforce |
@fpliger Yeah with an id it works, I already found that out as described in Expected Behavior. |
@fpliger Ok so I just found out it actually makes a difference.
For |
Not right now but I think that in the long term we can definitely generate an id for a |
We are answering at the same time :) Right. That's very easy to do but I don't think we have it in |
Yeah for
|
So, I think a good fix here would be:
we should also make sure this is clearly stated in the docs (as soon as we merge them this week) |
@fpliger Sounds good, I could implement this if you want |
That'd be great @yannickfunk ! @marimeireles was maybe going to look at this but if she didn't, it'd definitely be a great way to help! TY! |
Ok... I don't think @marimeireles started on it yet, @yannickfunk want to pick it up? :) |
Yeah I would pick it up @fpliger |
Currently, for the components: The state is the following:
Do we want to add it to all components where it is missing, or just to |
Great question. I think it definitely makes sense for the components that can execute a script and are likely to be mounted. (PyConfig and PyEnv are a bit special and I don't think it's necessary). With that mind, that list above should probably look like this:
|
@fpliger I created a PR |
Awesome! I think #400 closes this (tested locally and looks great). TY again @yannickfunk 🚀 Leaving this for posterity... The same example couple have been achieved with the more "PyScripty" form:
|
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
When you whant to use the pys-onClick attribute to define a python function as the click listener, and the html element has no id, it does not work.
Error:
To Reproduce
Using:
Use this html and click on the button:
Expected behavior
The message "clicked" should be logged to the console. With an id it works:
EDIT: Added chrome, firefox version number
The text was updated successfully, but these errors were encountered: