Spaces:
Runtime error
Runtime error
| from flask import Flask, request, jsonify, render_template_string | |
| from datetime import datetime | |
| app = Flask(__name__) | |
| # Mock database | |
| posts = [] | |
| # API to post an item | |
| def create_post(): | |
| data = request.json | |
| post = { | |
| "id": len(posts) + 1, | |
| "title": data.get("title"), | |
| "description": data.get("description"), | |
| "price": data.get("price"), | |
| "location": data.get("location"), | |
| "timestamp": datetime.utcnow().isoformat() | |
| } | |
| posts.append(post) | |
| return jsonify({"success": True, "post": post}), 201 | |
| # API to get posts by location | |
| def get_posts(): | |
| location = request.args.get("location", "").lower() | |
| filtered = [p for p in posts if location in p["location"].lower()] | |
| return jsonify({"posts": filtered}) | |
| # HTML frontend | |
| html_template = """ | |
| <!DOCTYPE html> | |
| <html lang="th"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>ตลาดข้างบ้าน</title> | |
| </head> | |
| <body> | |
| <h2>📦 ลงขายของ</h2> | |
| <form id="postForm"> | |
| <label>ชื่อสินค้า:</label><br> | |
| <input type="text" id="title"><br> | |
| <label>รายละเอียด:</label><br> | |
| <input type="text" id="description"><br> | |
| <label>ราคา (บาท):</label><br> | |
| <input type="number" id="price"><br> | |
| <label>สถานที่ (เช่น หอ A):</label><br> | |
| <input type="text" id="location"><br><br> | |
| <button type="submit">โพสต์เลย</button> | |
| </form> | |
| <h2>🔍 ค้นหาของใกล้คุณ</h2> | |
| <input type="text" id="searchLocation" placeholder="ใส่ชื่อสถานที่"> | |
| <button onclick="searchPosts()">ค้นหา</button> | |
| <div id="results"></div> | |
| <script> | |
| document.getElementById("postForm").onsubmit = async function(e) { | |
| e.preventDefault(); | |
| const data = { | |
| title: document.getElementById("title").value, | |
| description: document.getElementById("description").value, | |
| price: document.getElementById("price").value, | |
| location: document.getElementById("location").value | |
| }; | |
| const res = await fetch('/api/post', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(data) | |
| }); | |
| const result = await res.json(); | |
| alert("โพสต์เรียบร้อยแล้ว!"); | |
| }; | |
| async function searchPosts() { | |
| const location = document.getElementById("searchLocation").value; | |
| const res = await fetch(`/api/posts?location=${location}`); | |
| const result = await res.json(); | |
| let html = "<ul>"; | |
| result.posts.forEach(post => { | |
| html += `<li><b>${post.title}</b> - ${post.price} บาท<br>${post.description}<br><i>${post.location}</i><hr></li>`; | |
| }); | |
| html += "</ul>"; | |
| document.getElementById("results").innerHTML = html; | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| def index(): | |
| return render_template_string(html_template) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=8080) |