comparison Sphinx/source/plugins/python.rst @ 469:46949efa5818

Implementing basic paging using a Python script
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Aug 2020 16:42:11 +0200
parents fcc352fd4283
children 03dfb896ca4b
comparison
equal deleted inserted replaced
468:7d8b95b1b22c 469:46949efa5818
238 The route can then be accessed as:: 238 The route can then be accessed as::
239 239
240 $ curl http://localhost:8042/toto 240 $ curl http://localhost:8042/toto
241 ok 241 ok
242 242
243
244 .. _python-changes:
243 245
244 Listening to changes 246 Listening to changes
245 .................... 247 ....................
246 248
247 .. highlight:: python 249 .. highlight:: python
554 <https://docs.python.org/3/library/re.html>`__. It is evidently 556 <https://docs.python.org/3/library/re.html>`__. It is evidently
555 possible to adapt this script in order to use the DICOM conventions 557 possible to adapt this script in order to use the DICOM conventions
556 about `attribute matching 558 about `attribute matching
557 <http://dicom.nema.org/medical/dicom/2019e/output/chtml/part04/sect_C.2.2.2.html>`__. 559 <http://dicom.nema.org/medical/dicom/2019e/output/chtml/part04/sect_C.2.2.2.html>`__.
558 560
559 .. highlight:: python 561 .. highlight:: bash
560 562
561 Here is a sample call to retrieve all the studies that were last 563 Here is a sample call to retrieve all the studies that were last
562 updated in 2019 thanks to this Python script:: 564 updated in 2019 thanks to this Python script::
563 565
564 $ curl http://localhost:8042/tools/find -d '{"Level":"Study","Query":{},"Expand":true,"Metadata":{"LastUpdate":"^2019.*$"}}' 566 $ curl http://localhost:8042/tools/find -d '{"Level":"Study","Query":{},"Expand":true,"Metadata":{"LastUpdate":"^2019.*$"}}'
567
568
569 .. _python-paging:
570
571 Implementing basic paging
572 .........................
573
574 .. highlight:: python
575
576 As explained in the FAQ, the :ref:`Orthanc Explorer interface is
577 low-level <improving-interface>`, and is not adapted for
578 end-users. One common need is to implement paging of studies, which
579 calls for server-side sorting of studies. This can be done using the
580 following sample Python plugin that registers a new route
581 ``/sort-studies`` in the REST API of Orthanc::
582
583 import json
584 import orthanc
585
586 def GetStudyDate(study):
587 if 'StudyDate' in study['MainDicomTags']:
588 return study['MainDicomTags']['StudyDate']
589 else:
590 return ''
591
592 def SortStudiesByDate(output, uri, **request):
593 if request['method'] == 'GET':
594 # Retrieve all the studies
595 studies = json.loads(orthanc.RestApiGet('/studies?expand'))
596
597 # Sort the studies according to the "StudyDate" DICOM tag
598 studies = sorted(studies, key = GetStudyDate)
599
600 # Read the limit/offset arguments provided by the user
601 offset = 0
602 if 'offset' in request['get']:
603 offset = int(request['get']['offset'])
604
605 limit = 0
606 if 'limit' in request['get']:
607 limit = int(request['get']['limit'])
608
609 # Truncate the list of studies
610 if limit == 0:
611 studies = studies[offset : ]
612 else:
613 studies = studies[offset : offset + limit]
614
615 # Return the truncated list of studies
616 output.AnswerBuffer(json.dumps(studies), 'application/json')
617 else:
618 output.SendMethodNotAllowed('GET')
619
620 orthanc.RegisterRestCallback('/sort-studies', SortStudiesByDate)
621
622
623 .. highlight:: bash
624
625 Here is a sample call to this new REST route, that could be issued by
626 any JavaScript framework (the ``json_pp`` command-line pretty-prints a
627 JSON file)::
628
629 $ curl http://localhost:8042/sort-studies | json_pp
630
631 This route also implement paging (i.e. it can limit and offset the
632 returned studies)::
633
634 $ curl 'http://localhost:8042/sort-studies?offset=2&limit=2' | json_pp
635
636 Obviously, this basic sample can be improved in many ways. To improve
637 performance, one could for instance cache the result of
638 ``/studies?expand`` in memory by :ref:`listening to changes
639 <python-changes>` in the list of studies
640 (cf. ``orthanc.ChangeType.NEW_STUDY`` and
641 ``orthanc.ChangeType.DELETED``).
642
565 643
566 644
567 Performance and concurrency 645 Performance and concurrency
568 --------------------------- 646 ---------------------------
569 647