Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
-
from flask import Flask, jsonify, request
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
import sqlite3
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
-
app = Flask(__name__)
|
| 7 |
-
CORS(app)
|
| 8 |
|
| 9 |
# Database setup
|
| 10 |
DATABASE = 'restaurant.db'
|
|
@@ -44,47 +40,84 @@ def init_db():
|
|
| 44 |
if not os.path.exists(DATABASE):
|
| 45 |
init_db()
|
| 46 |
|
| 47 |
-
|
| 48 |
def get_categories():
|
| 49 |
with sqlite3.connect(DATABASE) as conn:
|
| 50 |
cursor = conn.cursor()
|
| 51 |
cursor.execute("SELECT * FROM Categories")
|
| 52 |
-
categories = [{"
|
| 53 |
-
return
|
| 54 |
|
| 55 |
-
|
| 56 |
-
def get_items():
|
| 57 |
-
category_id = request.args.get('category_id')
|
| 58 |
with sqlite3.connect(DATABASE) as conn:
|
| 59 |
cursor = conn.cursor()
|
| 60 |
cursor.execute("SELECT * FROM Items WHERE category_id = ?", (category_id,))
|
| 61 |
-
items = [{"
|
| 62 |
-
return
|
| 63 |
-
|
| 64 |
-
@app.route('/update-item', methods=['POST'])
|
| 65 |
-
def update_item():
|
| 66 |
-
data = request.json
|
| 67 |
-
item_id = data.get('id')
|
| 68 |
-
name = data.get('name')
|
| 69 |
-
description = data.get('description')
|
| 70 |
-
price = data.get('price')
|
| 71 |
-
category_id = data.get('category_id')
|
| 72 |
-
image = data.get('image')
|
| 73 |
|
|
|
|
| 74 |
with sqlite3.connect(DATABASE) as conn:
|
| 75 |
cursor = conn.cursor()
|
| 76 |
cursor.execute("UPDATE Items SET name = ?, description = ?, price = ?, category_id = ?, image = ? WHERE id = ?",
|
| 77 |
(name, description, price, category_id, image, item_id))
|
| 78 |
conn.commit()
|
| 79 |
-
return
|
| 80 |
|
| 81 |
-
@app.route('/deals', methods=['GET'])
|
| 82 |
def get_deals():
|
| 83 |
with sqlite3.connect(DATABASE) as conn:
|
| 84 |
cursor = conn.cursor()
|
| 85 |
-
cursor.execute("SELECT * FROM Items LIMIT 3")
|
| 86 |
-
deals = [{"
|
| 87 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import os
|
| 3 |
+
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Database setup
|
| 6 |
DATABASE = 'restaurant.db'
|
|
|
|
| 40 |
if not os.path.exists(DATABASE):
|
| 41 |
init_db()
|
| 42 |
|
| 43 |
+
# Gradio Functions
|
| 44 |
def get_categories():
|
| 45 |
with sqlite3.connect(DATABASE) as conn:
|
| 46 |
cursor = conn.cursor()
|
| 47 |
cursor.execute("SELECT * FROM Categories")
|
| 48 |
+
categories = [{"ID": row[0], "Name": row[1], "Image": row[2]} for row in cursor.fetchall()]
|
| 49 |
+
return categories
|
| 50 |
|
| 51 |
+
def get_items(category_id):
|
|
|
|
|
|
|
| 52 |
with sqlite3.connect(DATABASE) as conn:
|
| 53 |
cursor = conn.cursor()
|
| 54 |
cursor.execute("SELECT * FROM Items WHERE category_id = ?", (category_id,))
|
| 55 |
+
items = [{"ID": row[0], "Name": row[1], "Description": row[2], "Price": row[3], "Image": row[5]} for row in cursor.fetchall()]
|
| 56 |
+
return items
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
def update_item(item_id, name, description, price, category_id, image):
|
| 59 |
with sqlite3.connect(DATABASE) as conn:
|
| 60 |
cursor = conn.cursor()
|
| 61 |
cursor.execute("UPDATE Items SET name = ?, description = ?, price = ?, category_id = ?, image = ? WHERE id = ?",
|
| 62 |
(name, description, price, category_id, image, item_id))
|
| 63 |
conn.commit()
|
| 64 |
+
return f"Item {item_id} updated successfully!"
|
| 65 |
|
|
|
|
| 66 |
def get_deals():
|
| 67 |
with sqlite3.connect(DATABASE) as conn:
|
| 68 |
cursor = conn.cursor()
|
| 69 |
+
cursor.execute("SELECT * FROM Items LIMIT 3")
|
| 70 |
+
deals = [{"ID": row[0], "Name": row[1], "Description": row[2], "Price": row[3], "Image": row[5]} for row in cursor.fetchall()]
|
| 71 |
+
return deals
|
| 72 |
+
|
| 73 |
+
# Gradio Interface
|
| 74 |
+
def display_categories():
|
| 75 |
+
categories = get_categories()
|
| 76 |
+
return categories
|
| 77 |
+
|
| 78 |
+
def display_items(category_id):
|
| 79 |
+
items = get_items(category_id)
|
| 80 |
+
return items
|
| 81 |
+
|
| 82 |
+
def update_item_interface(item_id, name, description, price, category_id, image):
|
| 83 |
+
return update_item(item_id, name, description, price, category_id, image)
|
| 84 |
+
|
| 85 |
+
def display_deals():
|
| 86 |
+
return get_deals()
|
| 87 |
+
|
| 88 |
+
# Gradio App
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
+
gr.Markdown("# Dynamic Restaurant App")
|
| 91 |
+
|
| 92 |
+
with gr.Tab("Categories"):
|
| 93 |
+
gr.Markdown("### View Categories")
|
| 94 |
+
categories_output = gr.JSON()
|
| 95 |
+
gr.Button("Show Categories").click(display_categories, outputs=categories_output)
|
| 96 |
+
|
| 97 |
+
with gr.Tab("Items"):
|
| 98 |
+
gr.Markdown("### View Items by Category")
|
| 99 |
+
category_id_input = gr.Number(label="Enter Category ID")
|
| 100 |
+
items_output = gr.JSON()
|
| 101 |
+
gr.Button("Show Items").click(display_items, inputs=category_id_input, outputs=items_output)
|
| 102 |
+
|
| 103 |
+
with gr.Tab("Update Item"):
|
| 104 |
+
gr.Markdown("### Update an Item")
|
| 105 |
+
item_id_input = gr.Number(label="Item ID")
|
| 106 |
+
name_input = gr.Textbox(label="Name")
|
| 107 |
+
description_input = gr.Textbox(label="Description")
|
| 108 |
+
price_input = gr.Number(label="Price")
|
| 109 |
+
category_id_input_update = gr.Number(label="Category ID")
|
| 110 |
+
image_input = gr.Textbox(label="Image")
|
| 111 |
+
update_output = gr.Textbox(label="Update Status")
|
| 112 |
+
gr.Button("Update Item").click(
|
| 113 |
+
update_item_interface,
|
| 114 |
+
inputs=[item_id_input, name_input, description_input, price_input, category_id_input_update, image_input],
|
| 115 |
+
outputs=update_output
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
with gr.Tab("Deals"):
|
| 119 |
+
gr.Markdown("### View Best Deals")
|
| 120 |
+
deals_output = gr.JSON()
|
| 121 |
+
gr.Button("Show Deals").click(display_deals, outputs=deals_output)
|
| 122 |
|
| 123 |
+
demo.launch()
|
|
|