123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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
- app = Flask(__name__)
- @app.route('/', methods=['POST','GET'])
- def main():
- appath = app.root_path + '/'
- configurationFolder = 'parsers/configuration_files/'
- configurationFileName = 'configuration.json'
- confFilePath = appath + configurationFolder + configurationFileName
- try:
- formFields = getFormFields(confFilePath)
- except:
- return redirect(url_for('error', message='no valid config file found'))
- if request.method == 'POST':
- try:
- outputFolder = appath + 'samples/RDF/'
- fileFromRequest = request.files['FILE']
- filename = fileFromRequest.filename
- # If a file was uploaded, read data from file
- if filename != '':
- outFileName = filename.replace('.csv', '') + '.ttl'
- outFilePath = outputFolder + outFileName
- inFile = fileFromRequest.read()
- # try to create list of dictionaries keyed by header row
- parsefromfile(confFilePath, formFields, inFile, outFilePath)
- # Else read data from form
- else:
- data = {}
- for field in formFields:
- data[field] = request.form[field]
- check = [val for val in data.values() if val!='']
- if len(check)>0:
- outFileName = 'form_output.ttl'
- outFilePath = outputFolder + outFileName
- parse(confFilePath, formFields, [data], outFilePath)
- else:
- raise Exception("No data")
- except:
- return redirect(url_for('error', message='no CSV data, or CSV input file missing required data'))
- return redirect(url_for('main', download=outFileName))
- return render_template('index.html.j2', data=formFields)
- @app.route('/error/', methods=['POST','GET'])
- def error():
- if request.method == 'POST':
- return redirect(url_for('main'))
- else:
- return render_template('error.html.j2', message=request.args.get('message'))
- @app.route('/download/', methods=['POST', 'GET'])
- def download():
- appath = app.root_path + '/'
- outFileName = request.args.get('filename')
- outFilePath = appath + 'samples/RDF/' + outFileName
- return send_file(outFilePath, as_attachment=True)
- if __name__ == '__main__':
- app.run()
|