๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

app.py / index.html ๋ผˆ๋Œ€์ฝ”๋“œ ๋ณธ๋ฌธ

Deveolpment Study๐Ÿ—‚๏ธ

app.py / index.html ๋ผˆ๋Œ€์ฝ”๋“œ

bompeach 2022. 6. 16. 16:17

๋ผˆ๋Œ€์ฝ”๋“œ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

[์ฝ”๋“œ๋‹ˆ์ŠคํŽซ]

ํŒฌ๋ช…๋กapp.py

from flask import Flask, render_template, request, jsonify
app = Flask(__name__)

@app.route('/')
def home():
   return render_template('index.html')

@app.route("/homework", methods=["POST"])
def homework_post():
    sample_receive = request.form['sample_give']
    print(sample_receive)
    return jsonify({'msg':'POST ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})

@app.route("/homework", methods=["GET"])
def homework_get():
    return jsonify({'msg':'GET ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})

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

 

ํŒฌ๋ช…๋กindex.html

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>

    <title>์ดˆ๋ฏธ๋‹ˆํ™ˆํ”ผ - ํŒฌ๋ช…๋ก</title>

    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+KR:wght@200;300;400;500;600;700;900&display=swap" rel="stylesheet">
    <style>
        * {
            font-family: 'Noto Serif KR', serif;
        }
        .mypic {
            width: 100%;
            height: 300px;

            background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('์•„ํ‹ฐ์ŠคํŠธ ์‚ฌ์ง„URL');
            background-position: center 30%;
            background-size: cover;

            color: white;

            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        .mypost {
            width: 95%;
            max-width: 500px;
            margin: 20px auto 20px auto;

            box-shadow: 0px 0px 3px 0px black;
            padding: 20px;
        }

        .mypost > button {
            margin-top: 15px;
        }

        .mycards {
            width: 95%;
            max-width: 500px;
            margin: auto;
        }

        .mycards > .card {
            margin-top: 10px;
            margin-bottom: 10px;
        }
    </style>
    <script>
        $(document).ready(function(){
            set_temp()
            show_comment()
        });
        function set_temp(){
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/sparta_api/weather/seoul",
                data: {},
                success: function (response) {
                    $('#temp').text(response['temp'])
                }
            })
        }
        function save_comment(){
            $.ajax({
                type: 'POST',
                url: '/homework',
                data: {sample_give:'๋ฐ์ดํ„ฐ์ „์†ก'},
                success: function (response) {
                    alert(response['msg'])
                    window.location.reload()
                }
            })
        }
        function show_comment(){
            $.ajax({
                type: "GET",
                url: "/homework",
                data: {},
                success: function (response) {
                    alert(response["msg"])
                }
            });
        }
    </script>
</head>
<body>
    <div class="mypic">
        <h1>์•„ํ‹ฐ์ŠคํŠธ์ด๋ฆ„ ํŒฌ๋ช…๋ก</h1>
        <p>ํ˜„์žฌ๊ธฐ์˜จ: <span id="temp">36</span>๋„</p>
    </div>
    <div class="mypost">
        <div class="form-floating mb-3">
            <input type="text" class="form-control" id="name" placeholder="url">
            <label for="floatingInput">๋‹‰๋„ค์ž„</label>
        </div>
        <div class="form-floating">
            <textarea class="form-control" placeholder="Leave a comment here" id="comment"
                style="height: 100px"></textarea>
            <label for="floatingTextarea2">์‘์›๋Œ“๊ธ€</label>
        </div>
        <button onclick="save_comment()" type="button" class="btn btn-dark">์‘์› ๋‚จ๊ธฐ๊ธฐ</button>
    </div>
    <div class="mycards" id="comment-list">
        <div class="card">
            <div class="card-body">
                <blockquote class="blockquote mb-0">
                    <p>์ƒˆ๋กœ์šด ์•จ๋ฒ” ๋„ˆ๋ฌด ๋ฉ‹์ ธ์š”!</p>
                    <footer class="blockquote-footer">ํ˜ธ๋นต๋งจ</footer>
                </blockquote>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <blockquote class="blockquote mb-0">
                    <p>์ƒˆ๋กœ์šด ์•จ๋ฒ” ๋„ˆ๋ฌด ๋ฉ‹์ ธ์š”!</p>
                    <footer class="blockquote-footer">ํ˜ธ๋นต๋งจ</footer>
                </blockquote>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <blockquote class="blockquote mb-0">
                    <p>์ƒˆ๋กœ์šด ์•จ๋ฒ” ๋„ˆ๋ฌด ๋ฉ‹์ ธ์š”!</p>
                    <footer class="blockquote-footer">ํ˜ธ๋นต๋งจ</footer>
                </blockquote>
            </div>
        </div>
    </div>
</body>
</html>

 

 

์ด์ œ ์—ฌ๊ธฐ์—

์‘์›๋‚จ๊ธฐ๊ธฐ(post๋ฐฉ์‹)๊ณผ

์‘์›๋ณด๊ธฐ(get๋ฐฉ์‹)์„ ์—ฐ์Šตํ•ด๋ณด์ž. 

 

์•„์ฐธ! ์ง€๊ธˆ localhost:5000 ์ฃผ์†Œ๋Š” ์˜ํ™”๊ธฐ๋กํ•˜๊ธฐ ์ฃผ์†Œ๋กœ ๋˜์–ด์žˆ๋‹ค. 

ํŒฌ๋ช…๋ก ์ฃผ์†Œ๋„ localhost:5000์ด๋ผ๋Š”๋ฐ ๋‚˜๋Š” ์—ฌ์ „ํžˆ ์˜ํ™”๊ธฐ๋กํ•˜๊ธฐ์ฐฝ์œผ๋กœ ๋œฌ๋‹ค,

์ด๋Ÿด๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

 

์„ ์ƒ๋‹˜์˜ ์ฝ”๋ฉ˜ํŠธ^^!!

์›นํŽ˜์ด์ง€๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์„œ๋ฒ„๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•˜๊ฒŒ ๋˜์–ด์žˆ์ฃ ?
๊น€๋ด„๋‹˜์˜ ์›นํŽ˜์ด์ง€์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ฃผ๋Š” ์„œ๋ฒ„๊ฐ€ localhost:5000 ์ด๋ผ๋Š” API๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋“ค์„ ์ „๋‹ฌํ•ด์ฃผ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์ฃ ^^๊ฐ๊ฐ์˜ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ๊ฐ๊ฐ ๋‹ค๋ฅธ ์„œ๋ฒ„๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  ์žˆ๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค^^
์–ด๋–ค ์„œ๋ฒ„๋ฅผ ์ผœ๋‘์—ˆ๋А๋ƒ์— ๋”ฐ๋ผ ๊ฐ™์€ ์ฃผ์†Œ์—๋„ ๋‹ค๋ฅธ ๋ฐ˜์‘์ด ์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค^^localhost:5000๋ฅผ ์ž…๋ ฅํ•˜์…จ์„ ๋•Œ
์˜ํ™”๊ธฐ๋กํ•˜๊ธฐ ์‚ฌ์ดํŠธ๊ฐ€ ๋œจ๊ฒŒ ๋œ๋‹ค๋ฉด ํ˜„์žฌ ๊ทธ ์„œ๋ฒ„๊ฐ€ ๋Œ์•„๊ฐ€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค^^

 

** ๊ทธ๋ž˜์„œ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ• ๋•Œ๋Š” ๊ธฐ์กด ํ”„๋กœ์ ํŠธ๋ฅผ ๋‹ซ์•„์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค!!