Jayabalambika commited on
Commit
327ffd1
·
verified ·
1 Parent(s): c820a62

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import sqlite3
3
+ import pandas as pd
4
+
5
+ # Database file
6
+ db_file = "attendance_records.db"
7
+
8
+ # Helper Functions
9
+ def get_table_names():
10
+ """Retrieve the names of all tables in the database."""
11
+ conn = sqlite3.connect(db_file)
12
+ cursor = conn.cursor()
13
+ cursor.execute("""
14
+ SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';
15
+ """)
16
+ tables = [row[0] for row in cursor.fetchall()]
17
+ conn.close()
18
+ return tables
19
+
20
+ def query_table(table_name):
21
+ """Query the selected table and return its content as a DataFrame."""
22
+ conn = sqlite3.connect(db_file)
23
+ try:
24
+ df = pd.read_sql_query(f"SELECT * FROM {table_name}", conn)
25
+ except Exception as e:
26
+ conn.close()
27
+ return str(e)
28
+ conn.close()
29
+ return df
30
+
31
+ def display_table(table_name):
32
+ """Fetch and display the contents of the selected table."""
33
+ df = query_table(table_name)
34
+ if isinstance(df, pd.DataFrame):
35
+ return df
36
+ else:
37
+ return f"Error fetching data: {df}"
38
+
39
+ # Gradio App
40
+ def create_gradio_app():
41
+ with gr.Blocks() as app:
42
+ gr.Markdown("# Database Query Interface")
43
+ gr.Markdown("### Select a table to view its contents")
44
+
45
+ table_selector = gr.Dropdown(choices=get_table_names(), label="Select Table")
46
+ output = gr.Dataframe(label="Table Data", interactive=False)
47
+
48
+ table_selector.change(
49
+ fn=display_table,
50
+ inputs=table_selector,
51
+ outputs=output,
52
+ )
53
+
54
+ return app
55
+
56
+
57
+ app = create_gradio_app()
58
+ app.launch()