How to directly read file on pavlovia

URL of experiment: Cindy [PsychoPy]

Description of the problem: I need to parse a csv file directly to pull some experiment info (not condition files). I put this file in html/resources https://pavlovia.org/run/xfang/exp7/html/resources/tExp.csv
and tried var csvString = fs.readFileSync('resources/tExp.csv', 'utf-8'); but got error


I suppose resources are downloaded to a place during the preperation, where I am supposed to pull the file from, where is it then?

Hi @Xinzhu-Fang, try the following - it creates a list of JS objects (called result) with the contents of your csv file. Put this in a code component in the Begin Experiment tab in the JS code panel.

result = [];
function successFunction(data) {
    var allRows = data.split('\n'); // split rows at new line
    var headerRows = allRows[0].split(',');
    
    for (var i=1; i<allRows.length; i++) {
        var obj = {};
        var currentLine = allRows[i].split(',');
        for(var j=0;j<headerRows.length;j++){
              obj[headerRows[j]] = currentLine[j];
        }
        result.push(obj);
    }
}

$.ajax({
  url: 'resources/tExp.csv',
  dataType: 'text',
  async: false,
}).done(successFunction);

Using this will mean you do not need to import any external libraries.

1 Like

Thanks, @dvbridges Sorry for the late reply. I tried it and made some modifications besides var result = []; but it still didn’t work. Just came back from my vocation trying to catch up with my work, got a new error: TypeError: Cannot read property 'readPixels' of undefined

Hi @Xinzhu-Fang, the above code is tested and works ok for me, so perhaps something else is going wrong? Btw, you could also just use the trialHandler code for loading csv files. E.g.,

myList = new TrialHandler({
    psychoJS: psychoJS,
    nReps: 1, method: TrialHandler.Method.SEQUENTIAL,
    extraInfo: expInfo, originPath: undefined,
    trialList: "tExp.csv",
    seed: undefined, name: 'myList'});

aValue = myList.trialList[0]['variableName']  // Get value from row 0

Regarding the readPixels error, what is displayed when you run the following in your browser that runs your task?

https://www.khronos.org/registry/webgl/sdk/tests/conformance/more/functions/readPixels.html?webglVersion=1&quiet=0

1 Like

I am not sure what you mean by run the following. Run the code in console?


I am using chrome.

I meant to click the link, I was interested in the output considering your error. Judging by your screen dump, it looks like readPixels is working correctly as the test has passed.


here

I restarted my laptop, and the readPixels error is gone

1 Like