๐Ÿ‘ฉ‍๐Ÿ’ป Learn programming

[๋‚˜๋งŒ์˜ ๋‹จ์–ด์žฅ] 6. ์ƒ์„ธํŽ˜์ด์ง€ - jinja2๋กœ ๋‹จ์–ด ๋œป ๊ฐ€์ ธ์˜ค๊ธฐ

๋ฐ๊ตฅ์ž‰ 2022. 5. 4. 14:10
๋ฐ˜์‘ํ˜•

Jinja2 ํ…œํ”Œ๋ฆฟ ์–ธ์–ด

  • Flask ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์–ธ์–ด.
  • ‘ํ…œํ”Œ๋ฆฟ’์ด ๋˜๋Š” Html ๋ฌธ์„œ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ณณ์„ ํ‘œ์‹œํ•ด๋†“๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ํŒŒ์ด์ฐธ > setting > Template Languages > Jinja2 ์„ค์ •

 

โœ… Owlbot API ์š”์ฒญ Flask

r = requests.get(f"<https://owlbot.info/api/v4/dictionary/{keyword}>", headers={"Authorization": "Token [๋‚ดํ† ํฐ]"})
result = r.json()
print(result)

 

โœ… jinja2๋ฅผ ์ด์šฉํ•ด์„œ detail.html์— ๊ฐ’์„ ์ฑ„์›Œ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด detil.html๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” detail ํ•จ์ˆ˜ ์•ˆ์—
์‚ฌ์ „ API์— ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” ๋ถ€๋ถ„์ด ํ•„์š”.

@app.route('/detail/')
def detail(keyword):
    # API์—์„œ ๋‹จ์–ด ๋œป ์ฐพ์•„์„œ ๊ฒฐ๊ณผ ๋ณด๋‚ด๊ธฐ
    r = requests.get(f"<https://owlbot.info/api/v4/dictionary/{keyword}>", headers={"Authorization": "Token [๋‚ด ํ† ํฐ]"})
    # 1. url์˜ ์ผ๋ถ€๋ฅผ ๋ฐ›์•„์„œ keyword๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ด์„œ ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” url ๋’ค์—๋‹ค๊ฐ€ ๋„ฃ์–ด์คŒ
    # 2. header ์ •๋ณด ์•ˆ์— ๋‚ด ํ† ํฐ์„ ๋„ฃ์–ด์„œ ํ—ˆ๊ฐ€๋ฅผ ๋ฐ›์Œ
    result = r.json()
    # ๋ฐ›์•„์˜จ ๊ฒฐ๊ณผ๋Š” json ํ˜•ํƒœ์ด๊ณ , result๋ผ๋Š” ๋ณ€์ˆ˜์— ๋„ฃ์Œ
    print(result)
    return render_template("detail.html", word=keyword, result=result)
    # reult๋ฅผ detail.html์— result๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณด๋‚ด์ฃผ์„ธ์š”
<div class="container">
    <div class="d-flex justify-content-between align-items-end">
        <div>
            <h1 id="word" style="display: inline;">{{ word }}</h1> <!-- ๋‹จ์–ด -->
            <h5 id="pronunciation" style="display: inline;">/{{ result.pronunciation }}/</h5>  <!-- ๋ฐœ์Œ -->
        </div>
        <button id="btn-save" class="btn btn-outline-sparta btn-lg">save</button>
        <button id="btn-delete" class="btn btn-sparta btn-lg" style="display:none;">delete</button>
    </div>
    <hr>
    <div id="definitions">
        {% for definition in result.definitions %} <!--result ์•ˆ์— definitions ๋ผ๊ณ  ๋“ค์–ด์žˆ๋Š” ๋งŒํผ ๋ฐ˜๋ณต -->
            <div style="padding:10px">
                <i>{{ definition.type }}</i>  <!-- ํ’ˆ์‚ฌ -->
                <br>{{ definition.definition }}<br>  <!-- ๋œป -->
                <span class="example">{{ definition.example }}</span>  <!-- ์˜ˆ๋ฌธ -->
            </div>
        {% endfor %} <!-- ๋ฐ˜๋ณต๋ฌธ์€ ์—ฌ๊ธฐ๊นŒ์ง€ -->
    </div>
