Browse Source

choosing configurations

kora 1 year ago
parent
commit
d21d1614b9
3 changed files with 82 additions and 15 deletions
  1. 15 3
      app.py
  2. 10 0
      parsers/get_form_fields.py
  3. 57 12
      templates/index.html.j2

+ 15 - 3
app.py

@@ -1,6 +1,6 @@
 from flask import Flask, request, redirect, render_template, url_for, send_file
 from parsers.CSV_to_RDF_generico import parse, parsefromfile
-from parsers.get_form_fields import getFormFields
+from parsers.get_form_fields import getFormFields, getConfigs
 
 
 app = Flask(__name__)
@@ -14,6 +14,7 @@ def main():
     confFilePath = appath + configurationFolder + configurationFileName
 
     try:
+        configurations = getConfigs(configurationFolder)
         formFields = getFormFields(confFilePath)
     except:
         return redirect(url_for('error', message='no valid config file found'))
@@ -52,14 +53,14 @@ def main():
 
         return redirect(url_for('main', downloadFile=outFileName, downloadUrl=url_for('download')))
 
-    return render_template('index.html.j2', data=formFields)
+    return render_template('index.html.j2', data=formFields, configs=configurations)
 
 
 @app.route('/error/', methods=['POST','GET'])
 def error():
     if request.method == 'POST':
         return redirect(url_for('main'))
-    else:
+    elif request.method == 'GET':
         return render_template('error.html.j2', message=request.args.get('message'))
 
 
@@ -70,6 +71,17 @@ def download():
     outFilePath = appath + 'samples/RDF/' + outFileName
     return send_file(outFilePath, as_attachment=True)
 
+@app.route('/getdata/', methods=['GET'])
+def getData():
+    appath = app.root_path + '/'
+    configurationFolder = 'parsers/configuration_files/'
+    configurationFileName = request.args.get("confFileName")
+    confFilePath = appath + configurationFolder + configurationFileName
+
+    appia = getFormFields(confFilePath)
+    return appia
+
+
 
 if __name__ == '__main__':
     app.run()

+ 10 - 0
parsers/get_form_fields.py

@@ -3,6 +3,10 @@
 # Utilities to read/write csv files
 import json
 import re
+import os
+
+
+## FUNCTIONS
 
 def getFormFields(mapfilename):
 
@@ -43,3 +47,9 @@ def csvsFromVals(value: str):
     val_csvs = list( filter(lambda str1: str1.startswith('csv:'), val_parts) )
     val_csvs = map(lambda str1: str1.replace('csv:', ''), val_csvs)
     return val_csvs
+
+
+def getConfigs(pathName):
+    configDirFiles = os.listdir(pathName)
+    configFiles = [file1 for file1 in configDirFiles if file1.endswith('.json')]
+    return configFiles

+ 57 - 12
templates/index.html.j2

@@ -15,22 +15,29 @@
 
   {% include 'main_header.html.j2' %}
 
+
   <form id="fileForm" method="POST" enctype = "multipart/form-data" style="margin: 2em;  border-style: groove;  padding: 2em;">
     <div>
       Upload a CSV file:
       <fieldset>
-        <input type="file" id="FILE" name="FILE"><br/><br/>
+        <input type="file" id="FILE" name="FILE">
+        <b>Data Map: </b>
+        <select name="configs" id="configs" onChange="cambio()">
+          {% for entry in configs: %}
+            <option value={{entry}}>{{entry}}</option>
+          {% endfor %}
+        </select>
       </fieldset>
       <input id="submitted" type="submit" value="Activate Transformation" >
     </div>
     <div>
         <br/>... or manually enter the data of a record:<br/><br/>
-      <fieldset>
-        <legend>Record:</legend>
+      <fieldset id="fields">
+        {# <legend>Record:</legend>
         {% for entry in data: %}
-          <label for="nome">{{entry}}</label><br>
+          <label>{{entry}}</label><br>
           <input type="text" id="{{entry}}" name="{{entry}}" placeholder="data"><br><br>
-        {% endfor %}
+        {% endfor %} #}
       </fieldset>
     </div>
   </form>
@@ -40,15 +47,53 @@
     const downloadName = params.get('downloadFile');
     if(downloadName){
       document.getElementById("fileForm").reset()
-      $.get(params.get('downloadUrl'), { filename: downloadName }).done(function(data){
-        let link = document.createElement('a');
-        link.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(data);
-        link.download = downloadName;
-        link.click();
-      }
-    );
+      $.get(params.get('downloadUrl'), { filename: downloadName })
+        .done(
+          data => {
+            let link = document.createElement('a');
+            link.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(data);
+            link.download = downloadName;
+            link.click();
+          }
+        );
       alert("Transformation carried out successfully");
     }
+    cambio();
+
+    function cambio(){
+      console.log("CAMBIO!");
+      let fn = document.getElementById("configs").value;
+      console.log(fn);
+      $.get('/getdata/', { confFileName: fn })
+        .done(
+          data => {
+            let fields = document.getElementById("fields");
+            fields.innerHTML = "";
+            let legend = document.createElement('legend');
+            legend.appendChild(document.createTextNode('Record:'));
+            fields.appendChild(legend);
+            for(let field of data){
+              console.log('field', field);
+              let fieldLabel = document.createElement("label");
+              fieldLabel.appendChild(document.createTextNode(field));
+              fields.appendChild(fieldLabel);
+              fields.appendChild(document.createElement('br'));
+              let fieldInput = document.createElement('input')
+              fieldInput.setAttribute('type', 'text');
+              fieldInput.setAttribute('id', field);
+              fieldInput.setAttribute('name', field);
+              fieldInput.setAttribute('placeholder', 'data');
+              fields.appendChild(fieldInput);
+              fields.appendChild(document.createElement('br'));
+              fields.appendChild(document.createElement('br'));
+            }
+
+          }
+        );
+      var p = $("<p>Paragraph 3</p>");
+      // append the paragraph to the parent
+      $("#parent").append(p);
+    }
   </script>
 
 </body>