Isslam
July 16, 2023, 8:33pm
1
I am struggling with the below python code to update and deploy an existing form. I have looked at the examples here and here but still not figuring the issue.
I am a beginner in python but here is my code based on the above links:
import json
import requests
import pandas as pd
# converts Excel to json
def excel_to_json(excel_file_path):
try:
df = pd.read_excel(excel_file_path) # Read the Excel file
json_data = df.to_json(orient='records') # Convert DataFrame to JSON
return json_data
except Exception as e:
print("An error occurred during conversion:", str(e))
URL = 'https://kobo.humanitarianresponse.info/api/v2/'
TOKEN = 'TOKEN'
XFORM = 'FORM_ID'
FILE_PATH = 'XLS_PATH'
CURRENT_DEPLOYMENT = 'https://kobo.humanitarianresponse.info/api/v2/assets/%s/deployment/' % (XFORM)
headers = {'Authorization': f'Token {TOKEN}'}
asset_url = "%sassets/%s/" % (URL, XFORM)
# get form as json from kobo
response = requests.get(asset_url, headers=headers, params={'format': 'json'})
# Convert XLS to JSON
json_data = excel_to_json(FILE_PATH)
response = requests.patch(asset_url, headers=headers, data={'content': json.dumps(json_data)})
# Deploying new version
response = requests.get(asset_url, headers=headers, params={'format': 'json'})
version_to_deploy = response.json()['version_id']
deployed_version_id = response.json()['deployed_version_id']
print("to deploy:" + version_to_deploy)
print("deployed: " + deployed_version_id)
response = requests.patch(asset_url + 'deployment/', headers=headers, data={'version_id': version_to_deploy})
Both print statements at the bottom show me the same version_id which means my PATCH did not work.
Thanks
@Isslam , have you had a chance to look at this post discussed previously?
@uchechinedu01 that’s great! Regarding redeployment, it looks like you’ve left out the active attribute. I’m also not sure why you’re sending the version_id in the files dict — try something like this rather:
deployment_data = {
'version_id': version_to_deploy,
'active': True
}
response = requests.patch(asset_url + 'deployment/', headers=headers, data=deployment_data)
It could be helpful.
Isslam
July 18, 2023, 8:06am
3
Thank you @Kal_Lam , I have looked at the previous discussion but still not working in my case. I only managed to make the “Replace form” works but not 100%. I can see the form was replaced when I go to the website but the updated form has 0 questions. I believe it’s an error when converting my xls
to json
. I’m not sure how to fix it. Also the Re-deployment request is giving me an error response of 500.
Here is an updated version of my code:
import json
import requests
URL = 'https://kobo.humanitarianresponse.info/api/v2/'
TOKEN = 'token'
XFORM = 'form_id'
FILE_PATH = 'xls_path'
headers = {'Authorization': f'Token {TOKEN}'}
asset_url = "%sassets/%s/" % (URL, XFORM)
response = requests.get(asset_url, headers=headers, params={'format': 'json'})
print(response)
data = open(FILE_PATH, 'rb')
files = {"file_name": data}
try:
payload = {'filename': FILE_PATH}
files = {'content': open(FILE_PATH, 'rb')}
data = {'description': 'Replace form', 'metadata': json.dumps(payload)}
res = requests.patch(asset_url, headers=headers, data={'content': json.dumps(payload)})
print(res)
except:
print("Error")
response = requests.get(asset_url, headers=headers, params={'format': 'json'})
version_to_deploy = response.json()['version_id']
deployment_data = {
'version_id': version_to_deploy,
'active': True
}
headers = {
'Accept': 'application/json',
'Authorization': f'Token {TOKEN}'
}
print(version_to_deploy)
response = requests.patch(asset_url + 'deployment/', headers=headers, data=deployment_data)
print(response)
#response = requests.patch(asset_url + 'deployment/', headers=headers, data={'version_id': version_to_deploy})
#print(response)
Isslam
July 20, 2023, 3:04pm
5
This is what I’m using and I checked and they’re up to date. Thanks
Updating and deploying a form using an API with Python can vary greatly depending on the specific API and form service you are using. Below, I’ll provide a general guideline for working with APIs to update and deploy forms using Python:
Isslam
September 4, 2023, 2:59pm
7
Hi @chrisredfield . Thanks and I will be waiting for your response
Good Eveni ng. I hope the informati on was helpful to you