5. 파이썬

[플라스크] 계산기 템플릿

패스트코드블로그 2020. 5. 9. 16:28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<head><title>머신러닝</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container"><h2>머신러닝</h2>
    <p>지도학습(supervised), 자율학습(Un-supervised), 강화학습(reinforcement)</p>
    <table class="table">
        <thead>
        <tr class="danger">
            <th>No.</th>
            <th>예 제</th>
            <th>프로세스</th>
            <th>주요내용</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>1.</td>
            <td><a href="/move/ui_calc">UI 계산기</a> | <a href="/move/ai_calc">AI 계산기</a></td>
            <td>Modeling</td>
            <td>모델 생성, 저장, 재활용</td>
        </tr>
        
        </tbody>
    </table>
</div>
</body>
</html>
cs

 

 

calculator.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>계산기</title>
</head>
<body>
<form action="/ai_calc" method="post">
    첫번째 수: <br>
    <input type="text" name="num1"> <br>
    두번째 수: <br>
    <input type="text" name="num2"> <br>
    <select name="opcode" id="">
        <option value="add">+</option>
        <option value="sub">-</option>
        <option value="mul">*</option>
        <option value="div">/</option>
    </select>
    <br>
    <input type="submit" value="SUBMIT">
</form>
{% if result %}
<p>결과: {{result}}</p>
{% endif %}
 
<a href="/move/index">뒤로가기</a>
</body>
</html>
cs

 

app.py

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask
from flask import render_template, request, jsonify
import re
 
app = Flask(__name__)
 
@app.route("/move/<path>")
def move(path):
    return render_template('{}.html'.format(path))
 
@app.route('/calculate')
def calculate():
    stmt = request.args.get('stmt','NONE')
    if(stmt == 'NONE'):
        print('넘어온 값이 없음')
    else:
        print('넘어온 식: {}'.format(stmt)) # 5 + 8
        patt = '[0-9]+'
        op = re.sub(patt,'',stmt)
        print('넘어온 연산자: {}'.format(op))
        nums = stmt.split(op)
        result = 0
        n1 = int(nums[0])
        n2 = int(nums[1])
        if op == '+': result = n1 + n2
        elif op == '-': result = n1 - n2
        elif op == '*': result = n1 * n2
        elif op == '/': result = n1 / n2
 
    return jsonify(result = result)
 
@app.route("/calculate_ai", methods=["POST"])
def calculate_using_ai():
    num1 = request.form['num1']
    num2 = request.form['num2']
    opcode = request.form['opcode']
    c = Calculator(num1, num2, opcode)
    result = c.calc()
    render_params = {}
    render_params['result'= int(result)
    return render_template('calculator.html'**render_params)
 
 
 
 
@app.route("/")
def index():
    return render_template('index.html')
 
if __name__ == '__main__':
    app.debug = True
    app.run()
cs