app.py 1.7 KB

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