Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,39 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
-
from datetime import datetime
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
-
# In-memory
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def get_users():
|
| 42 |
-
# returns all users
|
| 43 |
-
return jsonify({
|
| 44 |
-
"count": len(USERS),
|
| 45 |
-
"users": USERS
|
| 46 |
-
})
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@app.get("/api/users/upsert")
|
| 50 |
-
def upsert_user_get():
|
| 51 |
-
"""
|
| 52 |
-
Example:
|
| 53 |
-
/api/users/upsert?id=123&first_name=Omar&username=omar123
|
| 54 |
-
"""
|
| 55 |
-
user_id = request.args.get("id", type=int)
|
| 56 |
-
first_name = request.args.get("first_name", default="", type=str)
|
| 57 |
-
username = request.args.get("username", default="", type=str)
|
| 58 |
-
|
| 59 |
-
if not user_id:
|
| 60 |
-
return jsonify({"ok": False, "error": "Missing required param: id"}), 400
|
| 61 |
-
|
| 62 |
-
is_new, u = upsert_user(user_id, first_name, username)
|
| 63 |
-
return jsonify({"ok": True, "is_new": is_new, "user": u})
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
@app.get("/show")
|
| 67 |
-
def show_users():
|
| 68 |
-
# Simple HTML table
|
| 69 |
-
html = """
|
| 70 |
-
<!doctype html>
|
| 71 |
-
<html>
|
| 72 |
-
<head>
|
| 73 |
-
<meta charset="utf-8" />
|
| 74 |
-
<title>Users</title>
|
| 75 |
-
<style>
|
| 76 |
-
body { font-family: Arial, sans-serif; padding: 20px; }
|
| 77 |
-
table { border-collapse: collapse; width: 100%; }
|
| 78 |
-
th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
|
| 79 |
-
th { background: #f5f5f5; }
|
| 80 |
-
.muted { color: #777; }
|
| 81 |
-
</style>
|
| 82 |
-
</head>
|
| 83 |
-
<body>
|
| 84 |
-
<h2>Saved Users ({{count}})</h2>
|
| 85 |
-
<p class="muted">In-memory only (will reset if server restarts).</p>
|
| 86 |
-
<table>
|
| 87 |
-
<thead>
|
| 88 |
-
<tr>
|
| 89 |
-
<th>ID</th>
|
| 90 |
-
<th>First name</th>
|
| 91 |
-
<th>Username</th>
|
| 92 |
-
<th>Created</th>
|
| 93 |
-
<th>Last seen</th>
|
| 94 |
-
</tr>
|
| 95 |
-
</thead>
|
| 96 |
-
<tbody>
|
| 97 |
-
{% for u in users %}
|
| 98 |
-
<tr>
|
| 99 |
-
<td>{{u.id}}</td>
|
| 100 |
-
<td>{{u.first_name}}</td>
|
| 101 |
-
<td>{{('@' + u.username) if u.username else ''}}</td>
|
| 102 |
-
<td>{{u.created_at}}</td>
|
| 103 |
-
<td>{{u.last_seen}}</td>
|
| 104 |
-
</tr>
|
| 105 |
-
{% endfor %}
|
| 106 |
-
</tbody>
|
| 107 |
-
</table>
|
| 108 |
-
</body>
|
| 109 |
-
</html>
|
| 110 |
-
"""
|
| 111 |
-
return render_template_string(html, users=USERS, count=len(USERS))
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
if __name__ == "__main__":
|
| 115 |
-
# Run: python server.py
|
| 116 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect, url_for
|
|
|
|
| 2 |
|
| 3 |
app = Flask(__name__)
|
| 4 |
|
| 5 |
+
# In-memory storage for chat messages
|
| 6 |
+
messages = []
|
| 7 |
+
|
| 8 |
+
# --- Home page ---
|
| 9 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 10 |
+
def index():
|
| 11 |
+
global messages
|
| 12 |
+
html = "<html><body>"
|
| 13 |
+
html += "<h2>Simple Chat Share</h2>"
|
| 14 |
+
|
| 15 |
+
if request.method == 'POST':
|
| 16 |
+
text = request.form.get('text')
|
| 17 |
+
if text:
|
| 18 |
+
messages.append(text)
|
| 19 |
+
return redirect(url_for('index'))
|
| 20 |
+
|
| 21 |
+
# Chat input form
|
| 22 |
+
html += "<form method='POST'>"
|
| 23 |
+
html += "Message:<br>"
|
| 24 |
+
html += "<input type='text' name='text' style='width:300px'>"
|
| 25 |
+
html += "<input type='submit' value='Send'>"
|
| 26 |
+
html += "</form><hr>"
|
| 27 |
+
|
| 28 |
+
# Display messages with copy button
|
| 29 |
+
html += "<h3>Messages:</h3>"
|
| 30 |
+
for i, msg in enumerate(messages[::-1], 1): # newest first
|
| 31 |
+
# Using simple input box to allow easy copy/paste
|
| 32 |
+
html += f"<p>{i}: <input type='text' value='{msg}' readonly style='width:300px'> <button onclick='this.previousElementSibling.select();document.execCommand(\"copy\");'>Copy</button></p>"
|
| 33 |
+
|
| 34 |
+
html += "</body></html>"
|
| 35 |
+
return html
|
| 36 |
+
|
| 37 |
+
# --- Run the app ---
|
| 38 |
+
if __name__ == '__main__':
|
| 39 |
+
app.run(host='0.0.0.0', port=7806)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|