Git Product home page Git Product logo

learning_python_flask's Introduction

leaning_python_flask

flask的基礎架設網站從以下程式碼開始:

from flask import Flask
def test():

   app = Flask(__name__)

   @app.route("/")
   def test():
       return render_template("test.html")
   app.run(debug=Ture)

if __name__ == '__main__':
    test()

因為我們要用到Flask這個物件,所以一開始就要做出宣告,* 是指引入所有flask內的函式,建議是只引入需要的函式,避免占空間和名字重複的問題。

from flask import *

初始化Flask物件,並貼上app這個標籤。接著我們就可以很方便的使用這個物件裡面的各種功能。

app = Flask(__name__)

創造出主網域下的"/"這個網址,並定義了請求(request)該網址時可用的手段。舉例來說,主網域是.123.com/" ,那麼用@app.route("/")就是創造出了".123.com/" 。而用@app.route("/456")就是創造出了"123.com/456" 。

@app.route("/")

告訴你怎樣的url可以call怎樣的function,url會這樣顯示localhost:5000/,斜線後是接你的函式名稱,例如:def test(),出來就是localhost:5000/test。

def home():

def為定義一個函式,後方為函式的名稱。

return render_template("test.html")

reture將內文顯現於介面,render_template()則是呼叫HTML的檔案,通常不會在python裡撰寫html的架構,所以需要這個功能呼叫html的檔案;檔案方面須注意放置的位置和名稱會在下方做說明。

app.run(debug=Ture)

就是執行的意思,debug救世會將更改程式碼儲存、重啟,變為更新後的樣子。

if __name__ == '__main__':

如果這個程式碼是主要執行檔的話才會執行。

HTML、CSS檔放置的位置

main為撰寫flask的python檔,當然名字能做更動,templates為html所要放置的資料夾名稱,static則為css所放置的資料夾名稱,名稱錯誤會抓不到檔案,這點需要小心。

Cover

learning_python_flask's People

Contributors

terry-eric avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.