๐ Flask ํ๋ ์์ํฌ: ์๋ฒ๋ฅผ ๊ตฌ๋์์ผ์ฃผ๋ ํธํ ์ฝ๋ ๋ชจ์. ์๋ฒ๋ฅผ ๊ตฌ๋ํ๋ ค๋ฉด ํ์ํ ๋ณต์กํ ์ผ๋ค์ ์ฝ๊ฒ ๊ฐ์ ธ๋ค ์ธ ์ ์์ต๋๋ค.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'This is Home!'
@app.route('/mypage')
def mypage():
return 'This is My Page!'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
Flask ์๋ฒ๋ฅผ ๋ง๋ค ๋, ํญ์,
ํ๋ก์ ํธ ํด๋ ์์,
ใดstatic ํด๋ (์ด๋ฏธ์ง, cssํ์ผ์ ๋ฃ์ด๋ก๋๋ค)
ใดtemplates ํด๋ (htmlํ์ผ์ ๋ฃ์ด๋ก๋๋ค)
ใดapp.py ํ์ผ
์ด๋ ๊ฒ ์ธ ๊ฐ๋ฅผ ๋ง๋ค์ด๋๊ณ ์์ํ์ธ์. ์ด์ ๊ฐ ํด๋์ ์ญํ ์ ์์๋ด
์๋ค!
(๊ผญ ์ฐธ๊ณ !! venv๋ ์ค์ ๋ก๋ ๋ณด์ด์ง๋ง, ์๋ณด์ธ๋ค~๋ผ๊ณ ์๊ฐํ์ธ์! ๊ธฐ์ตํ์์ฃ ?)
flask
๋ด์ฅํจ์ render_template
๋ฅผ ์ด์ฉํ์ฌ html
ํ์ผ ๋ถ๋ฌ์ค๊ธฐ ๋ฐ๋ก ์ด๊ฒ ํ๋ ์์ํฌ์ ์๋ ฅ!from flask import Flask, render_template
app = Flask(__name__)
## URL ๋ณ๋ก ํจ์๋ช
์ด ๊ฐ๊ฑฐ๋,
## route('/') ๋ฑ์ ์ฃผ์๊ฐ ๊ฐ์ผ๋ฉด ์๋ฉ๋๋ค.
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
๐ GET
โ ํต์์ ์ผ๋ก! ๋ฐ์ดํฐ ์กฐํ(Read)๋ฅผ ์์ฒญํ ๋
์) ์ํ ๋ชฉ๋ก ์กฐํ
โ ๋ฐ์ดํฐ ์ ๋ฌ : URL ๋ค์ ๋ฌผ์ํ๋ฅผ ๋ถ์ฌ key=value๋ก ์ ๋ฌ
โ ์: google.com?q=๋ถ๊ทน๊ณฐ
๐ POST
โ ํต์์ ์ผ๋ก! ๋ฐ์ดํฐ ์์ฑ(Create), ๋ณ๊ฒฝ(Update), ์ญ์ (Delete) ์์ฒญ ํ ๋
์) ํ์๊ฐ์
, ํ์ํํด, ๋น๋ฐ๋ฒํธ ์์
โ ๋ฐ์ดํฐ ์ ๋ฌ : ๋ฐ๋ก ๋ณด์ด์ง ์๋ HTML body์ key:value ํํ๋ก ์ ๋ฌ
1. ์ฐ์ต ์์ ๋ผ๋ ์ค๋นํ๊ธฐ
2. POST ์ฐ์ต (์ฃผ๋ฌธ ์ ์ฅ)
1. ์์ฒญ ์ ๋ณด : URL= /mars, ์์ฒญ ๋ฐฉ์ = POST
2. ํด๋ผ(ajax) โ ์๋ฒ(flask) : name, address, size
3. ์๋ฒ(flask) โ ํด๋ผ(ajax) : ๋ฉ์์ง๋ฅผ ๋ณด๋ (์ฃผ๋ฌธ ์๋ฃ!)
index.html
์ ์๋ฒ app.py
์ฐ๊ฒฐ ํ์ธํ๊ธฐname
, address
, size
์ ๋ณด๋ฅผ ๋ฐ์์, ์ ์ฅํ๋ฉด ๋๊ฒ ์ฃ ?[dbtest.py](http://dbtest.py)
ํ์ผ์ ๋ถ๋ฌ์จ๋ค.@app.route("/mars", methods=["POST"])
def mars_post():
name_receive = request.form['name_give']
address_receive = request.form['address_give']
size_receive = request.form['size_give']
doc = {
'name': name_receive,
'address': address_receive,
'size': size_receive
}
db.orders.insert_one(doc)
return jsonify({'msg': '์ฃผ๋ฌธ ์๋ฃ!'})
name
, address
, size
์ ๋ณด๋ฅผ ๋ณด๋ด์ค๋ค.function save_order() {
let name = $('#name').val()
let address = $('#address').val()
let size = $('#size').val()
$.ajax({
type: 'POST',
url: '/mars',
data: { name_give:name, address_give:address, size_give:size },
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
3. GET ์ฐ์ต (์ฃผ๋ฌธ ๋ณด์ฌ์ฃผ๊ธฐ)
orders
์ ์ฃผ๋ฌธ์ ๋ณด๋ฅผ ๋ด์์ ๋ด๋ ค์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค@app.route("/mars", methods=["GET"])
def mars_get():
orders_list = list(db.orders.find({},{'_id':False}))
return jsonify({'orders':orders_list})
function show_order() {
$('#order-box').empty()
$.ajax({
type: 'GET',
url: '/mars',
data: {},
success: function (response) {
let rows = response['orders']
for (let i = 0; i < rows.length; i++) {
let name = rows[i]['name']
let address = rows[i]['address']
let size = rows[i]['size']
let temp_html = `<tr>
<td>${name}</td>
<td>${address}</td>
<td>${size}</td>
</tr>`
$('#order-box').append(temp_html)
}
}
});
}
๐์ ๋ชฉ
, ์ธ๋ค์ผ ์ด๋ฏธ์ง
, ๋ด์ฉ
์ด๋ค.
์ด ๋ถ๋ถ์, 'meta'ํ๊ทธ๋ฅผ ํฌ๋กค๋ง ํจ์ผ๋ก์จ ๊ณตํต์ ์ผ๋ก ์ป์ ์ ์๋ค.
๋ฉํ ํ๊ทธ๋, ๋ถ๋ถ์ ๋ค์ด๊ฐ๋, ๋์ผ๋ก ๋ณด์ด๋ ๊ฒ(body) ์ธ์ ์ฌ์ดํธ์ ์์ฑ์ ์ค๋ช ํด์ฃผ๋ ํ๊ทธ๋ค์ ๋๋ค.
์) ๊ตฌ๊ธ ๊ฒ์ ์ ํ์ ๋ ์ค๋ช
๋ฌธ, ์ฌ์ดํธ ์ ๋ชฉ, ์นดํก ๊ณต์ ์ ํ์ ๋ ์ด๋ฏธ์ง ๋ฑ
head์ meta[property="og:image"]
์ ๊ฐ์ ธ์จ๋ค.
import requests
from bs4 import BeautifulSoup
url = 'https://movie.naver.com/movie/bi/mi/basic.naver?code=191597'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url,headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
og_image = soup.select_one('meta[property="og:image"]')
og_title = soup.select_one('meta[property="og:title"]')
og_description = soup.select_one('meta[property="og:description"]')
image = og_image['content']
title = og_title['content']
description = og_description['content']
1. ์ํ ์ฐ์ต 2 ์์ ๋ผ๋ ์ค๋นํ๊ธฐ
2. POST ์ฐ์ต (ํฌ์คํ ํ๊ธฐ)
app.py
์ฐ๊ฒฐurl
, star
, comment
์ ๋ณด๋ฅผ ๋ฐ์์ ์ ์ฅํ๊ธฐ @app.route("/movie", methods=["POST"])
def movie_post():
url_receive = request.form['url_give']
star_receive = request.form['star_give']
comment_receive = request.form['comment_give']
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
data = requests.get(url_receive, headers=headers)
soup = BeautifulSoup(data.text, 'html.parser')
og_image = soup.select_one('meta[property="og:image"]')
og_title = soup.select_one('meta[property="og:title"]')
og_description = soup.select_one('meta[property="og:description"]')
image = og_image['content']
title = og_title['content']
description = og_description['content']
doc = {
'image':image,
'title':title,
'desc':description,
'star':star_receive,
'comment':comment_receive
}
db.movies.insert_one(doc)
return jsonify({'msg':'POST ์ฐ๊ฒฐ ์๋ฃ!'})
index.html
์ฐ๊ฒฐ function posting() {
let url = $('#url').val()
let star = $('#star').val()
let comment = $('#comment').val()
$.ajax({
type: 'POST',
url: '/movie',
data: {url_give: url, star_give: star, comment_give: comment},
success: function (response) {
alert(response['msg'])
window.location.reload()
}
});
}
3. GET ์ฐ์ต (๋ณด์ฌ์ฃผ๊ธฐ)
app.py
์ฐ๊ฒฐ@app.route("/movie", methods=["GET"])
def movie_get():
movie_list = list(db.movies.find({},{'_id':False}))
return jsonify({'movies':movie_list})
index.html
์ฐ๊ฒฐfunction listing() {
$('#cards-box').empty()
$.ajax({
type: 'GET',
url: '/movie',
data: {},
success: function (response) {
let rows = response['movies']
for(let i = 0; i < rows.length; i++) {
let image = rows[i]['image']
let title = rows[i]['title']
let desc = rows[i]['desc']
let star = rows[i]['star']
let comment = rows[i]['comment']
let star_image = 'โญ'.repeat(star)
let temp_html = `<div class="col">
<div class="card h-100">
<img src="${image}"
class="card-img-top">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${desc}</p>
<p>${star_image}</p>
<p class="mycomment">${comment}</p>
</div>
</div>
</div>`
$('#cards-box').append(temp_html)
}
}
})
}
๐ ๋ง๋ณด๊ธฐ ์์