psychopy.org | Reference | Downloads | Github

Translating Python code into JS to avoid being stuck at 'initializing'

Description of the problem:

I have inserted a line of code in one of my text components to show participants their progress, i.e. how many more sentences to go. Works great in Builder, but not online, as the experiment gets stuck at ‘initializing’. I understand that JS has trouble with python f string format, so I’ve also tried .format and %, but neither seems to work online.

This is the code:

$f'Off to sentence {trials_2.thisN+2} of {trials_2.nTotal}'+"\n"+"\n"+'(press any key to continue)'

Any ideas?

Yes, f strings are a Python thing and the autotranslate doesn’t know how to handle those yet (it will do one day but it’s non-trivial).

For simple format conversion you could jsut use str() in both cases I believe

$'Off to sentence '+str(trials_2.thisN+2)

but google says the closest alternative to f-strings in JavaScript are Template strings: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Thanks @jon for your quick response. The experiment now gets passed the initializing stage, but as soon as the component is up, I receive the error message:

ReferenceError: str is not defined

try String() instead of str()

2 Likes

Thank you @jonathan.kominsky,
Doesn’t work in Builder now (cause now String is not defined), but it works online. Guess you gotta choose between the devil and the deep blue sea.

Interestingly, trials_2.thisN remained a fixed number, i.e. it wasn’t updated every repeat (although I’ve selected that option), but just gave me the overall count. I put in a myCount variable that I specified in the Code component instead. Works great.

Thanks again

1 Like