Flask
flask- Python의 마이크로 웹 프레임워크
코드가 비교적 단순하고 API서버를 만들기 매우 편리
flask 기본 코드
from flask import Flask
app = Flask(__name__) #어플리케이션의 이름
@app.route('/') #falsk에게 어떤 URL에 밑의 함수를 실행시키는지 확인
#실행 함수
def home():
return '메인 메뉴!'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True) # 로컬 서버로 실행
실행 후 http://localhost:5000/ 접속
from flask import Flask
app = Flask(__name__) #어플리케이션의 이름
@app.route('/') #falsk에게 어떤 URL에 밑의 함수를 실행시키는지 확인
#실행 함수
def home():
return '메인 메뉴!'
@app.route('/homepage')
def homepage():
return '홈페이지!'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True) # 로컬 서버로 실행
@app.route('/homepage')
def homepage():
return '홈페이지!'
추가 후 http://localhost:5000/homepage 접속
flask에 Html 파일 불러오기
경로에 templates 추가
templates 폴더에 html 파일 추가
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>제목입니다</h1>
<button>버튼입니다</button>
</body>
</html>
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
*render_template - flask에서 제공하는 함수로 template에 저장된 html을 불러온다
return render_template('~') > 안에 templates 폴더에 추가한 html 파일 넣기
return render_template('index.html')
결과
'Python' 카테고리의 다른 글
Python] API 만들고 사용하기 (0) | 2022.11.03 |
---|---|
Python] 크롤링한 데이터 DB에 업데이트하기 (0) | 2022.11.03 |
Python] DB연결, 조작 (mongoDB 사용) (0) | 2022.11.02 |
Python] 크롤링 - bs4 (Pycharm) (0) | 2022.11.02 |
Python] Requests 패키지 사용하기 (pycharm) (0) | 2022.11.02 |