grszkth.fr blog


PsychoPy as a formr survey

Published: 26 Aug 2020
Updated: 27 Aug 2020

This is a quick write up, how I set up an online experiment created with PsychoPy1 as a survey in formr.

You want to do this, because PsychoPy is not made for questionnaires or forms, but formr is. However, formr is not made for experiments.

In a lot of studies you have both. So friends of FOSS, and really anybody else I highly recommend to use formr and PsychoPy together.

Getting started in formr 🔗

In this approach formr is in charge. The experiment by PsychoPy is only a survey in a bigger formr run2.

Here our experiment is the second survey of a run.

After completing the first survey (in my case a consent form), formr sends participants to the external link.

See the {{login_code}} part? This is replaced by formr with a unique code of the participant and assigned to code.

You wouldn’t guess it, but PsychoPy is able to pick up this code. But only after some tweaking.

PsychoPy takes over 🔗

In PsychoPy, go to Experiment Settings and add a Field named code.

Now, instead of an info dialog field for participant input, code is read from the URL. Participants won’t be able to change it easily.

Default can be empty, as it will be overwritten by the formr code anyway. Be aware, that this works only for participants who were sent by formr to the experiment. Others get an info dialog presented and can choose their code freely.

That’s it! PsychoPy not only knows who is coming, but it automatically stores code in the data file.

For convenience, however, I assign the first 6 characters of the code to PsychoPy’s internal variable participant. To do so I use a Code component3 at the beginning of the experiment:

expInfo['participant'] = expInfo['code'].slice(0, 6);

The variable participant is comparable to formR’s variable login_code. Both are preset and used for some internals. I want participant shorter because it is used for the file name in PsychoPy, and the original login_code is really really long!

Back to formr 🔗

Now you might want to return to some questionnaires on formr.

To return from the online experiment to formr we can use PsychoPy’s ability to redirect. Normally this is set in the “Online” tab in the experiment settings.

However, the assignment of the variables seems to happen before code from the URL is read. I, once again, use a Code component to reassign the relevant variables, this time at the end of the experiment:

psychoJS.setRedirectUrls(
    // complete
    ('https://ss20empra.formr.org/?code=' + expInfo['code']),
    // incomplete
    ('https://ss20empra.formr.org/'));

Note, this is not necessary as formr recognizes participants with cookies.

However, this gives you more control. It let’s PsychoPy speak to formr. For example, now you can check whether participants completed the experiment or not (see the google sheet how this is done in formr).

In my case, I rather impolitely asked participants to start over again if they visit the online study site a second time but without a code. I assume they did not complete the experiment. So I ask formr to log them out:

Wrap up 🔗

This is a very basic setup to include a PsychoPy online study as a survey into a formr run and identify participants regardless.

Note that this setup has a lot of potential for fine tuning. One can easily give and take multiple variables4 in either direction, to/from formR/PsychoPy.

In my case, returning a complete variable instead of code might be a good idea with some jumping rules within formr. By that participants escaping the experiment can be asked for reasons.

Or you can return some performance measures back to formr, there is a lot of potential here!

Hope you find this helpful!

By the way: Here is a link to a study of mine where I did all this. The json-file is exported from formr. The accompanying Pavlovia repository is linked as well. With these two, the complete study could be recreated. Please give credit, if you do. :hugging_face:

By the way 2: I take advantage of URL parameters here. For further reading into the topic I recommend to have a look at PsychoPy’s official documentation or formr’s examples.


  1. Handling names with PsychoPy and its ecosystem is not easy! Note, that I refer to PsychoPy as the builder, who in the end renders JavaScript code for online experiments. ↩︎

  2. What is a run? What is a survey? Get familiar with formR’s concepts here↩︎

  3. Code components for online studies require code in JavaScript. ↩︎

  4. Just by adding multiple ?code=abc&var1=xyz&var2=123 to the URL. ↩︎