01.Flask ๊ธฐ์ดˆ - Vue & Falsk & Bootstrap

ID์งฑ์žฌยท2021๋…„ 4์›” 26์ผ
0

Flask

๋ชฉ๋ก ๋ณด๊ธฐ
6/8
post-thumbnail

๐ŸŒˆ

๐Ÿ”ฅ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ๊ณผ CDN

๐Ÿ”ฅ Vue์™€ CDN

๐Ÿ”ฅ REST API method ์ •์˜ํ•˜๊ธฐ


1. ๋ถ€ํŠธ์ŠคํŠธ๋žฉ๊ณผ CDN

  • CDN(Contents Delivery Network)์€ ์ง€๋ฆฌ์ , ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๊ฑฐ๋ฆฌ๊ฐ€ ์žˆ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ์„œ๋ฒ„์˜ ์ปจํ…์ธ ๋ฅผ ๋” ๋น ๋ฅด๊ฒŒ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ 
  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ฉ€๋ฆฌ์žˆ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ปจํ…์ธ ๋ฅผ ๋‹ค์šด ๋ฐ›์œผ๋ฉด ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์‚ฌ์šฉ์ž์™€ ๊ฐ€๊นŒ์šด ๊ณณ์— ์œ„์น˜ํ•œ Cashe Server์— ํ•ด๋‹น ์ปจํ…์ธ ๋ฅผ ์ œ๊ณตํ•ด ๋†“๊ณ , ์š”์ฒญํ•˜๋Š” ๊ฒฝ์šฐ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ Cache Server๊ฐ€ ์‘๋‹ตํ•จ
  • ๋ถ€ํŠธ์ŠคํŠธ๋žฉ๊ณผ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ํŒŒ์ผ์„ ๋‹ค์šด ๋ฐ›์•„ ์ž‘์—… ๊ฒฝ๋กœ์— ํŒŒ์ผ์„ ์œ„์น˜์‹œ์ผœ๋†“๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜, ์ด๋Ÿฐ CDN ๊ธฐ์ˆ ์„ ์ด์šฉํ•˜์—ฌ ๊ฒฝ๋กœ(css, javascript)๋ฅผ ์ง€์ •ํ•ด ๋†“๊ณ ๋„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Œ
  • ์ด๋ ‡๊ฒŒ ๋‹ค์šด๋กœ๋“œ ๋ฐฉ๋ฒ•๊ณผ, CDN๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ
  • ๋ถ€ํŠธ์ŠคํŠธ๋žฉ CDN ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• : https://getbootstrap.com/docs/5.0/getting-started/introduction/

2. Vue์™€ CDN

  • Vue๋ฅผ CDN ๋ฐฉ์‹์œผ๋กœ html์— ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ ์ฝ”๋“œ body ์•„๋žซ ๋ถ€๋ถ„์— ์ถ”๊ฐ€
  • ์ฒซ๋ฒˆ์งธ <script src>๊ฐ€ Vue๋ฅผ CDN ๋ฐฉ์‹์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ์ด๊ณ , ๋‘๋ฒˆ์งธ <script src>๋Š” axios ๋ฐฉ์‹์œผ๋กœ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€๋œ ๊ฒƒ

โœ๐Ÿป html์—์„œ Vue๋ฅผ CDN์œผ๋กœ ์—ฐ๊ฒฐ

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  • Vue๋ฅผ ํ†ตํ•ด html์„ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•œ ์˜์—ญ์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ œ์–ดํ•˜๊ณ  ์‹ถ์€ ์œ„์น˜๋ฅผ div๋กœ ๊ฐ์‹ธ๊ณ , id๊ฐ’์„ ์„ธํŒ…
  • ์ œ์–ด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์˜€์œผ๋ฉด, ๋ฒ”์œ„ ๋‚ด ํ•„์š”ํ•œ ๊ณณ์— event ๋„ฃ์–ด์คŒ
  • ๐Ÿ” v-on:click="axios_test" : ํด๋ฆญ event๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด "axios_text"๋ฅผ ์‹คํ–‰

โœ๐Ÿป html์š”์†Œ๋ฅผ Vue๋กœ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ• : <div id=""></div>

    <div id="test_vue">
      <center>
        <button type="button" class="btn btn-primary" v-on:click="axios_test">
          GET
        </button>
        <button type="button" class="btn btn-secondary">POST</button>
        <button type="button" class="btn btn-success">PUT</button>
        <button type="button" class="btn btn-danger">DELETE</button>
      </center>
    </div>
  • event ๋ฐœ์ƒ๋˜์—ˆ์„ ๋•Œ, ์‹คํ–‰๋  ๊ธฐ๋Šฅ์„ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ
  • el์€ Vue ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ id๊ฐ’ ์ž…๋ ฅ
  • mthods๋Š” ๊ตฌ์ฒด์ ์ธ ํ–‰์œ„์— ๋Œ€ํ•ด ํ•จ์ˆ˜๋กœ ์ง€์ •
  • Vue๋ฅผ CDN์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ๋•Œ, axios๋„ CDN์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— axiosํ•จ์ˆ˜๋กœ flask์™€ ์—ฐ๊ฒฐ

