Skip to content

Commit

Permalink
download: Add individual CSV's
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Nov 8, 2022
1 parent 7b005fa commit 4f1ec15
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
36 changes: 28 additions & 8 deletions cove_ofds/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,28 +292,37 @@ def __init__(self, supplied_data):
self.csvs_zip_filename = os.path.join(
self.supplied_data.data_dir(), "flatten", "flattened.csvs.zip"
)
self.output_dir = os.path.join(
self.supplied_data.data_dir(), "flatten", "flattened"
)

def process(self, process_data: dict) -> dict:

# TODO don't run if already done
output_dir = os.path.join(self.supplied_data.data_dir(), "flatten", "flattened")
os.makedirs(output_dir, exist_ok=True)

os.makedirs(self.output_dir, exist_ok=True)

flatten_kwargs = {
"output_name": output_dir,
"output_name": self.output_dir,
"root_list_path": "networks",
}

flattentool.flatten(process_data["json_data_filename"], **flatten_kwargs)

# Make Zip file of all CSV files
with zipfile.ZipFile(self.csvs_zip_filename, "w") as out_zip:
for f in os.listdir(output_dir):
if os.path.isfile(os.path.join(output_dir, f)) and f.endswith(".csv"):
out_zip.write(os.path.join(output_dir, f), arcname=f)
for f in self._get_list_csv_filenames():
out_zip.write(os.path.join(self.output_dir, f), arcname=f)

return process_data

def _get_list_csv_filenames(self):
return [
f
for f in os.listdir(self.output_dir)
if os.path.isfile(os.path.join(self.output_dir, f)) and f.endswith(".csv")
]

def get_context(self):
context = {}
# XLSX
Expand Down Expand Up @@ -342,13 +351,24 @@ def get_context(self):
context["can_download_ods"] = False
# CSVs
if os.path.exists(self.csvs_zip_filename):
context["can_download_csvs_zip"] = True
context["can_download_csvs"] = True
context["download_csvs_zip_url"] = os.path.join(
self.supplied_data.data_url(), "flatten", "flattened.csvs.zip"
)
context["download_csvs_zip_size"] = os.stat(ods_filename).st_size
context["download_csv_individual_files"] = [
{
"name": f,
"size": os.stat(os.path.join(self.output_dir, f)).st_size,
"url": os.path.join(
self.supplied_data.data_url(), "flatten", "flattened", f
),
}
for f in self._get_list_csv_filenames()
]

else:
context["can_download_csvs_zip"] = False
context["can_download_csvs"] = False
# done!
return context

Expand Down
12 changes: 11 additions & 1 deletion cove_ofds/templates/cove_ofds/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,23 @@ <h4 class="panel-title">
{% endfor %}
</ul>
{% else %}
{% if can_download_csvs_zip %}
{% if can_download_csvs %}
<p>Compressed:</p>
<ul class="list-unstyled">
<li>
<span class="glyphicon glyphicon-download" aria-hidden="true"></span>
<a href="{{download_csvs_zip_url}}">{% trans 'CSVs in a ZIP file' %} ({{ download_csvs_zip_size|filesizeformat }})</a>
</li>
</ul>
<p>Uncompressed:</p>
<ul class="list-unstyled">
{% for file in download_csv_individual_files %}
<li>
<span class="glyphicon glyphicon-download" aria-hidden="true"></span>
<a href="{{file.url}}">{{ file.name }} ({{ file.size|filesizeformat }})</a>
</li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>
Expand Down

0 comments on commit 4f1ec15

Please sign in to comment.