Flask Tutorial(2) - Render_Template
2021. 5. 16. 17:32ㆍ[AI]/Data Engineering
Render_Template
- render_template 메소드를 활용해서 html 파일을 불러올 수 있음
- 보통 html 파일들은
templates
폴더에서 관리 - List 또는 Dictionary 자료구조도 보낼 수 있음
Example
- Folder Layout
TITANIC_FLASK
├── titanic.py
├── templates
│ └── base.html
- titanic.py
# filename : titanic.py
from flask import Flask,render_template
app = Flask(__name__) # Flask 의 instance 만들기
@app.route("/")
def first_page() :
x = [1,2,3,4,5]
y = {"Name" : "Bob", "Age" : 15}
return render_template('base.html',x=x,y=y) # base.html 에 x, y 를 보내겠다는 의미
if __name__ == "__main__" :
app.run() # 해당 파일 실행할 때 app 이 켜짐
- base.html
- html 에서 변수를 받아올 때는 {{}} 활용
- Jinja의 Template을 만드는 문법 중 하나
<html>
<head>
<title>
FIRST PAGE
</title>
</head>
<body>
<h1>FIRST PAGE</h1>
<p>SUCCESS!</p>
<h2>LIST</h2>
x = {{x}}
<br/><br/>
<h2>Dictionary</h2>
y = {{y}}
</body>
</html>
출력
728x90
'[AI] > Data Engineering' 카테고리의 다른 글
Web Scraping + Example (뉴스 기사 제목 스크래핑) (0) | 2021.05.16 |
---|---|
Flask Tutorial(1) - Creating Website (0) | 2021.05.16 |
Flask Tutorial(3) - Jinja Template & Syntax (0) | 2021.05.16 |
Flask Tutorial(4) - Request (0) | 2021.05.16 |
Flask Tutorial(5) - SQLAlchemy Database (0) | 2021.05.16 |