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

**URL:** <https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649>\
**Category:** Online experiments\
**Created:** [March 22, 2020, 8:49pm UTC](https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649 "2020-03-22T20:49:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Daugs](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/robert_daugs/32/6704_2.png) [@Robert\_Daugs](https://discourse.psychopy.org/u/Robert_Daugs)\
**Post date:** [March 22, 2020, 8:49pm UTC](https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649/1 "2020-03-22T20:49:45Z")

</div>

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:

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

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [March 22, 2020, 9:00pm UTC](https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649/2 "2020-03-22T21:00:26Z")

</div>

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

```auto
$'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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

---

<div class="post-metadata">

**Author:** ![Robert\_Daugs](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/robert_daugs/32/6704_2.png) [@Robert\_Daugs](https://discourse.psychopy.org/u/Robert_Daugs)\
**Post date:** [March 23, 2020, 12:45pm UTC](https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649/3 "2020-03-23T12:45:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)\
**Post date:** [March 23, 2020, 1:03pm UTC](https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649/4 "2020-03-23T13:03:40Z")

</div>

try String() instead of str()

---

<div class="post-metadata">

**Author:** ![Robert\_Daugs](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/robert_daugs/32/6704_2.png) [@Robert\_Daugs](https://discourse.psychopy.org/u/Robert_Daugs)\
**Post date:** [March 23, 2020, 8:10pm UTC](https://discourse.psychopy.org/t/translating-python-code-into-js-to-avoid-being-stuck-at-initializing/11649/5 "2020-03-23T20:10:38Z")

</div>

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
