본문 바로가기

회고록(TIL&WIL)

TIL 2022.05.17 머신러닝 개와 고양이 분류(image classification, blueprint)

혼자 따로 해본 개와 고양이 분류 image classification

https://colab.research.google.com/drive/1brVI5WYYChr1GYxQgyBTe5To_5iqQVQn#scrollTo=y64ONt4efn4n

 

Google Colaboratory Notebook

Run, share, and edit Python notebooks

colab.research.google.com

 

 

blueprint

templates/main_views.py

from flask import Blueprint

bp = Blueprint('main', __name__, url_prefix='/')


@bp.route('/hello')
def hello_pybo():
    return 'Hello, Pybo!'


@bp.route('/')
def index():
    return 'Pybo index'

Blueprint를 import 해온 후 bp라는 변수에 Blueprint 객체를 만들어주는데 bp 객체 생성시 사용된 __name__은 모듈명인 "main_views"가 인수로 전달된다. 첫번째 인수로 전달한 "main"은 블루프린트의 "별칭"이다 이 별칭은 나중에 자주 사용할 url_for 함수에서 사용된다. url_prefix는 라우팅 함수의 애너테이션 URL 앞에 기본으로 붙일 접두어 URL을 의미한다. 

 

 

__init__.py

from flask import Flask


def create_app():
    app = Flask(__name__)

    from .templates import main_views
    app.register_blueprint(main_views.bp)

    return app

main_views.py 파일에 생성한 블루프린트 객체 bp를 app.register_blueprint(main_views.bp)로 등록했다. 

 

이 상태로 localhost:5000에 접속하면 'Pybo Index' 가출력되고 localhost:5000/hello 에 접속하면 'hello Pybo' 가 출력됨