Skip to content

Commit 31cbbe1

Browse files
committed
use cached index.html if exists, changed tab name for thumbnails
1 parent c86e1e2 commit 31cbbe1

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

app.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def render_mmif(mmif_str, viz_id):
8585
mmif = Mmif(mmif_str)
8686
media = documents_to_htmls(mmif, viz_id)
8787
annotations = prep_annotations(mmif, viz_id)
88+
app.logger.debug(f"Prepared Annotations: {annotations.keys()}")
8889
return render_template('player.html',
8990
media=media, annotations=annotations)
9091

@@ -94,19 +95,24 @@ def upload_file(in_mmif):
9495
in_mmif_bytes = in_mmif.read()
9596
in_mmif_str = in_mmif_bytes.decode('utf-8')
9697
viz_id = hashlib.sha1(in_mmif_bytes).hexdigest()
97-
app.logger.debug(viz_id)
98+
app.logger.debug(f"Visualization ID: {viz_id}")
9899
path = cache.get_cache_path() / viz_id
99-
os.makedirs(path, exist_ok=True)
100-
set_last_access(path)
101-
with open(path / 'file.mmif', 'w') as in_mmif_file:
102-
in_mmif_file.write(in_mmif_str)
103-
html_page = render_mmif(in_mmif_str, viz_id)
104-
with open(os.path.join(path, "index.html"), "w") as f:
105-
f.write(html_page)
106-
# Perform cleanup
107-
t = Thread(target=cleanup)
108-
t.daemon = True
109-
t.run()
100+
app.logger.debug(f"Visualization Directory: {path}")
101+
try:
102+
os.makedirs(path)
103+
set_last_access(path)
104+
with open(path / 'file.mmif', 'w') as in_mmif_file:
105+
in_mmif_file.write(in_mmif_str)
106+
html_page = render_mmif(in_mmif_str, viz_id)
107+
with open(os.path.join(path, "index.html"), "w") as f:
108+
f.write(html_page)
109+
except FileExistsError:
110+
app.logger.debug("Visualization already cached")
111+
finally:
112+
# Perform cleanup
113+
t = Thread(target=cleanup)
114+
t.daemon = True
115+
t.run()
110116

111117
agent = request.headers.get('User-Agent')
112118
if 'curl' in agent.lower():

ocr.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ def render_ocr(mmif_id, vid_path, view_id, page_number):
130130
"""Iterate through frames and display the contents/alignments."""
131131
# Path for storing temporary images generated by cv2
132132
cv2_vid = cv2.VideoCapture(vid_path)
133-
f = open(cache.get_cache_path() / mmif_id / f"{view_id}-pages.json")
134-
frames_pages = json.load(f)
135-
page = frames_pages[str(page_number)]
133+
tn_data_fname = cache.get_cache_path() / mmif_id / f"{view_id}-pages.json"
134+
thumbnail_pages = json.load(open(tn_data_fname))
135+
page = thumbnail_pages[str(page_number)]
136136
prev_frame_cap = None
137137
path = make_image_directory(mmif_id)
138138
for frame_num, frame in page:
@@ -154,13 +154,10 @@ def render_ocr(mmif_id, vid_path, view_id, page_number):
154154
frame["id"] = pathlib.Path(tf.name).name
155155
prev_frame_cap = frame_cap
156156

157-
return render_template('ocr.html',
158-
vid_path=vid_path,
159-
view_id=view_id,
160-
page=page,
161-
n_pages=len(frames_pages),
162-
page_number=str(page_number),
163-
mmif_id=mmif_id)
157+
tn_page_html = render_template(
158+
'ocr.html', vid_path=vid_path, view_id=view_id, page=page,
159+
n_pages=len(thumbnail_pages), page_number=str(page_number), mmif_id=mmif_id)
160+
return tn_page_html
164161

165162

166163
def make_image_directory(mmif_id):

templates/ocr.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ <h4>
130130
"page_number": parseInt("{{page_number}}"),
131131
"mmif_id": "{{mmif_id}}"
132132
}
133-
if (page == BACKWARD) {
133+
if (page === BACKWARD) {
134134
data["page_number"] -= 1
135135
}
136-
else if (page == FORWARD) {
136+
else if (page === FORWARD) {
137137
data["page_number"] += 1
138138
}
139139
else {

utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def prep_annotations(mmif, viz_id):
146146
for ocr_view in ocr_views:
147147
if not ocr_view.annotations:
148148
continue
149-
tabname = "Frames-%s" % ocr_view.id
149+
tabname = "Thumbnails-%s" % ocr_view.id
150150
visualization = render_template("pre-ocr.html", view_id=ocr_view.id, tabname=tabname, mmif_id=viz_id)
151151
tabs.append((tabname, visualization))
152152
return tabs

0 commit comments

Comments
 (0)