# How to store image file paths on Pavlovia

**URL:** <https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698>\
**Category:** Online experiments\
**Tags:** images, pavlovia\
**Created:** [November 3, 2019, 1:44am UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698 "2019-11-03T01:44:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwhenry28](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jwhenry28](https://discourse.psychopy.org/u/jwhenry28)\
**Post date:** [November 3, 2019, 1:44am UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/1 "2019-11-03T01:44:56Z")

</div>

If this template helps then use it. If not then just delete and start from scratch.

**OS** : Win10  
**PsychoPy version** : 3.2.3  
**What are you trying to achieve?:**  
Export my code to HTML. I am specifically getting tripped up by the following code (shortened to 3 items for simplicity):

```auto
// Import stim file manually as JSON
imageStims = ['images/FRI_01.jpeg', 'images/FRI_02.jpeg', 'images/FRI_03.jpeg']

```

**What did you try to make it work?:**  
My original Python code does this using xlrd. The image file names are stored in an excel document,  
but I could not figure out how to do this with JavaScript (I am very new to JS). So I created an array with each image path name and stored it like you see above. However, this generates the following error message in the terminal when I click “Export to HTML”:

```auto
ERROR path is on mount "\\\\ Import stim file manually as JSON\nimageStims = ['images\\FRI_01.jpeg']\n\n", start on mount 'C:'

```

I have tried reformatting the JSON with double/single quotes and with/without a semicolon at the end, as I saw JSON’s formatted both ways on the internet. No avail.

What should I do? Thank you in advance!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [November 3, 2019, 9:12pm UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/2 "2019-11-03T21:12:22Z")

</div>

Hi @jwhenry28, if you are using Builder to create your experiment, you can use a loop to handle your image files for each trial. You would use 1 row for every repeat of the routine, and each row would contain the image filenames that you need - you can use multiple columns for multiple images on each trial. You can refer to each image using the column name. In addition, PsychoPy recognises your images in your conditions files as a resource, and moves them to the resources folder, which gets uploaded to Pavlovia when you sync your task. See the [PsychoPy Stroop tutorial](https://www.youtube.com/watch?v=VV6qhuQgsiI) for the basics of building a task using Builder.

---

<div class="post-metadata">

**Author:** ![jwhenry28](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jwhenry28](https://discourse.psychopy.org/u/jwhenry28)\
**Post date:** [November 5, 2019, 12:43am UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/3 "2019-11-05T00:43:19Z")

</div>

Thanks for the info. But this does not help my problem of failing to export to HTML. My python code does just as you described. I cannot figure out how to transcribe it to JavaScript. Here is my python code:

```auto
# access the stim file
inbook = xlrd.open_workbook(infile)
insheet = inbook.sheet_by_index(0)

# four categories of stimuli
cate1 = [] 
cate2 = [] 
cate3 = [] 
cate4 = [] 

# images are displayed on left or right of screen
leftImages = []
rightImages = []

# read the stimuli from excel sheet
threshhold = int(numItems / numCates)

for rowx in range(numItems):
    
    # read in the row
    row = insheet.row_values(rowx)
    
    # save image filepath to appropriate list
    # images must be grouped in a single column
    # in category order to work
    if (rowx < threshhold):
        cate1.append(row[0])
    elif (rowx < threshhold * 2):
        cate2.append(row[0])
    elif (rowx < threshhold * 3):
        cate3.append(row[0])
    else:
        cate4.append(row[0])

```

How can this be written in JavaScript? I cannot seem to find good documentation for working with Excel documents in JS.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [November 5, 2019, 10:28am UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/4 "2019-11-05T10:28:39Z")

</div>

There are two methods for working with Excel documents in the attached link:

> [@How to directly read file on pavlovia](https://discourse.psychopy.org/t/how-to-directly-read-file-on-pavlovia/7997/2):
>
> 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\<header…

You can use the first code I posted in that link, which is a custom function for reading xlsx/csv. Alternatively, you can use the TrialHandler code for reading xlsx/csv files. The rest of the code would look something like:

```javascript
// four categories of stimuli
cate1 = [];
cate2 = [];
cate3 = []; 
cate4 = []; 

// images are displayed on left or right of screen
leftImages = [];
rightImages = [];

// read the stimuli from excel sheet
threshhold = Math.round(numItems / numCates);

for (let rowx of [...Array(numItems).keys()]) {
    
    // read in the row
    row = NEW_JS_OBJECT_CONTAINING_DATA(rowx) // This will be your conditions file
    
    // save image filepath to appropriate list
    // images must be grouped in a single column
    // in category order to work

    if (rowx < threshhold) {
        cate1.push(row[0])
    } else if (rowx < threshhold * 2) {
        cate2.push(row[0])
    } else if (rowx < threshhold * 3) {
        cate3.push(row[0])
    } else {
        cate4.push(row[0])
    }
}

```

---

<div class="post-metadata">

**Author:** ![jwhenry28](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jwhenry28](https://discourse.psychopy.org/u/jwhenry28)\
**Post date:** [November 5, 2019, 7:14pm UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/5 "2019-11-05T19:14:22Z")

</div>

I am getting a similar error from the first solution:

```auto
168.5712 ERROR path is on mount "\\\\ Import stim file manually as JSON\nresult = [];\nfunction successFunction(data) {\n var allRows = data.split('\\n'); ", start on mount 'C:'

```

For the second solution, would I just replace `NEW_JS_OBJECT_CONTAINING_DATA` with imageStims from the code I posted in my original query?

---

<div class="post-metadata">

**Author:** ![jwhenry28](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jwhenry28](https://discourse.psychopy.org/u/jwhenry28)\
**Post date:** [November 5, 2019, 7:27pm UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/6 "2019-11-05T19:27:13Z")

</div>

Weirdly, deleting the comment at the top of the code solved the error. However, it appears that the resources are nowhere present:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/1/a1f319154b9dc3de88ecb890f5aef57978116a78.png)  
What happened to my Excel file?

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [November 5, 2019, 8:43pm UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/7 "2019-11-05T20:43:39Z")

</div>

Would mind sharing the link to your task? Not sure why you were getting the mount error, but can have a look if you share the link.

---

<div class="post-metadata">

**Author:** ![jwhenry28](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jwhenry28](https://discourse.psychopy.org/u/jwhenry28)\
**Post date:** [November 6, 2019, 5:44am UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/8 "2019-11-06T05:44:43Z")

</div>

[Here](https://gitlab.pavlovia.org/jwhenry28/curiosity-and-personality/tree/master/html) is the link to my repository.

I actually fixed the error, but my images don’t display. Additionally I was given the following code (in python) to add variables and values to the output csv file that prints at the end of an experiment:

```auto
thisExp.addData("foo", bar)

```

Which adds value bar to column foo. How can this be done with Javascript? I need to be able to keep track of the images that were displayed as they are random every time.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [November 6, 2019, 11:39am UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/9 "2019-11-06T11:39:49Z")

</div>

Ok I will have a look.

To save data with PsychoJS, you would use:

```javascript
psychoJS.experiment.addData("foo", bar);

```

Seems to upload ok for me, although there is the “thisExp” issue which is fixed with the code in this post.

---

<div class="post-metadata">

**Author:** ![jwhenry28](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jwhenry28](https://discourse.psychopy.org/u/jwhenry28)\
**Post date:** [November 10, 2019, 6:56pm UTC](https://discourse.psychopy.org/t/how-to-store-image-file-paths-on-pavlovia/9698/10 "2019-11-10T18:56:50Z")

</div>

Thanks. Was able to upload. However, I still am having all kinds of issues with the JS variables. Certain variables keep popping up as undefined even when I’ve clearly defined them in a code block (without using let or var). Is there any official documentation for this process that I’ve missed? All I’ve been able to find is [this](https://www.psychopy.org/PsychoPyManual.pdf) which doesn’t mention any of the advice you have given me so far.

How/where do I need to declare my variables so that they are accessible by all code chunks? Currently, it looks like anything that I declare that is not in a “Begin Experiment” tab is unable to be used by other chunks. However, I need to initialize several variables to values that are manipulated during the routine. My git code is [here](https://gitlab.pavlovia.org/jwhenry28/curiosity-and-personality).

Feel free to send me to documentation instead if there is any.