</div>
{{ result[”word”] }} = {{ result.word }}

โœ… ๋ฐœ์Œ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ณด์—ฌ์ฃผ๋„๋ก ์˜ˆ์™ธ์ฒ˜๋ฆฌ

{% if result.pronunciation %} <!-- {% if result.pronunciation != None %}์™€ ๊ฐ™์Œ. [result์˜ pronunciation์ด None์ด ์•„๋‹ˆ๋‹ค = ๊ฐ’์ด ์žˆ๋‹ค = true] -->
    <h5 id="pronunciation" style="display: inline;">/{{ result.pronunciation }}/</h5>
{% endif %}

 

โœ… ์˜ˆ๋ฌธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ณด์—ฌ์ฃผ๋„๋ก ์˜ˆ์™ธ์ฒ˜๋ฆฌ

{% if definition.example %}
    <span class="example">{{ definition.example }}</span>
{% endif %}

 

โœ… ์˜ˆ๋ฌธ์— HTML ํƒœ๊ทธ ์“ฐ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉ

<span class="example">{{ definition.example|safe }}</span>
  • definition.example์ด html์ผ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ์ด์ƒํ•œ ๊ฒƒ์„ ์‹œ๋„ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ทธ๋ƒฅ ๊พธ๋ฉฐ์ฃผ๋Š” ๊ฑฐ๋ผ ์•ˆ์ „ํ•˜๋‹ˆ๊นŒ
    ๊ทธ๋Œ€๋กœ ํ‘œ์‹œํ•˜์„ธ์š”.

 

โœ… ์ •์˜์™€ ์˜ˆ๋ฌธ์—์„œ ๊นจ์ง„ ๊ธ€์ž๋ฅผ ์—†์•ฐ

<br>{{ definition.definition.encode('ascii', 'ignore').decode('utf-8') }}<br> <!--๋œป ๋ถ€๋ถ„์˜ ๊นจ์ง„ ๊ธ€์ž ์—†์•ฐ-->
{% if definition.example %}
    <span class="example">{{ definition.example.encode('ascii', 'ignore').decode('utf-8')|safe }}</span> <!--์˜ˆ๋ฌธ ๋ถ€๋ถ„์˜ ๊นจ์ง„ ๊ธ€์ž ์—†์•ฐ-->
{% endif %}

 

โœ… definition์˜ example์˜ ๋ฌธ์ž์—ด์„ ascii ์ฝ”๋“œ์˜ ํ˜•ํƒœ๋กœ ์ธ์ฝ”๋”ฉ์„ ํ•ด์ค„๊ฑด๋ฐ,
ascii ์ฝ”๋“œ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ๋“ค์€ ๋ฌด์‹œํ•˜๊ณ  ๋ฐ”๋€Œ๋Š” ๊ฒƒ๋“ค์€ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”.

<div class="container">
    <div class="d-flex justify-content-between align-items-end">
        <div>
            <h1 id="word" style="display: inline;">{{ word }}</h1> 
            {% if result.pronunciation %}
                <h5 id="pronunciation" style="display: inline;">/{{ result.pronunciation }}/</h5>
            {% endif %}
        </div>
        <button id="btn-save" class="btn btn-outline-sparta btn-lg">save</button>
        <button id="btn-delete" class="btn btn-sparta btn-lg" style="display:none;">delete</button>
    </div>
    <hr>
    <div id="definitions">
        {% for definition in result.definitions %} 
		        <div style="padding:10px">
		            <i>{{ definition.type }}</i>  
		            <br>{{ definition.definition.encode('ascii', 'ignore').decode('utf-8') }}<br> 
                    {% if definition.example %}
                        <span class="example">{{ definition.example.encode('ascii', 'ignore').decode('utf-8')|safe }}</span> 
                    {% endif %}
		        </div>
        {% endfor %} 
    </div>
</div>
๋ฐ˜์‘ํ˜•