Try ERPNext Buy Support Partners Foundation

Integrate data from external source to Doctype

Hello, I’m currently working with a blood analyzer. The results are given in a text format and each value is separated with a comma.

I’ve already created the lab templates, and each lab test name is set to be a different number. The same number is used as identifier of that patient study on the blood analyzer.

I want to integrate the blood analyzer results with Erpnext, so this kind of lab tests won’t have the necessity to fill the form manually. Any suggestion on the best way to do this?

I’ve been reading this manual, but couldn’t quite find the right way to integrate both systems: https://frappeframework.com/docs/user/en/guides/integration

Any help will be greatly appreciated, Matias.

with CSV its easiest to use the API, I’m not too familiar but I know it can be automated fairly easily with the right tools. You just link the columns/roles/data to DocType Apis, run the automation, give it a refresh and it’ll be done. Alternatively you could do an Import but this would require you to use a certain format in the CSVs

1 Like

You’ve got two options: push and pull.

From whatever device is serving the csv files, you can push the data to your lab results doctype using the REST API framework. It’s simple, clean, and reliable.

From ERPNext, you can pull the data into your doctype using a custom method and a scheduler task. Every x minutes, ERPNext will poll your data source for new entries and add documents if they are.

Which of those is the best fit for your circumstances will depend a lot on the particularities of how your equipment works. To offer much more advice, we’d need to know a lot more about how the blood analyzer makes new data available to the network.

1 Like

Hi! Thanks for your answer. I’m trying the push method, but I’m having this error when I run the code from the windows CMD: “RuntimeError: no object bound to db.”

The code that we are running is a .py file:

import requests

# Login
url = "http://(domain)/api/method/login"
user_data = {"usr": "username@username.com", "pwd": "password"}
r = requests.post(url, data=user_data, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"})
print(r)
print(r.cookies)

# Sent Info
cookies = r.cookies
url = "http://(domain)/api/resource/Lab Test"
test_data = {"naming_series": "LP-", "template": "Análisis de Sangre"}
r = requests.post(url, data=test_data, cookies=cookies, headers={"Content-Type": "application/json", "Accept": "application/json"})
print(r.content)

# Logout
url = "http://(domain)/api/method/logout"
r = requests.get(url, cookies=cookies, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"})
print(r.content)

I think the problem is with the “#Sent Info” part, maybe we are calling something in the wrong way, and that’s why we have the runtime error.

Any suggestion on what should I change?

Is this a typo for user or intentional?

Hmm…nothing jumps out at me.

Does the command bench --site site.name mariadb work correctly?

Generally, you need to escape special characters like spaces in your url, but the requests library might do that automatically. I’ve not used password-based authentication much before. It should work the way you’re describing, but it adds another layer of complexity. If you try with token-based authentication, do you get the same problem?

We have no problem with the password-based authentication part, we tried a different code with the login and the logout and we got this:

This is the code:

import requests

# Login
url = "(domain)/api/method/login"
user_data = {"usr": "username@username.com", "pwd": "password"}
r = requests.post(url, data=user_data, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"})
print(r)
print(r.cookies)

# Sent Info
cookies = r.cookies
url = "http://(domain)/api/resource/Lab Test"
r = requests.get(url, cookies=cookies, headers={"Content-Type": "application/json", "Accept": "application/json"})
print(r.content)

# Logout
url = "http://(domain)/api/method/logout"
r = requests.get(url, cookies=cookies, headers={"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"})
print(r.content)

This is intentional, the login and logout parts works fine