comparison Sphinx/source/plugins/python.rst @ 345:f81b533a0fd0

python samples
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Mar 2020 08:14:49 +0100
parents fff45618262d
children bdf8757449e3
comparison
equal deleted inserted replaced
344:9f82ecc5a422 345:f81b533a0fd0
6 6
7 .. contents:: 7 .. contents::
8 8
9 Work-in-progress. 9 Work-in-progress.
10 10
11 The Python API is automatically generated from the `Orthanc plugin SDK
12 in C
13 <https://hg.orthanc-server.com/orthanc/file/Orthanc-1.5.7/Plugins/Include/orthanc/OrthancCPlugin.h>`__
14 using the `Clang <https://en.wikipedia.org/wiki/Clang>`__ compiler
15 front-end. The coverage of the C SDK is about 75% (105 functions are
16 automatically wrapped in Python out of a total of 139 functions in C).
11 17
12 18
13 Samples 19 Samples
14 ------- 20 -------
15 21
48 The route can then be accessed as:: 54 The route can then be accessed as::
49 55
50 $ curl http://localhost:8042/toto 56 $ curl http://localhost:8042/toto
51 ok 57 ok
52 58
59
60 Listening to changes
61 ....................
62
63 This sample uploads a DICOM file as soon as Orthanc is started::
64
65 import orthanc
66
67 def OnChange(changeType, level, resource):
68 if changeType == orthanc.ChangeType.ORTHANC_STARTED:
69 print('Started')
70
71 with open('/tmp/sample.dcm', 'rb') as f:
72 orthanc.RestApiPost('/instances', f.read())
73
74 elif changeType == orthanc.ChangeType.ORTHANC_STOPPED:
75 print('Stopped')
76
77 elif changeType == orthanc.ChangeType.NEW_INSTANCE:
78 print('A new instance was uploaded: %s' % resource)
79
80 orthanc.RegisterOnChangeCallback(OnChange)
81
82
83 Accessing the content of a new instance
84 ---------------------------------------
85
86 ::
87
88 import orthanc
89 import json
90 import pprint
91
92 def OnStoredInstance(dicom, instanceId):
93 print('Received instance %s of size %d (transfer syntax %s, SOP class UID %s)' % (
94 instanceId, dicom.GetInstanceSize(),
95 dicom.GetInstanceMetadata('TransferSyntax'),
96 dicom.GetInstanceMetadata('SopClassUid')))
97
98 # Print the origin information
99 if dicom.GetInstanceOrigin() == orthanc.InstanceOrigin.DICOM_PROTOCOL:
100 print('This instance was received through the DICOM protocol')
101 elif dicom.GetInstanceOrigin() == orthanc.InstanceOrigin.REST_API:
102 print('This instance was received through the REST API')
103
104 # Print the DICOM tags
105 pprint.pprint(json.loads(dicom.GetInstanceSimplifiedJson()))
106
107 orthanc.RegisterOnStoredInstanceCallback(OnStoredInstance)