Hi,
It's me again😁, recently I am working on generating some fake patient data for testing purpose with the help of Chat-GPT by using Python. And, at the same time I would like to share my learning curve.😑
1st of all for building a custom REST api service is easy by extending the %CSP.REST
Creating a REST Service Manually
Let's Start !😂
1. Create a class datagen.restservice which extends %CSP.REST
Class datagen.restservice Extends %CSP.REST
{
Parameter CONTENTTYPE = "application/json";
}
2. Add a function genpatientcsv() to generate the patient data, and package it into csv string
Class datagen.restservice Extends %CSP.REST
{
Parameter CONTENTTYPE = "application/json";
ClassMethod genpatientcsv() As %String [ Language = python ]
{
# w ##class(datagen.restservice).genpatientcsv()
# python.exe -m pip install faker
# python.exe -m pip install pandas
from faker import Faker
import random
import pandas as pd
from io import StringIO
# Initialize Faker
fake = Faker()
def generate_patient(patient_id):
return {
"PatientID": patient_id,
"Name": fake.name(),
"Gender": random.choice(["Male", "Female"]),
"DOB": fake.date_of_birth(minimum_age=0, maximum_age=100).strftime("%Y-%m-%d"),
"City": fake.city(),
"Phone": fake.phone_number(),
"Email": fake.email(),
"BloodType": random.choice(["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"]),
"Diagnosis": random.choice(["Hypertension", "Diabetes", "Asthma", "Healthy", "Flu"]),
"Height_cm": round(random.uniform(140, 200), 1),
"Weight_kg": round(random.uniform(40, 120), 1),
}
# Generate 10 patients
patients = [generate_patient(i) for i in range(1, 11)]
# Convert to DataFrame
df = pd.DataFrame(patients)
# Convert to CSV string (without saving to file)
csv_buffer = StringIO()
df.to_csv(csv_buffer, index=False)
csv_string = csv_buffer.getvalue()
return csv_string
}
}
you may test the function in the terminal by typing
w ##class(datagen.restservice).genpatientcsv()
.png)
3. Add a function GetMyDataCSV() in Python for populating the csv string as a csv file and then output through the REST api service. This can be achieve by,
3.1. calling the patient data generate function to get the csv string
3.2. set the %response.ContentType = "text/csv"
3.3. set the header "Content-Disposition" value to "attachment; filename=mydata.csv"
3.4. write the generated csv string as output
remember to pip install the related libraries
Class datagen.restservice Extends %CSP.REST
{
Parameter CONTENTTYPE = "application/json";
ClassMethod GetMyDataCSV() As %Status
{
// Build CSV string
Set tCSVString = ##class(datagen.restservice).genpatientcsv()
//Set headers and output CSV
Set %response.ContentType = "text/csv"
Do %response.SetHeader("Content-Disposition","attachment; filename=mydata.csv")
// Output the data
W tCSVString
Quit $$$OK
}
ClassMethod genpatientcsv() As %String [ Language = python ]
{
# w ##class(datagen.restservice).genpatientcsv()
# python.exe -m pip install faker
# python.exe -m pip install pandas
from faker import Faker
import random
import pandas as pd
from io import StringIO
# Initialize Faker
fake = Faker()
def generate_patient(patient_id):
return {
"PatientID": patient_id,
"Name": fake.name(),
"Gender": random.choice(["Male", "Female"]),
"DOB": fake.date_of_birth(minimum_age=0, maximum_age=100).strftime("%Y-%m-%d"),
"City": fake.city(),
"Phone": fake.phone_number(),
"Email": fake.email(),
"BloodType": random.choice(["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"]),
"Diagnosis": random.choice(["Hypertension", "Diabetes", "Asthma", "Healthy", "Flu"]),
"Height_cm": round(random.uniform(140, 200), 1),
"Weight_kg": round(random.uniform(40, 120), 1),
}
# Generate 10 patients
patients = [generate_patient(i) for i in range(1, 11)]
# Convert to DataFrame
df = pd.DataFrame(patients)
# Convert to CSV string (without saving to file)
csv_buffer = StringIO()
df.to_csv(csv_buffer, index=False)
csv_string = csv_buffer.getvalue()
return csv_string
}
}
4. Add the route to this function and compile the class
Class datagen.restservice Extends %CSP.REST
{
Parameter CONTENTTYPE = "application/json";
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/export/patientdata" Method="GET" Call="GetMyDataCSV"/>
</Routes>
}
ClassMethod GetMyDataCSV() As %Status
{
// Build CSV string
Set tCSVString = ##class(datagen.restservice).genpatientcsv()
//Set headers and output CSV
Set %response.ContentType = "text/csv"
Do %response.SetHeader("Content-Disposition","attachment; filename=mydata.csv")
// Output the data
W tCSVString
Quit $$$OK
}
ClassMethod genpatientcsv() As %String [ Language = python ]
{
# w ##class(datagen.restservice).genpatientcsv()
# python.exe -m pip install faker
# python.exe -m pip install pandas
from faker import Faker
import random
import pandas as pd
from io import StringIO
# Initialize Faker
fake = Faker()
def generate_patient(patient_id):
return {
"PatientID": patient_id,
"Name": fake.name(),
"Gender": random.choice(["Male", "Female"]),
"DOB": fake.date_of_birth(minimum_age=0, maximum_age=100).strftime("%Y-%m-%d"),
"City": fake.city(),
"Phone": fake.phone_number(),
"Email": fake.email(),
"BloodType": random.choice(["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"]),
"Diagnosis": random.choice(["Hypertension", "Diabetes", "Asthma", "Healthy", "Flu"]),
"Height_cm": round(random.uniform(140, 200), 1),
"Weight_kg": round(random.uniform(40, 120), 1),
}
# Generate 10 patients
patients = [generate_patient(i) for i in range(1, 11)]
# Convert to DataFrame
df = pd.DataFrame(patients)
# Convert to CSV string (without saving to file)
csv_buffer = StringIO()
df.to_csv(csv_buffer, index=False)
csv_string = csv_buffer.getvalue()
return csv_string
}
}
OK, now our code is ready. 😁 The next thing is to add the REST service to the web application
.png)
Input your Path, Namespace, and Rest service class name, and then Save
.png)
Assign the proper application role to this web application (because I am lazy, I just simply assign %All for testing 🤐)
.png)
OK everything is ready!!😁 Let's test the REST api!!!😂
Input the following path in a bowser
http://localhost/irishealth/csp/mpapp/export/patientdata
.png)
It trigger a file download, the file name is mydata.csv😗
Let's check the file 😊
.png)
Yeah!!! Work well!! 😁😁
Thank you so much for the reading. 😉
.png)
ボタンをクリックするだけで始められます👍.png)