Python lotto, calculator Program [1]

bi_sz·2022년 3월 17일
0

Python

목록 보기
7/15
post-thumbnail

지난게시글에서 Jupyter Notebook 에서 실행했던 lotto_Calculator.py 프로그램을 Anaconda 가상환경에서 VSCode 인터프리터를 사용하여 실행을 해 보도록 하겠습니다.

lotto_Calculator 폴더를 만들어 준 후 기본 틀을 잡아 두었습니다.

static 폴더와, templates 폴더를 만들어 주었고,
templates 폴더 안에는 render_template 을 사용할 lotto.html 파일을 넣어주었습니다.

기존 코드를 동일하게 사용하였습니다.

main.py

from flask import Flask, render_template
from flask import request
import random
app = Flask(__name__)
@app.route('/')
def index():
    return 'bi Hi~~!!!!!'  
@app.route('/lotto') /nickname
def lotto():
    lotto_list = list(range(1,46))
    lotto = sorted(random.sample(lotto_list, 6))
    return render_template("lotto.html", variable=lotto)
@app.route('/calc')
def calc():
    inputdata = request.args.get("inputdata", "")
    if inputdata:
        result = calc(inputdata)
    else:
        result = ""
    return (
        """<form action="" method="get">
                Calc : <input type="text" name="inputdata">
                <input type="submit" value="calc">
            </form>"""
        + "result:" + result
    )
def calc(inputdata):
    return str(eval(inputdata))  
if __name__ == "__main__":
    app.run(port=5555,debug=True)

lotto.html

<head>
    <style>
        /* 645 ball */
        .ball_645 {display:inline-block; border-radius:100%; text-align:center; vertical-align:middle; color:#fff; font-weight:500;}
        .ball_645.lrg {width:60px; height:60px; line-height:56px; font-size:28px}
        .ball_645.sml {width:24px; height:24px; line-height:22px; font-size:13px}
        .ball_645.not {color:#777}
        .ball_645.sml.not {font-weight:300}
        .ball_645.ball1 {background:#fbc400; text-shadow: 0px 0px 3px rgba(73, 57, 0, .8)}
        .ball_645.ball2 {background:#69c8f2; text-shadow: 0px 0px 3px rgba(0, 49, 70, .8)}
        .ball_645.ball3 {background:#ff7272; text-shadow: 0px 0px 3px rgba(64, 0, 0, .8)}
        .ball_645.ball4 {background:#aaa; text-shadow: 0px 0px 3px rgba(61, 61, 61, .8)}
        .ball_645.ball5 {background:#b0d840; text-shadow: 0px 0px 3px rgba(41, 56, 0, .8)}
        table tr td .ball_645.sml {margin:0 3px}
    </style>
</head>
<p>
    <span class="ball_645 lrg ball1">{{ variable[0] }}</span>
    <span class="ball_645 lrg ball2">{{ variable[1] }}</span>
    <span class="ball_645 lrg ball3">{{ variable[2] }}</span>
    <span class="ball_645 lrg ball4">{{ variable[3] }}</span>
    <span class="ball_645 lrg ball5">{{ variable[4] }}</span>
    <span class="ball_645 lrg ball5">{{ variable[5] }}</span>
</p>

> python main.py : main.py를 실행시켜 줍니다.

bi Hi ~~!!!! 가 잘 출력 됩니다.

http://localhost:{port}/lotto 경로로 접근하여 lotto 랜덤 번호가 출력되는 모습.

http://localhost:{port}/calc 경로로 접근하여 계산기가 출력되는 모습.

잘 작동 하네요.


conda 가상환경에서 flask로 lotto, calculator 를 구현하는 예제가 완성이 되었습니다.

0개의 댓글