Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from simple_salesforce import Salesforce
|
3 |
+
|
4 |
+
# Initialize Flask App
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Initialize Salesforce connection
|
8 |
+
sf = Salesforce(username='your_username', password='your_password', security_token='your_token')
|
9 |
+
|
10 |
+
# Route for the home page
|
11 |
+
@app.route('/')
|
12 |
+
def index():
|
13 |
+
return render_template('index.html')
|
14 |
+
|
15 |
+
# Route to handle user input and get recipes from Salesforce
|
16 |
+
@app.route('/get_recipes', methods=['GET'])
|
17 |
+
def get_recipes():
|
18 |
+
ingredients = request.args.get('ingredients').split(',') # Get comma-separated ingredients
|
19 |
+
normalized_ingredients = [i.strip().lower() for i in ingredients]
|
20 |
+
|
21 |
+
# Query Salesforce to find matching recipes
|
22 |
+
query = f"SELECT Name, Description, Ingredients__c FROM Recipe WHERE Ingredients__c LIKE '%{'% OR Ingredients__c LIKE '.join(normalized_ingredients)}%'"
|
23 |
+
recipes = sf.query(query)
|
24 |
+
|
25 |
+
result = []
|
26 |
+
for recipe in recipes['records']:
|
27 |
+
result.append({
|
28 |
+
'name': recipe['Name'],
|
29 |
+
'description': recipe['Description'],
|
30 |
+
'ingredients': recipe['Ingredients__c'].split(','),
|
31 |
+
})
|
32 |
+
|
33 |
+
return jsonify(result)
|
34 |
+
|
35 |
+
if __name__ == '__main__':
|
36 |
+
app.run(debug=True)
|