<gh_stars>0
# Flask Module
# Need install flask before import

from flask import Flask
from flask import request
from flask import redirect
from flask import render_template
from flask import session 
import json

app = Flask(
    __name__,  # __name__ 代表目前執行的module
    static_folder="public",  # Read which folder # 預設是 "/static"
    static_url_path="/"  # Set the path for the folder
)

app.secret_key="Any String but be Secret" # Set a secret key for session

# 首頁：Create a route for "/"


@app.route("/")  # Declorator 以function為基礎提供更多功能
def index():  # Function to handle "/"
    print("請求方法", request.method)
    print("通訊協定", request.scheme)
    print("主機名稱", request.host)
    print("路徑", request.path)
    print("完整網址", request.url)
    print("Browser 和 OS", request.headers.get("user-agent"))
    print("語言偏好", request.headers.get("accept-language"))
    print("引薦網址", request.headers.get("referer"))
    # print(request.method)
    lang = request.headers.get("accept-language")
    if lang.startswith("en"):
        return redirect("/en")  # redirect to index_english
    else:
        return redirect("/zh")  # redirect to index_chinese


@app.route("/en")
def index_english():
    return json.dumps({  # json.dumps to convert json format to dictionary
        "Status": "Ok",
        "Text": "Hello Flask"
    })


@app.route("/zh")
def index_chinses():
    return json.dumps({
        "Status": "Okay",
        "Text": "你好, 歡迎光臨"
    }, ensure_ascii=False)  # turn off ASCII

# Create a route for "/data"


@app.route("/data")
def handleData():  # Function to handle "/data"
    return "My Data"

# 加入動態路徑：Create a DYNAMIC route for "/user/<username>"


@app.route("/user/<username>")  # <username> is a parameter 參數
def handleUser(username):  # Function to handle "/data"
    # return "Hello "+username
    if username == "Dickson":
        return "你好 "+username
    else:
        return "Hello "+username


# 要求字串
@app.route("/getSum")
def getSum():
    maxNumber = request.args.get("max", 100)
    print("Max Number: "+maxNumber)
    maxNumber = int(maxNumber)

    minNumber = request.args.get("min", 1)
    print("Min Number: "+minNumber)
    minNumber = int(minNumber)

    result = 0
    for n in range(minNumber, maxNumber):
        result += n
    return "結果： "+str(result)

# Bring template engine to frontend (Browser)


@app.route("/templateEngine")
def templateEngine():
    # render_template() 可以將templates內的檔案字串傳到frontend
    return render_template("test", name="Dickson")


@app.route("/templateEnginePage1")
def templateEnginePage1():
    return render_template("index.html")


@app.route("/templateEnginePage2")
def templateEnginePage2():
    return render_template("page2.html")


@app.route("/templateEngineShow", methods=["POST"])
def templateEngineShow():
    # name = request.args.get("name","") # GET 運用url string query傳資料
    name = request.form["name"] # POST 運用將url string query隱藏
    return "Hello! "+name

@app.route("/templateEngineCalculate", methods=["POST"])
def templateEngineCalculate():
    # number = request.args.get("number","")
    number = request.form["number"]
    number=int(number)
    result=0
    for n in range(1,number+1):
        result+=n
    return render_template("/result.html", answer=result)

@app.route("/testSession")
def testSession():
    name=request.args.get("name","")
    session["username"]=name # Store name data into session (username)
    return "Hello! "+name


@app.route("/testSession2")
def testSession2():
    name=session["username"] #restore the session (username)
    return name+", Long Time No See!"




# app.run() # 啟動Server
app.run(port=3000)  # Port改成3000

# Flask Module
# Need install flask before import

from flask import Flask
from flask import request
from flask import redirect
from flask import render_template
from flask import session 
import json

app = Flask(
    __name__,  # __name__ 代表目前執行的module
    static_folder="public",  # Read which folder # 預設是 "/static"
    static_url_path="/"  # Set the path for the folder
)

app.secret_key="Any String but be Secret" # Set a secret key for session

# 首頁：Create a route for "/"


