Antoni09 commited on
Commit
3b953cc
·
verified ·
1 Parent(s): 355462b

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +22 -0
server.py CHANGED
@@ -1,6 +1,12 @@
1
  import hashlib
2
  import json
3
  import os
 
 
 
 
 
 
4
  import uuid
5
  from datetime import datetime
6
  from decimal import Decimal, ROUND_HALF_UP, getcontext
@@ -43,6 +49,22 @@ if __name__ == "__main__":
43
  port = int(os.environ.get("PORT", "7860"))
44
  app.run(host="0.0.0.0", port=port, debug=False)
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  getcontext().prec = 10
48
 
 
1
  import hashlib
2
  import json
3
  import os
4
+ from flask import Flask, render_template, request, redirect, url_for
5
+ from sqlalchemy import create_engine, text
6
+
7
+ app = Flask(__name__)
8
+ engine = create_engine(os.environ["DATABASE_URL"], pool_pre_ping=True)
9
+
10
  import uuid
11
  from datetime import datetime
12
  from decimal import Decimal, ROUND_HALF_UP, getcontext
 
49
  port = int(os.environ.get("PORT", "7860"))
50
  app.run(host="0.0.0.0", port=port, debug=False)
51
 
52
+ @app.get("/")
53
+ def index():
54
+ # Pobierz ostatnie notatki i pokaż w HTML
55
+ with engine.begin() as conn:
56
+ rows = conn.execute(text(
57
+ "SELECT id, body, created_at FROM notes ORDER BY id DESC LIMIT 20"
58
+ )).mappings().all()
59
+ return render_template("index.html", notes=rows)
60
+
61
+ @app.post("/add")
62
+ def add():
63
+ body = request.form.get("body","").strip()
64
+ if body:
65
+ with engine.begin() as conn:
66
+ conn.execute(text("INSERT INTO notes (body) VALUES (:b)"), {"b": body})
67
+ return redirect(url_for("index"))
68
 
69
  getcontext().prec = 10
70