โœ๐Ÿป ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
      const app = new Vue({
        el: "#test_vue",
        methods: {
          axios_test() {
            axios("http://localhost:8082/test", {
              method: "get",
            })
              .then((response) => {
                console.log(response.data);
              })
              .catch((error) => {
                console.log(error);
              });
          },
        },
      });
    </script>
  • CROS(cross origin Resourc Sharing)๋ฅผ ์ด์šฉํ•˜์—ฌ ์›น์—์„œ ์‚ฌ์šฉํ•˜๋Š” HTTP request๋Š” ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Œ
  • ์„ค์น˜ ๋ฐฉ๋ฒ• : pip3 install flask_cors
  • <script></script> ์•ˆ์— ์ฝ”๋“œ์—์„œ ์‹คํ–‰๋˜๋Š” HTTP request๋Š” ๋‹ค๋ฅธ ์„œ๋ฒ„๋กœ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด CROS์ž„
  • ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์น˜ ํ›„, Flask ๊ฐ์ฒด๋ฅผ CROS()์˜ ํŒŒ๋งˆ๋ฆฌํ„ฐ๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋จ

โœ๐Ÿป python

from flask import Flask, request, make_response, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/test", methods=['GET'])
def test():
    return make_response(jsonify(success=True), 200)
if __name__ == '__main__':
    app.run(host="0.0.0.0", port="8082")

3. REST API method ์ •์˜ํ•˜๊ธฐ

  • flask API ์ •์˜ ์‹œ, route์— ์–ด๋–ค method๋ฅผ ์ง€์›ํ•ด์ค„ ์ง€ methods ์†์„ฑ์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด๋จ
  • GET/PUT/DELETE๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์ด ๊ฐ–์œผ๋‚˜, POST๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฆ„

โœ๐Ÿป 4๊ฐ€์ง€ ๋ฒ„ํŠผ ์ƒ์„ฑ ํ›„ Vue๋กœ click event ๊ฑธ์–ด์ฃผ๊ธฐ

  <body>
    <div id="app">
      <br />
      <center>
        <button type="button" class="btn btn-primary" v-on:click="test_get">
          GET
        </button>
        <button type="button" class="btn btn-secondary" v-on:click="test_post">
          POST
        </button>
        <button type="button" class="btn btn-success" v-on:click="test_put">
          PUT
        </button>
        <button type="button" class="btn btn-danger" v-on:click="test_delete">
          DELETE
        </button>
      </center>
    </div>
  </body>

โœ๐Ÿป ๊ฐ ๋ฒ„ํŠผ๋งˆ๋‹ค click ๋ฐœ์ƒ ์‹œ, ์ž‘๋™ํ•˜๋Š” ๊ฐ์ฒด ์ƒ์„ฑ

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        methods: {
          test_get: () => {
            axios("http://localhost:8082/test", {
              method: "get",
              params: {
                email: "test@test.com",
              },
            })
              .then((response) => {
                console.log(response.data["status"]);
              })
              .catch((error) => {
                console.log(error);
              });
          },
          test_post: () => {
            axios("http://localhost:8082/test", {
              method: "post",
              data: {
                email: "test@test.com",
              },
            })
              .then((response) => {
                console.log(response.data["status"]);
              })
              .catch((error) => {
                console.log(error);
              });
          },
          test_put: () => {
            axios("http://localhost:8082/test", {
              method: "put",
              params: {
                email: "test@test.com",
              },
            })
              .then((response) => {
                console.log(response.data["status"]);
              })
              .catch((error) => {
                console.log(error);
              });
          },
          test_delete: () => {
            axios("http://localhost:8082/test", {
              method: "delete",
              params: {
                email: "test@test.com",
              },
            })
              .then((response) => {
                console.log(response.data["status"]);
              })
              .catch((error) => {
                console.log(error);
              });
          },
        },
      });
    </script>
  • @app.route("/test", methods=['GET', 'POST', 'PUT', 'DELETE']) : route ๊ฒฝ๋กœ(/test์˜)์— ์ง€์›ํ•˜๋Š” methods๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Œ(methods๋ฅผ ๋ณ„๋„๋กœ ์ ์–ด์ฃผ์ง€ ์•Š์œผ๋ฉด get๋ฅผ ์ง€์›)
  • get ๋ฐฉ์‹ : request.args.get('key ์ด๋ฆ„') โ‡ข key์— ํ•ด๋‹นํ•˜๋Š” value๊ฐ’ ํ˜ธ์ถœ
  • post ๋ฐฉ์‹ : request.get_json() โ‡ข {'key':'velue'}๋กœ ๊ฐ€์ ธ์˜ด

โœ๐Ÿป python

from flask import Flask, request, make_response, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # CORS์— app ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์คŒ
# method ๋ฐฉ์‹๋ณ„ ์ถ”์ถœ ๋ฐฉ๋ฒ•(post๋งŒ ๋‹ค๋ฆ„)
@app.route("/test", methods=['GET', 'POST', 'PUT', 'DELETE'])
def test():
    if request.method == 'POST':
        print('POST')
        data = request.get_json()
        print(data)
        print(data['email'])
    if request.method == 'GET':
        print('GET')
        user = request.args.get('email')
        print(user)
    if request.method == 'PUT':
        print('PUT')
        user = request.args.get('email')
        print(user)
    if request.method == 'DELETE':
        print('DELETE')
        user = request.args.get('email')
        print(user)
    return make_response(jsonify({'status': True}), 200)
if __name__ == '__main__':
    app.run(host="0.0.0.0", port="8082")

profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€