From 921726b7bf2ced0cbb348f366d8a2955400567c3 Mon Sep 17 00:00:00 2001 From: zsloan Date: Wed, 19 Oct 2016 22:11:20 +0000 Subject: Rewrote export CSV in python Still need to add metadata and change it to where only selected traits are exported --- wqflask/wqflask/export_traits.py | 28 ++++++++ .../static/new/javascript/search_results.js | 38 ++++++++++ wqflask/wqflask/templates/search_result_page.html | 84 +++++++--------------- wqflask/wqflask/views.py | 12 ++++ 4 files changed, 105 insertions(+), 57 deletions(-) create mode 100644 wqflask/wqflask/export_traits.py (limited to 'wqflask') diff --git a/wqflask/wqflask/export_traits.py b/wqflask/wqflask/export_traits.py new file mode 100644 index 00000000..280566d7 --- /dev/null +++ b/wqflask/wqflask/export_traits.py @@ -0,0 +1,28 @@ +from __future__ import print_function, division + +import operator +import csv +import xlsxwriter +import StringIO + +import simplejson as json + +from pprint import pformat as pf + +def export_search_results_csv(targs): + + table_data = json.loads(targs['export_data']) + table_headers = table_data['headers'] + table_rows = table_data['rows'] + + buff = StringIO.StringIO() + writer = csv.writer(buff) + + writer.writerow(table_headers) + for trait_info in table_rows: + writer.writerow(trait_info) + + csv_data = buff.getvalue() + buff.close() + + return csv_data \ No newline at end of file diff --git a/wqflask/wqflask/static/new/javascript/search_results.js b/wqflask/wqflask/static/new/javascript/search_results.js index 746564fd..54a61a45 100644 --- a/wqflask/wqflask/static/new/javascript/search_results.js +++ b/wqflask/wqflask/static/new/javascript/search_results.js @@ -155,10 +155,48 @@ $(function() { }); } }; + + export_traits = function() { + trait_data = get_traits_from_table("trait_table") + }; + + get_traits_from_table = function(table_name) { + trait_table = $('#'+table_name); + table_dict = {}; + + headers = []; + trait_table.find('th').each(function () { + if ($(this).data('export')){ + headers.push($(this).data('export')) + } + }); + table_dict['headers'] = headers; + + rows = []; + trait_table.find('tbody tr').each(function (i, tr) { + this_row = []; + $(tr).find('td').each(function(j, td){ + if ($(td).data('export')){ + this_row.push($(td).data('export')); + } + }); + rows.push(this_row); + }); + table_dict['rows'] = rows; + console.log("TABLEDICT:", table_dict); + + json_table_dict = JSON.stringify(table_dict); + $('input[name=export_data]').val(json_table_dict); + + $('#export_form').attr('action', '/export_traits_csv'); + $('#export_form').submit(); + }; + $("#select_all").click(select_all); $("#deselect_all").click(deselect_all); $("#invert").click(invert); $("#add").click(add); $("#remove").click(remove); + $("#export_traits").click(export_traits); $('.trait_checkbox, .btn').click(change_buttons); }); \ No newline at end of file diff --git a/wqflask/wqflask/templates/search_result_page.html b/wqflask/wqflask/templates/search_result_page.html index 708c83eb..13993e83 100644 --- a/wqflask/wqflask/templates/search_result_page.html +++ b/wqflask/wqflask/templates/search_result_page.html @@ -13,7 +13,6 @@ 'GeneNetwork found {}.'.format(numify(results|count, "record", "records"))) }}