# Downloading resources in code

**URL:** <https://discourse.psychopy.org/t/downloading-resources-in-code/36260>\
**Category:** Online experiments\
**Created:** [August 30, 2023, 8:21pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260 "2023-08-30T20:21:45Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 30, 2023, 8:21pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/1 "2023-08-30T20:21:45Z")

</div>

Hi @wakecarter, you seem to know a lot about using resources in an online experiment. I would really appreciate if you could take a look at my problem below.  
I basically run an experiment that download at least 2000 videos my 2000 videos are located in 20 different subfolders which are located in 2 different folders. for instance, depending on the trial my participants are in it will show stimuli from different folders.

I have three different variables that keep track of which folder and subfolder. The first one is id\_group that stays constant (Stimuli/anger\_male\_/) second is id\_groups(1 & 2) and third is stim\_block (from 0 to 9) which can give something like this:

id\_group = 1 & stimblock = 0 then the output:

Stimuli/anger\_male\_0 which contains 50 videos from 0 to 49.  
The idea here is to download only the resources I need right before the trial starts to prevent any buffer problems or to have my participants wait forever while downloading the 2000 videos.

So I thought of coding something like this:

```auto
  id_group_temp = (id_group + id_group0);
var path_block = id_group_temp + stim_block.toString();

var resourcePaths = [
    {'name': path_block + '/test_trial_sample_0000.mp4', 'path': path_block + '/test_trial_sample_0000.mp4'},
    {'name': path_block + '/test_trial_sample_0001.mp4', 'path': path_block + '/test_trial_sample_0001.mp4'},
    // ... all the way up to ...
    {'name': path_block + '/test_trial_sample_0049.mp4', 'path': path_block + '/test_trial_sample_0049.mp4'},

  {'name': path_block + '/test_trial_anti_sample_0000.mp4', 'path': path_block + '/test_trial_anti_sample_0000.mp4'},
    // ... all the way up to ...
    {'name': path_block + '/test_trial_anti_sample_0049.mp4', 'path': path_block + '/test_trial_anti_sample_0049.mp4'}
];

psychoJS.downloadResources(resourcePaths);

```

I basically put this piece of code in the routing right before my trial loops start, in the same routine where the id\_group variable gets assigned.  
But nothing is being downloaded. Any idea on what I could be doing wrong?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [August 31, 2023, 7:17am UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/2 "2023-08-31T07:17:37Z")

</div>

From my [Crib Sheet](https://docs.google.com/document/d/1TSEgiG3SeGSr7LWoRahTZm7vk_loU8zKoDQh2sV1KFw/edit#heading=h.jjxqgpd2r1z5):

> Create a list of dictionaries, e.g. stimuli=;  
> stimuli.push({name:(filename),path: (filenameWithPath)});
> 
> psychoJS.\_serverManager.prepareResources(stimuli);
> 
> Use the filename without the path in the experiment components or (for local compatibility) put the filename with the path as the name in the list of dictionaries.

downloadResources was used in an earlier version. What version are you using?

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 31, 2023, 9:18am UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/3 "2023-08-31T09:18:07Z")

</div>

Thank you for your reply!

Could you please clarify what you mean here, please?  
“ Use the filename without the path in the experiment components or (for local compatibility) put the filename with the path as the name in the list of dictionaries.” I’m not quite sure to understand what the first part mean? Where should I put the path name then, for online compatibility?

Also, I’m using the latest version. You are right I should change it to

> psychoJS.\_serverManager.prepareResources(stimuli);

Also, I thought of implementing this code in the routine right before the trial\_loop starts. Do you think this is the right place to put this code snippet?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [August 31, 2023, 9:32am UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/4 "2023-08-31T09:32:35Z")

</div>

> [@frenchie4neuro](#):
>
> ```auto
> {'name': path_block + '/test_trial_sample_0000.mp4', 'path': path_block + '/test_trial_sample_0000.mp4'},
>     
> 
> ```

Here you are putting the path in the name and path fields. This is probably the best option, because it means that your experiment will look for the files in the same place when running locally as online. I think maybe I’ll simplify my crib sheet to encourage people to just use the full path both times.

I would recommend putting the code snippet in Begin Routine of your first routine, so that the resources can be downloading while the participant is reading the instructions.

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 31, 2023, 12:31pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/5 "2023-08-31T12:31:38Z")

</div>

I understand the rationale here. The way the experiment is defined right now is that all participants read the instructions and then they get assign to their respective block (id\_group\_temp). Would you recommend to put the instructions in the same routine as id\_group\_temp which should also contain the resource code snippet?  
The only reason I’m asking is because in order for the resource to be downloaded from the proper folders id\_group\_temp has to be assigned first.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [August 31, 2023, 1:08pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/6 "2023-08-31T13:08:53Z")

</div>

Is there any reason why you would prefer to assign them to their block after they have read the instructions?

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 31, 2023, 1:34pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/7 "2023-08-31T13:34:06Z")

</div>

Given that the instructions do not depend on which block they are assigned to, as every participant will complete the exact same task(just different stimuli will be provided). We thought it was easier to just put the instructions at the beginning.

Would you recommend that we change this? Or should I use another option to have them wait until the resources are being downloaded?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [August 31, 2023, 1:44pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/8 "2023-08-31T13:44:49Z")

</div>

> [@frenchie4neuro](#):
>
> Would you recommend that we change this?

Yes, because it means that the experiment is busy (downloading resources) while the participant is reading.

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 31, 2023, 2:20pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/9 "2023-08-31T14:20:04Z")

</div>

