๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

4-3_Flask ์‹œ์ž‘ํ•˜๊ธฐ - HTMLํŒŒ์ผ ์ฃผ๊ธฐ ๋ณธ๋ฌธ

[WEB & AI] (feat. ์ทจ์ค€)/์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ_์›น๊ฐœ๋ฐœ์ข…ํ•ฉ๋ฐ˜

4-3_Flask ์‹œ์ž‘ํ•˜๊ธฐ - HTMLํŒŒ์ผ ์ฃผ๊ธฐ

๋ถ€์‹œ๋งค๋‚˜_HA 2024. 3. 25. 21:40
728x90
๋ฐ˜์‘ํ˜•

โ… . ์ด๋ฒˆ ํšŒ์ฐจ ํ•™์Šต ๋ชฉํ‘œ (goal)


1. ๋ฐฐ์šฐ๊ณ ์ž ํ•˜๋Š” ๊ฒƒ

  • flask ์—์„œ ๋งŒ๋“  ๋ฃฐ์„ ๋”ฐ๋ผ๊ฐ€์•ผ ํ•จ โญโญโญ


โ…ก. flask ํ”„๋ ˆ์ž„์›Œํฌ์˜ rule ์„ ๋”ฐ๋ผ๊ฐ€๋ณด๊ธฐ


1. ํด๋” ๊ตฌ์กฐ ๋”ฐ๋ผํ•˜๊ธฐ

  • templates : HTML ํŒŒ์ผ์ด ๋‹ด๊ธฐ๋Š” ๊ณณ
  • static : CSS, ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ๋‹ด๊ธฐ๋Š” ๊ณณ

html ์ด ๋“ค์–ด๊ฐ€๋Š” ์ž๋ฆฌ!

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
   return '<button> ๋‚˜๋Š” ๋ฒ„ํŠผ์ด๋‹ค. </button>' # โœ… ์—ฌ๊ธฐ๊ฐ€ html ๋“ค์–ด๊ฐ€๋Š” ์ž๋ฆฌ, templates ์ค‘ index.html ํŒŒ์ผ ์ž‘์„ฑํ•ด์„œ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ. 


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

  • ๊ฒฐ๊ณผ๋ฌผ


templates ์—์„œ ์ž‘์„ฑํ•ด์„œ html ๋“ค์–ด๊ฐ€๋Š” ์ž๋ฆฌ์— ๋„ฃ๊ธฐ

  • ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ
# โœ… templates ์—์„œ html ๊ฐ€์ ธ์˜ค๊ธฐ
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
   return render_template('index.html')   # โœ… templates ์—์„œ html ๊ฐ€์ ธ์˜ค๊ธฐ

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

  • ๊ฒฐ๊ณผ๋ฌผ

  • localhost:5000

    • python app.py ํ•ด์„œ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ = flask ๋ฅผ ๊ฑฐ์นจ = ์„œ๋ฒ„๊ฐ€ ๋™์ž‘ํ•ด์„œ ์—ด๋ฆฐ ๊ฒฐ๊ณผ
    • ์ง€๊ธˆ ๋งŒ๋“  ์„œ๋ฒ„๊ฐ€ ๋‚˜ํ•œํ…Œ ์›นํŽ˜์ด์ง€๋ฅผ ์ค€ ๊ฒƒ
    • ๋„ค์ด๋ฒ„ ์„œ๋ฒ„๊ฐ€ ๋‚˜ ํ•œํ…Œ ์ค€ ๊ฒƒ ์ฒ˜๋Ÿผ
    • ์ธํ„ฐ๋„ท์— ๋ฐฐํฌ ํ•˜๋ฉด ๋ณผ ์ˆ˜ ์žˆ์Œ
  • ํŒŒ์ด์ฐธ > ์šฐํด๋ฆญ > ์‹คํ–‰ ๊ฒฐ๊ณผ

    • app.py ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์Œ. = flask ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š์Œ = ๊ทธ๋ƒฅ, ๋‚ด ์ปดํ“จํ„ฐ์— ์žˆ๋Š” ํŒŒ์ผ์„ ์—ฐ ๊ฒƒ ์ž„.
728x90
๋ฐ˜์‘ํ˜•
Comments