Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,33 @@
|
|
| 1 |
import sqlite3
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
# Database setup
|
| 6 |
DATABASE = 'restaurant.db'
|
| 7 |
|
|
|
|
|
|
|
| 8 |
def init_db():
|
| 9 |
with sqlite3.connect(DATABASE) as conn:
|
| 10 |
cursor = conn.cursor()
|
| 11 |
-
# Create tables
|
| 12 |
cursor.execute('''
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
''')
|
| 19 |
cursor.execute('''
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
''')
|
| 30 |
-
#
|
| 31 |
cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (1, 'Breakfast', 'breakfast.png')")
|
| 32 |
cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (2, 'Ramen', 'ramen.png')")
|
| 33 |
cursor.execute("INSERT OR IGNORE INTO Items (id, name, description, price, category_id, image) VALUES "
|
|
@@ -36,10 +36,11 @@ def init_db():
|
|
| 36 |
"(2, 'Classic Ramen', 'Delicious ramen with pork and egg.', 12.99, 2, 'ramen.png')")
|
| 37 |
conn.commit()
|
| 38 |
|
| 39 |
-
|
| 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:
|
|
@@ -48,6 +49,7 @@ def get_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()
|
|
@@ -55,69 +57,29 @@ def get_items(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 |
-
|
| 76 |
-
return categories
|
| 77 |
|
| 78 |
-
def display_items(category_id):
|
| 79 |
-
items = get_items(category_id)
|
| 80 |
-
return items
|
| 81 |
|
| 82 |
-
def
|
| 83 |
-
return
|
| 84 |
|
| 85 |
-
def display_deals():
|
| 86 |
-
return get_deals()
|
| 87 |
|
| 88 |
# Gradio App
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
-
gr.Markdown("#
|
| 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()
|
|
|
|
|
|
| 1 |
import sqlite3
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
|
|
|
|
| 5 |
DATABASE = 'restaurant.db'
|
| 6 |
|
| 7 |
+
|
| 8 |
+
# Initialize database
|
| 9 |
def init_db():
|
| 10 |
with sqlite3.connect(DATABASE) as conn:
|
| 11 |
cursor = conn.cursor()
|
|
|
|
| 12 |
cursor.execute('''
|
| 13 |
+
CREATE TABLE IF NOT EXISTS Categories (
|
| 14 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 15 |
+
name TEXT NOT NULL,
|
| 16 |
+
image TEXT NOT NULL
|
| 17 |
+
)
|
| 18 |
''')
|
| 19 |
cursor.execute('''
|
| 20 |
+
CREATE TABLE IF NOT EXISTS Items (
|
| 21 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 22 |
+
name TEXT NOT NULL,
|
| 23 |
+
description TEXT,
|
| 24 |
+
price REAL NOT NULL,
|
| 25 |
+
category_id INTEGER NOT NULL,
|
| 26 |
+
image TEXT NOT NULL,
|
| 27 |
+
FOREIGN KEY (category_id) REFERENCES Categories(id)
|
| 28 |
+
)
|
| 29 |
''')
|
| 30 |
+
# Add sample data
|
| 31 |
cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (1, 'Breakfast', 'breakfast.png')")
|
| 32 |
cursor.execute("INSERT OR IGNORE INTO Categories (id, name, image) VALUES (2, 'Ramen', 'ramen.png')")
|
| 33 |
cursor.execute("INSERT OR IGNORE INTO Items (id, name, description, price, category_id, image) VALUES "
|
|
|
|
| 36 |
"(2, 'Classic Ramen', 'Delicious ramen with pork and egg.', 12.99, 2, 'ramen.png')")
|
| 37 |
conn.commit()
|
| 38 |
|
| 39 |
+
|
| 40 |
if not os.path.exists(DATABASE):
|
| 41 |
init_db()
|
| 42 |
|
| 43 |
+
|
| 44 |
# Gradio Functions
|
| 45 |
def get_categories():
|
| 46 |
with sqlite3.connect(DATABASE) as conn:
|
|
|
|
| 49 |
categories = [{"ID": row[0], "Name": row[1], "Image": row[2]} for row in cursor.fetchall()]
|
| 50 |
return categories
|
| 51 |
|
| 52 |
+
|
| 53 |
def get_items(category_id):
|
| 54 |
with sqlite3.connect(DATABASE) as conn:
|
| 55 |
cursor = conn.cursor()
|
|
|
|
| 57 |
items = [{"ID": row[0], "Name": row[1], "Description": row[2], "Price": row[3], "Image": row[5]} for row in cursor.fetchall()]
|
| 58 |
return items
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def display_categories():
|
| 62 |
+
return get_categories()
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
def display_items(category_id):
|
| 66 |
+
return get_items(category_id)
|
| 67 |
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Gradio App
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("# Restaurant App")
|
| 72 |
|
| 73 |
with gr.Tab("Categories"):
|
| 74 |
+
gr.Markdown("### View All Categories")
|
| 75 |
categories_output = gr.JSON()
|
| 76 |
gr.Button("Show Categories").click(display_categories, outputs=categories_output)
|
| 77 |
+
|
| 78 |
with gr.Tab("Items"):
|
| 79 |
gr.Markdown("### View Items by Category")
|
| 80 |
category_id_input = gr.Number(label="Enter Category ID")
|
| 81 |
items_output = gr.JSON()
|
| 82 |
gr.Button("Show Items").click(display_items, inputs=category_id_input, outputs=items_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
demo.launch()
|
| 85 |
+
|