Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import sqlite3
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# Database setup
|
| 7 |
DB_PATH = "students.db"
|
|
@@ -37,6 +38,27 @@ def register_student(name, age, image_file):
|
|
| 37 |
conn.close()
|
| 38 |
return f"Student {name} registered successfully!"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Initialize the database
|
| 41 |
setup_database()
|
| 42 |
|
|
@@ -44,7 +66,7 @@ setup_database()
|
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
with gr.Row():
|
| 46 |
gr.Markdown("### Student Registration")
|
| 47 |
-
|
| 48 |
with gr.Row():
|
| 49 |
name_input = gr.Textbox(label="Name", placeholder="Enter the student's name")
|
| 50 |
age_input = gr.Number(label="Age", precision=0)
|
|
@@ -59,5 +81,22 @@ with gr.Blocks() as demo:
|
|
| 59 |
outputs=output,
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Launch the Gradio app
|
| 63 |
demo.launch()
|
|
|
|
| 2 |
import sqlite3
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
|
| 7 |
# Database setup
|
| 8 |
DB_PATH = "students.db"
|
|
|
|
| 38 |
conn.close()
|
| 39 |
return f"Student {name} registered successfully!"
|
| 40 |
|
| 41 |
+
def view_students():
|
| 42 |
+
conn = sqlite3.connect(DB_PATH)
|
| 43 |
+
cursor = conn.cursor()
|
| 44 |
+
cursor.execute("SELECT id, name, age, image_path FROM students")
|
| 45 |
+
records = cursor.fetchall()
|
| 46 |
+
conn.close()
|
| 47 |
+
|
| 48 |
+
# Format data for display
|
| 49 |
+
if records:
|
| 50 |
+
data = []
|
| 51 |
+
for record in records:
|
| 52 |
+
student_id, name, age, image_path = record
|
| 53 |
+
if image_path and os.path.exists(image_path):
|
| 54 |
+
image = Image.open(image_path)
|
| 55 |
+
else:
|
| 56 |
+
image = None
|
| 57 |
+
data.append({"ID": student_id, "Name": name, "Age": age, "Image": image})
|
| 58 |
+
return pd.DataFrame(data).drop(columns=["Image"]).to_string(), data # Display text table without images
|
| 59 |
+
else:
|
| 60 |
+
return "No records found.", None
|
| 61 |
+
|
| 62 |
# Initialize the database
|
| 63 |
setup_database()
|
| 64 |
|
|
|
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
with gr.Row():
|
| 68 |
gr.Markdown("### Student Registration")
|
| 69 |
+
|
| 70 |
with gr.Row():
|
| 71 |
name_input = gr.Textbox(label="Name", placeholder="Enter the student's name")
|
| 72 |
age_input = gr.Number(label="Age", precision=0)
|
|
|
|
| 81 |
outputs=output,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
with gr.Row():
|
| 85 |
+
gr.Markdown("### View Registered Students")
|
| 86 |
+
|
| 87 |
+
view_button = gr.Button("View Records")
|
| 88 |
+
record_output = gr.Textbox(label="Student Records")
|
| 89 |
+
image_output = gr.Image(label="Images", type="pil", visible=False) # For displaying images if needed
|
| 90 |
+
|
| 91 |
+
def view_records_handler():
|
| 92 |
+
records, images = view_students()
|
| 93 |
+
return records
|
| 94 |
+
|
| 95 |
+
view_button.click(
|
| 96 |
+
view_records_handler,
|
| 97 |
+
inputs=[],
|
| 98 |
+
outputs=record_output,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
# Launch the Gradio app
|
| 102 |
demo.launch()
|