about summary refs log tree commit diff
diff options
context:
space:
mode:
authorzsloan2016-10-19 22:11:20 +0000
committerzsloan2016-10-19 22:11:20 +0000
commit921726b7bf2ced0cbb348f366d8a2955400567c3 (patch)
tree5b170a3547db09e3e18c022d822288dc1d791804
parent0d52804f6144eb9d3d01dae254c85806d05dd484 (diff)
downloadgenenetwork2-921726b7bf2ced0cbb348f366d8a2955400567c3.tar.gz
Rewrote export CSV in python
Still need to add metadata and change it to where only selected traits are exported
-rw-r--r--wqflask/wqflask/export_traits.py28
-rw-r--r--wqflask/wqflask/static/new/javascript/search_results.js38
-rw-r--r--wqflask/wqflask/templates/search_result_page.html84
-rw-r--r--wqflask/wqflask/views.py12
4 files changed, 105 insertions, 57 deletions
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"))) }}
 
     <div class="container">
-
         <input type="hidden" name="uc_id" id="uc_id" value="{{ uc_id }}">
 
         <!-- Need to customize text more for other types of searches -->
@@ -54,12 +53,16 @@
           <button class="btn btn-default" id="deselect_all"><span class="glyphicon glyphicon-remove"></span> Deselect All</button>
           <button class="btn btn-default" id="invert"><span class="glyphicon glyphicon-resize-vertical"></span> Invert</button>
           <button class="btn btn-default" id="add" disabled><span class="glyphicon glyphicon-plus-sign"></span> Add</button>
-          <!--<button class="btn btn-default"><span class="glyphicon glyphicon-download"></span> Download Table</button>-->
           <button id="redraw" class="btn btn-default">Reset Columns</button>
           <input type="text" id="searchbox" class="form-control" style="width: 200px; display: inline;" placeholder="Search This Table For ...">
           <input type="text" id="select_top" class="form-control" style="width: 200px; display: inline;" placeholder="Select Top ...">
           <br />
           <br />
+          <form id="export_form" method="POST" action="/export_traits_csv">
+          <input type="hidden" name="export_data" id="export_data" value="">
+          <button class="btn btn-default" id="export_traits">Download CSV</button>
+          </form>
+          <br />
 <!--
           Removing this until more options are added and work correctly
           {% if dataset.type == 'ProbeSet' %}
@@ -82,11 +85,11 @@
                         <th style="background-color: #eeeeee;"></th>
                     {% for header in header_fields %}
                         {% if header == 'Max LRS' %}
-                        <th style="background-color: #eeeeee; text-align: right;">Max<br>LRS</th>
+                        <th data-export="Max LRS" style="background-color: #eeeeee; text-align: right;">Max<br>LRS</th>
                         {% elif header == 'Additive Effect' %}
-                        <th style="background-color: #eeeeee; text-align: right;">Additive<br>Effect<a href="http://genenetwork.org//glossary.html#A" target="_blank"><sup style="color:#f00"> ?</sup></a></th>
+                        <th data-export="Additive Effect" style="background-color: #eeeeee; text-align: right;">Additive<br>Effect<a href="http://genenetwork.org//glossary.html#A" target="_blank"><sup style="color:#f00"> ?</sup></a></th>
                         {% else %}
-                        <th style="background-color: #eeeeee;">{{header}}</th>
+                        <th data-export="{{header}}" style="background-color: #eeeeee;">{{header}}</th>
                         {% endif %}
                     {% endfor %}
                     </tr>
@@ -97,8 +100,8 @@
                     <TR id="trait:{{ this_trait.name }}:{{ this_trait.dataset.name }}">
                         <TD><INPUT TYPE="checkbox" NAME="searchResult" class="checkbox trait_checkbox" style="transform: scale(1.5);" VALUE="{{ data_hmac('{}:{}'.format(this_trait.name, this_trait.dataset.name)) }}">
                         </TD>
-                        <TD align="right">{{ loop.index }}</TD>
-                        <TD>
+                        <TD data-export="{{ loop.index }}" align="right">{{ loop.index }}</TD>
+                        <TD data-export="{{ this_trait.name }}">
                             <a href="{{ url_for('show_trait_page',
                                     trait_id = this_trait.name,
                                     dataset = dataset.name
@@ -107,26 +110,26 @@
                             </a>
                         </TD>
                         {% if dataset.type == 'ProbeSet' %}
-                            <TD>{{ this_trait.symbol }}</TD>
-                            <TD>{{ this_trait.description_display }}</TD>
-                            <TD>{{ this_trait.location_repr }}</TD>
-                            <TD align="right">{{ '%0.3f' % this_trait.mean|float }}</TD>
-                            <TD align="right">{{ '%0.3f' % this_trait.LRS_score_repr|float }}</TD>
-                            <TD>{{ this_trait.LRS_location_repr }}</TD>
-                            <TD align="right">{{ '%0.3f' % this_trait.additive|float }}</TD>
+                            <TD data-export="{{ this_trait.symbol }}">{{ this_trait.symbol }}</TD>
+                            <TD data-export="{{ this_trait.description_display }}">{{ this_trait.description_display }}</TD>
+                            <TD data-export="{{ this_trait.location_repr }}">{{ this_trait.location_repr }}</TD>
+                            <TD data-export="{{ '%0.3f' % this_trait.mean|float }}" align="right">{{ '%0.3f' % this_trait.mean|float }}</TD>
+                            <TD data-export="{{ '%0.3f' % this_trait.LRS_score_repr|float }}" align="right">{{ '%0.3f' % this_trait.LRS_score_repr|float }}</TD>
+                            <TD data-export="{{ this_trait.LRS_location_repr }}">{{ this_trait.LRS_location_repr }}</TD>
+                            <TD data-export="{{ '%0.3f' % this_trait.additive|float }}" align="right">{{ '%0.3f' % this_trait.additive|float }}</TD>
                         {% elif dataset.type == 'Publish' %}
