Skip to content
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

Calling a JS function with a dict in Brython 3.13 doesn't work. #2542

Open
goffi-contrib opened this issue Jan 23, 2025 · 2 comments
Open

Calling a JS function with a dict in Brython 3.13 doesn't work. #2542

goffi-contrib opened this issue Jan 23, 2025 · 2 comments

Comments

@goffi-contrib
Copy link
Contributor

Hello,

I'm using tippy.js with Brython in my project. Here is how I do a typical call:

            tippy(
                reaction_btn,
                {
                    "trigger": "click",
                    "content": self.get_reaction_panel,
                    "appendTo": document.body,
                    "placement": "bottom",
                    "interactive": True,
                    "theme": "light",
                    "onShow": lambda __, message_elt=message_elt: (
                        message_elt.classList.add("has-popup-focus")
                    ),
                    "onHide": lambda __, message_elt=message_elt: (
                        message_elt.classList.remove("has-popup-focus")
                    ),
                },
            )

https://repos.goffi.org/libervia-web/file/5d9889f14012/libervia/web/pages/chat/_browser/__init__.py#l542

So I'm using a python dict for calling the JS function. This was working well until now, but with Brython 3.13, it's not working anymore, and the result is as if I were using and empty dict, the dict to JS object conversion doesn't seem to work as expected.

For the context, here is how I import tippy:

from browser import document, load, window

document.head.insertAdjacentHTML("beforeend", '<link rel="stylesheet" type="text/css" href="/__b/node_modules/tippy.js/dist/tippy.css">')
document.head.insertAdjacentHTML("beforeend", '<link rel="stylesheet" type="text/css" href="/__b/node_modules/tippy.js/themes/light.css">')
document.head.insertAdjacentHTML("beforeend", '<link rel="stylesheet" type="text/css" href="/__b/node_modules/tippy.js/themes/light-border.css">')
load("/__b/node_modules/@popperjs/core/dist/umd/popper.min.js")
load("/__b/node_modules/tippy.js/dist/tippy-bundle.umd.min.js")
tippy = window.tippy

I can make is work if I do that:

            args = window.Object.new()
            args_dict = {
                    "trigger": "click",
                    "content": self.get_reaction_panel,
                    "appendTo": document.body,
                    "placement": "bottom",
                    "interactive": True,
                    "theme": "light",
                    "onShow": lambda __, message_elt=message_elt: (
                        message_elt.classList.add("has-popup-focus")
                    ),
                    "onHide": lambda __, message_elt=message_elt: (
                        message_elt.classList.remove("has-popup-focus")
                    ),
                }
            for k, v in args_dict.items():
                args[k] = v

            tippy(
                reaction_btn,
                args,
            )

So it seems that something has changed in Brython Python dict => JS object conversion which breaks the use of Python dict directly in JS function.

Thanks!

@goffi-contrib goffi-contrib changed the title Calling a JS function with a dict in Brython 3.13 doesn't work. Calling a JS function with a dict in Brython 3.13 doesn't work. Jan 23, 2025
@goffi-contrib
Copy link
Contributor Author

goffi-contrib commented Jan 23, 2025

Note: As a workaround, I can do that for the moment:

from javascript import pyobj2jsobj
from js_modules.tippy_js import tippy as tippy_ori

def tippy(target, data):
    return tippy_ori(target, pyobj2jsobj(data))

But:

  1. pyobj2jsobj is not documented at https://brython.info/static_doc/3.13/fr/javascript.html.
  2. It would be more dev-friendly if conversion was done automatically, like it used to be.

@PierreQuentel
Copy link
Contributor

Bonjour Jérôme,

There hasn't been a change (at least, volontary...) to modify this feature in version 3.13, arguments passed to a JS function as a dictionary should still be translated to Javascript objects.

I tried to reproduce the bug, but in this example the data passed as a dict is correctly handled by tippy

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython.js"
        crossorigin="anonymous">
</script>
<script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython_stdlib.js"
        crossorigin="anonymous">
</script>

</head>
<body>

<button id="myButton">My button</button>

<script type="text/python" debug="2">
from browser import window, load

# use "browser.load" as in the bug report. Files stored in subdirectory "tippy"
load('tippy/popper.min.js')
load('tippy/tippy-bundle.umd.js')

window.tippy('#myButton', {'content': 'My Brython tooltip !'})
</script>

</body>
</html>

It is the same with the development version and with the latest stable release 3.13.0.

I tried to load the whole libervia project but couldn't make it work to reproduce the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants