Help using a library bundled up with OSWeb: csv-parse
Hi there,
In my quest to find a way to use csv files as a source for loops in OSWeb, I've been investigating different possible avenues. I just found out that OS includes a series of Javascript libraries, among which one called csvParse: https://osdoc.cogsci.nl/3.3/manual/javascript/about/
This sounds really exciting to solve my problem, but I just can't seem to get it to work. I tried the code appearing on the page above (I used a different csv file, but I made sure that file was in the pool):
const conditions = csvParse( pool['attentional-capture-jobs.csv'].data, {columns: true} )
I ran that in my browser but get an error message saying that csvParse is not defined. So I guess I somehow have to tell OS to load up some library I want to call csvParse. So I consulted the link appearing on the OS website: https://csv.js.org/parse/api/sync/#sync-apim and I tried preceding the above code with:
const csvParse = require('csv-parse/lib/sync')
... but get a message saying that "require" is not defined.
I thought it might be because I ran it in my browser from OS (though the documentation indicates that the csv-parse is a library bundled up with OSWeb, so that it should run fine when running my task in the browser from OS - perhaps my installation did not include it? How can I check?. So, instead I tried to run the task through JATOS (I run JATOS on my university's server). But I got the same error message. Searching the server, I could not locate anything that looks like the 'csv-parse" library. So I thought that maybe it's because the library is not actually included with JATOS. I therefore installed it on my server ("npm intall csv"). I located the sync library on my server and updated my code:
const csvParse = require('node_modules/csv-parse/lib/sync')
Here is the location of the sync.js on my server. Is the path above correct?
https://forum.cogsci.nl/uploads/930/YKOFG85NYXPI.pngHere is the OS task I tried:
I was wondering whether anybody out there could confirm whether my code is correct and a sample task that does work in OS when run through the browser, might why I get a "require" not defined error message and how to solve that, and whether I actually need to worry about installing csv-parse on my server etc...
Any help would be really appreciated! Thanks!
Fabrice.
Comments
Hi @Fab ,
This functionality is only available as of OSWeb 1.4. I suspect that you're using OSWeb 1.3. Is that correct?
You can get OSWeb 1.4. either by downloading the latest prerelease of OpenSesame 3.3.10 (which is what I would do), or by updating the OSWeb extension specifically. See also:
Once you've done that, you can use
csvParse()
directly, as shown in the docs. (You don't have to, and in fact can't, userequire()
in aninline_javascript
item in the same that you couldimport
a library in a Pythoninline_script
.)I hope this clears things up!
— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi @sebastiaan,
Many thanks for this! That's awesome! I just downloaded and installed the megapack for the latest prerelease of OpenSesame 3.3.10 and indeed the code works smoothly 😀.
The latest release and OSWeb 1.4 brings some really cool new feature!: In fact, I just I read that files are now supported as a source for loop, which is awesome! Now I no longer have to work out a way around that limitation in the previous version (in fact, I even no longer have to worry about parsing csv files for this purpose!).
I think that with this you solved not only this thread but also another two I recently posted (I added an update to these based on your reply).
Many thanks! Really cool! 😀👍️
Fabrice.