Mercurial > hg > orthanc-book
comparison Sphinx/source/plugins/python.rst @ 347:04fae9d4b65f
auto-routing
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 27 Mar 2020 11:24:19 +0100 |
parents | bdf8757449e3 |
children | d8359cecdc89 |
comparison
equal
deleted
inserted
replaced
346:bdf8757449e3 | 347:04fae9d4b65f |
---|---|
124 import orthanc | 124 import orthanc |
125 import pydicom | 125 import pydicom |
126 | 126 |
127 def DecodeInstance(output, uri, **request): | 127 def DecodeInstance(output, uri, **request): |
128 if request['method'] == 'GET': | 128 if request['method'] == 'GET': |
129 # Retrieve the instance ID from the regular expression (*) | |
129 instanceId = request['groups'][0] | 130 instanceId = request['groups'][0] |
131 # Get the content of the DICOM file | |
130 f = orthanc.GetDicomForInstance(instanceId) | 132 f = orthanc.GetDicomForInstance(instanceId) |
133 # Parse it using pydicom | |
131 dicom = pydicom.dcmread(io.BytesIO(f)) | 134 dicom = pydicom.dcmread(io.BytesIO(f)) |
135 # Return a string representation the dataset to the caller | |
132 output.AnswerBuffer(str(dicom), 'text/plain') | 136 output.AnswerBuffer(str(dicom), 'text/plain') |
133 else: | 137 else: |
134 output.SendMethodNotAllowed('GET') | 138 output.SendMethodNotAllowed('GET') |
135 | 139 |
136 orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance) | 140 orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance) # (*) |
137 | 141 |
138 .. highlight:: bash | 142 .. highlight:: bash |
139 | 143 |
140 This can be called as follows:: | 144 This can be called as follows:: |
141 | 145 |
142 $ curl http://localhost:8042/pydicom/19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5 | 146 $ curl http://localhost:8042/pydicom/19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5 |
143 | 147 |
148 | |
149 Auto-routing studies | |
150 .................... | |
151 | |
152 .. highlight:: python | |
153 | |
154 Here is a sample Python plugin that routes any :ref:`stable study | |
155 <lua-callbacks>` to a modality named ``samples`` (as declared in the | |
156 ``DicomModalities`` configuration option):: | |
157 | |
158 import orthanc | |
159 | |
160 def OnChange(changeType, level, resourceId): | |
161 if changeType == orthanc.ChangeType.STABLE_STUDY: | |
162 print('Stable study: %s' % resourceId) | |
163 orthanc.RestApiPost('/modalities/sample/store', resourceId) | |
164 | |
165 orthanc.RegisterOnChangeCallback(OnChange) |