Python to JS Trouble

After much agony and help from others in this forum, I have successfully created my experiment in the builder. I have multiple code components in the experiment, most of which are unfortunately somewhat complicated. I am using Psychopy v2020.1.3, and using the Auto → JS code in all cases. I’m pretty sure that the translation to JS is the issue, but I don’t know how to code in Python or JS, so I’m kind of stuck here.

I’m uploading pictures of my current code components with some notes, which will hopefully be helpful. I can upload a link to the experiment as well if needed.







I’m hoping that the ones that don’t work are easy fixes! I’ve read through the forums on loops not working/not breaking for online experiment, but unfortunately I wasn’t able to get anything working on my own. I’m assuming that the ones I don’t yet know about (since the experiment doesn’t progress that far on Pavlovia) won’t work the way they are either.

Any help is greatly appreciated!

Some of the answers are on my crib sheet.

At the moment I’m solving most issues by:

  1. Changing the way I write my Python code (including avoiding imports).
  2. Adding JavaScript definitions and functions to a code_JS component (for which I have to switch to JS, or both if it includes colour definitions).
  3. Changing .append to .push wherever it appears (for which I have to switch to Both).

Specifically, how about trying len(Email_Response.keys)==0 instead of not Email_Response.keys?

Best wishes,

Wakefield

1 Like

Thank you, I will definitely take a look into this and see what I can do!

I have attempted to use len(Email_Response.keys)==0 as suggested, but I get an error.

TypeError: Cannot read property ‘length’ of undefined

I assume I somehow need to define Email_Response, but I’m not sure how or where.

In general, you’ll need to set your Code Type to ‘Both’ instead of ‘Auto>JS’ to modify the auto-translation. The problem is that that setting is the default, every time you close the code snippet it will go back to it and you need to be careful that it doesn’t accidentally overwrite the correct code.

For the text input, go to the text input demo and use their Javascript code (Sign in · GitLab) > download the whole demo, open it in builder and take those code snippets.

For adding data, you’ll need

psychoJS.experiment.addData

Are you using a rating scale in your code? From what I understand (although maybe it has changed by now), rating scales aren’t implemented yet on Pavlovia

Finally, with regards to your if conditions in general, I found that JS doesn’t like this:

if (((Popup_Presence ===1) && Email_Response.keys))

It wants:

if (Popup_Presence ===1 && Email_Response.keys)

So different ways of putting brackets.
Hope this helps a little. I feel your pain, it took me 4 weeks in total to get my study up in running in builder and online.

So I was actually able to figure this out (with some help from the people above)! Here is a summary of what I had to do (referencing the initial pictures I posted) for anyone else who needs:

  1. Picture 2: I added “continueRoutine=False” to the first IF statement before the nReps line. The auto to JS translated fine.
  2. Picture 3: This worked fine, no adjustments needed.
  3. Picture 4: “event.clearEvents” manually changed to “psychoJS.eventManager.clearEvents”
  4. Picture 5: “event.getKeys” manually changed to “psychoJS.eventManager.getKeys”
  5. Picture 6: “thisExp.addData” manually changed to “psychoJS.experiment.addData”
  6. Picture 7: “Popup_Loop.finished” manually changed to “trials.finished”
1 Like

Steps 3-5 can be solved using a couple of definitions in a code_JS component in the first routine. I keep manual translations to a minimum.