-프로젝트 만들기
new project
html file 만들기
-app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
name = '이미자'
return render_template('index.html', name=name)
@app.route('/book_add', methods=['GET'])
def book_add():
return render_template('book_add.html')
@app.route('/book_add_process', methods=['POST'])
def book_add_process():
return render_template('book_add_result.html')
if __name__ == '__main__':
app.run()
-index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>안녕 {{ name }}</h3>
</body>
</html>
-book_add.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>서적 등록</title>
</head>
<body>
<form action="/book_add_process" method="post">
<table width='300px' border="1" bordercolor="'blue">
<tr>
<td>
제목
</td>
<td>
<input type="text" name="title">
</td>
</tr>
<tr>
<td>
표지
</td>
<td>
<input type="file" name="file">
</td>
</tr>
<tr>
<td>
저자
</td>
<td>
<input type="text" name="author">
</td>
</tr>
<tr>
<td>
가격
</td>
<td>
<input type="text" name="price">
</td>
</tr>
<tr>
<td>
ISBN
</td>
<td>
<input type="text" name="isbn">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="등 록">
</td>
</tr>
</table>
</form>
</body>
</html>
-book_add_result.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>서적 등록</title>
</head>
<body>
서적 등록 결과
</body>
</html>
-mongodb 연동
pip install pymongo
from pymongo import MongoClient
-book_add.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>서적 등록</title>
</head>
<body>
<form action="/book_add_process" method="post" enctype="multipart/form-data">
<table width="300px" border="1" bordercolor="blue">
<tr>
<td>
제목
</td>
<td>
<input type="text" name="title">
</td>
</tr>
<tr>
<td>
표지
</td>
<td>
<input type="file" name="file">
</td>
</tr>
<tr>
<td>
저자
</td>
<td>
<input type="text" name="author">
</td>
</tr>
<tr>
<td>
가격
</td>
<td>
<input type="text" name="price">
</td>
</tr>
<tr>
<td>
ISBN
</td>
<td>
<input type="text" name="isbn">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="등 록">
</td>
</tr>
</table>
</form>
</body>
</html>
-app.py
import base64
from flask import Flask, render_template, request
from pymongo import MongoClient
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
name = '이미자'
return render_template('index.html', name=name)
@app.route('/book_add', methods=['GET'])
def book_add():
return render_template('book_add.html')
@app.route('/book_add_process', methods=['POST'])
def book_add_process():
client = MongoClient("mongodb://localhost:27017/")
database = client["ok-db"]
collection = database["books"]
title = request.form['title']
file = request.files['file']
author = request.form['author']
price = request.form['price']
isbn = request.form['isbn']
encoded_data = base64.b64encode(file.read())
doc = {'title': title, 'encoded_data': encoded_data, 'author': author,
'price': price, 'created_date': datetime.now()}
collection.insert_one(doc)
return render_template('book_add_result.html')
if __name__ == '__main__':
app.run()
- studio 3t
import base64
from flask import Flask, render_template, request
from pymongo import MongoClient
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
name = '이미자'
return render_template('index.html', name=name)
@app.route('/book_add', methods=['GET'])
def book_add():
return render_template('book_add.html')
@app.route('/book_add_process', methods=['POST'])
def book_add_process():
client = MongoClient("mongodb://localhost:27017/")
database = client["ok-db"]
collection = database["books"]
title = request.form['title']
file = request.files['file']
author = request.form['author']
price = request.form['price']
isbn = request.form['isbn']
encoded_data = base64.b64encode(file.read())
doc = {'title': title, 'encoded_data': encoded_data, 'author': author,
'price': price, 'created_date': datetime.now()}
result = collection.insert_one(doc)
book_add_result = None
if result.inserted_id is not None:
print(result.inserted_id)
book_add_result = "정상 등록"
else:
book_add_result = "등록 실패"
return render_template('book_add_result.html',
book_add_result=book_add_result)
if __name__ == '__main__':
app.run()
print에서 밑에 id 뜸
db.books.find({"_id": ObjectId("여기 넣기")})
확인할 수 있음
-도서 추가/ id 검색해보기
-app.py
import base64
from bson import ObjectId
from flask import Flask, render_template, request
from pymongo import MongoClient
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
name = '이미자'
return render_template('index.html', name=name)
@app.route('/book_add', methods=['GET'])
def book_add():
return render_template('book_add.html')
@app.route('/book_add_process', methods=['POST'])
def book_add_process():
client = MongoClient("mongodb://localhost:27017/")
database = client["ok-db"]
collection = database["books"]
title = request.form['title']
file = request.files['file']
author = request.form['author']
price = request.form['price']
isbn = request.form['isbn']
encoded_data = base64.b64encode(file.read())
doc = {'title': title, 'encoded_data': encoded_data, 'author': author,
'price': price, 'created_date': datetime.now()}
result = collection.insert_one(doc)
book_add_result = None
if result.inserted_id is not None:
print(result.inserted_id)
book_add_result = "정상 등록"
else:
book_add_result = "등록 실패"
return render_template('book_add_result.html',
book_add_result=book_add_result)
@app.route('/book_id_search', methods=['GET'])
def book_id_search():
return render_template('book_id_search.html')
@app.route('/book_id_search_process', methods=['POST'])
def book_id_search_process():
client = MongoClient()
database = client["ok-db"]
collection = database["books"]
_id = request.form['id']
query = {'_id': ObjectId(_id)}
doc = collection.find_one(query)
title = doc['title']
return render_template('book_id_search_result.html', title=title)
if __name__ == '__main__':
app.run()
-book_id_search.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>서적 검색(id)</title>
</head>
<body>
<form action="/book_id_search_process" method="post">
id:
<input type="test" name="id">
<input type="submit" value="검 색">
</form>
</body>
</html>
-book_id_search_result.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>서적 검색(id) 결과</title>
</head>
<body>
<h3>서적 검색(id) 결과</h3>
<table width="300px" border="1" bordercolor="blue">
<tr>
<td>
제목
</td>
<td>
{{title}}
</td>
</tr>
<tr>
<td>
표지
</td>
<td>
</td>
</tr>
<tr>
<td>
저자
</td>
<td>
</td>
</tr>
<tr>
<td>
가격
</td>
<td>
</td>
</tr>
<tr>
<td>
ISBN
</td>
<td>
</td>
</tr>
</table>
</body>
</html>
-나머지는 위의 코드와 동일
- 사진도 같이 나오게 하기
-app.py
import base64
from bson import ObjectId
from flask import Flask, render_template, request
from pymongo import MongoClient
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
name = '이미자'
return render_template('index.html', name=name)
@app.route('/book_add', methods=['GET'])
def book_add():
return render_template('book_add.html')
@app.route('/book_add_process', methods=['POST'])
def book_add_process():
client = MongoClient("mongodb://localhost:27017/")
database = client["ok-db"]
collection = database["books"]
title = request.form['title']
file = request.files['file']
author = request.form['author']
price = request.form['price']
isbn = request.form['isbn']
encoded_data = base64.b64encode(file.read())
doc = {'title': title, 'encoded_data': encoded_data, 'author': author,
'price': price, 'created_date': datetime.now()}
result = collection.insert_one(doc)
book_add_result = None
if result.inserted_id is not None:
print(result.inserted_id)
book_add_result = "정상 등록"
else:
book_add_result = "등록 실패"
return render_template('book_add_result.html',
book_add_result=book_add_result)
@app.route('/book_id_search', methods=['GET'])
def book_id_search():
return render_template('book_id_search.html')
@app.route('/book_id_search_process', methods=['POST'])
def book_id_search_process():
client = MongoClient()
database = client["ok-db"]
collection = database["books"]
_id = request.form['id']
query = {'_id': ObjectId(_id)}
doc = collection.find_one(query)
title = doc['title']
decoded_data = doc['encoded_data'].decode('utf-8')
img_src_data = f'data:image/png;base64, {decoded_data}'
return render_template('book_id_search_result.html', title=title, img_src_data=img_src_data)
if __name__ == '__main__':
app.run()
-book_id_search_result.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>서적 검색(id) 결과</title>
</head>
<body>
<h3>서적 검색(id) 결과</h3>
<table width="300px" border="1" bordercolor="blue">
<tr>
<td>
제목
</td>
<td>
{{title}}
</td>
</tr>
<tr>
<td>
표지
</td>
<td>
<img src="{{ img_src_data }}">
</td>
</tr>
<tr>
<td>
저자
</td>
<td>
</td>
</tr>
<tr>
<td>
가격
</td>
<td>
</td>
</tr>
<tr>
<td>
ISBN
</td>
<td>
</td>
</tr>
</table>
</body>
</html>
구구단
-5단
-gugudan.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul{
list-style: none;
}
</style>
</head>
<body>
<ul>
{% for gugu in gugu_list %}
<li>{{ gugu }}</li>
{% endfor %}
</ul>
</body>
</html>
-app.py
import base64
from bson import ObjectId
from flask import Flask, render_template, request
from pymongo import MongoClient
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
name = '이미자'
return render_template('index.html', name=name)
@app.route('/book_add', methods=['GET'])
def book_add():
return render_template('book_add.html')
@app.route('/book_add_process', methods=['POST'])
def book_add_process():
client = MongoClient("mongodb://localhost:27017/")
database = client["ok-db"]
collection = database["books"]
title = request.form['title']
file = request.files['file']
author = request.form['author']
price = request.form['price']
isbn = request.form['isbn']
encoded_data = base64.b64encode(file.read())
doc = {'title': title, 'encoded_data': encoded_data, 'author': author,
'price': price, 'created_date': datetime.now()}
result = collection.insert_one(doc)
book_add_result = None
if result.inserted_id is not None:
print(result.inserted_id)
book_add_result = "정상 등록"
else:
book_add_result = "등록 실패"
return render_template('book_add_result.html',
book_add_result=book_add_result)
@app.route('/book_id_search', methods=['GET'])
def book_id_search():
return render_template('book_id_search.html')
@app.route('/book_id_search_process', methods=['POST'])
def book_id_search_process():
client = MongoClient()
database = client["ok-db"]
collection = database["books"]
_id = request.form['id']
query = {'_id': ObjectId(_id)}
doc = collection.find_one(query)
title = doc['title']
decoded_data = doc['encoded_data'].decode('utf-8')
img_src_data = f'data:image/png;base64, {decoded_data}'
return render_template('book_id_search_result.html', title=title, img_src_data=img_src_data)
@app.route('/gugudan', methods=['GET'])
def gugudan():
dan = 5
gugu_list = [] # list()
for i in range(1, 10):
gugu = f'{dan} × {i} = {dan * i}'
gugu_list.append(gugu)
return render_template('gugudan.html', gugu_list=gugu_list)
if __name__ == '__main__':
app.run()