Skip to content

Commit 4f10926

Browse files
committed
now can invalidate cache, added invalidation btn for single viz screen
1 parent 31cbbe1 commit 4f10926

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

app.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ def upload():
6363
return render_template('upload.html')
6464

6565

66+
@app.route('/decache', methods=['GET', 'POST'])
67+
def invalidate_cache():
68+
app.logger.debug(f"Request to invalidate cache on {request.args}")
69+
if not request.args.get('viz_id'):
70+
cache.invalidate_cache()
71+
return redirect("/upload")
72+
viz_id = request.args.get('viz_id')
73+
in_mmif = open(cache.get_cache_path() / viz_id / 'file.mmif', 'rb').read()
74+
cache.invalidate_cache([viz_id])
75+
return upload_file(in_mmif)
76+
77+
6678
@app.route('/display/<viz_id>')
6779
def display(viz_id):
6880
try:
@@ -85,14 +97,14 @@ def render_mmif(mmif_str, viz_id):
8597
mmif = Mmif(mmif_str)
8698
media = documents_to_htmls(mmif, viz_id)
8799
annotations = prep_annotations(mmif, viz_id)
88-
app.logger.debug(f"Prepared Annotations: {annotations.keys()}")
100+
app.logger.debug(f"Prepared Annotations: {[annotation[0] for annotation in annotations]}")
89101
return render_template('player.html',
90-
media=media, annotations=annotations)
102+
media=media, viz_id=viz_id, annotations=annotations)
91103

92104

93105
def upload_file(in_mmif):
94106
# Save file locally
95-
in_mmif_bytes = in_mmif.read()
107+
in_mmif_bytes = in_mmif if isinstance(in_mmif, bytes) else in_mmif.read()
96108
in_mmif_str = in_mmif_bytes.decode('utf-8')
97109
viz_id = hashlib.sha1(in_mmif_bytes).hexdigest()
98110
app.logger.debug(f"Visualization ID: {viz_id}")

cache.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ def get_cache_relpath(full_path):
1717
return str(full_path)[len(app.static_folder):]
1818

1919

20+
def invalidate_cache(viz_ids):
21+
if not viz_ids:
22+
app.logger.debug("Invalidating entire cache.")
23+
shutil.rmtree(get_cache_path())
24+
os.makedirs(get_cache_path())
25+
else:
26+
for v in viz_ids:
27+
app.logger.debug(f"Invalidating {v} from cache.")
28+
shutil.rmtree(get_cache_path() / v)
29+
30+
2031
def set_last_access(path):
2132
with open(os.path.join(path, "last_access.txt"), "w") as f:
2233
f.write(str(time.time()))

templates/player.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
<form action="/upload">
101101
<input type="submit" value="Upload another file" />
102102
</form>
103+
<form action="/decache?viz_id={{ viz_id }}" method="POST">
104+
<input type="submit" value="Invalidate cache and regenerate visualization" />
105+
</form>
103106
</div>
104107
<h1 class="title">Visualizing MMIF</h1>
105108
<div class="right"></div>

0 commit comments

Comments
 (0)