Cistern commited on
Commit
ec269ee
·
verified ·
1 Parent(s): 295139a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -12,7 +12,7 @@ app = FastAPI()
12
  conn = sqlite3.connect(DB_PATH, check_same_thread=False)
13
  cursor = conn.cursor()
14
 
15
- # ⚡ Create indexes for faster filtering
16
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_city ON owners(City);")
17
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_name ON owners(Name);")
18
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_mobile ON owners([Mobile No]);")
@@ -20,18 +20,16 @@ conn.commit()
20
 
21
 
22
  # =========================
23
- # API endpoint: fetch data chunk
24
  # =========================
25
  @app.get("/api/data")
26
  def get_data(offset: int = 0, city: str = "", search: str = ""):
27
  query = "SELECT * FROM owners WHERE 1=1"
28
  params = []
29
 
30
- # City filter
31
  if city:
32
  query += " AND LOWER(City) LIKE ?"
33
  params.append(f"%{city.lower()}%")
34
- # Search by Name or Mobile
35
  if search:
36
  query += " AND (LOWER(Name) LIKE ? OR [Mobile No] LIKE ?)"
37
  params.extend([f"%{search.lower()}%", f"%{search}%"])
@@ -40,11 +38,16 @@ def get_data(offset: int = 0, city: str = "", search: str = ""):
40
  params.extend([PAGE_SIZE, offset])
41
 
42
  df = pd.read_sql_query(query, conn, params=params)
 
 
 
 
 
43
  return JSONResponse(df.to_dict(orient="records"))
44
 
45
 
46
  # =========================
47
- # API endpoint: count rows
48
  # =========================
49
  @app.get("/api/count")
50
  def get_count(city: str = "", search: str = ""):
@@ -67,7 +70,7 @@ def get_count(city: str = "", search: str = ""):
67
 
68
 
69
  # =========================
70
- # Frontend page with filters and infinite scroll
71
  # =========================
72
  @app.get("/", response_class=HTMLResponse)
73
  def home():
 
12
  conn = sqlite3.connect(DB_PATH, check_same_thread=False)
13
  cursor = conn.cursor()
14
 
15
+ # Create indexes for fast filtering/searching
16
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_city ON owners(City);")
17
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_name ON owners(Name);")
18
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_mobile ON owners([Mobile No]);")
 
20
 
21
 
22
  # =========================
23
+ # API endpoint: fetch paginated data with filters/search
24
  # =========================
25
  @app.get("/api/data")
26
  def get_data(offset: int = 0, city: str = "", search: str = ""):
27
  query = "SELECT * FROM owners WHERE 1=1"
28
  params = []
29
 
 
30
  if city:
31
  query += " AND LOWER(City) LIKE ?"
32
  params.append(f"%{city.lower()}%")
 
33
  if search:
34
  query += " AND (LOWER(Name) LIKE ? OR [Mobile No] LIKE ?)"
35
  params.extend([f"%{search.lower()}%", f"%{search}%"])
 
38
  params.extend([PAGE_SIZE, offset])
39
 
40
  df = pd.read_sql_query(query, conn, params=params)
41
+
42
+ # Convert unsafe float values to JSON-safe
43
+ df = df.replace([float('inf'), float('-inf')], None) # replace infinity
44
+ df = df.where(pd.notnull(df), None) # replace NaN with None
45
+
46
  return JSONResponse(df.to_dict(orient="records"))
47
 
48
 
49
  # =========================
50
+ # API endpoint: total count for pagination
51
  # =========================
52
  @app.get("/api/count")
53
  def get_count(city: str = "", search: str = ""):
 
70
 
71
 
72
  # =========================
73
+ # Frontend page with infinite scroll + filters
74
  # =========================
75
  @app.get("/", response_class=HTMLResponse)
76
  def home():