Reading/Writing Excel Files (Pavlovia)

URL of experiment:
https://run.pavlovia.org/AEastwood07/iat/html/

Description of the problem:
I’ve read through several discourse threads and haven’t been able to find a problem that is similar to mine. I have a task that writes an excel file using some python code which is then later called as a conditions file in the psychopy builder. The task itself is made up of two phases (learning and testing). In the learning phase, participants are introduced to two personas each with an associated face stimuli and some text information. The testing phase uses the exact same images in an implicit association task (IAT). In order to present a randomised pair of stimuli in the learning phases and the exact same stimuli in the testing phase I have used inline code to write the conditions file for each block of the IAT. I’ve used the following code to produce the condition files:

import openpyxl
import random

image_list = ['stimuli/female1.jpg','stimuli/female2.jpg','stimuli/female3.jpg',
    'stimuli/female4.jpg','stimuli/female5.jpg','stimuli/female6.jpg',
    'stimuli/female7.jpg','stimuli/female8.jpg','stimuli/female9.jpg',
    'stimuli/female10.jpg','stimuli/female11.jpg','stimuli/female12.jpg',
    'stimuli/female13.jpg','stimuli/female14.jpg','stimuli/female15.jpg',
    'stimuli/female16.jpg','stimuli/female17.jpg','stimuli/female18.jpg',
    'stimuli/female19.jpg','stimuli/female20.jpg','stimuli/female21.jpg',
    'stimuli/female22.jpg','stimuli/female23.jpg','stimuli/female24.jpg',
    'stimuli/female25.jpg','stimuli/female26.jpg','stimuli/female27.jpg',
    'stimuli/female28.jpg','stimuli/female29.jpg','stimuli/female30.jpg',
    'stimuli/female31.jpg','stimuli/female32.jpg','stimuli/female33.jpg',
    'stimuli/female34.jpg','stimuli/female35.jpg']

# randomise image list
random.shuffle(image_list)

# assigning row to variable
off_stim = image_list[0]
non_off_stim = image_list[1]

replacement = {'off': off_stim, 'control': non_off_stim}

# cong_test
wb = openpyxl.load_workbook("Templates/cong_test.xlsx")  # !!! Adjust file name !!!
for ws in wb.worksheets:
    # Iterate over the columns and rows, search for the text and replace
    for row in ws.iter_rows():
        for cell in row:
            if cell.value in replacement.keys():
                cell.value = replacement.get(cell.value)
wb.save("cong_test.xlsx")

# cong_train
wb = openpyxl.load_workbook("Templates/cong_train.xlsx")  # !!! Adjust file name !!!
for ws in wb.worksheets:
    # Iterate over the columns and rows, search for the text and replace
    for row in ws.iter_rows():
        for cell in row:
            if cell.value in replacement.keys():
                cell.value = replacement.get(cell.value)
wb.save("cong_train.xlsx")

# incong_test
wb = openpyxl.load_workbook("Templates/incong_test.xlsx")  # !!! Adjust file name !!!
for ws in wb.worksheets:
    # Iterate over the columns and rows, search for the text and replace
    for row in ws.iter_rows():
        for cell in row:
            if cell.value in replacement.keys():
                cell.value = replacement.get(cell.value)
wb.save("incong_test.xlsx")

# incong_train
wb = openpyxl.load_workbook("Templates/incong_train.xlsx")  # !!! Adjust file name !!!
for ws in wb.worksheets:
    # Iterate over the columns and rows, search for the text and replace
    for row in ws.iter_rows():
        for cell in row:
            if cell.value in replacement.keys():
                cell.value = replacement.get(cell.value)
wb.save("incong_train.xlsx")

The logic here is I have several template excel condition files with ‘place-holder’ labels in particular cells. I then use a find and replace function to replace the placeholder with a randomised stimuli. This works locally on PsychoPy but obviously leads to the ‘initialising the experiment’ screen because of the use of:

import openpyxl
import random

Which of course don’t translate to JS. My question is, are there any other methods of similarly updating the conditions files for each participant using JS so that my task works on Pavlovia? I’m drawing a bit of a blank and am wondering if there is a far simpler solution. For context there is a list of 35 face stimuli, and I have repeated the above process for name stimuli (total of 50 names). I’m sure I’ve made this a lot harder than it probably needed to be…

Hope this makes some kind of sense and thanks all in advance,

Andy

You ave a try like this:
with open(“txt”,w)

Hi @wen

If you are able to create a simple demo which saves some user data to the Pavlovia server mid experiment and then reads it in again later, I would LOVE to see it.

In the case of Pavlovia Server, using the import method is not recommended because it often does not compile to JavaScript so I recommend using Numpy as the basis for data processing。

I can give it a try