@@ -12,12 +12,100 @@ import sys
12
12
import traceback
13
13
import urllib
14
14
import cgi
15
- import dft
15
+ import jinja2
16
+ from nibabel import dft
17
+
18
+ def html_unicode (u ):
19
+ return cgi .escape (u .encode ('utf-8' ))
16
20
17
21
# this is the directory containing the DICOM data, or None for all cached data
18
- base_dir = '/Users/ch/Desktop/umms/dft/trunk/data/t '
22
+ base_dir = '/path/to/DICOM '
19
23
base_dir = None
20
24
25
+ template_env = jinja2 .Environment (autoescape = True )
26
+ template_env .filters ['urlquote' ] = urllib .quote
27
+
28
+ index_template = """<html><head><title>data</title></head>
29
+ <body>
30
+ Home
31
+ <br />
32
+ <br />
33
+ {% for p in patients|sort %}
34
+ Patient: <a href="{{ p|urlquote }}/">{{ p }}</a>
35
+ <br />
36
+ {% if patients[p]|length == 1 %}
37
+ 1 study
38
+ {% else %}
39
+ {{ patients[p]|length }} studies
40
+ {% endif %}
41
+ <br />
42
+ {% endfor %}
43
+ </body>
44
+ </html>
45
+ """
46
+
47
+ patient_template = """<html><head><title>data</title></head>
48
+ <body>
49
+ <a href="../">Home</a> -> Patient {{ studies[0].patient_name_or_uid() }}
50
+ <br />
51
+ <br />
52
+ Patient name: {{ studies[0].patient_name }}
53
+ <br />
54
+ Patient ID: {{ studies[0].patient_id }}
55
+ <br />
56
+ Patient birth date: {{ studies[0].patient_birth_date }}
57
+ <br />
58
+ Patient sex: {{ studies[0].patient_sex }}
59
+ <br />
60
+ <ul>
61
+ {% for s in studies %}
62
+ <li><a href="{{ s.date|urlquote }}_{{ s.time|urlquote }}/">Study {{ s.uid }}</a></li>
63
+ <ul>
64
+ <li>Date: {{ s.date }}</li>
65
+ <li>Time: {{ s.time }}</li>
66
+ <li>Comments: {{ s.comments }}</li>
67
+ <li>Series: {{ s.series|length }}</li>
68
+ {% endfor %}
69
+ </ul>
70
+ </body>
71
+ </html>
72
+ """
73
+
74
+ patient_date_time_template = """
75
+ <html><head><title>data</title></head>
76
+ <body>
77
+ <a href="../../">Home</a> -> <a href="../../{{ study.patient_name_or_uid() }}/">Patient {{ study.patient_name_or_uid() }}</a> -> Study {{ study.date}} {{ study.time }}
78
+ <br />
79
+ <br />
80
+ Patient name: <a href="../../{{ study.patient_name_or_uid() }}/">{{ study.patient_name }}</a>
81
+ <br />
82
+ Study UID: {{ study.uid }}
83
+ <br />
84
+ Study date: {{ study.date }}
85
+ <br />
86
+ Study time: {{ study.time }}
87
+ <br />
88
+ Study comments: {{ study.comments }}
89
+ {% if study.series|length == 0 %}
90
+ <br />
91
+ No series.
92
+ {% else %}
93
+ <ul>
94
+ {% for s in study.series %}
95
+ <li>Series {{ s.number }} (<a href="{{ s.number }}/nifti">NIfTI</a>)</li>
96
+ <ul>
97
+ <li>Series UID: {{ s.uid }}</li>
98
+ <li>Series description: {{ s.description }}</li>
99
+ <li>Series dimensions: {{ s.rows }}x{{ s.columns }}x{{ s.storage_instances|length }}</li>
100
+ </ul>
101
+ <img src="{{ s.number }}/png" />
102
+ {% endfor %}
103
+ </ul>
104
+ {% endif %}
105
+ </body>
106
+ </html>
107
+ """
108
+
21
109
class HandlerError :
22
110
23
111
def __init__ (self , status , output ):
@@ -60,28 +148,6 @@ def handler(environ):
60
148
return ('200 OK' , 'image/png' , png (parts [0 ], parts [1 ], parts [2 ]))
61
149
raise HandlerError ('404 Not Found' , "%s not found\n " % environ ['PATH_INFO' ])
62
150
63
- def index (environ ):
64
- patients = {}
65
- for s in dft .get_studies (base_dir ):
66
- patients .setdefault (s .patient_name_or_uid (), []).append (s )
67
- output = ''
68
- output += '<html><head><title>data</title></head>\n '
69
- output += '<body>\n '
70
- output += 'Home\n '
71
- output += '<br />\n '
72
- output += '<br />\n '
73
- for p in sorted (patients ):
74
- output += 'Patient: <a href="%s/">%s</a>\n ' % (urllib .quote (p .encode ('utf-8' )), html_unicode (p ))
75
- output += '<br />\n '
76
- if len (patients [p ]) == 1 :
77
- output += '1 study\n '
78
- else :
79
- output += '%d studies' % len (patients [p ])
80
- output += '<br />\n '
81
- output += '</body>\n '
82
- output += '</html>\n '
83
- return output
84
-
85
151
def study_cmp (a , b ):
86
152
if a .date < b .date :
87
153
return - 1
@@ -93,40 +159,20 @@ def study_cmp(a, b):
93
159
return 1
94
160
return 0
95
161
96
- def html_unicode (u ):
97
- return cgi .escape (u .encode ('utf-8' ))
162
+ def index (environ ):
163
+ patients = {}
164
+ for s in dft .get_studies (base_dir ):
165
+ patients .setdefault (s .patient_name_or_uid (), []).append (s )
166
+ template = template_env .from_string (index_template )
167
+ return template .render (patients = patients ).encode ('utf-8' )
98
168
99
169
def patient (patient ):
100
170
studies = [ s for s in dft .get_studies () if s .patient_name_or_uid () == patient ]
101
171
if len (studies ) == 0 :
102
172
raise HandlerError ('404 Not Found' , 'patient %s not found\n ' % patient )
103
173
studies .sort (study_cmp )
104
- output = ''
105
- output += '<html><head><title>data</title></head>\n '
106
- output += '<body>\n '
107
- output += '<a href="../">Home</a> -> Patient %s\n ' % html_unicode (studies [0 ].patient_name_or_uid ())
108
- output += '<br />\n '
109
- output += '<br />\n '
110
- output += 'Patient name: %s\n ' % html_unicode (studies [0 ].patient_name )
111
- output += '<br />\n '
112
- output += 'Patient ID: %s\n ' % html_unicode (studies [0 ].patient_id )
113
- output += '<br />\n '
114
- output += 'Patient birth date: %s\n ' % html_unicode (studies [0 ].patient_birth_date )
115
- output += '<br />\n '
116
- output += 'Patient sex: %s\n ' % html_unicode (studies [0 ].patient_sex )
117
- output += '<br />\n '
118
- output += '<ul>\n '
119
- for s in studies :
120
- output += '<li><a href="%s_%s/">Study %s</a></li>\n ' % (urllib .quote (s .date ), urllib .quote (s .time ), html_unicode (s .uid ))
121
- output += '<ul>\n '
122
- output += '<li>Date: %s</li>\n ' % html_unicode (s .date )
123
- output += '<li>Time: %s</li>\n ' % html_unicode (s .time )
124
- output += '<li>Comments: %s</li>\n ' % html_unicode (s .comments )
125
- output += '<li>Series: %d</li>\n ' % len (s .series )
126
- output += '</ul>\n '
127
- output += '</body>\n '
128
- output += '</html>\n '
129
- return output
174
+ template = template_env .from_string (patient_template )
175
+ return template .render (studies = studies ).encode ('utf-8' )
130
176
131
177
def patient_date_time (patient , date_time ):
132
178
study = None
@@ -139,38 +185,8 @@ def patient_date_time(patient, date_time):
139
185
break
140
186
if study is None :
141
187
raise HandlerError , ('404 Not Found' , 'study not found' )
142
- output = ''
143
- output += '<html><head><title>data</title></head>\n '
144
- output += '<body>\n '
145
- output += '<a href="../../">Home</a> -> <a href="../../%s/">Patient %s</a> -> Study %s %s\n ' % (urllib .quote (study .patient_name_or_uid ()), html_unicode (study .patient_name_or_uid ()), html_unicode (study .date ), html_unicode (study .time ))
146
- output += '<br />\n '
147
- output += '<br />\n '
148
- output += 'Patient name: <a href="/../%s/">%s</a>\n ' % (urllib .quote (study .patient_name_or_uid ()), html_unicode (study .patient_name ))
149
- output += '<br />\n '
150
- output += 'Study UID: %s\n ' % html_unicode (study .uid )
151
- output += '<br />\n '
152
- output += 'Study date: %s\n ' % html_unicode (study .date )
153
- output += '<br />\n '
154
- output += 'Study time: %s\n ' % html_unicode (study .time )
155
- output += '<br />\n '
156
- output += 'Study comments: %s\n ' % html_unicode (study .comments )
157
- if len (study .series ) == 0 :
158
- output += '<br />\n '
159
- output += 'No series.\n '
160
- else :
161
- output += '<ul>\n '
162
- for s in study .series :
163
- output += '<li>Series %s (<a href="%s/nifti">NIfTI</a>)</li>\n ' % (html_unicode (s .number ), html_unicode (s .number ))
164
- output += '<ul>\n '
165
- output += '<li>Series UID: %s</li>\n ' % html_unicode (s .uid )
166
- output += '<li>Series description: %s</li>\n ' % html_unicode (s .description )
167
- output += '<li>Series dimensions: %dx%dx%d</li>\n ' % (s .rows , s .columns , len (s .storage_instances ))
168
- output += '</ul>\n '
169
- output += '<img src="%s/png" />\n ' % html_unicode (s .number )
170
- output += '</ul>\n '
171
- output += '</body>\n '
172
- output += '</html>\n '
173
- return output
188
+ template = template_env .from_string (patient_date_time_template )
189
+ return template .render (study = study ).encode ('utf-8' )
174
190
175
191
def nifti (patient , date_time , scan ):
176
192
study = None
0 commit comments