프젝 끝!

우리조 발표자ㅋㅋㅋㅋㅋㅋㅋ구욥
1. Python Framework 종류
2. Frontend
3. Web
실습
@app.route('/hello')
def test():
return 'Hello Flask'
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = '8080')
# host는 내 pc를 의미

app = Flask(__name__)
@app.route('/example')
def example_func():
return html2
if __name__ == '__main__' :
app.run(host = '127.0.0.1', port = '8081')

app = Flask(__name__)
@app.route('/')
def hello():
return '<h1> hello world </h1>'
@app.route('/profile/<username>') #/hello 경로로 요청 시
def get_profile(username):
return 'hello '+ username
@app.route('/first/<username>') #/first 경로로 요쳥 시
def hello_first(username):
return '<h1> hello {}!! </h1>'.format(username)
if __name__ == '__main__': # 현재 위치에서 직접 실행하면 서버 실행, 외부 import하는 경우에는 서버를 실행시키지 마라
app.run(host='0.0.0.0', port = '8080') #localhost: 내 pc, port: 외부 경로 접근

REST API (Representational State Transfer) (Application Programming Interface)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('json_test')
def hello_json():
data = {'name':'김대리', 'family':'김순자'}
return jsonify(data) #data를 json 형식으로 client에게 전달할 수 있다
@app.route('server_info')
def server_json():
data = {'server_name':'localhost', 'server_port':'8080'}
return jsonify(data)
if __name__ == '__main__' :
app.run(host = '127.0.0.1', port = '8080')


app = Flask(__name__)
def add_file(data):
return data + 5
@app.route('/')
def hell():
return "<h1> hello world </h1>"
@app.route('/message/<int:message_id>')
def get_message(message_id):
return 'message _id : {} '.format(message_id)
@app.route('/first/<int:message_id>')
def get_message1(message_id):
data = add_file(message_id)
return 'message _id : {} '.format(data)
if __name__ == '__main__' :
app.run(host = '127.0.0.1', port = '8080')

(준비) vscode에서 live server 설치

url = "https://www.daum.net/news/search/q='빅데이터'&p = 10"
res = request.get(url)
url = "https://www.daum.net/news/search", data = {'query':'빅데이터', 'page':10}
res = request.post(url, data = data)
실습
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
@app.route('/login')
def login():
username = request.args.get('user_name')
if username == 'dave':
data = {'auth':'success'}
else:
data = {'auth':'failed'}
return jsonify(data)
@app.route('/login-request')
def hello_html():
return render_template('login.html')
# render_template :html script file을 return
#login.html은 templates 폴더 안에 넣어두기
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = '8080')
emmet 설정하기
설정 -> extentions -> emmet
체크

추가

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello_haein') #sub경로에서 값을 받는 경우
def hello_name():
return render_template('variable.html')
if __name__ == "__main__":
app.run(host = '127.0.0.1', port = '8080')

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello/<user>') #sub경로에서 값을 받는 경우
def hello_name2(user):
return render_template('variable1.html', name = user)
if __name__ == "__main__":
app.run(host = '127.0.0.1', port = '8080')


from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello_loop')
def hello_name():
value_list = ['Python', 'java', 'html']
return render_template('loop.html', value = value_list)
if __name__ == "__main__":
app.run(host = '0.0.0.0', port = '8080')
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<li>{{value}}</li>
</ul>
</body>
</html>

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
{% for value in values%} <!-- 파이썬 문법 사용-->
<li>{{value}}</li>
{% endfor %}
</ul>
</body>
</html>

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello_if/<int:score>')
def hello_html(score):
return render_template('condition.html', score = score)
if __name__ == "__main__":
app.run(host = '127.0.0.1', port = '8080')
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--학점출력-->
{% if score >= 90 %}
<h3> A학점입니다.</h3>
{% elif score >= 80 %}
<h3> B학점입니다.</h3>
{% else %}
<h3> C학점입니다.</h3>
{% endif %}
</body>
</html>

from flask import Flask, render_template
import requests
app = Flask(__name__)
@app.route('/google')
def get_google():
res = requests.get('https://www.google.co.kr')
return res.text
@app.route('/naver')
def get_naver():
res = requests.get('https://www.naver.com')
return res.text
@app.route('/daum')
def get_daum():
res = requests.get('https://www.daum.net')
return res.text
if __name__ == "__main__":
app.run(host = '127.0.0.1', port = '8080')



from flask import Flask, render_template, request
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
import time
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/daum_news')
def result():
keyword = request.args.get('keyword')
page_num = request.args.get('page')
daum_news_titles = []
daum_news_url = 'https://search.daum.net/search?w=news&nil_search=btn&DA=NTB&enc=utf8&cluster=y&cluster_page=1&q={}&p={}'
driver = webdriver.Chrome()
for page in range(1, int(page_num)+1):
url = daum_news_url.format(keyword, page)
print(url)
driver.get(url)
time.sleep(2)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
title_path = '#dnsColl > div:nth-child(1) > ul > li > div.c-item-content > div.item-bundle-mid > div.item-title > strong > a'
for li in soup.select(title_path):
# print(li.text)
daum_news_titles.append(li.text)
return render_template('daum_news.html', daum_news = daum_news_titles)
if __name__ == "__main__":
app.run(host = '127.0.0.1', port = '8080')
앞으로 남은거