Code Monkey home page Code Monkey logo

flask-practice's Introduction

建立 Flask 專案

本文的參考專案 https://github.com/shinder/flask-practice,本部落格 qops

首先如之前的文章 建立專案

md flask-practice #建立專案資料夾
cd flask-practice #到專案目錄
python3 -m venv venv #安裝虛擬環境
source venv/bin/activate #啟動虛擬環境(mac)

安裝 Flask:

pip install flask

可以使用下列兩個命列中的一個查看安裝的套件:

pip list
pip freeze

建立 app 資料夾,用來存放自己撰寫的程式,並在裡面建立 main.py

from flask import Flask

# __name__ 用來 application 的相對位置
# 若是直接啟動的程式 __name__ 為 '__main__'
# 若是被滙入, __name__ 會是被滙入的名稱
app = Flask(__name__)

# decorators 後面定義的 function 會變成 decorators 的參數
# 類似 JavaScript 的 callback function
@app.route('/')
def index():
    return '<h2>哈囉 Flask</h2>'

在專案目錄建立執行的 run.sh,在 mac 記得將檔案屬性設定為 +x 可執行:

# mac
export FLASK_APP=app/main.py
export FLASK_ENV=development
flask run --host=localhost --port=5000

# windows
# set FLASK_APP=app/hello.py
# set FLASK_ENV=development
# flask run

在 terminal 執行 run.sh,並在瀏覽器拜訪 localhost:5000 即可看到我們的第一個頁面。

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.