반응형
1. flask 설치
# Flask 설치 $ pip install flask # Flask 확인 $ flask --version
2. test 폴더 하나 만들고 들어가서 app.py 파일 만들어준다.
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello Flask'
@app.route('/info')
def info():
return 'Info'
Flask 서버 구동 확인하기
콘솔창에서 아래의 명령어 입력한다.
flask run
http://127.0.0.1:5000/ 로 들어가면 확인할 수 있다.
템플릿 추가하기
pyflask 폴더 내에 templates 폴더를 추가하고, index.html과 info.html 파일을 추가한다.
app.py에 템플릿 코드 추가
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/info')
def info():
return return render_template('info.html')
templates/index.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Flask Index</title>
</head>
<body>
<h1>Hello Flask</h1>
<p>This page is for Flask tutorial.</p>
</body>
</html>templates/info.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Flask Info</title>
</head>
<body>
<p>This page is Info page.</p>
</body>
</html>
추가된 페이지 확인
app.py의 router를 통해 http://127.0.0.1:5000/과 http://127.0.0.1:5000/info/에 접속하면 index.html과 info.html를 확인할 수 있다.
위의 글은
다음의 블로그를 똑같이 따라해보았습니다.!
반응형
'IT' 카테고리의 다른 글
| spring boot 웹사이트 to cloud(ubuntu) (0) | 2020.07.14 |
|---|---|
| flask kill 하는 방법 (0) | 2020.05.14 |
| Jython 사용방법 (0) | 2020.05.05 |
| 에러 OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized (0) | 2020.05.02 |
| Python cv2 설치 (0) | 2020.05.02 |