python으로 flask서버 구현

ganadara·2023년 2월 3일
0

python

목록 보기
8/10

flask

from flask import Flask, request, render_template
import pickle

my_count = 0

app = Flask(__name__)

def build_input_page(my_count):
    page = f'''
    <html>
    <head> 
        <title> 헬로월드</title>
    </head>
    <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='192.168.10.36',port=5000)
from flask import Flask

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

app = Flask(__name__)

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

app.run(host="192.168.10.36",port=5000)
from flask import Flask, request, render_template
import pickle

my_count = 0

app = Flask(__name__)
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("width_length")
    
    
    page =f"""
    수신결과{sepal_length},{sepal_width},{petal_length},{petal_width}
    """
    return page

def build_input_page(my_count):
    page=f"""
    <html>
    <body>
    꽃 측정원 님 안녕하세요<br>
    지금 입고된 꽃의 치수를 cm단위로 입력해 주세요<br>
    저희 [나만돈벌어tm] 회사의 머신러닝 분류기가 꽃의 종류를 판단해 줍니다.<br>
    그러면 그 결과에 맞추어 창고에 넣어 주세요<br>
    <form action = "http://192.168.10.36:5000/result" method="get">
    sepal_length : <input type="text" name="sepal_length"></input>cm<br>
    sepal_width : <input type="text" name="sepal_width"></input>cm<br>
    petal_length : <input type="text" name="petal_length"></input>cm<br>
    petal_width :<input type="text" name="petal_width"></input>cm<br>
    <input type="submit"></input>cm<br>
    </form>
    </body>
    </html>
    """
    
    return page

app = Flask(__name__)

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

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

app.run(host="192.168.10.36",port=5000)
from flask import Flask, request
import pickle

with open("pickled_model_iris.bin", "rb") as f:
        dt_model_loaded = pickle.load(f)

# 여기로 이동해서 들어오게 됨
# http://192.168.10.104:5000/result?sepal_length=1&sepal_width=2&petal_length=3&petal_width=4

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_lenth = float(petal_length)
    petal_width = float(petal_width)
    
    with open("pickled_model_iris.bin", "rb") as f:
        dt_model_loaded = pickle.load(f)
    
    res = int(dt_model_loaded.predict([[sepal_length, sepal_width, petal_length, petal_width]]))
    label_name = ['setosa', 'versiclor', 'virginica']
    
    page = f"""
    수신결과{sepal_length}, {sepal_width}, {petal_length}, {petal_width}, <br>
    판단결과{label_names[res]}꽃 입니다. 창고에 넣어주세요
    
    <a href ="http://192.168.10.36:5000/input"> 다른 꽃 치수도 재러 가요
    """
    return page
# input 페이지의 수신 결과 : 1, 2, 3, 4 이렇게 페이지가 나오게 된다.
# get이 잘 되고 있다는 것으로 확인됨
    
    
def build_input_page():
    page = f"""
    <html>
    
    <body>
        꽃 측정원 님 안녕하세요 <br>
        지금 입고된 꽃의 치수를 cm 단위로 입력해 주세요 <br>
        저희 [꽃같은tm] 회사의 머신러닝 분류기가 꽃의 종류를 판단해 줍니다.
        그러면 그 결과에 맞추어 창고에 넣어 주세요
    <form action="http://192.168.10.36:5000/result" method="get">
    sepal_length : <input type = "text" name="sepal_length"></input> cm <br>
    sepal_width : <input type = "text" name="sepal_width"></input> cm <br>
    petal_length : <input type = "text" name="petal_length"></input> cm <br>
    petal_width : <input type = "text" name="petal_width"></input> cm <br>
    <input type = "submit"></input><br>
    
    
    </form>
    </body>
    </html> 
    
    
    """
    return page

# 프론트엔드에서 백엔드에 값을 넘겨주는 방식 (get 방식, post 방식)
# http://192.168.10.104:5000/result?sepal_length=1&sepal_width=2&petal_length=3&petal_width=4


def build_home_page():
    page = """
    <html>
    <body>
    <h1> Iris 분류기 </h1>
    <p> Iris 분류 시스템 입니다. </h1>
    <a href = "http://192.168.10.36:5000/input"> 새로 입고된 꽃의 치수를 입력하러 갑니다. <br>
    </body>
    </html>
    """
    
app = Flask(__name__)
    
@app.route("/")
def app_home():
    page = build_input_page()
    return page    
    
@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='192.168.10.36', port=5000)

flask와 html연결하기

html파일을 templates폴더 안에

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def app_home():
    return render_template("flask_index.html", var1="넌 모르잖아", var2 = "알록달록한 세상")

app.run(host="192.168.10.36", port=5000)
<html>
    <head>
            <link rel="stylesheet"
            href = "{{url_for('static', filename='test.css')}}">
        </head>
        <body>
            재준아 {{var1}}, {{var2}}
        </body>
</html>

profile
DL 공부중

0개의 댓글