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 Including Multimedia in Forms | Community Health Toolkit 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.

Thank you, interestingly I tried the command with the url but it didn’t succeed, let me try again with the url, username and admin password
Thank you

A partner has shared this question in regards to referencing external data in forms. The partner would like to create a single form for a CHW/user with multiple pages for vaccination decisions, this form is supposed to have a set of external parameters prefilled which can be set at a device level once.

These are some of the options the partner is considering:

  • If the parameters are stored in CSV, can we leverage XLS form pull data feature and does CHT support this?
  • Is there any other way to store these parameters as settings which can be used in the CHT form?
  • Is it possible to create a user in the hierarchy such as an Admin who can update this information once and the information will be available when a CHW creates a new form for vaccination decision

Happy to hear your thoughts @magp18, @diana, @jkuester and @Babacar_Gueye

Hi @antony

What kind of parameters are we talking about? Large lists? A couple of values?

If the parameters are stored in CSV, can we leverage XLS form pull data feature and does CHT support this?

As @gareth already mentioned, I don’t think CHT supports pull data.

Is there any other way to store these parameters as settings which can be used in the CHT form?

The user-settings doc is loaded into every form, so if we’re talking about a few parameters, they can be saved in the user-settings doc to be used in any form.

Is it possible to create a user in the hierarchy such as an Admin who can update this information once and the information will be available when a CHW creates a new form for vaccination decision

Are you talking about updating via the UI? Or via cht-conf?
For z-score, which I think would work similarly to this request, updates are made with cht-conf.

1 Like