Commit 86820641 authored by Romain Loth's avatar Romain Loth

import/export terms table: only *label* column is mandatory + better css

parent 29ff4b0c
...@@ -444,30 +444,53 @@ def import_ngramlists(fname, delimiter=DEFAULT_CSV_DELIM, ...@@ -444,30 +444,53 @@ def import_ngramlists(fname, delimiter=DEFAULT_CSV_DELIM,
n_added_ng = 0 n_added_ng = 0
n_group_relations = 0 n_group_relations = 0
# columntype => int
columns = {}
# load CSV + initial checks # load CSV + initial checks
for i, csv_row in enumerate(ngrams_csv_rows): for i, csv_row in enumerate(ngrams_csv_rows):
# fyi # fyi
n_read_lines +=1 n_read_lines +=1
# print("---------------READ LINE %i" % i) # print("---------------READ LINE %i" % i)
# headers
if i == 0:
n_cols = len(csv_row)
for j, colname in enumerate(csv_row):
if colname in ['label', 'status', 'forms']:
columns[colname] = j
else:
raise ValueError('Wrong header "%s" on line %i (only possible headers are "label", "forms" and "status")' % (colname, n_read_lines))
if 'label' not in columns:
raise ValueError('CSV must contain at least one column with the header "label"')
if not len(csv_row): if not len(csv_row):
continue continue
try: # try:
# £TODO this_list_type optionnel => default="map" # mandatory column
# £TODO pré-diagnostic => retrouver les col_id this_row_label = str(csv_row[columns['label']])
this_list_type = str(csv_row[0])
this_row_label = str(csv_row[1])
this_row_forms = str(csv_row[2])
# string normalizations # other columns or their default values
this_row_label = normalize_terms(normalize_chars(this_row_label)) if 'status' in columns:
this_list_type = str(csv_row[columns['status']])
else:
this_list_type = 'map'
except: if 'forms' in columns:
if i == 0: this_row_forms = str(csv_row[columns['forms']])
print("IMPORT WARN: (skip line) probable header line at CSV %s:l.0" % fname) else:
continue this_row_forms = ''
else:
raise ValueError("Error on CSV read line %i" %n_read_lines) # string normalizations
this_row_label = normalize_terms(normalize_chars(this_row_label))
# except:
# if i == 0:
# print("IMPORT WARN: (skip line) probable header line at CSV %s:l.0" % fname)
# continue
# else:
# raise ValueError("Error on CSV read line %i" % i)
# --- term checking # --- term checking
if not len(this_row_label) > 0: if not len(this_row_label) > 0:
...@@ -475,7 +498,6 @@ def import_ngramlists(fname, delimiter=DEFAULT_CSV_DELIM, ...@@ -475,7 +498,6 @@ def import_ngramlists(fname, delimiter=DEFAULT_CSV_DELIM,
continue continue
# --- check correct list type # --- check correct list type
# £TODO this_list_type optionnel => default="map"
if not this_list_type in ['stop','main','map']: if not this_list_type in ['stop','main','map']:
print("IMPORT WARN: (skip line) wrong list type at CSV %s:l.%i" % (fname, i)) print("IMPORT WARN: (skip line) wrong list type at CSV %s:l.%i" % (fname, i))
continue continue
......
...@@ -80,15 +80,20 @@ class CSVLists(APIView): ...@@ -80,15 +80,20 @@ class CSVLists(APIView):
csv_file = request.data['csvfile'] csv_file = request.data['csvfile']
# import the csv # import the csv
new_lists = import_ngramlists(csv_file) try:
del csv_file new_lists = import_ngramlists(csv_file)
del csv_file
# merge the new_lists onto those of the target corpus
log_msg = merge_ngramlists(new_lists, onto_corpus=corpus_node) # merge the new_lists onto those of the target corpus
log_msg = merge_ngramlists(new_lists, onto_corpus=corpus_node)
return JsonHttpResponse({ return JsonHttpResponse({
'log': log_msg, 'log': log_msg,
}, 200) }, 200)
except Exception as e:
return JsonHttpResponse({
'err': str(e),
}, 400)
......
...@@ -87,7 +87,7 @@ p.note > label { ...@@ -87,7 +87,7 @@ p.note > label {
opacity: 0.3; opacity: 0.3;
} }
tr:hover { #my-ajax-table tr:hover {
cursor: pointer; cursor: pointer;
font-weight: bold; font-weight: bold;
} }
......
...@@ -7,6 +7,53 @@ ...@@ -7,6 +7,53 @@
<link rel="stylesheet" type="text/css" href="{% static "lib/gargantext/tables.css"%}"/> <link rel="stylesheet" type="text/css" href="{% static "lib/gargantext/tables.css"%}"/>
<link rel="stylesheet" type="text/css" href="{% static "lib/gargantext/charts.css"%}"/> <link rel="stylesheet" type="text/css" href="{% static "lib/gargantext/charts.css"%}"/>
<style>
#formatinfos-announce {
font-size: 12px ;
padding-top: .5em;
}
#formatinfos {
background-color: #CCC ;
font-size: 12px ;
padding: 1em;
border-radius: 1em;
margin: 1.5em;
}
#formatinfos table {
margin-left: 2.5em;
margin-bottom: 1em;
margin-top: 1em;
}
#formatinfos tr:hover {
font-weight: normal
}
#formatinfos td {
color: inherit ;
hover: none ;
}
#formatinfos h4 {
font-size: 14px;
color: #777;
font-weight: bold;
}
#formatinfos p {
font-size: 14px;
}
#formatinfos em {
font-weight:bold ;
}
</style>
<script type="text/javascript" src="{% static "lib/d3/d3.js"%}"></script> <script type="text/javascript" src="{% static "lib/d3/d3.js"%}"></script>
<script type="text/javascript" src="{% static "lib/d3/crossfilter.js"%}"></script> <script type="text/javascript" src="{% static "lib/d3/crossfilter.js"%}"></script>
<script type="text/javascript" src="{% static "lib/d3/dc.js"%}"></script> <script type="text/javascript" src="{% static "lib/d3/dc.js"%}"></script>
...@@ -126,7 +173,7 @@ ...@@ -126,7 +173,7 @@
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Import a CSV term list</h3> <h3 id="myModalLabel">Import a Termlist</h3>
</div> </div>
<div class="modal-body" id="uploadform"> <div class="modal-body" id="uploadform">
<form id="csvimportform" <form id="csvimportform"
...@@ -134,8 +181,36 @@ ...@@ -134,8 +181,36 @@
enctype="multipart/form-data" enctype="multipart/form-data"
method="post"> method="post">
{% csrf_token %} {% csrf_token %}
<label>From your disk:</label> <label>From a CSV on your disk:</label>
<input type="file" id="csvfile" accept="text/csv"> <input type="file" id="csvfile" accept="text/csv">
<p id="formatinfos-announce">
<span
id="formatinfos-icon"
class="glyphicon glyphicon-triangle-right"
onclick="toggleFormatInfos()"></span>
More infos about CSV expected format
</p>
<div id="formatinfos" style="display:none;">
<h4>Example table</h4>
<table class="table-condensed note">
<tr><th>status</th> <th>label</th> <th>forms</th></tr>
<tr><td>map</td> <td>barograph</td> <td></td></tr>
<tr><td>map</td> <td>seafaring</td> <td>seafarer|&amp;|ocean travel</td></tr>
<tr><td>main</td> <td>electromagnetic</td> <td>electro-magnetic</td></tr>
</table>
<h4>Remarks</h4>
<ul>
<li>Tabulation is the expected delimiter between columns.</li>
<li>The only mandatory column is <em>label</em>.</li>
<li>If <em>status</em> is absent, default target status is "map"</li>
<li>When a column is there, always add its header on the 1st line: <em>status</em>, <em>label</em>, <em>forms</em></li>
<li>The label will be always added as a form, even if it's not in 'forms' column</li>
<li>The string <em>|&amp;|</em> (3 characters) is the expected delimiter between forms.</li>
</ul>
</div>
<br/> <br/>
<label>From another corpus:</label> <label>From another corpus:</label>
<p>TODO</p> <p>TODO</p>
...@@ -153,6 +228,28 @@ ...@@ -153,6 +228,28 @@
<script type="text/javascript" src="{% static "lib/gargantext/NGrams_dyna_chart_and_table.js" %}"></script> <script type="text/javascript" src="{% static "lib/gargantext/NGrams_dyna_chart_and_table.js" %}"></script>
<script type="text/javascript"> <script type="text/javascript">
var formatInfosOpen = false;
function toggleFormatInfos() {
// when already open => we close
if (formatInfosOpen) {
// hide div
$('#formatinfos').hide()
// change icon
$('#formatinfos-icon')[0].classList.remove('glyphicon-triangle-bottom')
$('#formatinfos-icon')[0].classList.add('glyphicon-triangle-right')
// toggle flag
formatInfosOpen = false;
}
else {
// opposite case
$('#formatinfos').show()
$('#formatinfos-icon')[0].classList.remove('glyphicon-triangle-right')
$('#formatinfos-icon')[0].classList.add('glyphicon-triangle-bottom')
formatInfosOpen = true;
}
}
/* merci c24b ! /* merci c24b !
* Uses csvimportroute variable from the django template * Uses csvimportroute variable from the django template
...@@ -202,7 +299,7 @@ function postCSV(e){ ...@@ -202,7 +299,7 @@ function postCSV(e){
xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken")); xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
}, },
success: function(response) { success: function(response) {
my_html = "<h2 color='green'>IMPORT OK ! </h2>" my_html = '<h3 style="color:green">IMPORT OK</h3>'
my_html += "<p class='note'>" + response['log'].replace(/\n/g, '<br/>') + "</p>" my_html += "<p class='note'>" + response['log'].replace(/\n/g, '<br/>') + "</p>"
my_html += "<p'>(this page will reload in 3s)</p>" my_html += "<p'>(this page will reload in 3s)</p>"
$('#formanswer').html(my_html); $('#formanswer').html(my_html);
...@@ -211,7 +308,10 @@ function postCSV(e){ ...@@ -211,7 +308,10 @@ function postCSV(e){
setTimeout("location.reload(true)", 3000); setTimeout("location.reload(true)", 3000);
}, },
error: function(result) { error: function(result) {
$('#formanswer').html('Erreur'); my_html = '<h3 style="color:red">Error</h3>'
my_html += "<p class='note'>please correct your CSV file and retry</p>"
my_html += "<p>"+ result.responseJSON['err']+"</p>"
$('#formanswer').html(my_html);
console.error(result); console.error(result);
}, },
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment