[Flask] Local Server 구동하기

Yewon Kim·2020년 6월 23일
0

Flask

목록 보기
1/3

가상환경(venv) 만들기

1) 📁프로젝트 폴더 생성

2) 터미널을 통해 가상환경 설치

Visual Studio Code

터미널(T) ▶ 새 터미널(Ctrl + Shift + `) ▶ 아래 code 실행

python -m venv 가상환경명(예: projectvenv)

3) 📁프로젝트 폴더 내 📄app.py 파일 생성

4) 새 터미널 열기

Flask 설치

1) 새 터미널에서 아래 코드 실행

pip install flask

2) flask 설치 완료

📄app.py 준비

1) 📄app.py 에서 아래 코드 입력 후 실행

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
   return 'This page is an Index page.'

if __name__ == '__main__':  
   app.run('0.0.0.0', port=5000, debug=True)

2) Chrome에서 http://localhost:5000/ 에 접속하여 server 연결 확인

index.html 준비

1) 📄app.py가 http://localhost:5000/ 에 정상적으로 연결되었다면, 아래와 같이 프로젝트 폴더 구조 생성

📁static 폴더 (img, css, js 등)
📁templates 폴더 (html)
└📄index.html
📄app.py

2) 📄index.html에 example code 작성

   <!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta name="viewport" 
             content="width=device-width, initial-scale=1.0">
       <title>Index Page</title>
   </head>
   <body>
       <h1>
           This is an Index page.
       </h1>
   </body>
   </html>

3) 📄app.py에서 render_template() 함수로 index.html 불러오기

@app.route('/')
def index():
   return render_template('index.html')

4) Chrome에서 http://localhost:5000/ 에 재접속하여 index.html이 정상적으로 구현되었는지 확인

5) 완료

profile
산업의 안팎에서 투자의 원칙을 배우고 싶은 학생입니다.

0개의 댓글