Flask

Lee·2021년 12월 22일
0

웹개발 종합반

목록 보기
6/8

flask 프레임워크

  • 서버를 구동시켜주는 편한 코드 모음
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def home():
   return render_template('index.html') #templates 폴더 안에 있는 index.html파일을 client에게 전달
   
@app.route('/mypage')
def mypage():
   return 'mypage 입니다.'

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

프레임워크를 사용할 때에는 정해진 규칙을 지켜야 한다.

Flask 서버를 만들 때 프로젝트 폴더 안에 3가지를 생성한다.

  • static 폴더 (이미지, css파일을 넣어둡니다)
  • templates 폴더 (html파일을 넣어둡니다)
  • app.py 파일

static 폴더 : CSS나 image파일을 담아 둘 때 사용

templates 폴더

  • html파일들을 담아 둘 때 사용
  • flask의 내장함수인 render_template를 이용

주의
서버에 요청하여 index.html을 불러와야 한다. index파일을 실행하여 열지 말 것

profile
발전하고 싶은 백엔드 개발자

0개의 댓글