sivakumar330 commited on
Commit
abb0ffc
·
verified ·
1 Parent(s): 279108c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -27
app.py CHANGED
@@ -1,70 +1,59 @@
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
- import json
4
- import os
5
 
6
- app = Flask(__name__)
7
  CORS(app)
8
 
9
- # ==============================
10
- # SAFE JSON FILE LOADING
11
- # ==============================
12
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
13
  JSON_PATH = os.path.join(BASE_DIR, "doctors.json")
14
 
15
- if not os.path.exists(JSON_PATH):
16
- raise FileNotFoundError(f"doctors.json not found at {JSON_PATH}")
17
-
18
  with open(JSON_PATH, "r", encoding="utf-8") as f:
19
  DOCTORS = json.load(f)
20
 
21
- # ==============================
22
  # API : GET DOCTORS
23
- # ==============================
24
  @app.route("/doctors", methods=["GET"])
25
  def get_doctors():
26
  district = request.args.get("district")
27
  disease = request.args.get("disease", "").lower()
28
 
29
  if not district:
30
- return jsonify({"error": "district is required"}), 400
31
 
32
- # 1️⃣ District filter
33
  result = [
34
  d for d in DOCTORS
35
  if d.get("district", "").lower() == district.lower()
36
  ]
37
 
38
- # 2️⃣ Disease filter (optional)
39
  if disease:
40
  result = [
41
  d for d in result
42
  if any(disease in s.lower() for s in d.get("specialist", []))
43
  ]
44
 
45
- # 3️⃣ Google Map URL (lat, lon)
46
  for d in result:
47
- lat = d.get("lat")
48
- lon = d.get("lon")
49
- if lat and lon:
50
- d["map_url"] = f"https://www.google.com/maps?q={lat},{lon}"
51
- else:
52
- d["map_url"] = None
53
 
54
  return jsonify({
55
  "count": len(result),
56
  "doctors": result
57
  })
58
 
59
- # ==============================
60
- # HEALTH CHECK (OPTIONAL)
61
- # ==============================
62
  @app.route("/")
63
  def health():
64
  return jsonify({"status": "API running"})
65
 
66
- # ==============================
67
- # RUN (LOCAL ONLY)
68
- # ==============================
69
  if __name__ == "__main__":
70
  app.run(host="0.0.0.0", port=5000, debug=True)
 
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
+ import json, os
 
4
 
5
+ app = Flask(__name__, static_folder="static", static_url_path="/static")
6
  CORS(app)
7
 
8
+ # -----------------------------
9
+ # LOAD JSON SAFELY
10
+ # -----------------------------
11
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
12
  JSON_PATH = os.path.join(BASE_DIR, "doctors.json")
13
 
 
 
 
14
  with open(JSON_PATH, "r", encoding="utf-8") as f:
15
  DOCTORS = json.load(f)
16
 
17
+ # -----------------------------
18
  # API : GET DOCTORS
19
+ # -----------------------------
20
  @app.route("/doctors", methods=["GET"])
21
  def get_doctors():
22
  district = request.args.get("district")
23
  disease = request.args.get("disease", "").lower()
24
 
25
  if not district:
26
+ return jsonify({"error": "district required"}), 400
27
 
28
+ # District filter
29
  result = [
30
  d for d in DOCTORS
31
  if d.get("district", "").lower() == district.lower()
32
  ]
33
 
34
+ # Disease filter
35
  if disease:
36
  result = [
37
  d for d in result
38
  if any(disease in s.lower() for s in d.get("specialist", []))
39
  ]
40
 
41
+ # Add image + map url
42
  for d in result:
43
+ d["map_url"] = f"https://www.google.com/maps?q={d['lat']},{d['lon']}"
44
+ d["image_url"] = f"/static/images/{d['image']}"
 
 
 
 
45
 
46
  return jsonify({
47
  "count": len(result),
48
  "doctors": result
49
  })
50
 
51
+ # -----------------------------
52
+ # HEALTH CHECK
53
+ # -----------------------------
54
  @app.route("/")
55
  def health():
56
  return jsonify({"status": "API running"})
57
 
 
 
 
58
  if __name__ == "__main__":
59
  app.run(host="0.0.0.0", port=5000, debug=True)