Cistern commited on
Commit
b516d8e
ยท
verified ยท
1 Parent(s): bdf19e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -25
app.py CHANGED
@@ -1,12 +1,13 @@
1
  import sqlite3
2
  import pandas as pd
3
  from fastapi import FastAPI
 
4
 
5
  DB_PATH = "CarOwners_Full_Final.db"
6
 
7
  app = FastAPI()
8
 
9
- # Connect to DB
10
  conn = sqlite3.connect(DB_PATH, check_same_thread=False)
11
  cursor = conn.cursor()
12
 
@@ -27,69 +28,92 @@ def get_schema(table_name):
27
  return cursor.fetchall()
28
 
29
 
 
 
 
 
 
 
 
 
30
  # =========================
31
  # GET FIRST 100 ROWS
32
  # =========================
33
  def get_preview(table_name):
34
- query = f"SELECT * FROM {table_name} LIMIT 100;"
35
- return pd.read_sql_query(query, conn)
36
 
37
 
38
  # =========================
39
- # INSPECT DATABASE (startup log)
40
  # =========================
41
  def inspect_db():
42
  tables = get_tables()
43
- print("\n๐Ÿ“Š TABLES FOUND:", tables)
 
 
44
 
45
  for table in tables:
46
  print(f"\n๐Ÿ”น TABLE: {table}")
47
 
48
- # schema
49
  schema = get_schema(table)
50
  print("Columns:")
51
  for col in schema:
52
  print(col)
53
 
54
- # row count
55
- cursor.execute(f"SELECT COUNT(*) FROM {table}")
56
- count = cursor.fetchone()[0]
57
  print("Total rows:", count)
58
 
59
- # preview
60
  df = pd.read_sql_query(f"SELECT * FROM {table} LIMIT 5;", conn)
61
  print(df)
62
 
 
 
63
 
64
- # Run at startup
65
  inspect_db()
66
 
67
 
68
  # =========================
69
- # API ROUTES
70
  # =========================
71
-
72
- @app.get("/")
73
  def home():
74
- return {"message": "Car DB API is running"}
75
 
 
76
 
77
- @app.get("/tables")
78
- def tables():
79
- return {"tables": get_tables()}
80
 
 
 
 
 
 
 
81
 
82
- @app.get("/schema/{table_name}")
83
- def schema(table_name: str):
84
- return {"schema": get_schema(table_name)}
 
 
 
85
 
 
 
 
 
86
 
87
- @app.get("/preview/{table_name}")
88
- def preview(table_name: str):
89
- df = get_preview(table_name)
90
- return df.to_dict(orient="records")
91
 
 
92
 
 
 
 
 
93
  @app.get("/search")
94
  def search(table: str, column: str, query: str):
95
  sql = f"""
 
1
  import sqlite3
2
  import pandas as pd
3
  from fastapi import FastAPI
4
+ from fastapi.responses import HTMLResponse
5
 
6
  DB_PATH = "CarOwners_Full_Final.db"
7
 
8
  app = FastAPI()
9
 
10
+ # Connect DB
11
  conn = sqlite3.connect(DB_PATH, check_same_thread=False)
12
  cursor = conn.cursor()
13
 
 
28
  return cursor.fetchall()
29
 
30
 
31
+ # =========================
32
+ # GET ROW COUNT
33
+ # =========================
34
+ def get_row_count(table_name):
35
+ cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
36
+ return cursor.fetchone()[0]
37
+
38
+
39
  # =========================
40
  # GET FIRST 100 ROWS
41
  # =========================
42
  def get_preview(table_name):
43
+ return pd.read_sql_query(f"SELECT * FROM {table_name} LIMIT 100;", conn)
 
44
 
45
 
46
  # =========================
47
+ # INSPECT DATABASE (PRINT EVERYTHING)
48
  # =========================
49
  def inspect_db():
50
  tables = get_tables()
51
+
52
+ print("\n================ DATABASE INFO ================")
53
+ print("Tables:", tables)
54
 
55
  for table in tables:
56
  print(f"\n๐Ÿ”น TABLE: {table}")
57
 
58
+ # Schema
59
  schema = get_schema(table)
60
  print("Columns:")
61
  for col in schema:
62
  print(col)
63
 
64
+ # Row count
65
+ count = get_row_count(table)
 
66
  print("Total rows:", count)
67
 
68
+ # First 5 rows
69
  df = pd.read_sql_query(f"SELECT * FROM {table} LIMIT 5;", conn)
70
  print(df)
71
 
72
+ print("=============================================")
73
+
74
 
 
75
  inspect_db()
76
 
77
 
78
  # =========================
79
+ # UI HOMEPAGE
80
  # =========================
81
+ @app.get("/", response_class=HTMLResponse)
 
82
  def home():
83
+ tables = get_tables()
84
 
85
+ html = "<h1>๐Ÿš— Car Database Explorer</h1>"
86
 
87
+ if not tables:
88
+ return "<h2>โŒ No tables found. DB not loaded properly.</h2>"
 
89
 
90
+ for table in tables:
91
+ html += f"<h2>๐Ÿ“Š Table: {table}</h2>"
92
+
93
+ # Row count
94
+ count = get_row_count(table)
95
+ html += f"<p><b>Total Rows:</b> {count}</p>"
96
 
97
+ # Schema
98
+ schema = get_schema(table)
99
+ html += "<h3>Columns:</h3><ul>"
100
+ for col in schema:
101
+ html += f"<li>{col}</li>"
102
+ html += "</ul>"
103
 
104
+ # First 100 rows
105
+ df = get_preview(table)
106
+ html += "<h3>First 100 Rows:</h3>"
107
+ html += df.to_html(index=False)
108
 
109
+ html += "<hr>"
 
 
 
110
 
111
+ return html
112
 
113
+
114
+ # =========================
115
+ # SEARCH API
116
+ # =========================
117
  @app.get("/search")
118
  def search(table: str, column: str, query: str):
119
  sql = f"""