Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python.rst @ 741:97e6a0431c5e Orthanc-1.9.6
Orthanc 1.9.6
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 21 Jul 2021 12:30:42 +0200 |
parents | e6386c012438 |
children | 56d48f6e52cc |
rev | line source |
---|---|
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 .. _python-plugin: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 Python plugin for Orthanc |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 ========================= |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 .. contents:: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 |
371
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
9 |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
10 Overview |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
11 -------- |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
12 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
13 This plugin can be used to write :ref:`Orthanc plugins |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
14 <creating-plugins>` using the `Python programming language |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
15 <https://en.wikipedia.org/wiki/Python_(programming_language)>`__ |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
16 instead of the more complex C/C++ programming languages. |
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
18 Python plugins have access to more features and a more consistent SDK |
709
9d5e17100a8d
update statistics from 2.0 to 3.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
708
diff
changeset
|
19 than :ref:`Lua scripts <lua>`. The largest part of the Python API is |
9d5e17100a8d
update statistics from 2.0 to 3.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
708
diff
changeset
|
20 automatically generated from the `Orthanc plugin SDK in C |
741 | 21 <https://hg.orthanc-server.com/orthanc/file/Orthanc-1.9.6/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h>`__ |
345 | 22 using the `Clang <https://en.wikipedia.org/wiki/Clang>`__ compiler |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
23 front-end. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
24 |
709
9d5e17100a8d
update statistics from 2.0 to 3.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
708
diff
changeset
|
25 As of release 3.2 of the plugin, the coverage of the C SDK is about |
9d5e17100a8d
update statistics from 2.0 to 3.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
708
diff
changeset
|
26 87% (138 functions are automatically wrapped in Python out of a total |
9d5e17100a8d
update statistics from 2.0 to 3.2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
708
diff
changeset
|
27 of 158 functions from the Orthanc SDK 1.8.1). |
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 |
371
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
29 |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
30 Source code |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
31 ----------- |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
32 |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
33 * Link to the `official releases of this plugin |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
34 <https://www.orthanc-server.com/browse.php?path=/plugin-python>`__. |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
35 |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
36 * Link to the `code repository |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
37 <https://hg.orthanc-server.com/orthanc-python/>`__. |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
38 |
5cb37e6b014b
explicit links to the source code of python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
39 |
353 | 40 Licensing |
41 --------- | |
42 | |
43 Pay attention to the fact that this plugin is licensed under the terms | |
44 of the `AGPL license | |
45 <https://en.wikipedia.org/wiki/GNU_Affero_General_Public_License>`__. | |
46 | |
47 This has an important consequence: If you distribute Orthanc to | |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
48 clients together with one Python script, or if you put an Orthanc |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
49 server equipped with one Python script on a Web portal, you **must** |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
50 disclose the source code of your Python script to the Orthanc |
354 | 51 community under the terms of the AGPL license. |
52 | |
53 We suggest you to put the source code of your Python scripts on the | |
54 dedicated `"OrthancContributed" repository on GitHub | |
55 <https://github.com/jodogne/OrthancContributed/tree/master/Plugins>`__, | |
56 and/or to send it to the `Orthanc Users | |
57 <https://groups.google.com/forum/#!forum/orthanc-users>`__ discussion | |
58 group. | |
353 | 59 |
60 Check out the :ref:`FAQ about licensing <licensing>` for more context. | |
61 | |
62 | |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
63 Usage |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
64 ----- |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
65 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
66 Docker |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
67 ...... |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
68 |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
69 .. highlight:: python |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
70 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
71 The most direct way of starting Orthanc together with the Python |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
72 plugin is through :ref:`Docker <docker>`. Let's create the file |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
73 ``/tmp/hello.py`` that contains the following basic Python script:: |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
74 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
75 print('Hello world!') |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
76 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
77 .. highlight:: json |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
78 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
79 Let's also create the file ``/tmp/orthanc.json`` that contains the |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
80 following minimal :ref:`configuration for Orthanc <configuration>`:: |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
81 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
82 { |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
83 "StorageDirectory" : "/var/lib/orthanc/db", |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
84 "RemoteAccessAllowed" : true, |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
85 "Plugins" : [ |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
86 "/usr/local/share/orthanc/plugins" |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
87 ], |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
88 "PythonScript" : "/etc/orthanc/hello.py" |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
89 } |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
90 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
91 .. highlight:: bash |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
92 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
93 Given these two files, Orthanc can be started as follows:: |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
94 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
95 $ docker run -p 4242:4242 -p 8042:8042 --rm \ |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
96 -v /tmp/orthanc.json:/etc/orthanc/orthanc.json:ro \ |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
97 -v /tmp/hello.py:/etc/orthanc/hello.py:ro \ |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
98 jodogne/orthanc-python |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
99 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
100 .. highlight:: text |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
101 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
102 You'll see the following excerpt in the log, which indicates that the Python plugin is properly loaded:: |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
103 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
104 W0331 15:48:12.990661 PluginsManager.cpp:269] Registering plugin 'python' (version mainline) |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
105 W0331 15:48:12.990691 PluginsManager.cpp:168] Python plugin is initializing |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
106 W0331 15:48:12.990743 PluginsManager.cpp:168] Using Python script "hello.py" from directory: /etc/orthanc |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
107 W0331 15:48:12.990819 PluginsManager.cpp:168] Program name: /usr/local/sbin/Orthanc |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
108 Hello world! |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
109 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
110 |
405 | 111 `Here <https://bitbucket.org/osimis/orthanc-setup-samples/src/master/docker/python/>`__ is a full example |
112 of a more complex setup using the :ref:`osimis/orthanc <docker-osimis>` images. | |
113 | |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
114 Compiling from source |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
115 ..................... |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
116 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
117 .. highlight:: bash |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
118 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
119 The procedure to compile this plugin from source is similar to that |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
120 for the :ref:`core of Orthanc <compiling>`. The following commands |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
121 should work for most UNIX-like distribution (including GNU/Linux):: |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
122 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
123 $ mkdir Build |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
124 $ cd Build |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
125 $ cmake .. -DPYTHON_VERSION=3.7 -DSTATIC_BUILD=ON -DCMAKE_BUILD_TYPE=Release |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
126 $ make |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
127 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
128 Before running CMake, make sure that the Python interpreter and its |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
129 associated development library are installed. On Ubuntu 18.04 LTS, you |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
130 would for instance install packages ``libpython3.7-dev`` and |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
131 ``python3.7``. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
132 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
133 The compilation will produce the shared library ``OrthancPython``, |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
134 that can be loaded by properly setting the ``Plugins`` |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
135 :ref:`configuration option <configuration>` of Orthanc. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
136 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
137 **Warning:** The shared library is only compatible with the Python |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
138 interpreter whose version corresponds to the value of the |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
139 ``PYTHON_VERSION`` argument that was given to CMake. |
501
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
140 |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
141 **Note for OS X:** As indicated by `Stephen Douglas Scotti |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
142 <https://groups.google.com/g/orthanc-users/c/RnmZKFv8FaY/m/HhvOD2A2CAAJ>`__, |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
143 here is a sample invocation of CMake to force the version of Python to |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
144 be used on OS X:: |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
145 |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
146 $ cmake .. -DPYTHON_VERSION=3.8 -DSTATIC_BUILD=ON -DCMAKE_BUILD_TYPE=Release \ |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
147 -DPYTHON_LIBRARY=/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/libpython3.8.dylib \ |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
148 -DPYTHON_INCLUDE_DIR=/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/include/python3.8/ |
98592b9e7ad5
force version of python on os x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
490
diff
changeset
|
149 |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
150 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
151 Microsoft Windows |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
152 ................. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
153 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
154 Pre-compiled binaries for Microsoft Windows `are also available |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
155 <https://www.orthanc-server.com/browse.php?path=/plugin-python>`__. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
156 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
157 Beware that one version of the Python plugin can only be run against |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
158 one version of the Python interpreter. This version is clearly |
442
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
159 indicated in the filename of the precompiled binaries. |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
160 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
161 .. highlight:: text |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
162 |
442
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
163 You are of course free to compile the plugin from sources. You'll have |
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
164 to explicitly specify the path to your Python installation while |
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
165 invoking CMake. For instance:: |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
166 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
167 C:\orthanc-python\Build> cmake .. -DPYTHON_VERSION=2.7 -DPYTHON_WINDOWS_ROOT=C:/Python27 \ |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
168 -DSTATIC_BUILD=ON -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 15 2017" |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
169 |
442
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
170 **Note about debug builds**: Usually, building Python modules such as the Python |
377
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
171 plugin for Orthanc in debug mode (where ``_DEBUG`` is defined) leads to a module |
442
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
172 (``.exe`` or ``.dll``) that requires a debug build of Python, and debug versions of all |
377
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
173 the Python libraries. This is quite cumbersome, for it requires building Python |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
174 on your own or downloading additional debug files. |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
175 |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
176 Since using a debug build of Python is only necessary in very specific cases |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
177 (such as the debugging of code at the boundary between Python and an extension), |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
178 we have changed the default behavior to use the release Python library by default. |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
179 |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
180 This means that you are able to build this plugin in debug mode with the |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
181 standard Python distribution. |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
182 |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
183 In case you need to use the Python debug libraries, you can instruct the build |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
184 system to do so by setting the ``PYTHON_WINDOWS_USE_RELEASE_LIBS`` CMake option, |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
185 that is ``ON`` by default, to ``OFF``. The previous build example would then be, |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
186 should you require a full debug build:: |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
187 |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
188 C:\orthanc-python\Build> cmake .. -DPYTHON_VERSION=2.7 -DPYTHON_WINDOWS_ROOT=C:/Python27 \ |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
189 -DSTATIC_BUILD=ON -DPYTHON_WINDOWS_USE_RELEASE_LIBS=OFF \ |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
190 -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 15 2017" |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
191 |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
192 Please note that this CMake option only impacts **debug** builds under Windows, |
ffe62e6c086f
Added a note about the debug libraries when building under Windows with Visual Studio.
Benjamin Golinvaux <bgo@osimis.io>
parents:
375
diff
changeset
|
193 when using (any version of) the Microsoft Visual Studio compiler. |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
194 |
442
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
195 The precompiled binaries all use release (i.e. non-debug) versions of Python. |
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
196 |
8b2c648c0f46
clarifications about windows precompiled binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
405
diff
changeset
|
197 |
364
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
198 Configuration options |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
199 --------------------- |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
200 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
201 The only two configuration options that are available for this plugin |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
202 are the following: |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
203 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
204 * ``PythonScript`` indicates where the Python script is located. If |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
205 this configuration option is not provided, the Python plugin is not |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
206 started. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
207 |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
208 * ``PythonVerbose`` is a Boolean value to make the Python interpreter |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
209 verbose. |
234de55ed125
usage of the python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
354
diff
changeset
|
210 |
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
211 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
212 Samples |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
213 ------- |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
214 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
215 Extending the REST API |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
216 ...................... |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
217 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
218 Here is a basic Python script that registers two new routes in the |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
219 REST API: |
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
220 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
221 .. literalinclude:: python/extending-rest-api.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
222 :language: python |
343
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
223 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
224 .. highlight:: json |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
225 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
226 Here is the associated minimal configuration file for Orthanc |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
227 (provided the Python script is saved as ``rest.py``):: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
228 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
229 { |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
230 "Plugins" : [ "." ], |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
231 "PythonScript" : "rest.py", |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
232 "PythonVerbose" : false |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
233 } |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
234 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
235 .. highlight:: bash |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
236 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
237 The route can then be accessed as:: |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
238 |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
239 $ curl http://localhost:8042/toto |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
240 ok |
fff45618262d
creating the documentation of the Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
241 |
469
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
242 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
243 .. _python-changes: |
345 | 244 |
245 Listening to changes | |
246 .................... | |
247 | |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
248 This sample uploads a DICOM file as soon as Orthanc is started: |
345 | 249 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
250 .. literalinclude:: python/listening-changes.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
251 :language: python |
345 | 252 |
590
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
253 |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
254 .. warning:: |
600
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
255 In releases <= 3.0 of the Python plugin, deadlocks might emerge if |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
256 you call other core primitives of Orthanc (such as the REST API) in |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
257 your callback function. This issue has been `fixed in release 3.1 |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
258 <https://hg.orthanc-server.com/orthanc-python/rev/46fe70776d61>`__. |
590
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
259 |
600
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
260 As a **temporary workaround** against such deadlocks in releases <= |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
261 3.0, if you have to call other primitives of Orthanc, you should make |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
262 these calls in a separate thread, passing the pending events to be |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
263 processed through a message queue. Here is the template of a possible |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
264 solution to postpone such deadlocks as much as possible by relying on |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
265 the multithreading primitives of Python: |
591
2397b0e12bc8
solution to avoid deadlock in Python OnChange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
590
diff
changeset
|
266 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
267 .. literalinclude:: python/changes-deadlock-3.0.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
268 :language: python |
600
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
269 |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
270 Beware that **this workaround is imperfect** and deadlocks have been |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
271 observed even if using it! Make sure to upgrade your plugin to solve |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
272 this issue for good. Note that this temporary workaround is not needed |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
273 in releases >= 3.1 of the plugin. |
4038ae299b18
note about deadlocks in python plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
591
diff
changeset
|
274 |
591
2397b0e12bc8
solution to avoid deadlock in Python OnChange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
590
diff
changeset
|
275 |
590
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
276 |
345 | 277 Accessing the content of a new instance |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
278 ....................................... |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
279 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
280 .. literalinclude:: python/accessing-new-instance.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
281 :language: python |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
282 |
590
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
283 .. warning:: |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
284 Your callback function will be called synchronously with |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
285 the core of Orthanc. This implies that deadlocks might emerge if |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
286 you call other core primitives of Orthanc in your callback (such |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
287 deadlocks are particular visible in the presence of other plugins |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
288 or Lua scripts). It is thus strongly advised to avoid any call to |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
289 the REST API of Orthanc in the callback. If you have to call other |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
290 primitives of Orthanc, you should make these calls in a separate |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
291 thread, passing the pending events to be processed through a |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
292 message queue. |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
293 |
404aca6160f0
warning about deadlock in python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
568
diff
changeset
|
294 |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
295 Calling pydicom |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
296 ............... |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
297 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
298 Here is a sample Python plugin that registers a REST callback to dump |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
299 the content of the dataset of one given DICOM instance stored in |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
300 Orthanc, using `pydicom <https://pydicom.github.io/>`__: |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
301 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
302 .. literalinclude:: python/pydicom.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
303 :language: python |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
304 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
305 .. highlight:: bash |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
306 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
307 This callback can be called as follows:: |
346
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
308 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
309 $ curl http://localhost:8042/pydicom/19816330-cb02e1cf-df3a8fe8-bf510623-ccefe9f5 |
bdf8757449e3
more python samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
345
diff
changeset
|
310 |
347 | 311 |
312 Auto-routing studies | |
313 .................... | |
314 | |
315 Here is a sample Python plugin that routes any :ref:`stable study | |
642
a76d83a00c68
definition of stable resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
638
diff
changeset
|
316 <stable-resources>` to a modality named ``samples`` (as declared in the |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
317 ``DicomModalities`` configuration option): |
347 | 318 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
319 .. literalinclude:: python/autorouting-1.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
320 :language: python |
348 | 321 |
539 | 322 Note that, if you want to use an orthanc plugin to transfer the study, |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
323 you should use the ``RestApiPostAfterPlugins()`` method: |
539 | 324 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
325 .. literalinclude:: python/autorouting-2.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
326 :language: python |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
327 |
539 | 328 |
352 | 329 Rendering a thumbnail using PIL/Pillow |
330 ...................................... | |
348 | 331 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
332 .. literalinclude:: python/pil.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
333 :language: python |
351 | 334 |
335 | |
369
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
336 .. _python-introspection: |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
337 |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
338 Inspecting the available API |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
339 ............................ |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
340 |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
341 Thanks to Python's introspection primitives, it is possible to inspect |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
342 the API of the ``orthanc`` module in order to dump all the available |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
343 features: |
369
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
344 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
345 .. literalinclude:: python/inspect-api.py |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
346 :language: python |
369
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
347 |
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
702
diff
changeset
|
348 |
373
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
349 .. _python-scheduler: |
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
350 |
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
351 Scheduling a task for periodic execution |
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
352 ........................................ |
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
353 |
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
354 The following Python script will periodically (every second) run the |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
355 function ``Hello()`` thanks to the ``threading`` module: |
373
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
356 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
357 .. literalinclude:: python/periodic-execution.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
358 :language: python |
373
847996394688
Scheduling a task for periodic execution
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
369
diff
changeset
|
359 |
378
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
360 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
361 .. _python-metadata: |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
362 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
363 Filtering and returning metadata |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
364 ................................ |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
365 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
366 Besides the main DICOM tags, Orthanc associates some metadata to each |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
367 resource it stores (this includes the date of last update, the |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
368 transfer syntax, the remote AET...). People are often interested in |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
369 getting such metadata while calling the ``/tools/find`` route in the |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
370 :ref:`REST API <rest-find>`, or even in filtering this metadata the |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
371 same way they look for DICOM tags. |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
372 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
373 This feature is not built in the core of Orthanc, as metadata is not |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
374 indexed in the Orthanc database, contrarily to the main DICOM |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
375 tags. Filtering metadata requires a linear search over all the |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
376 matching resources, which induces a cost in the performance. |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
377 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
378 Nevertheless, here is a full sample Python script that overwrites the |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
379 ``/tools/find`` route in order to give access to metadata: |
378
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
380 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
381 .. literalinclude:: python/filtering-metadata.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
382 :language: python |
378
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
383 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
384 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
385 **Warning:** In the sample above, the filtering of the metadata is |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
386 done using Python's `library for regular expressions |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
387 <https://docs.python.org/3/library/re.html>`__. It is evidently |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
388 possible to adapt this script in order to use the DICOM conventions |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
389 about `attribute matching |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
390 <http://dicom.nema.org/medical/dicom/2019e/output/chtml/part04/sect_C.2.2.2.html>`__. |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
391 |
469
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
392 .. highlight:: bash |
378
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
393 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
394 Here is a sample call to retrieve all the studies that were last |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
395 updated in 2019 thanks to this Python script:: |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
396 |
16dc3561b41e
Filtering and returning metadata using Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
375
diff
changeset
|
397 $ curl http://localhost:8042/tools/find -d '{"Level":"Study","Query":{},"Expand":true,"Metadata":{"LastUpdate":"^2019.*$"}}' |
369
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
398 |
181b02cea7ab
inspecting Python API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
364
diff
changeset
|
399 |
469
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
400 .. _python-paging: |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
401 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
402 Implementing basic paging |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
403 ......................... |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
404 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
405 As explained in the FAQ, the :ref:`Orthanc Explorer interface is |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
406 low-level <improving-interface>`, and is not adapted for |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
407 end-users. One common need is to implement paging of studies, which |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
408 calls for server-side sorting of studies. This can be done using the |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
409 following sample Python plugin that registers a new route |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
410 ``/sort-studies`` in the REST API of Orthanc: |
469
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
411 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
412 .. literalinclude:: python/paging.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
413 :language: python |
469
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
414 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
415 .. highlight:: bash |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
416 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
417 Here is a sample call to this new REST route, that could be issued by |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
418 any JavaScript framework (the ``json_pp`` command-line pretty-prints a |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
419 JSON file):: |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
420 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
421 $ curl http://localhost:8042/sort-studies | json_pp |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
422 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
423 This route also implement paging (i.e. it can limit and offset the |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
424 returned studies):: |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
425 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
426 $ curl 'http://localhost:8042/sort-studies?offset=2&limit=2' | json_pp |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
427 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
428 Obviously, this basic sample can be improved in many ways. To improve |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
429 performance, one could for instance cache the result of |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
430 ``/studies?expand`` in memory by :ref:`listening to changes |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
431 <python-changes>` in the list of studies |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
432 (cf. ``orthanc.ChangeType.NEW_STUDY`` and |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
433 ``orthanc.ChangeType.DELETED``). |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
434 |
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
435 |
551
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
436 .. _python_excel: |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
437 |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
438 Creating a Microsoft Excel report |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
439 ................................. |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
440 |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
441 As Orthanc plugins have access to any installed Python module, it is |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
442 very easy to implement a server-side plugin that generates a report in |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
443 the Microsoft Excel ``.xls`` format. Here is a working example: |
551
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
444 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
445 .. literalinclude:: python/excel.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
446 :language: python |
551
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
447 |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
448 If opening the ``http://localhost:8042/report.xls`` URI, this Python |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
449 will generate a workbook with one sheet that contains the list of |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
450 studies, with the patient ID, the patient name and the study |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
451 description. |
d0332c5a2cb8
Python plugin: excel generation example
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
539
diff
changeset
|
452 |
469
46949efa5818
Implementing basic paging using a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
453 |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
454 .. _python_authorization: |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
455 |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
456 Forbid or allow access to REST resources (authorization, new in 3.0) |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
457 .................................................................... |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
458 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
459 The following Python script installs a callback that is triggered |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
460 whenever the HTTP server of Orthanc is accessed: |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
461 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
462 .. literalinclude:: python/authorization-1.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
463 :language: python |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
464 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
465 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
466 If access is not granted, the ``Filter`` callback must return |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
467 ``False``. As a consequence, the HTTP status code would be set to |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
468 ``403 Forbidden``. If access is granted, the ``Filter`` must return |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
469 ``true``. The ``request`` argument contains more information about the |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
470 request (such as the HTTP headers, the IP address of the caller and |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
471 the GET arguments). |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
472 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
473 Note that this is similar to the ``IncomingHttpRequestFilter()`` |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
474 callback that is available in :ref:`Lua scripts <lua-filter-rest>`. |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
475 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
476 Thanks to Python, it is extremely easy to call remote Web services for |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
477 authorization. Here is an example using the ``requests`` library: |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
478 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
479 .. literalinclude:: python/authorization-2.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
480 :language: python |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
481 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
482 This filter could be used together with the following Web service |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
483 implemented using `Node.js |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
484 <https://en.wikipedia.org/wiki/Node.js>`__: |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
485 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
486 .. literalinclude:: python/authorization-node-service.js |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
487 :language: javascript |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
488 |
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
489 |
708
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
490 .. _python_lookup_dictionary: |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
491 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
492 Lookup DICOM dictionary (new in 3.2) |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
493 .................................... |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
494 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
495 Python plugins can access the dictionary of the DICOM tags that are |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
496 handled by Orthanc: |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
497 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
498 .. literalinclude:: python/lookup-dictionary.py |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
499 :language: python |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
500 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
501 .. highlight:: text |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
502 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
503 Note how Python introspection is used in order to map the values in |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
504 enumeration ``orthanc.ValueRepresentation`` to a string description of |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
505 the value representation. If started, the plugin above would output |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
506 the following information in the Orthanc logs:: |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
507 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
508 W0611 14:04:08.563957 PluginsManager.cpp:168] Entry in the dictionary: { |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
509 "Element": 32, |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
510 "Group": 16, |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
511 "MaxMultiplicity": 1, |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
512 "MinMultiplicity": 1, |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
513 "ValueRepresentation": 11 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
514 } |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
515 W0611 14:04:08.563975 PluginsManager.cpp:168] Name of the value representation: LO |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
516 |
cd70d23f34bc
Lookup DICOM dictionary in Python scripts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
517 |
695
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
518 .. _python_create_dicom: |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
519 |
696 | 520 Creating DICOM instances (new in 3.2) |
695
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
521 ..................................... |
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
522 |
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
523 The following sample Python script will write on the disk a new DICOM |
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
524 instance including the traditional Lena sample image, and will decode |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
525 the single frame of this DICOM instance: |
695
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
526 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
527 .. literalinclude:: python/create-dicom.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
528 :language: python |
695
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
529 |
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
530 |
706
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
531 .. _python_pil_conversions: |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
532 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
533 Conversions between Orthanc and Python images (new in 3.2) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
534 .......................................................... |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
535 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
536 The Python method ``orthanc.Image.GetImageBuffer()`` returns a copy of |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
537 the memory buffer of an image that is handled Orthanc. Conversely, the |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
538 Python function ``orthanc.CreateImageFromBuffer()`` can be used to |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
539 create an Orthanc image from a Python buffer. Taken together, these |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
540 two functions can be used to do bidirectional conversions between |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
541 Orthanc images and Python images. |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
542 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
543 Here is a full working example using PIL/Pillow that shows how to |
707 | 544 decode one frame of a DICOM instance using Orthanc, then to modify |
545 this image using PIL, and finally to upload the modified image as a | |
546 new DICOM instance: | |
706
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
547 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
548 .. literalinclude:: python/pil-conversions.py |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
549 :language: python |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
550 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
704
diff
changeset
|
551 |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
552 .. _python_dicom_scp: |
695
f4fc12ed00ee
creating dicom instances in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
678
diff
changeset
|
553 |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
554 Handling DICOM SCP requests (new in 3.2) |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
555 ........................................ |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
556 |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
557 Starting with release 3.2 of the Python plugin, it is possible to |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
558 replace the C-FIND SCP and C-MOVE SCP of Orthanc by a Python |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
559 script. This feature can notably be used to create a custom DICOM |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
560 proxy. Here is a minimal example: |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
561 |
704
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
562 .. literalinclude:: python/dicom-find-move-scp.py |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
563 :language: python |
ba2403ebd4b7
moving python samples in separate files (3)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
703
diff
changeset
|
564 |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
565 |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
566 .. highlight:: text |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
567 |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
568 In this sample, the C-FIND SCP will send one single answer that |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
569 reproduces the values provided by the SCU:: |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
570 |
699 | 571 $ findscu localhost 4242 -S -k QueryRetrieveLevel=STUDY -k PatientName=TEST -k SeriesDescription= |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
572 I: --------------------------- |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
573 I: Find Response: 1 (Pending) |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
574 I: |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
575 I: # Dicom-Data-Set |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
576 I: # Used TransferSyntax: Little Endian Explicit |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
577 I: (0008,0005) CS [ISO_IR 100] # 10, 1 SpecificCharacterSet |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
578 I: (0008,0052) CS [HELLO0-STUDY] # 12, 1 QueryRetrieveLevel |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
579 I: (0008,103e) LO [HELLO1- ] # 8, 1 SeriesDescription |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
580 I: (0010,0010) PN [HELLO2-TEST ] # 12, 1 PatientName |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
581 I: |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
582 |
698 | 583 A more realistic Python script could for instance call the route |
584 ``/modalities/{...}/query`` in the :ref:`REST API <rest-find-scu>` of | |
585 Orthanc using ``orthanc.RestApiPost()``, in order to query the content | |
586 a remote modality through a second C-FIND SCU request (this time | |
587 issued by Orthanc as a SCU). | |
588 | |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
589 The C-MOVE SCP can be invoked as follows:: |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
590 |
699 | 591 $ movescu localhost 4242 -aem TARGET -aec SOURCE -aet MOVESCU -S -k QueryRetrieveLevel=IMAGE -k StudyInstanceUID=1.2.3.4 |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
592 |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
593 The C-MOVE request above would print the following information in the |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
594 Orthanc logs:: |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
595 |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
596 W0610 18:30:36.840865 PluginsManager.cpp:168] C-MOVE request to be handled in Python: { |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
597 "AccessionNumber": "", |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
598 "Level": "INSTANCE", |
699 | 599 "OriginatorAET": "MOVESCU", |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
600 "OriginatorID": 1, |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
601 "PatientID": "", |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
602 "SOPInstanceUID": "", |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
603 "SeriesInstanceUID": "", |
699 | 604 "SourceAET": "SOURCE", |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
605 "StudyInstanceUID": "1.2.3.4", |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
606 "TargetAET": "TARGET" |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
607 } |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
608 |
710 | 609 It is now up to your Python callback to process the C-MOVE SCU request, |
697
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
610 for instance by calling the route ``/modalities/{...}/store`` in the |
7581dc208323
Handling DICOM SCP requests using a Python plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
696
diff
changeset
|
611 :ref:`REST API <rest-store-scu>` of Orthanc using |
700
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
612 ``orthanc.RestApiPost()``. It is highly advised to create a Python |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
613 thread to handle the request, in order to avoid blocking Orthanc as |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
614 much as possible. |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
615 |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
616 |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
617 .. _python_worklists: |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
618 |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
619 Handling worklist SCP requests (new in 3.2) |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
620 ........................................... |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
621 |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
622 Starting with release 3.2 of the Python plugin, it is possible to |
701
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
623 answer :ref:`worklist queries <worklist>` using a Python script. This |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
624 is especially useful to easily create a bridge between Orthanc, |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
625 HL7/FHIR messages and RIS systems. Indeed, Python provides many tools |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
626 to handle HL7 such as `python-hl7 library |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
627 <https://python-hl7.readthedocs.io/en/latest/>`__. |
700
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
628 |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
629 The following Python script reproduces features similar to the |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
630 :ref:`sample modality worklists plugin <worklists-plugin>`: |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
631 |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
632 .. literalinclude:: python/worklist.py |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
633 :language: python |
56a06ca9ec20
Handling worklist SCP requests in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
634 |
701
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
635 .. highlight:: text |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
636 |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
637 Here is the result of this plugin on a sample call:: |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
638 |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
639 $ findscu -W -k "ScheduledProcedureStepSequence[0].Modality=MR" 127.0.0.1 4242 |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
640 I: --------------------------- |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
641 I: Find Response: 1 (Pending) |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
642 I: |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
643 I: # Dicom-Data-Set |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
644 I: # Used TransferSyntax: Little Endian Explicit |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
645 I: (0008,0005) CS [ISO_IR 100] # 10, 1 SpecificCharacterSet |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
646 I: (0040,0100) SQ (Sequence with explicit length #=1) # 18, 1 ScheduledProcedureStepSequence |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
647 I: (fffe,e000) na (Item with explicit length #=1) # 10, 1 Item |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
648 I: (0008,0060) CS [MR] # 2, 1 Modality |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
649 I: (fffe,e00d) na (ItemDelimitationItem for re-encoding) # 0, 0 ItemDelimitationItem |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
650 I: (fffe,e0dd) na (SequenceDelimitationItem for re-encod.) # 0, 0 SequenceDelimitationItem |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
651 I: |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
652 I: --------------------------- |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
653 I: Find Response: 2 (Pending) |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
654 I: |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
655 I: # Dicom-Data-Set |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
656 I: # Used TransferSyntax: Little Endian Explicit |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
657 I: (0008,0005) CS [ISO_IR 100] # 10, 1 SpecificCharacterSet |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
658 I: (0040,0100) SQ (Sequence with explicit length #=1) # 18, 1 ScheduledProcedureStepSequence |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
659 I: (fffe,e000) na (Item with explicit length #=1) # 10, 1 Item |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
660 I: (0008,0060) CS [MR] # 2, 1 Modality |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
661 I: (fffe,e00d) na (ItemDelimitationItem for re-encoding) # 0, 0 ItemDelimitationItem |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
662 I: (fffe,e0dd) na (SequenceDelimitationItem for re-encod.) # 0, 0 SequenceDelimitationItem |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
663 I: |
f093160dd7f4
cross-references regarding worklists
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
700
diff
changeset
|
664 |
727 | 665 |
666 .. _pynetdicom: | |
667 | |
668 Replacing DICOM SCP of Orthanc by pynetdicom | |
669 ............................................ | |
670 | |
671 .. highlight:: json | |
672 | |
673 Thanks to Python plugins, it is also possible to replace the built-in | |
674 DICOM SCP of Orthanc by `pynetdicom | |
675 <https://pydicom.github.io/pynetdicom/stable/examples/storage.html>`__ | |
676 so as to customize how the DICOM protocol is handled. Firstly, in the | |
677 configuration file, make sure to disable the Orthanc SCP by setting | |
678 ``DicomServerEnabled`` to ``false``:: | |
679 | |
680 { | |
681 "Plugins" : [ "." ], | |
682 "PythonScript" : "pynetdicom.py", | |
683 "DicomServerEnabled" : false | |
684 } | |
685 | |
686 Secondly, here a basic plugin illustrating how to start and stop the | |
687 pynetdicom server, and handle incoming C-STORE requests: | |
688 | |
689 .. literalinclude:: python/pynetdicom.py | |
690 :language: python | |
691 | |
692 As can be seen in this listing, whenever the pynetdicom receives an | |
693 incoming C-STORE request, it makes a POST call to the URI | |
694 ``/instances`` in the REST API of Orthanc in order to store the | |
695 embedded DICOM dataset into Orthanc. Obviously, one can build more | |
696 complex DICOM servers by handling more messages than C-STORE alone. | |
697 | |
698 | |
556
6a3d48510b0b
Python sample: "Forbid or allow access to REST resources (authorization)", deprecating advanced authorization plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
555
diff
changeset
|
699 |
351 | 700 Performance and concurrency |
701 --------------------------- | |
702 | |
471
a735476a0e6e
note about fork on windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
703 **Important:** This section only applies to UNIX-like systems. The |
a735476a0e6e
note about fork on windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
704 ``multiprocessing`` package will not work on Microsoft Windows as the |
a735476a0e6e
note about fork on windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
705 latter OS has a different model for `forking processes |
a735476a0e6e
note about fork on windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
706 <https://en.wikipedia.org/wiki/Fork_(system_call)>`__. |
a735476a0e6e
note about fork on windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
462
diff
changeset
|
707 |
555
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
708 Using slave processes |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
709 ..................... |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
710 |
351 | 711 Let us consider the following sample Python script that makes a |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
712 CPU-intensive computation on a REST callback: |
351 | 713 |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
714 .. literalinclude:: python/multiprocessing-1.py |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
715 :language: python |
351 | 716 |
717 .. highlight:: text | |
718 | |
719 Calling this REST route from the command-line returns the time that is | |
720 needed to compute 30 million times a squared root on your CPU:: | |
721 | |
722 $ curl http://localhost:8042/computation | |
723 computation done in 4.208 seconds | |
724 | |
725 Now, let us call this route three times concurrently (we use bash):: | |
726 | |
727 $ (curl http://localhost:8042/computation & curl http://localhost:8042/computation & curl http://localhost:8042/computation ) | |
728 computation done in 11.262 seconds | |
729 computation done in 12.457 seconds | |
730 computation done in 13.360 seconds | |
731 | |
732 As can be seen, the computation time has tripled. This means that the | |
733 computations were not distributed across the available CPU cores. | |
734 This might seem surprising, as Orthanc is a threaded server (in | |
735 Orthanc, a pool of C++ threads serves concurrent requests). | |
736 | |
737 The explanation is that the Python interpreter (`CPython | |
738 <https://en.wikipedia.org/wiki/CPython>`__ actually) is built on the | |
739 top of a so-called `Global Interpreter Lock (GIL) | |
740 <https://en.wikipedia.org/wiki/Global_interpreter_lock>`__. The GIL is | |
741 basically a mutex that protects all the calls to the Python | |
742 interpreter. If multiple C++ threads from Orthanc call a Python | |
353 | 743 callback, only one can proceed at any given time. Note however that |
744 the GIL only applies to the Python script: The baseline REST API of | |
745 Orthanc is not affected by the GIL. | |
351 | 746 |
747 The solution is to use the `multiprocessing primitives | |
748 <https://docs.python.org/3/library/multiprocessing.html>`__ of Python. | |
749 The "master" Python interpreter that is initially started by the | |
750 Orthanc plugin, can start several `children processes | |
751 <https://en.wikipedia.org/wiki/Process_(computing)>`__, each of these | |
752 processes running a separate Python interpreter. This allows to | |
753 offload intensive computations from the "master" Python interpreter of | |
754 Orthanc onto those "slave" interpreters. The ``multiprocessing`` | |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
755 library is actually quite straightforward to use: |
351 | 756 |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
757 .. literalinclude:: python/multiprocessing-2.py |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
758 :language: python |
351 | 759 |
760 .. highlight:: text | |
761 | |
762 Here is now the result of calling this route three times concurrently:: | |
763 | |
764 $ (curl http://localhost:8042/computation & curl http://localhost:8042/computation & curl http://localhost:8042/computation ) | |
765 computation done in 4.211 seconds | |
766 computation done in 4.215 seconds | |
767 computation done in 4.225 seconds | |
768 | |
769 As can be seen, the calls to the Python computation now fully run in | |
770 parallel (the time is cut down from 12 seconds to 4 seconds, the same | |
771 as for one isolated request). | |
772 | |
773 Note also how the ``multiprocessing`` library allows to make a fine | |
774 control over the computational resources that are available to the | |
775 Python script: The number of "slave" interpreters can be easily | |
776 changed in the constructor of the ``multiprocessing.Pool`` object, and | |
777 are fully independent of the threads used by the Orthanc server. | |
778 | |
555
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
779 Obviously, an in-depth discussion about the ``multiprocessing`` |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
780 library is out of the scope of this document. There are many |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
781 references available on Internet. Also, note that ``threading`` is not |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
782 useful here, as Python multithreading is also limited by the GIL, and |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
783 is more targeted at dealing with costly I/O operations or with the |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
784 :ref:`scheduling of commands <python-scheduler>`. |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
785 |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
786 |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
787 Slave processes and the "orthanc" module |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
788 ........................................ |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
789 |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
790 Very importantly, pay attention to the fact that **only the "master" |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
791 Python interpreter has access to the Orthanc SDK**. The "slave" |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
792 processes have no access to the ``orthanc`` module. |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
793 |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
794 You must write your Python plugin so as that all the calls to |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
795 ``orthanc`` are moved from the slaves process to the master |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
796 process. For instance, here is how you would parse a DICOM file in a |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
797 slave process: |
352 | 798 |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
799 .. literalinclude:: python/multiprocessing-3.py |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
800 :language: python |
352 | 801 |
802 Communication primitives such as ``multiprocessing.Queue`` are | |
803 available to exchange messages from the "slave" Python interpreters to | |
555
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
804 the "master" Python interpreter for more advanced scenarios. |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
805 |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
806 NB: Starting with release 3.0 of the Python plugin, it is possible to |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
807 call the REST API of Orthanc from a slave process in a more direct |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
808 way. The function ``orthanc.GenerateRestApiAuthorizationToken()`` can |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
809 be used to create an authorization token that provides full access to |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
810 the REST API of Orthanc (without have to set credentials in your |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
811 plugin). Any HTTP client library for Python, such as `requests |
6fb469a3c382
Python plugin: documentation of orthanc.GenerateRestApiAuthorizationToken()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
554
diff
changeset
|
812 <https://requests.readthedocs.io/en/master/>`__, can then be used to |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
813 access the REST API of Orthanc. Here is a minimal example: |
352 | 814 |
702
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
815 .. literalinclude:: python/multiprocessing-4.py |
6e02cd89eb6a
moving python samples in separate files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
701
diff
changeset
|
816 :language: python |