Flask(7)

majaeh43ยท2021๋…„ 12์›” 9์ผ
0

Eager to learn Flask ๐Ÿ”ฅ

๋ชฉ๋ก ๋ณด๊ธฐ
7/7

Blueprint

  • ์›น ์„œ๋ฒ„์˜ ์ฝ”๋“œ๋ฅผ ์ปดํฌ๋„ŒํŠธํ™” ์‹œ์ผœ์„œ ์—ญํ• , ํŠน์ง• ๋“ฑ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋‚˜๋ˆ ์คŒ!
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
    
app.run()

์ง€๊ธˆ์€ route๋กœ ๋“ฑ๋กํ•œ URL์ด "/" ํ•˜๋‚˜ ๋ฐ–์— ์—†์ง€๋งŒ, route์— ๋“ฑ๋กํ•œ URL์ด ์ˆ˜์‹ญ๊ฐœ๋ผ๋ฉด..?
๐Ÿฅฐ ^^ ์‰ฝ์ง€์•Š์ง€ ^^ ๐Ÿฅฐ ๊ทธ๋ž˜์„œ blueprint๊ฐ€ ํ•„์š”ํ•ด...!

1) API๋“ค์„ ๋ถ„๋ฅ˜/๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ
2) Flask์˜ ๊ธฐ๋Šฅ์ด ์ ์  ๋Š˜์–ด๋‚ ์ˆ˜๋ก, ์ฝ”๋“œ์˜ ์–‘๋„ ์ฆ๊ฐ€ํ•œ๋‹ค.
3) ์ด๋•Œ, Blueprint๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ธธ์–ด์ง„ ์ฝ”๋“œ๋ฅผ ๋ชจ๋“ˆํ™”ํ•ด์ฃผ์–ด ์ˆ˜์ • ๊ฐœ๋ฐœ๊ณผ ์œ ์ง€๋ณด์ˆ˜์— ์šฉ์ดํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ.
4) ํ•œ ํŒŒ์ผ์—์„œ (app.py) ์—ฌ๋Ÿฌ ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•จ !

์‚ฌ์šฉ๋ฐฉ๋ฒ•

# ๋จผ์ € ๊ธฐ์กด app.py์— blueprint๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ค˜์•ผํ•จ!
# app.py

from flask import Flask
import blue # blue.py ์ž„ํฌํŠธํ•ด์™€์ฃผ๊ณ 

app = Flask(__name__)
# blue.py์˜ bp ๊ฐ์ฒด๋ฅผ app ๊ฐ์ฒด์— ์ ์šฉ (blueprint๋ฅผ ๋“ฑ๋กํ•ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด๋จ)
app.register_blueprint(blue.bp)

@app.route('/')
def hello_world():
    return 'Hello, World!'

app.run()

URL Prefix

Blueprint๋ฅผ ํ†ตํ•ด, ํ”Œ๋ผ์Šคํฌ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๋ฅผ ๋ชจ๋“ˆ๋ณ„๋กœ ๊ตฌ์กฐํ™”ํ•˜์—ฌ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. Blueprint์˜ URL Prefix ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฝค ํŽธ๋ฆฌํ•˜๋‹ค.

# blue.py

from flask import Blueprint

bp = Blueprint('blue', __name__, url_prefix='/blue')
# ์ด๋ฆ„ ์„ ์–ธ

@bp.route("/1")
def print_blue():
	return "hello Blue! :)"

@bp.route("/2")
def print_blue2():
	return "hello Blue!2 :)"
    
#์ด์ œ ๋ธ”๋ฃจํ”„๋ฆฐํŠธ๋ฅผ ์„ ์–ธํ•  ๋•Œ, URL prefix๋ฅผ ์„ค์ •ํ•ด๋‘๋ฉด ์•„๋ž˜์— ์ƒ์„ฑ๋˜๋Š” ๋ชจ๋“  URL์€ ์•ž์— ๋ฏธ๋ฆฌ ์„ค์ •๋œ prefix ๋ฌธ์ž์—ด์ด ๋ถ™์€ ์ฑ„๋กœ ์„ ์–ธ๋จ!

  • ๐Ÿฅ•
