Error: no callback given (argument #1)

124 views
Skip to first unread message

Josh Gates

unread,
Jul 28, 2016, 10:24:27 PM7/28/16
to Glowscript Users
Has anyone seen this Glowscript error?

Error: no callback given (argument #1) If you're a streamline user, more info: github....

I'm getting this from a user-defined function. Code here: 

Any ideas?

Thanks,
jg

Bruce Sherwood

unread,
Jul 28, 2016, 11:09:21 PM7/28/16
to Glowscript Users
The problem is this, which is hard to document and hard to explain:

JavaScript does not support infinite loops, so the typical VPython program containing a "while True:" loop is impossible in JavaScript. Such a loop locks up the browser, as the browser cannot interrupt a running JavaScript program and therefore cannot create a display nor handle events. 

Part of the process in preparing a GlowScript program to run, whether written in JavaScript or Python (through the RapydScript transpiler), it is passed through the Streamline library, which uses a "wait" keyword as a signal to move the contents of a loop into a separate function that is scheduled to be called "asynchronously" at a future time determined by the rate value.

In a JavaScript program you have to write "rate(100, wait)" but in a VPython program your "rate(100)" is converted to "rate(100, wait)". Similarly, when you call a function f(x) that contains a rate statement, f(x) is converted to f(x, wait), and in certain circumstances it is possible to have a loop containing a rate in your own function, though I'm not clear under what circumstances this will work. Streamline cannot handle all situations where it is called upon to convert synchronous code to asynchronous code.

However, in your case the fix is very simple: Just delete the rate statement in your function, as it is not needed. The rate statement is needed only when updating of the screen is to occur during the loop. In your case, without the rate statement, the function will quickly do all the calculations and then exit, at which time the browser can update the screen.

You need to change np.arange to arange. The Python numpy package is not available in the JavaScript environment, but in the GlowScript environment arange and range are treated the same, with the behavior of numpy arange rather than with the restricted behavior of Python range.

You can see why it's been difficult to see how to document this......

Josh Gates

unread,
Jul 28, 2016, 11:17:16 PM7/28/16
to Glowscript Users
Thanks for the help, Bruce. I've been porting this thing from classic VP to Jupyter and now to GS, so I hadn't caught that yet! Easy enough, though - your rate fix works. I have another program, though, where an orbital motion happens during the loop, and which needs to be written to the screen. Do I just have to remove the rate and wait until it's over? No way to see it happen?

jg

Bruce Sherwood

unread,
Jul 29, 2016, 10:18:23 AM7/29/16
to Glowscript Users
There are two options in more complex situations like the one you mention. One is to write asynchronous code. Here's an example:


The statement "rate(200, move)" requests that the "move" function be executed again 1/200th of a second from now, and the browser can make changes to the page during the time between the current execution of "move" and the next.

Another possibility is to have just one loop, not in a function, which does different things depending on a state variable you establish.

The situation in GlowScript VPython is that for simple programs it's possible to write synchronous code despite the fact that one cannot write synchronous programs in JavaScript, which is what VPython compiles to, by the strategem of Streamline rewriting your program to be asynchronous code. This is a big win, especially for nonexpert programmers. But if you want to do certain kinds of complex programming you may have to abandon the synchronous programming scheme.
Reply all
Reply to author
Forward
0 new messages