Skip to content

Instantly share code, notes, and snippets.

@vancuong4662
Last active April 6, 2025 03:19
Show Gist options
  • Select an option

  • Save vancuong4662/af722b010a40d8f2b3cbdd863426fad4 to your computer and use it in GitHub Desktop.

Select an option

Save vancuong4662/af722b010a40d8f2b3cbdd863426fad4 to your computer and use it in GitHub Desktop.
Mẫu code pytthon chạy server Flask đơn giản
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