Mercurial > hg > orthanc-book
comparison Sphinx/source/plugins/python.rst @ 346:bdf8757449e3
more python samples
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 27 Mar 2020 08:41:45 +0100 |
parents | f81b533a0fd0 |
children | 04fae9d4b65f |
comparison
equal
deleted
inserted
replaced
345:f81b533a0fd0 | 346:bdf8757449e3 |
---|---|
58 | 58 |
59 | 59 |
60 Listening to changes | 60 Listening to changes |
61 .................... | 61 .................... |
62 | 62 |
63 .. highlight:: python | |
64 | |
63 This sample uploads a DICOM file as soon as Orthanc is started:: | 65 This sample uploads a DICOM file as soon as Orthanc is started:: |
64 | 66 |
65 import orthanc | 67 import orthanc |
66 | 68 |
67 def OnChange(changeType, level, resource): | 69 def OnChange(changeType, level, resource): |
79 | 81 |
80 orthanc.RegisterOnChangeCallback(OnChange) | 82 orthanc.RegisterOnChangeCallback(OnChange) |
81 | 83 |
82 | 84 |
83 Accessing the content of a new instance | 85 Accessing the content of a new instance |
84 --------------------------------------- | 86 ....................................... |
87 | |
88 .. highlight:: python | |
85 | 89 |
86 :: | 90 :: |
87 | 91 |
88 import orthanc | 92 import orthanc |
89 import json | 93 import json |
103 | 107 |
104 # Print the DICOM tags | 108 # Print the DICOM tags |
105 pprint.pprint(json.loads(dicom.GetInstanceSimplifiedJson())) | 109 pprint.pprint(json.loads(dicom.GetInstanceSimplifiedJson())) |
106 | 110 |
107 orthanc.RegisterOnStoredInstanceCallback(OnStoredInstance) | 111 orthanc.RegisterOnStoredInstanceCallback(OnStoredInstance) |
112 | |
113 | |
114 Calling pydicom | |
115 ............... | |
116 | |
117 .. highlight:: python | |
118 | |
119 Here is a sample Python plugin that registers a REST callback to dump | |
120 the content of the dataset of one given DICOM instance stored in | |
121 Orthanc, using `pydicom <https://pydicom.github.io/>`__:: | |
122 | |
123 import io | |
124 import orthanc | |
125 import pydicom | |
126 | |
127 def DecodeInstance(output, uri, **request): | |
128 if request['method'] == 'GET': | |
129 instanceId = request['groups'][0] | |
130 f = orthanc.GetDicomForInstance(instanceId) | |
131 dicom = pydicom.dcmread(io.BytesIO(f)) | |
132 output.AnswerBuffer(str(dicom), 'text/plain') | |
133 else: | |
134 output.SendMethodNotAllowed('GET') | |
135 | |
136 orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance) | |
137 | |
138 .. highlight:: bash | |
139 | |
140 This can be called as follows:: | |
141 | |
142 $ curl http://localhost:8042/pydicom/19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5 | |
143 |