> pip install flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Flask Server'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
-
templates폴더에 html파일 생성
-
flask 내장함수 render_template 사용
@app.route('/') def home(): return render_template('index.html')
-
static폴더 생성
-
{{ url_for('static', filename='image.jpg') }}
사용하여 url 표시<img src="{{ url_for('static', filename='image.jpg') }}"/> <link rel="stylesheet" href={{ url_for('static', filename='styple.css') }}>
@app.route('/test', methods=['GET'])
def test_get():
return jsonify({'result':'success', 'msg': '이 요청은 GET!'})
@app.route('/test', methods=['POST'])
def test_post():
return jsonify({'result':'success', 'msg': '이 요청은 POST!'})
> pip install pymongo
from pymongo import MongoClient
app = Flask(__name__)
client = MongoClient('localhost', 27017) # mongoDB는 27017 포트로 돌아갑니다.
db = client.testdb # 'testdb'라는 이름의 db를 만들거나 사용합니다.