Python Plug-In for OS X

I was able to compile the Python Plug-in for UBUNTU using the source code here: https://www.orthanc-server.com/browse.php?path=/plugin-python
and the 2.0 version of the plug-in. I had to build the 1.7.2 version of Orthanc on UBUNTU to get the plug-in to load, but it seems to be working now on
UBUNTU.

I am also interested in getting it to work on OS X. I have the precompiled package for OS X for Orthanc, so wondering if I actually need to compile the latest version of Orthanc
on OS X, and then also compile the Python Plug-in. Might be easier to just use UBUNTU since I don’t think OS X would be used in production, but I prefer OS X.

I tried compiling the Python plug-in and get to the “make” stage, but I run into a few errors:

`
boost::locale is disabled
– Found PkgConfig: /usr/local/bin/pkg-config (found version “0.29.2”)
– Checking for module ‘python-3.7-embed’
– No package ‘python-3.7-embed’ found
– Checking for module ‘python-3.7’
– Found python-3.7, version 3.7
– Configuring done
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run “cmake
–help-policy CMP0042” for policy details. Use the cmake_policy command to
set the policy and suppress this warning.

MACOSX_RPATH is not specified for the following targets:

OrthancPython

This warning is for project developers. Use -Wno-dev to suppress it.

– Generating done
– Build files have been written to: /Users/sscotti/Desktop/OrthancPython-2.0/build
host-168-214:build sscotti$ make
Scanning dependencies of target OrthancPython
[ 2%] Building CXX object CMakeFiles/OrthancPython.dir/Sources/Autogenerated/sdk.cpp.o
[ 4%] Building CXX object CMakeFiles/OrthancPython.dir/Sources/OnChangeCallback.cpp.o
[ 7%] Building CXX object CMakeFiles/OrthancPython.dir/Sources/OnStoredInstanceCallback.cpp.o
[ 9%] Building CXX object CMakeFiles/OrthancPython.dir/Sources/Plugin.cpp.o
/Users/sscotti/Desktop/OrthancPython-2.0/Sources/Plugin.cpp:127:10: fatal error: ‘link.h’ file not found
#include <link.h> // For dl_phdr_info
^~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/OrthancPython.dir/Sources/Plugin.cpp.o] Error 1
make[1]: *** [CMakeFiles/OrthancPython.dir/all] Error 2
make: *** [all] Error 2
`

Hello,

I have just committed a changeset to make the Python plugin compile on OS X:
https://hg.orthanc-server.com/orthanc-python/rev/42de8b600c0c

Note however that we won’t provide precompiled binaries of this plugin for OS X.

HTH,
Sébastien-

Thanks. That helps. I still get a few warnings (e.g. warning: ‘register’ storage class specifier is deprecated and incompatible with C++17), but it compiles to libOrthancPython.mainline.dylib (1.5 MB), so I’ll give it a try.

Thanks again ! I had to compile a new version of my OS X Orthanc to upgrade from 1.5.7 to 1.7.2 for High Sierra. That took about 30 minutes with my rather low-end Mac, but that worked, and the Python Plug-in now loads also, so now I have the most recent versions on the Mac. Not sure if I have to upgrade my other plug-ins also, but I guess I’ll find out. Looks like the Python plug-in source that I compiled requires Orthanc 1.7.2.

I slightly modified your script just to play around with Python since I am really a novice when it comes to Python, but probably not too hard to learn if you have a background in other languages.

`

import json
import orthanc

def GetStudyDate(study):
if ‘StudyDate’ in study[‘MainDicomTags’]:
return study[‘MainDicomTags’][‘StudyDate’]
else:
return ‘’

def SortStudiesByDate(output, uri, **request):
if request[‘method’] == ‘GET’:

Retrieve all the studies

studies = json.loads(orthanc.RestApiGet(‘/studies?expand’))

Sort the studies according to the “StudyDate” DICOM tag

studies = sorted(studies, key = GetStudyDate)
count = len(studies)

Read the limit/offset arguments provided by the user

offset = 0
if ‘offset’ in request[‘get’]:
offset = int(request[‘get’][‘offset’])

limit = 0
if ‘limit’ in request[‘get’]:
limit = int(request[‘get’][‘limit’])

Truncate the list of studies

if limit == 0:
studies = studies[offset : ]
else:
studies = studies[offset : offset + limit]

Return the truncated list of studies

studies.append({“count”:count})
studies.append({“limit”:limit})
studies.append({“offset”:offset})
output.AnswerBuffer(json.dumps(studies), ‘application/json’)
else:
output.SendMethodNotAllowed(‘GET’)

orthanc.RegisterRestCallback(‘/sort-studies’, SortStudiesByDate)
`

That just gets the total number of studies before truncation and returns the total count as well as the offset and limit back. You kind of need the total number of studies to calculate the total number of pages so that you can set up a pagination. Probably a better way since it just appends the count to the results (see below), but at least that works. I have a PHP script that handles the pagination display and API calls for a new / different page. The CURL results for a curl ‘http://localhost:8042/sort-studies?offset=2&limit=2’ | json_pp is show below. I had to install jsonpp with homebrew also. The total, limit and offset are just appended to the result.

[ { "LastUpdate" : "20200627T183320", "PatientMainDicomTags" : { "PatientID" : "DEV0000002", "PatientBirthDate" : "", "PatientName" : "", "PatientSex" : "M", "OtherPatientIDs" : "" }, "ID" : "07253e98-4539c4e3-4adf34f4-382bc2d3-0d6bb683", "Series" : [ "70569bdc-0d1ed6ba-0bbb4eb4-165e4d3a-8f56e4a5", "20b10681-8f312c6d-f744249a-4f473d9d-40b4a053", "e7542fa8-38876c3f-a01380fd-4339670f-834cd8c4" ], "ParentPatient" : "4481916d-55b353cc-ae4cbad4-4c0e7cbd-33c0b045", "IsStable" : true, "Type" : "Study", "MainDicomTags" : { "StudyInstanceUID" : "1.2.840.113619.2.256.50119152871.1435959722.9763", "AccessionNumber" : "DEVACC00000003", "InstitutionName" : "Chrissie Tomlinson Memorial Hospital", "ReferringPhysicianName" : "", "StudyDescription" : "Renal", "StudyID" : "", "StudyDate" : "20150703", "StudyTime" : "154202" } }, { "LastUpdate" : "20200627T183258", "ID" : "86e1a292-07ffb90a-50b2d752-3074f6bd-6895e89a", "Series" : [ "724c74e6-05cc6cc4-62c9c830-1bfda76c-3a435438", "ab78c679-ef037b0a-c267a1cc-dac2dfe7-456d1a3c", "8bccacc7-d470b154-446c649d-2f08a482-b18cd25d" ], "PatientMainDicomTags" : { "PatientSex" : "M", "PatientName" : "", "PatientBirthDate" : "19571116", "PatientID" : "DEV0000001" }, "Type" : "Study", "MainDicomTags" : { "StudyDate" : "20190829", "RequestingPhysician" : "", "StudyTime" : "083425", "StudyInstanceUID" : "2.16.840.1.114151.2411984198229487379168635092719400577270190829", "InstitutionName" : "MHealth CSC", "ReferringPhysicianName" : "", "AccessionNumber" : "DEVACC00000007", "StudyDescription" : "XR KNEE RT 3 VW", "StudyID" : "UC4839464" }, "ParentPatient" : "fa21ff2d-33e9b60a-daedf6a0-64d018da-682fd0a4", "IsStable" : true }, { "count" : 8 }, { "limit" : 2 }, { "offset" : 2 } ]

That is very helpful.