app.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from flask import Flask, request, redirect, render_template, url_for, send_file
  2. from parsers.CSV_to_RDF_generico import parse, parsefromfile
  3. from parsers.get_form_fields import getFormFields, getConfigs
  4. app = Flask(__name__)
  5. ##########################################################################
  6. # FRONT-END: routes that return a template or redirect to another FE route
  7. ##########################################################################
  8. @app.route('/', methods=['POST','GET'])
  9. def main():
  10. if request.method == 'POST':
  11. try:
  12. appath = app.root_path + '/'
  13. configurationFolder = 'parsers/configuration_files/'
  14. configurationFileName = 'configuration.json'
  15. confFilePath = appath + configurationFolder + configurationFileName
  16. outputFolder = appath + 'samples/RDF/'
  17. fileFromRequest = request.files['FILE']
  18. filename = fileFromRequest.filename
  19. # If a file was uploaded, read data from file
  20. if filename != '':
  21. outFileName = filename.replace('.csv', '') + '.ttl'
  22. outFilePath = outputFolder + outFileName
  23. inFile = fileFromRequest.read()
  24. # try to create list of dictionaries keyed by header row
  25. parsefromfile(confFilePath, formFields, inFile, outFilePath)
  26. # Else read data from form
  27. else:
  28. data = {}
  29. for field in formFields:
  30. data[field] = request.form[field]
  31. check = [val for val in data.values() if val!='']
  32. if len(check)>0:
  33. outFileName = 'form_output.ttl'
  34. outFilePath = outputFolder + outFileName
  35. parse(confFilePath, formFields, [data], outFilePath)
  36. else:
  37. raise Exception("No data")
  38. except:
  39. return redirect(url_for('error', message='no CSV data, or CSV input file missing required data'))
  40. return redirect(url_for('main', downloadFile=outFileName, downloadUrl=url_for('download')))
  41. return render_template('index.html.j2', data=formFields, configs=configurations)
  42. @app.route('/error/', methods=['POST','GET'])
  43. def error():
  44. if request.method == 'POST':
  45. return redirect(url_for('main'))
  46. elif request.method == 'GET':
  47. return render_template('error.html.j2', message=request.args.get('message'))
  48. ###################################
  49. # BACK-END: routes that return data
  50. ###################################
  51. @app.route('/download/', methods=['POST', 'GET'])
  52. def download():
  53. appath = app.root_path + '/'
  54. outFileName = request.args.get('filename')
  55. outFilePath = appath + 'samples/RDF/' + outFileName
  56. return send_file(outFilePath, as_attachment=True)
  57. @app.route('/getdata/', methods=['GET'])
  58. def getData():
  59. appath = app.root_path + '/'
  60. configurationFolder = 'parsers/configuration_files/'
  61. configurationFileName = request.args.get("confFileName")
  62. confFilePath = appath + configurationFolder + configurationFileName
  63. formData = getFormFields(confFilePath)
  64. return formData
  65. if __name__ == '__main__':
  66. app.run()