PsychoPy Python to Javascript crib sheet

Many people come to this forum because their experiment works fine locally but doesn’t work online. The reason is that the Python code needs to be translated to Javascript and the auto translate that was added to PsychoPy in January 2020 primarily translates the grammar rather than the individual functions.

If you want to run PsychoPy online I highly recommend that you:

  1. Write your experiment in Builder using version 2023.1.3 (or later).

  2. Study my crib sheet to familiarise yourself with the aspects that need manual translation or additional definitions.

  3. Where possible try to write the experiment so it works both locally and online. Some errors are easier to spot locally.

  4. Make suggestions to the crib sheet if you have a fix that can help others.

Older versions:

Best wishes,

Wakefield

Wakefield Morys-Carter MA (he/him)
Senior Psychology Demonstrator and Teaching Fellow
Oxford Brookes University
Treasurer and Membership Secretary
Association of Technical Staff in Psychology (ATSiP)

Vertical Enhancement of Statistics and Psychology Research
Twitter: @Psych_Stats #VESPRox
Resources: http://moryscarter.com/vespr/

17 Likes

I think it is super useful. I wish I had seen it before coding my first online experiment! :slight_smile:
I would also add that it is possible to test the online version on your local computer e.g. by means of IIS in Windows. And if you use Visual Studio Code, then there is an extension, which makes the process even easier: IIS Express.

3 Likes

@aisa2 has written a companion document for people needing help with getting started.

Hi @wakecarter @dvbridges
What would be the translation of the following code to JS?

minutes = int(timeRemaining/60.0)
seconds = int(timeRemaining - (minutes * 60.0))
timeText1 = str(minutes) + str(’:’) + str(seconds) + str(’ minutes’)

When I run it online it give the message that “minutes not defined”
Thanks

Hi,

This should auto translate in your code component. See below:

Try printing your minutes variable to check it looks as you expect - print(minutes)
Becca

Add print('timeRemaining: '+timeRemaining)

It might be you are trying to set minutes before timeRemaining has a value.

alternatively put minutes = 0 in a Begin Experiment tab.

Hello,

Thank you for uploading the crib sheet and the information relating to the conversion of Python to Java.

However, I seem to have been struggling to covert to following code from Python to Java, so I am unable to run the experiment on Pavlovia.

msg = “{}/{}”.format(trial.thisN+1, trial.nTotal)

I would be really helpful if you could help me.

Many thanks in advance.

Best wishes,
Lea

.thisN doesn’t work anymore so you need to use a dummy variable

Thank you very much!

Could you please explain to me what a dummy variable is?

Or could I ask you to show it to me using the script?

Many thanks in advance!

All the best,
Lea

Idx = 0 in a Begin Experiment code tab

Idx+=1 in End Routine in final routine within loop

Use Idx instead of trials.thisN

Many thanks!

I have tried that but something still seems to be unfortunately messy - it does not count the audio stimuli - please, see the picture below - the right bottom corner in blue:

image

If I may show you all my code and I ask you to comment on it:

BEGIN EXPERIMENT TAB:
Idx = 0

BEGIN ROUTINE TAB:
msg = “{}/{}”.format(Idx, zacvik.nTotal);

END ROUTINE TAB: Idx+=1

I would be really grateful if you could help me out.

Many thanks!
Lea

From my crib sheet:

Don’t
Use Python string formatting “my var = %s” % myVar

Do
Use string concatenation with type conversion “my var = “ + str(myVar)
Use str(int(myVar)) if you are getting .0 added to values that should be integers.

Thank you very much for your reply.

I am sorry for bothering you, however, this is all Greek to me.

I would really need to be shown how to rewrite the code so it counts all my stimuli within the loop on Pavlovia.

Many thanks!
Lea

msg = str(Idx)+'/'+str(zacvik.nTotal)

(if .nTotal works)

Thank you!

I have adapted the code accordingly.

Unfortunetely, running the experiment is not possible.

I have got the following error message:

How can this be fixed, please?

Many thanks,
Lea

I’m giving you Python code that needs auto translating. I’m not as comfortable with JS direct.

JavaScript is usually clever enough to turn numbers into strings without needing to use the equivalents of Python’s str() function (which I think are String() or .toString()). So try just dropping that from the code:

msg = Idx + '/' + zacvik.nTotal;

But as Wakefield notes, using the auto-translate feature from Python -> JavaScript will often go a long way to helping.

Many thanks!

It works!!!

Thank you very much!!!

It works!!!