Use pyODK to upload and publish media files?

I realise that I'm trying to fly before I can crawl, but I'm wondering if there is a way of updating and publishing media files using pyODK and/or via the API? I can't see anything documented, but it would be a great way for me to be able to automate a work-flow.

In my case I have a select_one_from_file (external CSV dataset) and the list frequently needs to be updated - it relates to 'live' projects where we are using the same form for different sites / clients. Having a consistent name for the project / site between enumerators helps to filter the dataset!

At present may work flow looks something like: update / create the csv (which I actually export from our projects database), log into Central, navigate to the form, create a new draft, drink coffee, upload the csv and publish the draft. Central then propagates the new version to the devices... doesn't sound much, but being able to do that more simply / quickly would be very welcome - and save the complaints about the list not being up to date!

I think this might also be useful for updating entities that have not been created by ODK? We often get given datasets and are asked to populate / audit: at present I'm using geopoints and basemaps to link data through select_one from map and when traces and polygons functions come on stream (no pressure :slight_smile: ) that will expand the possibilities. I am trying to avoid duplicated forms on Central, so will be looking for new ways of streamlining data collection... so an easy way of updating the external dataset would be a benefit again - hopefully not just to me...

Thanks...

client.forms.update is what you are asking.

Oh wow! Thank you for interpreting my suggestion, and making the link to how it's being worked on.

I do feel a bit like some parallel evolution has gone on here, looking @LN 's comments within the pull request:

"Script a form attachment update. This is useful for synchronizing with an external data source or updating entities based on form submissions that have come in. In that case the form version generally doesn't matter (the definition isn't changing anyway)."

Honestly, I hadn't read that (and wouldn't have posted here if I had!)

So, note to self - looks like github is the place to look when thinking about pyODK. I missed the single line change in README.md that is the key to this: "update: Create a new version of an existing Form" - I'm still not sure I would have made the intellectual leap, but its really obvious now you mention it :slight_smile:

As always, thanks for your tolerance to stupid questions / ideas, and for pointing me in the right direction.

I'll just need to work out how to use it now...

I thought it might be helpful for folk trawling the forum to have the example of the solution - so it's all in one place...

Thanks to the signposting by @aurdipas I was able to create a python script to update a particular form with a csv file based on the information in pyodk docs at GitHub... I had to update my version of pyodk so that it has the latest functions ( pip install --upgrade pyodk )

Note that I've referenced one project on the server - it seemed to be a sensible thing to do, but maybe that's not necessary. Happy to be given feedback on the script if it isn't particularly elegant!

from pyodk.client import Client

with Client(project_id=2) as client:
    client.forms.update(
        form_id="myformID",
        attachments=["\Path\to\my\file\ODKprojects.csv"])

I can now set this as a script to run in a single click - so much more likely to have the latest project list on the server. Unfortunately, it probably means fewer excuses to drink coffee...

Thanks for your help and for the work to make this happen.

3 Likes