-                            <TD>{{ this_trait.description_display }}</TD>
-                            <TD>{{ this_trait.authors }}</TD>
-                            <TD data-sort="{{ this_trait.pubmed_text }}">
+                            <TD data-export="{{ this_trait.description_display }}">{{ this_trait.description_display }}</TD>
+                            <TD data-export="{{ this_trait.authors }}">{{ this_trait.authors }}</TD>
+                            <TD data-export="{{ this_trait.pubmed_text }}" data-sort="{{ this_trait.pubmed_text }}">
                                 <a href="{{ this_trait.pubmed_link }}">
                                     {{ this_trait.pubmed_text }}
                                 </a>
                             </TD>
-                            <TD>{{ '%0.3f' % this_trait.LRS_score_repr|float }}</TD>
-                            <TD>{{ this_trait.LRS_location_repr }}</TD>
-                            <TD>{{ '%0.3f' % this_trait.additive|float }}</TD>
+                            <TD data-export="{{ '%0.3f' % this_trait.LRS_score_repr|float }}">{{ '%0.3f' % this_trait.LRS_score_repr|float }}</TD>
+                            <TD data-export="{{ this_trait.LRS_location_repr }}">{{ this_trait.LRS_location_repr }}</TD>
+                            <TD data-export="{{ '%0.3f' % this_trait.additive|float }}">{{ '%0.3f' % this_trait.additive|float }}</TD>
                         {% elif dataset.type == 'Geno' %}
-                            <TD>{{ this_trait.location_repr }}</TD>
+                            <TD data-export="{{ this_trait.location_repr }}">{{ this_trait.location_repr }}</TD>
                         {% endif %}
                     </TR>
                 {% endfor %}
@@ -235,18 +238,7 @@
                     { "type": "natural" }
                 ],
                 "order": [[1, "asc" ]],
-                "buttons": [
-                    {
-                       extend: 'csvHtml5',
-                       text: 'Download CSV',
-                       title: 'search_results',
-                       fieldBoundary: '"',
-                       exportOptions: {
-                           columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
-                       }
-                    }
-                ],
-                "sDom": "RZBtir",
+                "sDom": "RZtir",
                 "iDisplayLength": -1,
                 "bDeferRender": true,
                 "bSortClasses": false,
@@ -269,18 +261,7 @@
                     { "type": "natural" }
                 ],
                 "order": [[1, "asc" ]],
-                "buttons": [
-                    {
-                       extend: 'csvHtml5',
-                       text: 'Download CSV',
-                       title: 'search_results',
-                       fieldBoundary: '"',
-                       exportOptions: {
-                           columns: [1, 2, 3, 4, 5, 6, 7]
-                       }
-                    }
-                ],
-                "sDom": "RZBtir",
+                "sDom": "RZtir",
                 "iDisplayLength": -1,
                 "autoWidth": false,
                 "bDeferRender": true,
@@ -298,18 +279,7 @@
                     { "type": "natural", "width": "40%"}
                 ],
                 "order": [[1, "asc" ]],
-                "buttons": [
-                    {
-                       extend: 'csvHtml5',
-                       text: 'Download CSV',
-                       title: 'search_results',
-                       fieldBoundary: '"',
-                       exportOptions: {
-                           columns: [1, 2, 3]
-                       }
-                    }
-                ],
-                "sDom": "RZBtir",
+                "sDom": "RZtir",
                 "iDisplayLength": -1,
                 "autoWidth": true,
                 "bDeferRender": true,
diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py
index fb52165a..ec5035ce 100644
--- a/wqflask/wqflask/views.py
+++ b/wqflask/wqflask/views.py
@@ -33,6 +33,7 @@ import sqlalchemy
 from wqflask import app
 from flask import g, Response, request, make_response, render_template, send_from_directory, jsonify, redirect
 from wqflask import search_results
+from wqflask import export_traits
 from wqflask import gsearch
 from wqflask import update_search_results
 from wqflask import docs
@@ -338,6 +339,17 @@ def export_trait_csv():
                     mimetype='text/csv',
                     headers={"Content-Disposition":"attachment;filename=sample_data.csv"})
 
+@app.route('/export_traits_csv', methods=('POST',))
+def export_traits_csv():
+    """CSV file consisting of the traits from the search result page"""
+    logger.info("In export_traits_csv")
+    logger.info("request.form:", request.form)
+    csv_data = export_traits.export_search_results_csv(request.form)
+
+    return Response(csv_data,
+                    mimetype='text/csv',
+                    headers={"Content-Disposition":"attachment;filename=trait_list.csv"})
+
 @app.route('/export_perm_data', methods=('POST',))
 def export_perm_data():
     """CSV file consisting of the permutation data for the mapping results"""