

플라스크 객체 생성
from flask import Flask
app = Flask(name) => 는 언더 스코어 기호 ' ' 2개 연속
웹 요청 경로 지정 및 실행 함수 작성
@ap.route("\") <- 요청 경로 지정
def hello(): <- 실행 함수 작성
return "< h1 >Hello World!< \h1 >
app.route()는 플라스크 route() 함수의 장식자 (decorator)
함수의 장식자는 기존 함수의 코드를 변경하지 않고 함수의 동작을 조정
위 코드에서는 route() 함수 (메서드)의 장식자는 플라스크 웹 서버의 '/' 경로로 요청이 들어 오면 hello() 함수의 리턴 값을 웹 브라우저로 응답
플라스크 웹 서버 구동
ifname==" main":
app.run() <-실행되는 컴퓨터 IP 주소, 포트번호 5000
IP 주소와 포트 번호 명시적 지정 가능
app.run(host="127.0.0.1", port="8080")
from flask import Flask
# 플라스크 객체 생성
app = Flask(_name_)
# 웹 요청 경로 실행 함수
@app.route("/")
def hello():
return "<h1> Hello World!"<h1>
@app.route("hello")
def hello2():
return "<h1>안녕, 순천향 !! </h1>"
# 플라스크 서버 구동
if_name_=="_main_":
app.run() # 실행되는 컴퓨터 IP 주소, 포트번호 5000
# app.run(host="127.0.0.1" , port="8080")
from flask import Flask
# 플라스크 객체 생성
app = Flask(_name_)
# 웹 요청 경로 실행 함수
@app.route("/")
def hello():
return "<h1> Hello World! < /h1 >"
@app.route("/hello")
def hello2():
return "<h1> 안녕, 순천향 !! < /h1 >"
# 플라스크 서버 구동
if_name_=="_main_":
app.run()
# app.run(host="127.0.0.1", port="8080")
HTML 파일에 동적인 데이터를 표현
사용자에게 보여주는 부분 (HTML)과 처리하는 부분 (응용 코드, 파이썬)을 분리
from flask import Flask, render_template
# 플라스크 객체 생성
app = Flask(_name_)
# 라우팅 경로 요청 시 실행 함수
@app.route("/prices0")
def prices():
return render_template("prices0.html")
# 플라스크 서버 구동
if_name_=="_main_":
app.run # 실행되는 컴퓨터 IP 주소, 포트번호 5000
<html>
<head>
<title> Welcome to the Beans'R'Us Pricing Page</title>
</head>
<body>
<H2>Welcome to the Beans'R'Us Pricing Page</h2>
<p>Current price of coffee beans = <strong>$6.42</strong></p>
<p>Price on Thu Aug 27 16:30:59 2023</p>
<body>
</html>
from flask import Flask, render_template
# 플라스크 객체 생성
app = Flask(__name__)
# 라우팅 경로 요청 시 실행 함수
@app.route("/prices")
def prices():
import random
import time
p = random.uniform(4,7) # 원두 커피 가격을 랜덤으로 생성
price = round(p, 2) # 소수점 2자리까지 반올림
daytime = time.asctime() # 현재 시간
return render_template("prices.html", pr = price, dt = daytime)
# 플라스크 서버 구동
if __name__ == "__main__":
app.run() # 실행되는 컴퓨터 IP 주소, 포트번호 5000
<html>
<head>
<title>Welcome to the Beans'R'Us Pricing Page</title>
</head>
<body>
<h2>Welcome to the Beans'R'Us Pricing Page</h2>
<p>Current price of coffee beans = <strong>$ {{ pr }} </strong></p>
<p>Price on {{ dt }} </p>
</body>
</html>
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/for')
def loop_test():
dept_list = ['컴퓨터공학과', '전자공학과', '기계공학과', '화공과']
return render_template('loop.html', values=dept_list)
if __name__ == '__main__':
app.run(port="8080") # 8080 포트
<!DOCTYPE html>
<html>
<head>
<title>Loop</title>
</head>
<body>
<div id='main'>
<ul>
{% for value in values %}
<li>{{ value }}</li>
{% endfor %}
</ul>
</div>
</body>
</html>
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/if')
def if_test():
return render_template('condition.html', size = "Big")
if __name__ == '__main__':
app.run(port="8080") # 8080 포트
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id='main'>
{% if size == "Big" %}
<h1> 안녕, 컴퓨터공학과 ! </h1>
{% elif size == "Medium" %}
<h3> 안녕, 컴퓨터공학과 ! </h3>
{% elif size == "Small" %}
<h6> 안녕, 컴퓨터공학과 ! </h6>
{% else %}
안녕, 컴퓨터공학과 !
{% endif %}
<p> Size: {{ size }} </p>
</div>
</body>
</html>