|
import gradio as gr |
|
|
|
class CRUDApp: |
|
def __init__(self): |
|
self.data = [] |
|
|
|
def create(self, name, age): |
|
self.data.append({"name": name, "age": age}) |
|
return self.data |
|
|
|
def read(self): |
|
return self.data |
|
|
|
def update(self, index, name, age): |
|
if index < len(self.data): |
|
self.data[index] = {"name": name, "age": age} |
|
return self.data |
|
|
|
def delete(self, index): |
|
if index < len(self.data): |
|
del self.data[index] |
|
return self.data |
|
|
|
with gr.Blocks() as gradio_interface: |
|
gr.Markdown("CRUD Application") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
name_input = gr.Textbox(label="Name") |
|
age_input = gr.Number(label="Age") |
|
create_button = gr.Button("Create") |
|
|
|
with gr.Column(): |
|
read_button = gr.Button("Read") |
|
update_button = gr.Button("Update") |
|
delete_button = gr.Button("Delete") |
|
|
|
output = gr.Dataframe(label="Data") |
|
|
|
crud_app = CRUDApp() |
|
|
|
def create_event(name, age): |
|
return crud_app.create(name, age) |
|
|
|
def read_event(): |
|
return crud_app.read() |
|
|
|
def update_event(index, name, age): |
|
return crud_app.update(index, name, age) |
|
|
|
def delete_event(index): |
|
return crud_app.delete(index) |
|
|
|
create_button.click(fn=create_event, inputs=[name_input, age_input], outputs=[output]) |
|
read_button.click(fn=read_event, outputs=[output]) |
|
update_button.click(fn=update_event, inputs=[gr.Number(label="Index"), name_input, age_input], outputs=[output]) |
|
delete_button.click(fn=delete_event, inputs=[gr.Number(label="Index")], outputs=[output]) |
|
|
|
gradio_interface.launch() |