Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Noah
    @coolreader18
    @pmp-p yep
    Jan Max Meyer
    @phorward
    @coolreader18 yeah that now works now!
    Paul m. p. P.
    @pmp-p
    i've choosen async so i make await window.document.title in one deferred call
    Noah
    @coolreader18
    Ah, that's an interesting idea, would certainly reduce the cost of crossing the js/wasm boundary
    Paul m. p. P.
    @pmp-p
    not sure that means your whole python code must be async :/
    i made a poc over websocket before wasm https://github.com/pmp-p/aiolink
    the first try with wasm allowed me to interact with dom (almost) like brython does https://pmp-p.github.io/micropython-next/dom-rw.html?dev
    Jan Max Meyer
    @phorward
    Wow nice! Here's a working version of my program from above using your method... but it's a little bit crazy ;-)
    from _window import window
    import browser
    document = window.get_prop('document')
    createElement = window.get_prop('document').get_prop('createElement')
    
    for i in range(10):
        img = createElement.call(browser.jsstr("img"), this=document)
        img.set_prop("src", browser.jsstr("https://raw.githubusercontent.com/RustPython/RustPython/master/logo.png"))
        document.get_prop("body").get_prop("appendChild").call(img, this=document.get_prop("body"))
    @pmp-p thanks for the links, I'll check them out
    Noah
    @coolreader18
    Yeah, it's not the most ergonomic API 🙃 My thought was that people could make wrappers over the low level API to make it easier to use, e.g. with a __getattr__ for calling get_prop, or a call_method() method to call self.call(*args, this=self)
    Jan Max Meyer
    @phorward
    @coolreader18 yeah, that's exactly the thing I want to do first with our DOM library... it already wraps all HTML-Elements in its own Widget class, so there are only a few places to touch.
    ok that helps me a lot for now, thank you guys!
    Noah
    @coolreader18
    you're welcome 😄
    Mireille Raad
    @mireille-raad
    oh that looks cool. can you make it target a specific div by id , instead of body ?
    Jan Max Meyer
    @phorward

    @mireille-raad sure, this seems to work also

    from _window import window
    import browser
    document = window.get_prop('document')
    createElement = window.get_prop('document').get_prop('createElement')
    
    errorDiv = window.get_prop('document').get_prop('getElementById').call(browser.jsstr("error"), this=document)
    
    for i in range(10):
        img = createElement.call(browser.jsstr("img"), this=document)
        img.set_prop("src", browser.jsstr("https://raw.githubusercontent.com/RustPython/RustPython/master/logo.png"))
        errorDiv.get_prop("appendChild").call(img, this=errorDiv)

    it surely could be written shorter.

    Mireille Raad
    @mireille-raad
    thanks :D this def. helped.
    Screenshot from 2020-10-14 13-35-01.png
    @phorward do you want to add this under the snippets in the demo folder ?
    Jan Max Meyer
    @phorward
    @mireille-raad sure why not ;-)
    Jan Max Meyer
    @phorward
    @mireille-raad in what kind of environment do you run that?
    Mireille Raad
    @mireille-raad
    @phorward the code is under rustpython/wasm/notebook. https://github.com/RustPython/RustPython/tree/master/wasm/notebook
    my personal env. is ubuntu
    i also do OSX
    macOS*
    I can help you setup the same thing locally on yours if you want
    Jan Max Meyer
    @phorward
    ah cool. Thanks, I already got it to work. Is this a RustPythonic version of a notebook known from Iodide/Pyodide?
    Jan Max Meyer
    @phorward

    this is absolutely crazy ;-)

    from _window import window
    from browser import jsstr
    
    items = document.get_prop("getElementsByTagName").call(jsstr("div"), this=document)
    items = [items.get_prop("item").call(jsstr(str(i)), this=items) for i in range(0, int(items.get_prop("length").as_float()))]
    print(items)

    With Pyodide, this obviously more readable version does the same

    from js import window
    document = window.document
    
    items = document.getElementsByTagName("div")
    items = [items.item(i) for i in range(0, int(items.length))]

    I think I'll might build an easier interface for this in RustPython.

    Oh yes, one more question: @coolreader18 why can I import _window? is it the same like from browser import window?
    Jan Max Meyer
    @phorward
    Ok, now I'm getting stuck again with the available solution: Is there any way to get this working?
    from _window import window
    from browser import jsstr
    
    document = window.get_prop("document")
    runBtn = document.get_prop("getElementById").call(jsstr("run-btn"), this=document)
    
    def callback(e):
        print(e)
    
    runBtn.get_prop("addEventListener").call("click", callback, this=runBtn)
    Noah
    @coolreader18
    @phorward re: _window, that's the native module that expects the window object, if browser exports a window you should probably use that instead. I haven't worked on that stuff in a while, bit the reason there's a module _window separate from _js is because I wanted a sorta security feature, where python code can't get access to any JSValue unless explicitly passed one or given access to the window object (that's why JSValue has methods like new_from_str, rather than them just being functions in the _js module). Otherwise, even with just a string, you could do js_str.get_prop("__proto__").get_prop("__proto__") and get access to the JS Object.prototype, which is a security vulnerability
    Noah
    @coolreader18
    For the callback, u think you might have to add a method on JSValue like new_function, if you want to take a stab at it in wasm/lib/src/js_module.rs. Otherwise I could, but I remember it was tricky when I thought about it before, since callbacks passed to JS don't automatically get garbage collected, but there might be some way around that now.
    Jan Max Meyer
    @phorward
    Hello @coolreader18, thanks for your pull request in RustPython/RustPython#2290, however I yet didn't figure out how to use jsclosure now to resolve this issue? Or is it a complete different thing? I'm very happy about any hints :-) and hopefully I could also provide some code for RustPython soon. For me, it's very difficult to understand how all these modules in RustPython work together, but I think I indeed need just some time for this to understand.
    Noah
    @coolreader18
    I think you should be able to do it like this now (although not on the demo online until RustPython/RustPython#2256 is merged, you'd have to build it yourself locally):
    from browser import jsstr, jsclosure, window
    
    document = window.get_prop("document")
    runBtn = document.call_method("getElementById", jsstr("run-btn"))
    
    def callback(this, e):
        print(e)
    jscb = jsclosure(callback)
    runBtn.call_method("addEventListener", jsstr("click"), jscb.value)
    Jan Max Meyer
    @phorward

    Ah nice :-)

    With your code runBtn.call_method("addEventListener", jsstr("click"), jscb.value) I'm getting error TypeError: EventTarget.addEventListener: At least 2 arguments required, but only 1 passed.

    I then tried this code adapted from my draft above

    from _window import window
    from browser import jsstr, jsclosure
    
    document = window.get_prop("document")
    runBtn = document.get_prop("getElementById").call(jsstr("run-btn"), this=document)
    
    def callback(e):
        print(e)
    
    jscb = jsclosure(callback)
    
    runBtn.get_prop("addEventListener").call(jsstr("click"), jscb.value, this=runBtn)

    but when I hit the button, Firefox gets an exception:

    Uncaught Error: closure invoked recursively or destroyed already
        Ct http://localhost:8080/1.index.js:1
        __wbindgen_throw http://localhost:8080/index.js:1
        F http://localhost:8080/1.index.js:1
        c http://localhost:8080/1.index.js:1
        _ http://localhost:8080/1.index.js:1
        At http://localhost:8080/1.index.js:1
        P http://localhost:8080/1.index.js:1
        __wbg_apply_28bd29e919552baf http://localhost:8080/index.js:1
        U http://localhost:8080/1.index.js:1
        v http://localhost:8080/index.js:1
        EventListener.handleEvent* http://localhost:8080/index.js:1
        <anonymous> http://localhost:8080/index.js:1
        promise callback* http://localhost:8080/index.js:1
        s http://localhost:8080/index.js:1
        <anonymous> http://localhost:8080/index.js:1
        <anonymous> http://localhost:8080/index.js:1
    I've used your branch.
    Noah
    @coolreader18

    this seems to work for me:

    from browser import jsstr, jsclosure, window
    
    document = window.get_prop("document")
    runBtn = document.call_method("getElementById", jsstr("console"))
    
    def callback(this, e):
        print(e)
    jscb = jsclosure(callback)
    runBtn.get_prop("addEventListener").call(jsstr("click"), jscb.value, this=runBtn)

    When I click on the console textarea, it prints JsValue(MouseEvent)

    Noah
    @coolreader18
    Ah, just figured out what was up with the "2 arguments required one passed" error, I pushed a fix and it works now
    Jan Max Meyer
    @phorward
    @coolreader18 ok thanks now I got it to work!
    I'll now continue with my library integration, and when I made some more experience with this I'll gonna add some examples as pull request.
    Noah
    @coolreader18
    Sounds good!
    namoscagnm
    @namoscagnm
    Hi all!! I am a Python user since some years, and I love compilers and I enjoy Rust, so I helped already on a compiler for another language (Gleam, which compiles to Erlang)...I would like to help with something more used like Python instead of Erlang and was thinking whats the easiest way to make the first contribution to rustpython... could you tell me which is the easiest opened issue which includes programming? Thank youuu! Gustavo
    Noah
    @coolreader18
    Hi @namoscagnm! There's a guide to contributing via fixing unittests in the DEVELOPMENT.md document, if you like compilers maybe take a look at Lib/test/test_with.py? There might be a few tests there you can fix related to executing/compiling Python that should be a good introduction to the codebase
    Or maybe something in test_symtable if you're feeling ambitious?
    namoscagnm
    @namoscagnm
    I will try to do Lib/test/test_bool, test test_subclass()
    namoscagnm
    @namoscagnm
    hmm where is the definition of bool? I found the word bool in 895 results, so i have to weed some out
    maybe it would also be good to know wheer the definition of "class" is, since the test is trying to do "class C(bool)"
    namoscagnm
    @namoscagnm

    I found some code which could be relevant in vm/src/pyobj.rs and I would like to run the test "cargo run -- -m test test_bool -v" from inside "Lib/test" and have breakpoints inside pyobj.rs so I can get a better feeling of how trying to extending a class with "class C(bool)" will call the pyobj.rs. I believe that if I can set breakpoints there I will be eventually able to understand how to prevent the bool class from being subclassed.

    Could you give me a tip on how to put breakpoints inside the vm while running cargo run -- -m test test_bool -v ??

    Thanks

    namoscagnm
    @namoscagnm
    It would be nice also if the rust part could be debugged on the corresponding exact pyhon line of testing
    Jeong YunWon
    @youknowone
    pybool.rs for bool, pytype.rs for type