|
import gradio as gr |
|
import duckdb |
|
|
|
|
|
conn = duckdb.connect(database=':memory:') |
|
|
|
|
|
conn.execute('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price REAL)') |
|
|
|
|
|
conn.execute('INSERT INTO products (name, price) VALUES ("Product 1", 10.99), ("Product 2", 9.99), ("Product 3", 12.99)') |
|
|
|
|
|
def read_products(): |
|
cursor = conn.execute('SELECT * FROM products') |
|
return cursor.fetchall() |
|
|
|
|
|
def create_product(name, price): |
|
conn.execute('INSERT INTO products (name, price) VALUES (?, ?)', (name, price)) |
|
conn.commit() |
|
|
|
|
|
def update_product(id, name, price): |
|
conn.execute('UPDATE products SET name = ?, price = ? WHERE id = ?', (name, price, id)) |
|
conn.commit() |
|
|
|
|
|
def delete_product(id): |
|
conn.execute('DELETE FROM products WHERE id = ?', (id,)) |
|
conn.commit() |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("CRUD Interface for Products") |
|
|
|
|
|
name_input = gr.Textbox(label="Product Name") |
|
|
|
|
|
price_input = gr.Number(label="Product Price") |
|
|
|
|
|
create_button = gr.Button("Create Product") |
|
|
|
|
|
update_button = gr.Button("Update Product") |
|
|
|
|
|
delete_button = gr.Button("Delete Product") |
|
|
|
|
|
products_df = gr.DataFrame(label="Products") |
|
|
|
|
|
def create_product_callback(name, price): |
|
create_product(name, price) |
|
return read_products() |
|
|
|
|
|
def update_product_callback(id, name, price): |
|
update_product(id, name, price) |
|
return read_products() |
|
|
|
|
|
def delete_product_callback(id): |
|
delete_product(id) |
|
return read_products() |
|
|
|
|
|
create_button.click(fn=create_product_callback, inputs=[name_input, price_input], outputs=products_df) |
|
update_button.click(fn=update_product_callback, inputs=[gr.Textbox(label="Product ID"), name_input, price_input], outputs=products_df) |
|
delete_button.click(fn=delete_product_callback, inputs=[gr.Textbox(label="Product ID")], outputs=products_df) |
|
|
|
|
|
products_df.render() |
|
|
|
|
|
demo.launch() |