-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
58 lines (45 loc) · 1.56 KB
/
index.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
from flask import Flask
from flask import request
from flask import render_template
import search
import json
import os
from flask import redirect, url_for
from werkzeug.utils import secure_filename
import parseIngredients
from image_to_text import convert_to_text
UPLOAD_FOLDER = '/Users/arlenyu/archhacks/images"'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
@app.route('/')
def hello_world():
return render_template("index.html")
@app.route('/', methods=['GET','POST'])
def my_index_post():
try:
text = request.form['text']
name = search.get_food_name(text)
nutrients = json.loads(search.get_nutrients(search.get_food_id(text)))
except:
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
nutrients = json.loads(ast.literal_eval(convert_to_text(os.path.join(app.config['UPLOAD_FOLDER'], filename))))
name = " "
return render_template("")
return render_template("index.html", name = name, nutrients = nutrients)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
if __name__ == "__main__":
app.run()