from flask import Flask, request, redirect, render_template, url_for 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')) 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: outFilePath = outputFolder + 'form_output.ttl' parse(confFilePath, formFields, [data], outFilePath) else: raise Exception("No data") except: return redirect(url_for('error')) return redirect(url_for('main', success=True)) return render_template('index.html', 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') if __name__ == '__main__': app.run()