@app.route("/")  # Declorator 以function為基礎提供更多功能
def index():  # Function to handle "/"
    print("請求方法", request.method)
    print("通訊協定", request.scheme)
    print("主機名稱", request.host)
    print("路徑", request.path)
    print("完整網址", request.url)
    print("Browser 和 OS", request.headers.get("user-agent"))
    print("語言偏好", request.headers.get("accept-language"))
    print("引薦網址", request.headers.get("referer"))
    # print(request.method)
    lang = request.headers.get("accept-language")
    if lang.startswith("en"):
        return redirect("/en")  # redirect to index_english
    else:
        return redirect("/zh")  # redirect to index_chinese


@app.route("/en")
def index_english():
    return json.dumps({  # json.dumps to convert json format to dictionary
        "Status": "Ok",
        "Text": "Hello Flask"
    })


@app.route("/zh")
def index_chinses():
    return json.dumps({
        "Status": "Okay",
        "Text": "你好, 歡迎光臨"
    }, ensure_ascii=False)  # turn off ASCII

# Create a route for "/data"


@app.route("/data")
def handleData():  # Function to handle "/data"
    return "My Data"

# 加入動態路徑：Create a DYNAMIC route for "/user/<username>"


@app.route("/user/<username>")  # <username> is a parameter 參數
def handleUser(username):  # Function to handle "/data"
    # return "Hello "+username
    if username == "Dickson":
        return "你好 "+username
    else:
        return "Hello "+username


# 要求字串
@app.route("/getSum")
def getSum():
    maxNumber = request.args.get("max", 100)
    print("Max Number: "+maxNumber)
    maxNumber = int(maxNumber)

    minNumber = request.args.get("min", 1)
    print("Min Number: "+minNumber)
    minNumber = int(minNumber)

    result = 0
    for n in range(minNumber, maxNumber):
        result += n
    return "結果： "+str(result)

# Bring template engine to frontend (Browser)


@app.route("/templateEngine")
def templateEngine():
    # render_template() 可以將templates內的檔案字串傳到frontend
    return render_template("test", name="Dickson")


@app.route("/templateEnginePage1")
def templateEnginePage1():
    return render_template("index.html")


@app.route("/templateEnginePage2")
def templateEnginePage2():
    return render_template("page2.html")


@app.route("/templateEngineShow", methods=["POST"])
def templateEngineShow():
    # name = request.args.get("name","") # GET 運用url string query傳資料
    name = request.form["name"] # POST 運用將url string query隱藏
    return "Hello! "+name

@app.route("/templateEngineCalculate", methods=["POST"])
def templateEngineCalculate():
    # number = request.args.get("number","")
    number = request.form["number"]
    number=int(number)
    result=0
    for n in range(1,number+1):
        result+=n
    return render_template("/result.html", answer=result)

@app.route("/testSession")
def testSession():
    name=request.args.get("name","")
    session["username"]=name # Store name data into session (username)
    return "Hello! "+name


@app.route("/testSession2")
def testSession2():
    name=session["username"] #restore the session (username)
    return name+", Long Time No See!"




# app.run() # 啟動Server
app.run(port=3000)  # Port改成3000
# Flask Module # Need install flask before import # __name__ 代表目前執行的module # Read which folder # 預設是 "/static" # Set the path for the folder # Set a secret key for session # 首頁：Create a route for "/" # Declorator 以function為基礎提供更多功能 # Function to handle "/" # print(request.method) # redirect to index_english # redirect to index_chinese # json.dumps to convert json format to dictionary # turn off ASCII # Create a route for "/data" # Function to handle "/data" # 加入動態路徑：Create a DYNAMIC route for "/user/<username>" # <username> is a parameter 參數 # Function to handle "/data" # return "Hello "+username # 要求字串 # Bring template engine to frontend (Browser) # render_template() 可以將templates內的檔案字串傳到frontend # name = request.args.get("name","") # GET 運用url string query傳資料 # POST 運用將url string query隱藏 # number = request.args.get("number","") # Store name data into session (username) #restore the session (username) # app.run() # 啟動Server # Port改成3000