Community Health Toolkit

Pulling data from a different file

Hi everyone,

I was wondering how one would approach to use the XPath function pulldata() to extract data from an external csv file. I know how to use the function, but trying this week I just do not seem to be able to have the csv file in the application, as I always get the error “External instance ““MyFile”” is empty”. Uploading forms is very easy with your provided CLI. How would I proceed in order to have the csv file recognized and accessed by the form?

1 Like

I managed to upload the csv file as an attachment to the form. I have pulldata(my-csvfile, column needed, key column, key) but it does not seem to find the file still. Is there a way to refer to the csv in the attachments in the pulldata function ?

1 Like

Hi @magp18. Are you using the CHT Core Framework webapp or medic-collect android app?

1 Like

Hi @gareth , I am using the CHT Core Framework

1 Like

An update on what I have tried:
I tried to do the same as in https://docs.communityhealthtoolkit.org/apps/guides/forms/multimedia/ for csv file, replacing form=“image” with form=“csv” and “file-csv” both didnt work… I did not have an error but I tried to show it as if I was to show an image which did not show anything. When I tried to access one cell of the csv file with pulldata (“pulldata(‘FishCalc’,‘lengthMultiplier’,‘code’,‘RBS’)”, where lengthMultiplier is the column which has the cell I want, the code is the column where RBS is, and specifies an unique row, from which I want the value) I get the formlogicerror instance “FishCalc” does not exist in model. I tried different things as e.g. count(instance(‘FishCalc’)/root/item[lengthMultiplier>1]) (would not give me the same value as above but the value I get does not really matter, I just want to make it work and retrieve something from the csv) which leads to another formlogicerror: “instance “FishCalc” does not exist in model”
Maybe I need to access the file differently?
Second approach:
I added to the xml file (dummy file). And supposedly I should be able to access it like so: “count(instance(‘FishCalc’)/root/item[lengthMultiplier>1])” with calculate. But here I get a load error saying that the file is empty and a formlogic error as the expression could not be evaluated. Is there maybe something to the path of how I can access it in the XML? Like the csv handler should be jr://file-csv/Path/tofile.csv maybe my path is wrong? should it rather be something like jr://file-csv/_attachments/FishCalc.csv because of having it attached to my form in the database?

I think this will need a modification to CHT Core. Some similar examples are the image loading code that you found, or the z-score xpath extension which loads the data for the calculation from the db.

I’m hoping to find some time to look into this further, but I’m not sure when that will be. Please update here as you find more. Also feel free to raise a feature request in github.

This is actually useful information. The ultimate goal would be to have some kind of lookup table, either as a csv file or in the database, that by having the weight and age of a patient (by filling out a form), could be evaluated to see what dosage the patient should be receiving. As I see it z-score is doing something similar right? If I would like to do the same but with a different table, if I understand it correctly I would have to upload a document with the table, analogous to the z_score, and then probably define a function with JavaScript, similar to z_score that can be called using the two parameters I mentioned and outputs the dosage. Am I correct?

Yes, essentially. My intention was to try and find a way to do that generically rather than specifically to any one dataset. It should be possible to provide it to the pulldata() function so it behaves as you’d expect.

Would you know how it would be possible to use pulldata to access the file? I have been trying but I am not able to make it work, as I always get an error that instance \“FishCalc\” does not exist in model\ with all my attempts, when I use FishCalc as path to file unless the path is different. I also tried to upload it as a jpg and try to access it and now I actually see something (a broken jpg symbol ) but tells me it is loading. But the problem remains to access the values in the csv with FishCalc as path to file, is there a different path to access it or where will it be stored?

I don’t think it’s possible in the CHT right now and will require some core code changes to intercept or override the pulldata function and retrieve the file from the database.