Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials, firestore
|
| 4 |
+
from flask import Flask, render_template, redirect, url_for
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Initialize Firebase Admin with service account key file
|
| 9 |
+
cred = credentials.Certificate("serviceAccountKey.json") # NOT google-services.json
|
| 10 |
+
firebase_admin.initialize_app(cred)
|
| 11 |
+
db = firestore.client()
|
| 12 |
+
|
| 13 |
+
@app.route("/")
|
| 14 |
+
def index():
|
| 15 |
+
return redirect(url_for("list_all_schemes"))
|
| 16 |
+
|
| 17 |
+
@app.route("/schemes")
|
| 18 |
+
def list_all_schemes():
|
| 19 |
+
# path: data/<docId>/Schemes
|
| 20 |
+
parent_doc_id = "qo3JakPyOUWzdEOFYWbv" # put your real document id here
|
| 21 |
+
schemes_ref = db.collection("data").document(parent_doc_id).collection("Schemes")
|
| 22 |
+
docs = schemes_ref.stream()
|
| 23 |
+
|
| 24 |
+
schemes_list = []
|
| 25 |
+
for doc in docs:
|
| 26 |
+
schemes_list.append({"id": doc.id, "data": doc.to_dict()})
|
| 27 |
+
return render_template("all_schemes.html", schemes=schemes_list)
|
| 28 |
+
|
| 29 |
+
@app.route("/scheme/<scheme_id>")
|
| 30 |
+
def show_scheme(scheme_id):
|
| 31 |
+
# path: data/<docId>/Schemes/<scheme_id>
|
| 32 |
+
parent_doc_id = "qo3JakPyOUWzdEOFYWbv" # put your real document id here
|
| 33 |
+
scheme_ref = db.collection("data").document(parent_doc_id).collection("Schemes").document(scheme_id)
|
| 34 |
+
scheme = scheme_ref.get()
|
| 35 |
+
|
| 36 |
+
if scheme.exists:
|
| 37 |
+
scheme_data = scheme.to_dict()
|
| 38 |
+
return render_template("schemes.html", scheme=scheme_data)
|
| 39 |
+
else:
|
| 40 |
+
return "Scheme not found", 404
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
app.run(debug=True)
|