Open uniquejava opened 4 years ago
https://docs.scrapy.org/en/latest/intro/tutorial.html
官方文档: https://klein.readthedocs.io/en/latest/introduction/1-gettingstarted.html#this-introduction
为什么要用? Going asynchronous: from Flask to Twisted Klein
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
stores = [{
'name': 'My Wonderful Store',
'items': [{
'name': 'My Item',
'price': 15.99
}]
}]
@app.route('/')
def home():
return render_template('index.html')
@app.route('/stores', methods=['POST'])
def create_store():
data = request.get_json()
new_store = {'name': data['name'], 'items': []}
stores.append(new_store)
return jsonify(new_store)
@app.route('/stores/<string:name>')
def get_store(name):
for store in stores:
if store['name'] == name:
return jsonify(store)
return jsonify({'message': 'store not found.'})
@app.route('/stores')
def get_stores():
return jsonify({'stores': stores})
@app.route('/stores/<string:name>/items', methods=['POST'])
def create_item_in_store(name):
data = request.get_json()
for store in stores:
if store['name'] == name:
new_item = {'name': data['name'], 'price': data['price']}
store['items'].append(new_item)
return jsonify(new_item)
return jsonify({'message': 'store not found'})
@app.route('/stores/<string:name>/items')
def get_items_in_store(name):
for store in stores:
if store['name'] == name:
return jsonify(store['items'])
return jsonify({'message': 'no items found in this store.'})
app.run(port=5000, debug=True)
see also https://github.com/uniquejava/blog/issues/62
pyenv
pyenv相当于node.js中的n或nvm, 用来全局切换python的版本(在pyenv作用范围内)
virtualenv类似于不带
-g
参数的npm,基于当前python解释器给项目创建一个隔离的python环境。show me the code!
详见: https://gtbensmagazine.com/2019/05/07/the-right-and-wrong-way-to-set-python-3-as-default-on-macos/
virtualenv
注意: 只有在项目根目录下创建venv, vs code才能自动检测到
如果不是在项目根目录下。 可以自行修改.vscode/settings.json文件
pip (不要用pip freeze命令!手工维护requirements.txt)
hello flask
api.py
最新的flask (1.1.0+) 已经不需要jsonify了。👏
.flaskenv
gitignore
详见: https://blog.miguelgrinberg.com/post/how-to-create-a-react--flask-project