app.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from flask import Flask, request, redirect, render_template, url_for
  2. from parsers.CSV_to_RDF_generico import parse, parsefromfile
  3. from parsers.get_form_fields import getFormFields
  4. app = Flask(__name__)
  5. @app.route('/', methods=['POST','GET'])
  6. def main():
  7. appath = app.root_path + '/'
  8. configurationFolder = 'parsers/configuration_files/'
  9. configurationFileName = 'configuration.json'
  10. confFilePath = appath + configurationFolder + configurationFileName
  11. try:
  12. formFields = getFormFields(confFilePath)
  13. except:
  14. return redirect(url_for('error'))
  15. if request.method == 'POST':
  16. outputFolder = 'samples/RDF/'
  17. fileFromRequest = request.files['FILE']
  18. filename = fileFromRequest.filename
  19. if filename != '':
  20. outFileName = fileFromRequest.filename.replace('.csv', '') + '.ttl'
  21. outFilePath = appath + outputFolder + outFileName
  22. inFile = fileFromRequest.read()
  23. # try to create list of dictionaries keyed by header row
  24. parsefromfile(confFilePath, formFields, inFile, outFilePath)
  25. else:
  26. # Da completare
  27. data = {}
  28. for field in formFields:
  29. data[field] = request.form[field]
  30. check = [val for val in data.values() if val!='']
  31. if len(check)>0:
  32. outFilePath = appath + outputFolder + 'form_output.ttl'
  33. parse(confFilePath, formFields,[data], outFilePath)
  34. else:
  35. return redirect(url_for('error'))
  36. return redirect(url_for('main'))
  37. return render_template('index.html', data=formFields)
  38. @app.route('/error/', methods=['POST','GET'])
  39. def error():
  40. if request.method == 'POST':
  41. return redirect(url_for('main'))
  42. else:
  43. return render_template('error.html')
  44. if __name__ == '__main__':
  45. app.run()