๐Ÿ“‘ ์ค‘์š” ํ‚ค์›Œ๋“œ
@app.route ์‚ฌ์šฉํ•ด์„œ url ์ƒ์„ฑ
render_template ์‚ฌ์šฉํ•ด์„œ HTML ํŒŒ์ผ ์—ฐ๊ฒฐ
jsonify๋กœ json ํ˜•์‹ ๋ฐ์ดํ„ฐ ์ „์†ก
GET์€ request.args.get(~), POST๋Š” request.form[~]
Blueprint๋กœ ์ฝ”๋“œ ๋ชจ๋“ˆํ™”! ์œ ์ง€๋ณด์ˆ˜ good!
jinja ํ…œํ”Œ๋ฆฟ์œผ๋กœ html ๋‚ด์—์„œ python๋ฌธ๋ฒ•์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ ๊ฐ€๋Šฅ

!!!!!!

  • ๐Ÿฅ•๐Ÿฅ•
1-1) __name__?
ํŒŒ์ด์ฌ์˜ .py ํŒŒ์ผ์—๋Š” ์ˆจ๊ฒจ์ ธ ์žˆ๋Š” ๋ณ€์ˆ˜: __name__.
์ด ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜.
์ฆ‰, ์ง€๊ธˆ py ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๋ผ๋Š” ๊ฒƒ.
example.py ๋ฉด example์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ __name__ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ.

1-2) __name__, __main__?
__name__์—๋Š” ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์ด ๋“ค์–ด๊ฐ€๋Š”๋ฐ ๋“ค์–ด๊ฐ€๊ธฐ๋„ ํ•˜๊ณ  ์•„๋‹ˆ๊ธฐ๋„ ํ•จ.

์™œ๋ƒ๋ฉด, ๋ฉ”์ธ์œผ๋กœ ์ง์ ‘ ์‹คํ–‰ํ•œ ํŒŒ์ผ์˜ ๊ฒฝ์šฐ์—๋Š” __name__์— ํŒŒ์ผ ์ด๋ฆ„์ด ์•„๋‹Œ "__main__" ์ด๋ผ๋Š” ๋ฌธ์ž์—ด์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. ๋ฉ”์ธ์œผ๋กœ ์ง์ ‘ ์‹คํ–‰ํ•œ ํŒŒ์ผ์ด๋ผ๋Š” ๊ฒƒ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ.

first.py, second.py ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ
first.py์—์„œ second.py๋ฅผ import ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ ,
second.py์—์„œ๋„ first.py๋ฅผ import ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ
๊ฐ์ž๋ฅผ import ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด,
first.py ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ -
first.py์˜ __name__์€ "__main__" ์ด๊ณ , second.py์˜ __name__์€ "second"๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋จ!

๋ฐ˜๋Œ€๋กœ second.py๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ -
first.py์˜  __name__์€ "first" ์ด๊ณ , second.py์˜ __name__์€ "__main__"์ด ๋จ!

1-3) ์ •๋ฆฌํ•˜๋ฉด,
์ง์ ‘ ์‹คํ–‰๋œ ํŒŒ์ผ __name__ == "__main__" 
์ž„ํฌํŠธ ๋ผ์„œ ๋ชจ๋“ˆ๋กœ ์‚ฌ์šฉ๋œ .py ํŒŒ์ผ __name__ == "๋ชจ๋“ˆ์ด๋ฆ„"

**๊ผญ** ์•Œ์•„์•ผ ํ•  ๊ฒƒ์€ ๋‘ ๊ฐ€์ง€!
(1) ํŒŒ์ผ์ด ์ž„ํฌํŠธ ๋  ๋•Œ ํ•ด๋‹น ํŒŒ์ผ์ด ์‹คํ–‰์ด ๋˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค.
(print ํ•จ์ˆ˜ ๊ฐ™์€ ๊ฑฐ ๋ฐ”๋กœ ์ถœ๋ ฅ๋จ)
(2) ๋ฉ”์ธ์œผ๋กœ ์‹คํ–‰ํ•œ ํŒŒ์ผ์˜ __name__์—๋Š” "__main__"์ด ๋“ค์–ด๊ฐ„๋‹ค.

์ฐธ๊ณ 1: https://velog.io/@suhyun-guri/Flask-Flask-%EA%B8%B0%EC%B4%88
์ฐธ๊ณ 2: https://blog.naver.com/shino1025/221239549339
์ฐธ๊ณ 3: https://blockdmask.tistory.com/545

profile
๊ฐœ๋ฐœ ๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€