Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 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 |
PAGE_SIZE = 200 # rows per page
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
# Connect
|
| 12 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 13 |
cursor = conn.cursor()
|
| 14 |
|
| 15 |
-
# ⚡ Create
|
| 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 |
-
#
|
| 24 |
# =========================
|
| 25 |
-
|
|
|
|
| 26 |
query = "SELECT * FROM owners WHERE 1=1"
|
| 27 |
params = []
|
| 28 |
|
| 29 |
-
# City filter
|
| 30 |
if city:
|
| 31 |
query += " AND LOWER(City) LIKE ?"
|
| 32 |
params.append(f"%{city.lower()}%")
|
| 33 |
-
|
| 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,13 +38,14 @@ def fetch_rows(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 df
|
| 44 |
|
| 45 |
|
| 46 |
# =========================
|
| 47 |
-
#
|
| 48 |
# =========================
|
| 49 |
-
|
|
|
|
| 50 |
query = "SELECT COUNT(*) FROM owners WHERE 1=1"
|
| 51 |
params = []
|
| 52 |
|
|
@@ -57,62 +56,124 @@ def count_rows(city: str = "", search: str = ""):
|
|
| 57 |
query += " AND (LOWER(Name) LIKE ? OR [Mobile No] LIKE ?)"
|
| 58 |
params.extend([f"%{search.lower()}%", f"%{search}%"])
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
# =========================
|
| 65 |
-
#
|
| 66 |
# =========================
|
| 67 |
@app.get("/", response_class=HTMLResponse)
|
| 68 |
-
def home(
|
| 69 |
-
# Get query parameters
|
| 70 |
-
offset = int(request.query_params.get("offset", 0))
|
| 71 |
-
city = request.query_params.get("city", "")
|
| 72 |
-
search = request.query_params.get("search", "")
|
| 73 |
-
|
| 74 |
-
# Fetch data
|
| 75 |
-
df = fetch_rows(offset, city, search)
|
| 76 |
-
total = count_rows(city, search)
|
| 77 |
-
|
| 78 |
-
# HTML page
|
| 79 |
html = """
|
| 80 |
<html>
|
| 81 |
<head>
|
| 82 |
-
<title>Car Owners Data</title>
|
| 83 |
<style>
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
th {
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
</style>
|
| 89 |
</head>
|
| 90 |
<body>
|
| 91 |
<h1>📋 Car Owners Data</h1>
|
| 92 |
-
<p>Total rows matching filter: {total}</p>
|
| 93 |
<div class="filter">
|
| 94 |
-
<
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
<input type="submit" value="Filter">
|
| 98 |
-
<input type="hidden" name="offset" value="0">
|
| 99 |
-
</form>
|
| 100 |
</div>
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
return HTMLResponse(html)
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import pandas as pd
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 5 |
|
| 6 |
DB_PATH = "CarOwners_Full_Final.db"
|
| 7 |
PAGE_SIZE = 200 # rows per page
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Connect SQLite
|
| 12 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 13 |
cursor = conn.cursor()
|
| 14 |
|
| 15 |
+
# ⚡ Create indexes for fast search
|
| 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 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 |
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 |
+
return JSONResponse(df.to_dict(orient="records"))
|
| 42 |
|
| 43 |
|
| 44 |
# =========================
|
| 45 |
+
# API endpoint: count rows
|
| 46 |
# =========================
|
| 47 |
+
@app.get("/api/count")
|
| 48 |
+
def get_count(city: str = "", search: str = ""):
|
| 49 |
query = "SELECT COUNT(*) FROM owners WHERE 1=1"
|
| 50 |
params = []
|
| 51 |
|
|
|
|
| 56 |
query += " AND (LOWER(Name) LIKE ? OR [Mobile No] LIKE ?)"
|
| 57 |
params.extend([f"%{search.lower()}%", f"%{search}%"])
|
| 58 |
|
| 59 |
+
if params:
|
| 60 |
+
cursor.execute(query, tuple(params))
|
| 61 |
+
else:
|
| 62 |
+
cursor.execute(query)
|
| 63 |
+
total = cursor.fetchone()[0]
|
| 64 |
+
return {"total": total}
|
| 65 |
|
| 66 |
|
| 67 |
# =========================
|
| 68 |
+
# Frontend page
|
| 69 |
# =========================
|
| 70 |
@app.get("/", response_class=HTMLResponse)
|
| 71 |
+
def home():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
html = """
|
| 73 |
<html>
|
| 74 |
<head>
|
| 75 |
+
<title>Car Owners Data - Infinite Scroll</title>
|
| 76 |
<style>
|
| 77 |
+
body { font-family: Arial; margin: 20px; }
|
| 78 |
+
table { border-collapse: collapse; width: 100%; }
|
| 79 |
+
th, td { border: 1px solid #ddd; padding: 8px; }
|
| 80 |
+
th { background-color: #f2f2f2; }
|
| 81 |
+
#loader { text-align: center; padding: 10px; }
|
| 82 |
+
.filter { margin-bottom: 20px; }
|
| 83 |
</style>
|
| 84 |
</head>
|
| 85 |
<body>
|
| 86 |
<h1>📋 Car Owners Data</h1>
|
|
|
|
| 87 |
<div class="filter">
|
| 88 |
+
<label>City: <input type="text" id="city"></label>
|
| 89 |
+
<label>Search (Name or Mobile): <input type="text" id="search"></label>
|
| 90 |
+
<button onclick="applyFilter()">Apply</button>
|
|
|
|
|
|
|
|
|
|
| 91 |
</div>
|
| 92 |
+
<table id="data-table">
|
| 93 |
+
<thead id="thead">
|
| 94 |
+
<tr>
|
| 95 |
+
<th>Name</th>
|
| 96 |
+
<th>Mobile No</th>
|
| 97 |
+
<th>Address</th>
|
| 98 |
+
<th>Pin Code</th>
|
| 99 |
+
<th>City</th>
|
| 100 |
+
<th>Submodel</th>
|
| 101 |
+
<th>Model</th>
|
| 102 |
+
<th>Assettype</th>
|
| 103 |
+
<th>Misstatus</th>
|
| 104 |
+
<th>Tenor</th>
|
| 105 |
+
<th>Roi</th>
|
| 106 |
+
<th>Irr</th>
|
| 107 |
+
<th>Noadvemi</th>
|
| 108 |
+
<th>Emiamt</th>
|
| 109 |
+
<th>Netltv</th>
|
| 110 |
+
<th>Mfrsubded</th>
|
| 111 |
+
<th>Mfrsubnded</th>
|
| 112 |
+
<th>Dlrsubded</th>
|
| 113 |
+
<th>Dlrsubnded</th>
|
| 114 |
+
<th>Dsasubded</th>
|
| 115 |
+
<th>Promotiondesc</th>
|
| 116 |
+
<th>Product</th>
|
| 117 |
+
</tr>
|
| 118 |
+
</thead>
|
| 119 |
+
<tbody id="tbody"></tbody>
|
| 120 |
+
</table>
|
| 121 |
+
<div id="loader">Loading...</div>
|
| 122 |
+
|
| 123 |
+
<script>
|
| 124 |
+
let offset = 0;
|
| 125 |
+
const PAGE_SIZE = 200;
|
| 126 |
+
let loading = false;
|
| 127 |
+
let cityFilter = "";
|
| 128 |
+
let searchFilter = "";
|
| 129 |
+
|
| 130 |
+
async function loadData() {
|
| 131 |
+
if (loading) return;
|
| 132 |
+
loading = true;
|
| 133 |
+
document.getElementById('loader').innerText = "Loading...";
|
| 134 |
+
let response = await fetch(`/api/data?offset=${offset}&city=${cityFilter}&search=${searchFilter}`);
|
| 135 |
+
let data = await response.json();
|
| 136 |
+
let tbody = document.getElementById('tbody');
|
| 137 |
+
|
| 138 |
+
for (let row of data) {
|
| 139 |
+
let tr = document.createElement('tr');
|
| 140 |
+
for (let key in row) {
|
| 141 |
+
let td = document.createElement('td');
|
| 142 |
+
td.innerText = row[key] === null ? "" : row[key];
|
| 143 |
+
tr.appendChild(td);
|
| 144 |
+
}
|
| 145 |
+
tbody.appendChild(tr);
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
if (data.length < PAGE_SIZE) {
|
| 149 |
+
document.getElementById('loader').innerText = "No more data";
|
| 150 |
+
} else {
|
| 151 |
+
document.getElementById('loader').innerText = "Scroll down to load more...";
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
offset += PAGE_SIZE;
|
| 155 |
+
loading = false;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
// Infinite scroll
|
| 159 |
+
window.onscroll = function() {
|
| 160 |
+
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 50) {
|
| 161 |
+
loadData();
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
function applyFilter() {
|
| 166 |
+
cityFilter = document.getElementById('city').value;
|
| 167 |
+
searchFilter = document.getElementById('search').value;
|
| 168 |
+
offset = 0;
|
| 169 |
+
document.getElementById('tbody').innerHTML = "";
|
| 170 |
+
loadData();
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Initial load
|
| 174 |
+
loadData();
|
| 175 |
+
</script>
|
| 176 |
+
</body>
|
| 177 |
+
</html>
|
| 178 |
+
"""
|
| 179 |
return HTMLResponse(html)
|