2018年9月24日 星期一

Flask Route

Flask是python網站開發微框架。


File->Setting-選到自己的Project->Project Interpreter-> ┼ (右邊加號)>搜尋flask->install package
  • Flask中的路由
新增一檔案名為app.py,import Flask
#making available the code you need to build web apps with flask
from flask import Flask, render_template
# create an instance of the Flask class for our web app
app = Flask(__name__)


# when the user navigates to localhost:5000, 
# the home function will run and it will return its output on the webpage.
@app.route("/")def hello():
    # return "<h3>Hello World! 123</h3>"
    # 利用render_template()導到html檔案,
    # 要注意的是:1.需import render_template 2.該html檔要放在根目錄的Templates目錄之下    return render_template("home.html")

# If the input to the route method was something else, let’s say ‘/abc/’, 
# the function output would be shown when the user visited localhost:5000/abc/
@app.route("/abc")def hello2():
    return 'Hello World! 45678'

# 執行該application,當debug=True,開發環境中則不必重啟server,重新整理網頁即可
app.run(debug=True) 



點選RUN執行,即可看到結果。



沒有留言:

張貼留言