Last active
April 6, 2025 03:19
-
-
Save vancuong4662/af722b010a40d8f2b3cbdd863426fad4 to your computer and use it in GitHub Desktop.
Mẫu code pytthon chạy server Flask đơn giản
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import webbrowser | |
| import threading | |
| import os | |
| from flask import Flask, render_template, request, jsonify | |
| app = Flask(__name__) | |
| # Bật chế độ debug | |
| app.config['DEBUG'] = True | |
| # Địa chỉ server Flask | |
| HOST = "127.0.0.1" | |
| PORT = 4662 | |
| @app.route("/") | |
| def index(): | |
| return render_template("index.html") | |
| # Chỉ mở trình duyệt nếu đây là tiến trình chính (tránh auto-reload) | |
| def open_browser(): | |
| if os.environ.get("WERKZEUG_RUN_MAIN") == "true": # Chỉ chạy khi Flask khởi động lần đầu | |
| webbrowser.open(f"http://{HOST}:{PORT}/", new=2) | |
| if __name__ == "__main__": | |
| threading.Thread(target=open_browser).start() | |
| app.run(host=HOST, port=PORT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment