app = Flask(__name__, '/')
static 資料夾就會看成類似 apache 的 document root。
本文的參考專案 https://github.com/shinder/flask-practice
將處理的方法寫到 modules/functions.py 裡
from flask import request # 滙入 request
def show_cookies():
return request.cookies
在主要檔案 main.py 滙入:
import modules.functions
# 語法:add_url_rule(rule, endpoint=None, view_func=None, provide_automatic_options=None, **options)
app.add_url_rule('/show-cookies', 'show-cookies', modules.functions.show_cookies)
# app.add_url_rule('/show-cookies', 'show-cookies')
# app.view_functions['show-cookies'] = modules.functions.show_cookies
本文的參考專案 https://github.com/shinder/flask-practice
from flask import request # 滙入 request
import json
@app.route('/save-headers')
def save_headers():
dict1 = {
'cookies': {}
}
for i in request.headers:
print(i) # 查看取出的 headers 資料
dict1[i[0]] = i[1]
# 查看 cookies
for i in request.cookies:
dict1['cookies'][i] = request.cookies[i]
file1 = open('headers.json', 'w')
file1.write(json.dumps(dict1)) # 存成 JSON
return dict1
本文的參考專案 https://github.com/shinder/flask-practice
首先如之前的文章 建立專案。
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 即可看到我們的第一個頁面。
# 大標題
## 次標題
### 細標題
另一種大標 (不建議使用)
===
另一種次標 (不建議使用)
---
條列方式一: - (建議不要用 * 和 +)
- 123
- 456
- 789
條列方式二: * (不建議使用)
* 123
* 456
* 789
條列方式三: + (不建議使用)
+ 123
+ 111
+ 333
+ 222
+ 456
+ 789
條列方式四: 使用數字加點 (數字可以不管順序,但不建議)
1. 123
1. 333
2. 777
2. 456
3. 789
### 程式碼呢? 前面加 \t 或 4個空白
const func = a => a*a;
const func2 = ()=>{
let r = 0;
for(let i=1; i<=10; i++){
r+=i;
}
return r;
};
const func = a => a*a;
``` javascript
const func2 = ()=>{
let r = 0;
for(let i=1; i<=10; i++){
r+=i;
}
return r;
};
```
### 行內程式碼
這是個行內的 `console.log(me);` 程式碼
這是個行內的 ``console.log(bill`s);`` 程式碼
---
上下是分隔線 (不建議使用 *** )
***
*斜體*
**粗體**
_斜體_
__粗體__
區塊引言
>123
>456
>78945
### 連結
[This link](http://example.net/) has no title attribute.
### 表格
| column0 | column1 | aaa
| ------- | ------- | -----
| 123 | 456 | 789
| abc | def | ghi
| aaa | bbb | ccc
npm i @angular/cli -g
ng new my-proj
ng new hello-cli --routing
ng serve ng serve --prod
http://localhost:4200
ng build --prod
ng generate component my-component ng g c my-component
python3 -m venv venv
source venv/bin/activate
deactivate