Flask

SungMin·2023년 2월 3일
0

Sesac-Python

목록 보기
11/11

Flask

  • 플라스크로 간단한 서버를 구현해볼 수 있다.

  • 기본 구조 : 괄호 안 주소를 입력하여 해당 페이지로 이동할 수 있다.

from flask import Flask

# 기본 페이지
app = Flask(__name__)

@app.route("/")
def anyname():
    return "ㅋㅋㅋㅋ"

@app.route("/app1")
def app1():
    return "app1입니다."

@app.route("/app2")
def app2():
    return "app2에요."

app.run(host="localhost", port=5000)

  • 방문 확인 수 구현
# 프론트페이지와 카운팅 부분 분리
from flask import Flask

my_count = 0
app = Flask(__name__)

def build_input_page(my_count):
    page = f"""
    <html>
    <body>
        <p>{my_count}번 째 방문입니다.</p>
    </body>
    </html>
    """
    return page

@app.route("/")
def app_input():
    global my_count
    my_count+=1
    page = build_input_page(my_count)
    return page

app.run(host="localhost", port=5000)

  • 머신러닝 때 실습했던 꽃잎 데이터 입력기 만들기
# 꽃잎 데이터 입력 추가
from flask import Flask, request

def build_result_page():
    sepal_length = request.args.get("sepal_length")
    sepal_width = request.args.get("sepal_width")
    petal_length = request.args.get("petal_length")
    petal_width = request.args.get("petal_width")
    
    page = f"""
    수신결과{sepal_length}, {sepal_width}, {petal_length}, {petal_width}
    """
    return page

def build_input_page():
    page = f"""
    <html>
    <body>
    방문자님 안녕하세요.<br>
    입고된 꽃의 치수를 cm단위로 입력해주세요.<br>
    저희 [ATM]회사의 머신러닝 분류기가 꽃의 종류를 판단해 줍니다.<br>
    <form action="http://localhost:5000/result" method="get">
    <input type = "text" name = "sepal_length"></input>cm<br>
    <input type = "text" name = "sepal_width"></input>cm<br>
    <input type = "text" name = "petal_length"></input>cm<br>
    <input type = "text" name = "petal_width"></input>cm<br>
    <input type = "submit"></input><br>
    </form>
    </body>
    </html>
    """
    return page

app = Flask(__name__)

@app.route("/input")
def app_input():
    page = build_input_page()
    return page

@app.route("/result", methods=["POST", "GET"])
def app_result():
    page = build_result_page()
    return page

app.run(host="localhost", port=5000)

  • 홈페이지 구현
def build_home_page():
    page = """
    <html>
    <body>
    <h1>Iris 분류기</h1>
    <p>Iris 분류기 입니다.</p>
    <a href="http://localhost:5000/input">새로 들어온 꽃의 치수 입력하러 가기.</a>
    </body>
    </html>
    """
    return page

app = Flask(__name__)

@app.route("/")
def app_home():
    page = build_home_page()
    return str(page)

  • 결과 출력기
with open("pickled_model_iris.bin", "rb") as f:
        dt_model_loaded = pickle.load(f)

def build_result_page():
    sepal_length = request.args.get("sepal_length")
    sepal_width = request.args.get("sepal_width")
    petal_length = request.args.get("petal_length")
    petal_width = request.args.get("petal_width")
    
    sepal_length = float(sepal_length)
    sepal_width = float(sepal_width)
    petal_length = float(petal_length)
    petal_width = float(petal_width)
    
    global dt_model_loaded
        
    res = int(dt_model_loaded.predict([[sepal_length, sepal_width, petal_length, petal_width]]))
    label_names = ['setosa', 'versicolor', 'virginica']
    
    page = f"""
    수신결과{sepal_length}, {sepal_width}, {petal_length}, {petal_width}
    판단결과{label_names[res]}꽃 입니다. 창고에 넣어주세요.    
        
    <a href = "http://localhost:5000/input">다른 꽃 재러 가기</a>
    """
    return page

profile
초보 개발자의 학습 저장용 블로그

0개의 댓글