forked from neuroquery/neuroquery_apps
/
utils.py
27 lines (23 loc) · 809 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import re
import tempfile
import pathlib
from base64 import b64encode
def query_map_filename(s):
s = re.sub(r'([^\sa-zA-Z0-9])+', '', s)
s = s.strip()
s = s.lower()
s = re.sub(r'\s+', '_', s)
s = s[:72]
s = s or 'map'
return "{}.nii.gz".format(s)
def download_img_link(
img, description="brain_map", label="Download brain map"):
with tempfile.TemporaryDirectory() as tmp_dir:
img_path = str(pathlib.Path(tmp_dir) / "image.nii.gz")
img.to_filename(img_path)
with open(img_path, "rb") as f:
img_data = f.read()
file_name = query_map_filename(description)
return ("<a href='data:application/gzip;base64,{}' "
"download='{}'>{}</a>".format(
b64encode(img_data).decode("utf-8"), file_name, label))