
app.py
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
from pymongo import MongoClient
import certifi
ca = certifi.where()
client = MongoClient('mongodb+srv://test:sparta@cluster0.4ggm2nj.mongodb.net/Cluster0?retryWrites=true&w=majority', tlsCAFile =ca)
db = client.dbsparta
@app.route('/')
def home():
return render_template('index.html')
@app.route("/bucket", methods=["POST"])
def bucket_post():
bucket_receive = request.form['bucket_give']
bucket_list = list(db.bucket.find({} , {'_id' : False }))
count = len(bucket_list) + 1
doc = {
'num' : count,
'bucket' : bucket_receive,
'done' : 0
}
db.bucket.insert_one(doc)
return jsonify({'msg': '등록 완료!'})
@app.route("/bucket/done", methods=["POST"])
def bucket_done():
num_receive = request.form['num_give']
db.bucket.update_one({'num' : int (num_receive)}, {'$set' : {'done' : 1}})
return jsonify({'msg': ' 버킷 완료!'})
@app.route("/bucket", methods=["GET"])
def bucket_get():
bucket_list = list (db.bucket.find({} , {'_id' : False }))
return jsonify({'buckets': bucket_list})
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)
index.html
<script>
$(document).ready(function () {
show_bucket();
});
function show_bucket() {
$.ajax({
type: "GET",
url: "/bucket",
data: {},
success: function (response) {
let rows = response['buckets']
for ( let i = 0 ; i < rows.length ; i ++) {
let bucket = rows[i]['bucket']
let num = rows[i]['num']
let done = rows[i]['done']
let temp_html = ``
if (done == 0) {
temp_html = `<li>
<h2>✅ ${bucket}</h2>
<button onclick="done_bucket(#{num})" type="button" class="btn btn-outline-primary">완료!</button>
</li>`
} else {
temp_html = `<li>
<h2 class="done">#{bucket}</h2>
</li>`
}
$('#bucket-list'). append(temp_html)
}
}
});
}
function save_bucket() {
$.ajax({
type: "POST",
url: "/bucket",
data: {bucket_give: bucket},
success: function (response) {
alert(response["msg"])
window.location.reload()
}
});
}
function done_bucket(num) {
$.ajax({
type: "POST",
url: "/bucket/done",
data: {bucket_give: num},
success: function (response) {
alert(response["msg"])
window.location.reload()
}
});
}
</script>
/usr/local/bin/python3.8 /Users/js.k/Desktop/sparta/project/bucket/app.py
* Serving Flask app 'app'
* Debug mode: on
Address already in use
Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.
On macOS, try disabling the 'AirPlay Receiver' service from System Preferences -> Sharing.
종료 코드 1(으)로 완료된 프로세스
이렇게 오류가 뜹니다.