Ok let me try to change this. Also, I should implement those changes in the builder, right? Not in the Gitlab?

Should I also add the resources in the online settings under additional resources or it will make them download at the beginning of the experiment?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [August 31, 2023, 2:23pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/10 "2023-08-31T14:23:22Z")

</div>

Changes should always be make in Builder.

Since you are using code, you shouldn’t add the resources to Experiment Settings / Online since that will download all of them. In similar circumstances, I have added the resources for the practice trials there.

Also – it may be important to note that the resource that appears first in your list will be downloaded first, so you may want to ensure that the download order is the same as the presentation order.

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 31, 2023, 4:08pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/11 "2023-08-31T16:08:39Z")

</div>

![Screen Shot 2023-08-31 at 12.04.40 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/7/976aa341b36200982985893044e972d45e786239.jpeg)

Given that my instructions are split into two routines and that change\_id\_folder is the routine where id\_group\_temp is being assigned, where do you think I should implement the code for the resources to be downloaded so that it’s being downloaded in the background of displaying the instructions? Please note that trial\_2 routine is where the stimuli are being presented to the participants.

Thank you so much for your help so far.

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [August 31, 2023, 6:10pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/12 "2023-08-31T18:10:39Z")

</div>

I went ahead and tried implementing the code per your instruction, however, I’m getting this error

![Screen Shot 2023-08-31 at 1.59.46 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/8/988dd0bcce76d0a974ba3e317c6759c6d3c20db2.png)

I think the reason might be that I clicked on the spacebar to go to the following routine, and maybe the resources haven’t been downloaded, yet. Could that be possible? If yes, how do I prevent anyone from going to the next routine until all the resources have been downloaded?

I have attached the gitlab [URL](https://gitlab.pavlovia.org/ccnlab/class_motion_anger_new)

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [September 1, 2023, 9:33am UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/13 "2023-09-01T09:33:57Z")

</div>

To check if a resource is available you can add the following to a Please wait / Loading routine.

Pick the name of the last resource required for the next stage of the experiment.

See [Loading sounds on the fly - #15 by thomas\_pronk](https://discourse.psychopy.org/t/loading-sounds-on-the-fly/13393/15)

**Each Frame**

```auto
try {
psychoJS.serverManager.getResource(expInfo["participant"] + ".jpg");
continueRoutine = false;
} 
catch (e) {}

```

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [September 1, 2023, 11:23am UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/14 "2023-09-01T11:23:48Z")

</div>

The thing is the next resource available can be any of my 50 stimuli as this is randomly assigned from the excel condition file. So, I’m not sure which one will be next.

I have followed all the steps from this post

> [@Deal with condition files on psychojs/pavlovia](https://discourse.psychopy.org/t/deal-with-condition-files-on-psychojs-pavlovia/31114/10):
>
> Thank you so much! I finally got it to work, but also I struggled a bit so I figured I should share my journey here for other people’s reference. I created a routine called ‘loadResource’ in the very beginning and added the code psychoJS.\_serverManager.prepareResources in the begin Routine part. I have two kinds of resources to load: condition files and some images. Right off the bat the condition files work just fine, however, the images just wouldn’t work. I have a line: example\_image.setImage…

But, I keep getting the same errors. Any help would be appreciated. I’m losing hope…

Originally, when all the resources for the experiment were being downloaded at the beginning(~2000 videos), it wouldn’t take a very long time (5-7 min). But, some browsers (i.e., chrome) wouldn’t load any of them, it would just crashed. Firefox would load some of them but not all of them. Hence, why I wanted to dynamically download only necessary resources. I think it we’re having some buffer problem. So maybe there is an easier fix here…

---

<div class="post-metadata">

**Author:** ![frenchie4neuro](https://avatars.discourse-cdn.com/v4/letter/f/bc79bd/32.png) [@frenchie4neuro](https://discourse.psychopy.org/u/frenchie4neuro)\
**Post date:** [September 1, 2023, 7:14pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/15 "2023-09-01T19:14:29Z")

</div>

> [@Downloading resources in code](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/13):
>
> To check if a resource is available you can add the following to a Please wait / Loading routine. Pick the name of the last resource required for the next stage of the experiment. See [Loading sounds on the fly - #15 by thomas\_pronk](https://discourse.psychopy.org/t/loading-sounds-on-the-fly/13393/15) Each Frame try { psychoJS.serverManager.getResource(expInfo["participant"] + ".jpg"); continueRoutine = false; } catch (e) {}

For some reason nothing happens when I implemented the above code, any suggestions @thomas_pronk ?  
Upon checking my code, the stimuli array is correctly populated with my stimuli paths, everything works well. Except this line of code:

```auto
psychoJS._serverManager.prepareResources(stimuli);

```

It doesn’t download anything at all.

---

<div class="post-metadata">

**Author:** ![rkempner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rkempner/32/13432_2.png) [@rkempner](https://discourse.psychopy.org/u/rkempner)\
**Post date:** [April 2, 2024, 2:43pm UTC](https://discourse.psychopy.org/t/downloading-resources-in-code/36260/16 "2024-04-02T14:43:09Z")

</div>

Hi @wakecarter

I had been using this code

```auto
stimuli=[];
stimuli.push({name:(filename),path: (filenameWithPath)}); psychoJS._serverManager.prepareResources(stimuli);

```

and it was working great back in December. Now it breaks and gives the error in the photo. Do you know if something changed with pavlovia in the last few months to make this not work anymore?

 ![Screen Shot 2024-04-02 at 10.42.36 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/d/9d2d37e92066b5fe4936c7f67692f0010f76f451.png)
