-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
executable file
·86 lines (72 loc) · 2.41 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
The Konpile web application provides a simple interface for uploading
excel, csv, and json data files and compiling them into a
configuration file.
"""
import os
import io
import pathlib
from flask import Flask, render_template, request, send_file
import konpile as kon
application = Flask(__name__)
with open('templates/default.j2', 'r') as f:
default_template = f.read()
@application.route('/', methods=['GET'])
def index():
return render_template('index.html', default=default_template)
@application.route('/process', methods=['POST'])
def process():
"""
Accepts a file of type .xlsx, .csv, or .json and returns a JSON
object containing the processed data. Processing is done by the
konpile module which expects a file path as input.
"""
try:
# Get the file from the request
file = request.files['file']
# Save the file to a temporary location
filepath = pathlib.Path(file.filename)
file.save(filepath)
# Process the file
data = kon.process_file(filepath)
# Delete the temporary file
filepath.unlink()
# Return the data as a JSON object
return data
except Exception as e:
print(e)
# Return a status code of 500
return {'error': str(e)}, 500
@application.route('/compile', methods=['POST'])
def compile():
"""
Accepts a JSON object containing a Jinja2 template and a
JSON object containing the data to be rendered into the template.
"""
try:
# Get the template and data from the request
template = request.json['template']
data = request.json['data']
# Render the template
rendered = kon.render_string(template, data)
# Create a file-like object in memory and convert the file-like
# object to a BytesIO object
print(rendered)
file = io.BytesIO(rendered.encode('utf-8'))
file.seek(0)
# Return the file as an attachment
return send_file(file, download_name='rendered.txt', as_attachment=True)
except Exception as e:
print(e)
# Return a status code of 500
return {'error': str(e)}, 500
def start():
"""
Start the web server.
"""
debugging = os.environ.get('DEBUG', True)
port = os.environ.get('PORT', 8080)
application.run(host='0.0.0.0', port=port, debug=debugging)
if __name__ == '__main__':
start()