Calling a Brython defined function from JS

79 views
Skip to first unread message

salman aslam

unread,
Jun 24, 2022, 4:50:28 PM6/24/22
to brython
Hello,

I am trying to invoke a function defined using Brython from a javascript script. It seems like the function is not defined at the time the document is ready but is after some time. See the example below where initially their is an undefined error stating that getTime is not defined but if I manually call getTime in the console or if it is triggered any time after, it is defined.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script
      integrity="sha512-LDmvEqUAHongGVdMnKgr2Wid1mkoi7gToSQQ8jO4DzO1s9nL9voMw1wqDOVEbjJQTgg2tWWtSRuty59RrA/Sew=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>
    <script
      integrity="sha512-FBqhztZDfTnU57jHB7Cc0gFSpm5Zsq1kmZFanaHOE791Y1rXt/teJDfazFC0TG3q8FCEwOn1bZs6IHB+BCS8HA=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>
    <script
      integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    ></script>
  </head>
  <body onload="brython()">
    <script type="text/python3">
      from browser import window
      import time

      def getTime():
        return time.time()

      window.getTime = getTime
    </script>
    <script>
      $(document).ready(() => {
        console.log("is ready");
        getTime();
      });
    </script>
  </body>
</html>


Thanks,
Sal

Pierre Quentel

unread,
Jun 26, 2022, 3:39:32 PM6/26/22
to brython
Hello,

It works for me on all browsers if you replace getTime() by window.getTime() in the Javascript program.

Edward Elliott

unread,
Jun 27, 2022, 3:12:12 AM6/27/22
to bry...@googlegroups.com
Original description sounds like expected behavior to me.  Brython runs as an onload event, while embedded js runs at document ready.  These occur at different times:

onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

The .ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate....
Note that although the DOM always becomes ready before the page is fully loaded,

So onload event fires after ready ().  But with differences between (non-chromium) browsers, this may not be true across all platforms.  Async execution is tricky.

Edward

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/6a559b65-c001-4eab-a749-b6fd3d02981en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages