diff --git a/cellxgene_gateway/filecrawl.py b/cellxgene_gateway/filecrawl.py index 3b0234c..8ce66b7 100644 --- a/cellxgene_gateway/filecrawl.py +++ b/cellxgene_gateway/filecrawl.py @@ -23,7 +23,7 @@ def recurse_dir(path): x[:-4] if x.endwith('.csv') else x), "path": os.path.join(full_path, x).replace(env.cellxgene_data, ""), } for x in os.listdir(full_path) if x.endswith('.csv') and os.path.isfile(os.path.join(full_path, x))] - return [{"name":'new', "path":full_path.replace(env.cellxgene_data, "")}] + entries + return [{"name":'new', "class":'new', "path":full_path.replace(env.cellxgene_data, "")}] + entries def make_entry(el): full_path = os.path.join(path, el) @@ -56,12 +56,14 @@ def render_entries(entries): def get_url(entry): return f"view/{ entry['path'].lstrip('/') }" +def get_class(entry): + return f" class='{entry['class']}'" if 'class' in entry else '' def render_annotations(entry): if len(entry['annotations']) > 0: - return ' | annotations: ' + ", ".join([f"{a['name']}" for a in entry['annotations']]) + return ' | annotations: ' + ", ".join([f"{a['name']}" for a in entry['annotations']]) else: - return ''; + return '' def render_entry(entry): if entry["type"] == "file": diff --git a/cellxgene_gateway/gateway.py b/cellxgene_gateway/gateway.py index 441cdd9..b3d34ec 100644 --- a/cellxgene_gateway/gateway.py +++ b/cellxgene_gateway/gateway.py @@ -17,6 +17,7 @@ import json from flask import ( Flask, redirect, + make_response, render_template, request, send_from_directory, @@ -163,11 +164,16 @@ if env.enable_upload: def filecrawl(): entries = recurse_dir(env.cellxgene_data) rendered_html = render_entries(entries) - return render_template( + resp = make_response(render_template( "filecrawl.html", extra_scripts=get_extra_scripts(), rendered_html=rendered_html, - ) + )) + resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" + resp.headers["Pragma"] = "no-cache" + resp.headers["Expires"] = "0" + resp.headers['Cache-Control'] = 'public, max-age=0' + return resp @app.route("/filecrawl/") def do_filecrawl(path): diff --git a/cellxgene_gateway/path_util.py b/cellxgene_gateway/path_util.py index cf93d1c..08e5ccf 100644 --- a/cellxgene_gateway/path_util.py +++ b/cellxgene_gateway/path_util.py @@ -28,12 +28,13 @@ def get_key(path): if trimmed.endswith('.h5ad') and data_file_exists(trimmed): # 1) somedir/dataset.h5ad: a dataset return CacheKey(trimmed, trimmed, None) - elif trimmed.endswith('.csv') and data_file_exists(trimmed): + elif trimmed.endswith('.csv'): - # 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotaitons file. + # 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotations file. annotations_dir = os.path.split(trimmed)[0] dataset = make_h5ad(annotations_dir) if data_file_exists(dataset): + data_dir_ensure(annotations_dir) return CacheKey(trimmed, dataset, trimmed) elif trimmed.endswith('_annotations') and data_dir_exists(trimmed): # 3) somedir/dataset_annotations: an annotation directory. The corresponding h5ad must exist, but the directory may not. @@ -74,6 +75,10 @@ def data_dir_exists(dataset): file_path = os.path.join(env.cellxgene_data, dataset) validate_is_dir(file_path) return True +def data_dir_ensure(dataset): + file_path = os.path.join(env.cellxgene_data, dataset) + if not os.path.exists(file_path): + os.makedirs(file_path) def get_file_path(key): dataset = key.dataset @@ -82,12 +87,9 @@ def get_file_path(key): return file_path def get_annotation_file_path(key): - print(f"getting annotaiton_file_path for {key.annotation_file}") if key.annotation_file is None: return None if key.annotation_file == '': return '' file_path = os.path.join(env.cellxgene_data, key.annotation_file) - print(f"getting annotaiton_file_path for {key}, file_path {file_path}") - validate_is_file(file_path) return file_path diff --git a/cellxgene_gateway/static/js/annotation.js b/cellxgene_gateway/static/js/annotation.js new file mode 100644 index 0000000..9698a13 --- /dev/null +++ b/cellxgene_gateway/static/js/annotation.js @@ -0,0 +1,21 @@ +// neandertal javascript +const new_annotation_callback = (() =>{ + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + const pickn = (c, n) => n==0?'':c.substr(Math.random()*c.length,1) + pickn(c,n-1); + const suffix = `_${pickn(chars,8)}.csv`; + return (e) => { + e.preventDefault(); + const el = $(e.target); + const href = el.attr('href'); + const base = prompt(`Name your annotations collection\nnote: the suffix "${suffix}" will be appended`); + if (base !== null && base.length > 0) { + if (/^[0-9a-zA-Z_]+$/.test(base)) { + window.location = `${href}/${base}${suffix}`; + } else { + alert("Error: name must match ^[0-9a-zA-Z_]+$\nthat is, only numbers, letters and underscore are allowed") + } + } + return false; + } +})() + diff --git a/cellxgene_gateway/templates/filecrawl.html b/cellxgene_gateway/templates/filecrawl.html index a6a5948..7d59e34 100644 --- a/cellxgene_gateway/templates/filecrawl.html +++ b/cellxgene_gateway/templates/filecrawl.html @@ -16,7 +16,8 @@ {% for script in extra_scripts %} - {% endfor %} + {% endfor %} + @@ -41,5 +42,10 @@
  • homepage
  • +