01.Flask ๊ธฐ์ดˆ - Jinja template

ID์งฑ์žฌยท2021๋…„ 4์›” 22์ผ
3

Flask

๋ชฉ๋ก ๋ณด๊ธฐ
4/8
post-thumbnail

๐ŸŒˆ Jinja template

๐Ÿ”ฅ Jinja ํ…œํ”Œ๋ฆฟ์ด๋ž€?

๐Ÿ”ฅ ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ : {{๋ณ€์ˆ˜๋ช…}}

๐Ÿ”ฅ ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ : {% for %} ~ {% endfor %}

๐Ÿ”ฅ ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ : {% if %} {% elif %} {% else %} ~ {% endfor %}

๐Ÿ”ฅ ํฌ๋กค๋งํ•˜์—ฌ html ์ •๋ณด ์ „๋‹ฌํ•˜๊ธฐ


1. Jinja ํ…œํ”Œ๋ฆฟ์ด๋ž€?

  • ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•์œผ๋กœ html์—์„œ {}, {{}} ๋“ฑ์˜ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ python ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๊ฐ€๋Šฅ
  • ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์€ ๋ณ€์ˆ˜๋ฅผ return๊ฐ’์œผ๋กœ html๊ณผ ํ•จ๊ป˜ ๋„˜๊ฒจ์ค€ ๋’ค, html์—์„œ jinja ํ…œํ”Œ๋ฆฟ ๊ทœ์น™์— ๋”ฐ๋ผ ํ™”๋ฉด์— ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Œ
  • Jinja ํ…œํ”Œ๋ฆฟ html์—์„œ ์ฃผ์„์€ {# ์ฃผ์„์œผ๋กœ ์ฒ˜๋ฆฌํ•  ๋‚ด์šฉ #}์œผ๋กœ ํ‘œ์‹œ

2. ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ : {{๋ณ€์ˆ˜๋ช…}}

  • Jinaja Templates์€ flask์—์„œ ์ œ๊ณตํ•˜๋Š” ์—”์ง„์œผ๋กœ html์„ ๋ Œ๋”๋งํ•  ๋•Œ, ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ํ•ด๋‹น html์—์„œ ์ „๋‹ฌ๋œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • http://127.0.0.1:8080/hello/jaewon์— ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚จ
  • http://127.0.0.1:8080/hello/์˜ ๋’ค์— ๊ฒฝ๋กœ(<user>)์— ๋”ฐ๋ผ ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์ด ๋ณ€๊ฒฝ๋จ
  • Jinja2 Tempaltes ๋ณ€์ˆ˜ ๋„˜๊ธฐ๊ธฐ : {{}}
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello/<user>') # <user> ์ƒ์„ฑ
def hello_name(user): # <user> ๋ณ€์ˆ˜๊ฐ’์„ ํ•จ์ˆ˜๋กœ ๋„˜๊น€
    return render_template('variable.html', name1=user, name2='This is Jinja2 template') # ๋ณ€์ˆ˜๋“ค์„ html๋กœ ๋„˜๊น€
if __name__ == '__main__':
    app.run(host="0.0.0.0", port="8080")
  • html์—์„œ Jinja2 templates ๋ณ€์ˆ˜ ํ™œ์šฉ ๋ฐฉ๋ฒ• : {{}}
<!DOCTYPE html>
<html>
  <body>
    <h1>Hello {{ name1 }}</h1>
    <h2>{{ name2 }}</h2>
    <h2>URL : http://127.0.0.1:8080/hello/{{ name1 }}</h2>
  </body>
</html>


3. ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ : {% for %} ~ {% endfor %}

  • {% for %} ~ {% endfor %}์ด ์‚ฌ์ด์— html์ฝ”๋“œ์™€ {{๋ณ€์ˆ˜๋ฅผ}} ์œ„์น˜์‹œํ‚ค๋ฉด for๋ฌธ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ๋“ค์—ฌ์“ฐ๊ธฐ๋Š” ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋จ
  • rangeํ•จ์ˆ˜๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, len() ํ•จ์ˆ˜๋ฅผ ์“ฐ๊ณ ์žํ•  ๋•Œ๋Š” {% for value in ranage(values | length) %} ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•จ
  • {{ loop.index }} ๋Š” for๋ฌธ์—์„œ enumerate์ฒ˜๋Ÿผ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธธ ๋•Œ ์‚ฌ์šฉํ•˜๊ณ , 1๋ฒˆ๋ถ€ํ„ฐ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ž„
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello_loop')
def hello_name():
    value_list = ['list1', 'list2', 'list3'] # ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์•„ html๋กœ return
    return render_template('loop.html', values=value_list)
if __name__ == '__main__':
    app.run(host="0.0.0.0", port="8080")
  • โœhtml์—์„œ Jinja2 templates for๋ฌธ ํ™œ์šฉ ๋ฐฉ๋ฒ•
<html>
  <body>
    <ol>
      {% for value in values %}
      <li>{{ value }}</li>
      {% endfor %}
    </ol>
    <ul>
      {% for i in range(values | length) %}
      <li>{{ values[i] }} : {{ loop.index }}๋ฒˆ์งธ</li>
      {% endfor %}
    </ul>
  </body>
</html>


4. ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ : {% if %} {% elif %} {% else %} ~ {% endfor %}

  • {% %} ์˜ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ, if, elif, else ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ , {% endfor %}์œผ๋กœ ์กฐ๊ฑด๋ฌธ ์ข…๋ฃŒ
  • ๋“ค์—ฌ์“ฐ๊ธฐ ๊ทœ์น™ ๋ฌด์‹œ
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello_if')
def hello_html():
    value = 27
    return render_template('condition.html', data=value)
if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8080")
  • html์—์„œ Jinja2 templates if๋ฌธ ํ™œ์šฉ ๋ฐฉ๋ฒ•
<html>
  <body>
    {% if data >= 30 %}
    <h3>30๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.</h3>
    {% elif data > 25 %}
    <h3>25๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.</h3> 
    {% else %}
    <h3>{{ data }}</h3>
    {% endif %}
  </body>
</html>


5. ํฌ๋กค๋งํ•˜์—ฌ html ์ •๋ณด ์ „๋‹ฌํ•˜๊ธฐ : google ํด๋ก 

  • ๊ธฐ์กด์— ์žˆ๋Š” ์‚ฌ์ดํŠธ๋ฅผ requests๋ฅผ ํ†ตํ•ด html์„ ์ „๋ถ€ ํฌ๋กค๋งํ•œ ๋’ค, ์ด๋ฅผ flask์—์„œ return์œผ๋กœ ๋˜์ง€๋ฉด ํŽ˜์ด์ง€๊ฐ€ ๋ณต์‚ฌ๋จ
  • ๋ฌผ๋ก , ์ผ๋ถ€ css ํšจ๊ณผ๋‚˜ imgํŒŒ์ผ์€ ์‹ค์ œ google ์„œ๋ฒ„์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ์šฉ๋˜์ง€ ์•Š์Œ
from flask import Flask, render_template
import requests
app = Flask(__name__)
@app.route('/google')
def get_google():
    res = requests.get('https://www.google.com')
    return res.text
if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8080")

profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€