Git Product home page Git Product logo

Comments (12)

nasrin1748 avatar nasrin1748 commented on June 16, 2024

The following code works for me.

123

from pyscript.

JeffersGlass avatar JeffersGlass commented on June 16, 2024

@nasrin1748 Yes, currentScript works fine in the main thread - it’s specifically when the using the worker attribute that there seems to be an issue.

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

This is awkward ... I got confused by the target export which is supposed to work out of the box in workers too.

True that currentScript is now polluted on main only: https://github.com/pyscript/polyscript/blob/bb0ae0bc90fbe7b6671ecb8202d79194f7d3c382/esm/script-handler.js#L59-L62

This must be fixed !!!

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

OK, I am confused ... but at least I remembered correctly:

// common life-cycle handlers for any node
export default async (main, wrap, element, hook) => {
const isAsync = hook.endsWith("Async");
const isBefore = hook.startsWith("onBefore");
// make it possible to reach the current target node via Python
// or clean up for other scripts executing around this one
(isBefore ? before : after)(element);
for (const fn of main(hook)) {
if (isAsync) await fn(wrap, element);
else fn(wrap, element);
}
};

Will investigate and fix this soon, thanks again for the report!

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

FYI I've fixed (or better, implemented) currentScript as part of the polyscript logic and module.

This is guaranteed to return the right node even if invoked lazily, which is the opposite of what document.currentScript offers, as that doesn't always guarantee the reference is the expected one.

More work needs to be done in PyScript to somehow expose that in either main or worker code, but I start believing we should always use the polyscript.currentScript instead of monkey-patching, in an inevitable error prone way, the global document reference. I don't think we should necessarily deprecate that, it's still part of the standard, but I think this should be a better way forward:

from pyscript import currentScript

We already normalize window and document, and adding this export would likely never fail expectations and it can be always available in both main and workers thanks to polyscript logic behind.

@JeffersGlass @ntoll @madhur-tandon what do you think?

edit

from pyscript.

ntoll avatar ntoll commented on June 16, 2024

LGTM - I rather like @JeffersGlass 's suggestion of a get_current_script() function discussed in the Tuesday call.

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

let's please discuss only here: #1930 (reply in thread)

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

fixed via __terminal__

from pyscript.

nasrin1748 avatar nasrin1748 commented on June 16, 2024

So finally what's going to work?
from pyscript import get_current_script
from pyscript import current_script
import pyscript
I am unable to import any of these.

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

@nasrin1748 from polyscript import currentScript works reliably only in workers while there is NO solution on main because a script can be async, its bootstrap is async too so other screipts might have stolen that reference already, and listeners might run any time in the future and we don't get to catch their execution out of the box and we don't want to override native DOM APIs all over.

In short, document.currentScript is problematic on the Web on main and it's as problematic in here too so it won't get fixed on main because it cannot. The __terminal__ fixes the specific terminal case in a consistent way.

from pyscript.

nasrin1748 avatar nasrin1748 commented on June 16, 2024

@WebReflection i tried importing it in workers .Not sure where i am going wrong.
https://_12.pyscriptapps.com/broad-dust/latest/

from pyscript.

WebReflection avatar WebReflection commented on June 16, 2024

@nasrin1748 you are in a current release, we haven't released that yet.

This week we should, but if you don't want to wait for it, feel free to use the unstable channel:
https://cdn.jsdelivr.net/npm/@pyscript/core/dist/core.js

from pyscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.