환경
- Windonws 10
가상환경 생성 및 필요 패키지 설치
conda create -n flask
- 생성된 가상환경을 활성화한다.
conda activate flask
- pip3을 사용하여 가상환경 내에 필요한 패키지를 설치한다.
pip3 install flask
app.py 작성하기
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__=="__main__":
app.run(host="127.0.0.1", port="1234", debug=True)
Flask 인스턴스를 생성하고 @app.route()
를 사용하여 각 웹페이지에 접속할 수 있도록 한다.
app.run()
내 host, port를 직접 지정할 수 있다.
정상적으로 실행된다면 다음과 같은 출력을 확인할 수 있다.
위 주소로 접속시 다음과 같은 에러를 볼 수 있었다.
위 코드에서 return render_template('index.html')
에서 반환하려는 함수 인자로 사용되는 index.html를 찾을 수 없는 문제가 발생한 것을 알 수 있다.
만든 index.html
을 작성한다.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>페이지 제목</title>
</head>
<body>
<h1>H1 태그 사용</h1>
</body>
</html>
다시 app.py
를 실행시키면 정상적으로 index.html 작성 내용이 보이는 것을 확인할 수 있다.
참고자료
https://wikidocs.net/book/4479
Uploaded by Notion2Tistory v1.1.0