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

python samples
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Mar 2020 08:14:49 +0100
parents fff45618262d
children bdf8757449e3
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst	Fri Mar 27 07:26:16 2020 +0100
+++ b/Sphinx/source/plugins/python.rst	Fri Mar 27 08:14:49 2020 +0100
@@ -8,6 +8,12 @@
 
 Work-in-progress.
 
+The Python API is automatically generated from the `Orthanc plugin SDK
+in C
+<https://hg.orthanc-server.com/orthanc/file/Orthanc-1.5.7/Plugins/Include/orthanc/OrthancCPlugin.h>`__
+using the `Clang <https://en.wikipedia.org/wiki/Clang>`__ compiler
+front-end.  The coverage of the C SDK is about 75% (105 functions are
+automatically wrapped in Python out of a total of 139 functions in C).
 
 
 Samples
@@ -50,3 +56,52 @@
   $ curl http://localhost:8042/toto
   ok
 
+  
+Listening to changes
+....................
+
+This sample uploads a DICOM file as soon as Orthanc is started::
+
+   import orthanc
+
+   def OnChange(changeType, level, resource):
+       if changeType == orthanc.ChangeType.ORTHANC_STARTED:
+           print('Started')
+
+           with open('/tmp/sample.dcm', 'rb') as f:
+               orthanc.RestApiPost('/instances', f.read())
+        
+        elif changeType == orthanc.ChangeType.ORTHANC_STOPPED:
+            print('Stopped')
+
+        elif changeType == orthanc.ChangeType.NEW_INSTANCE:
+            print('A new instance was uploaded: %s' % resource)
+
+    orthanc.RegisterOnChangeCallback(OnChange)
+
+
+Accessing the content of a new instance
+---------------------------------------
+
+::
+   
+  import orthanc
+  import json
+  import pprint
+
+  def OnStoredInstance(dicom, instanceId):
+      print('Received instance %s of size %d (transfer syntax %s, SOP class UID %s)' % (
+          instanceId, dicom.GetInstanceSize(),
+          dicom.GetInstanceMetadata('TransferSyntax'),
+          dicom.GetInstanceMetadata('SopClassUid')))
+
+      # Print the origin information
+      if dicom.GetInstanceOrigin() == orthanc.InstanceOrigin.DICOM_PROTOCOL:
+          print('This instance was received through the DICOM protocol')
+      elif dicom.GetInstanceOrigin() == orthanc.InstanceOrigin.REST_API:
+          print('This instance was received through the REST API')
+
+      # Print the DICOM tags
+      pprint.pprint(json.loads(dicom.GetInstanceSimplifiedJson()))
+
+  orthanc.RegisterOnStoredInstanceCallback(OnStoredInstance)