Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
dummy_data = [["Alice", "Springs"], ["Max", "Brown"]]
|
| 7 |
+
|
| 8 |
+
def fetch_data():
|
| 9 |
+
"""Fetch data from the data store.
|
| 10 |
+
In this case it's just an array.
|
| 11 |
+
NOTE: This should happen at page load."""
|
| 12 |
+
return dummy_data
|
| 13 |
+
|
| 14 |
+
def create_item(name, surname):
|
| 15 |
+
"""Create an item in the data store"""
|
| 16 |
+
dummy_data.append([name, surname])
|
| 17 |
+
return dummy_data
|
| 18 |
+
|
| 19 |
+
def update_item(out):
|
| 20 |
+
"""Update an item in the data store"""
|
| 21 |
+
dummy_data.clear()
|
| 22 |
+
for item in out.values.tolist():
|
| 23 |
+
dummy_data.append(item)
|
| 24 |
+
# handle the empty element dataframe returns
|
| 25 |
+
if dummy_data[-1] == ["", ""]:
|
| 26 |
+
dummy_data.pop(-1)
|
| 27 |
+
return dummy_data
|
| 28 |
+
|
| 29 |
+
def delete_item(name, surname):
|
| 30 |
+
"""Delete an item from the data store"""
|
| 31 |
+
if [name, surname] in dummy_data:
|
| 32 |
+
dummy_data.remove([name, surname])
|
| 33 |
+
return dummy_data
|
| 34 |
+
|
| 35 |
+
def filter_with_prefix(prefix):
|
| 36 |
+
"""Return all the item whoes surnames starting with prefix
|
| 37 |
+
else return an empty list"""
|
| 38 |
+
filtered_data = []
|
| 39 |
+
for idx, lst in enumerate(dummy_data):
|
| 40 |
+
if re.search(f"^{prefix}", lst[1], re.IGNORECASE):
|
| 41 |
+
filtered_data.append(dummy_data[idx])
|
| 42 |
+
return filtered_data
|
| 43 |
+
|
| 44 |
+
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("Built by: {}".format(@kysmatter))
|
| 46 |
+
out = gr.Dataframe(
|
| 47 |
+
headers=["Name", "Surname"],
|
| 48 |
+
datatype=["str", "str"],
|
| 49 |
+
col_count=(2, "fixed"),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
demo.load(fetch_data, None, out)
|
| 53 |
+
|
| 54 |
+
filter_prefix = gr.Text(label="Filter prefix")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
name = gr.Text(label="Name")
|
| 58 |
+
surname = gr.Text(label="Surname")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
create_btn = gr.Button("Create")
|
| 61 |
+
update_btn = gr.Button("Update")
|
| 62 |
+
delete_btn = gr.Button("Delete")
|
| 63 |
+
|
| 64 |
+
create_btn.click(create_item, [name, surname], out)
|
| 65 |
+
update_btn.click(update_item, out, out)
|
| 66 |
+
delete_btn.click(delete_item, [name, surname], out)
|
| 67 |
+
filter_prefix.change(filter_with_prefix, filter_prefix, out)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|