Hyperledger

본문 바로가기
사이트 내 전체검색


Hyperledger
Hyperledger

4. Flask 웹어플리케이션 기초

페이지 정보

작성자 관리자 댓글 1건 조회 4,333회 작성일 19-08-26 13:06

본문

4. Flask 웹어플리케이션 기초

실습소스를 다운로드 한다.

 

root@client:~# wget https://www.leelab.co.kr/down/fabric-samples/python-src.tar.gz 

관련 패키지를 설치한다.

 

root@client:~# mkdir pyApp
root@client:~# cd pyApp/
root@client:~/pyApp#
root@client:~/pyApp# pip install Flask

root@client:~/pyApp# pip install requests
 

기본 웹어플리케이션 ​만들기

웹어플리케이션 소스를 작성한다.

 

root@client:~/pyApp# vi myflask.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return '<h1>Hello world!</h1>'
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000,debug=True)
 

​웹어플리케이션을 실행한다.

root@client:~/pyApp# python myflask.py
 * Serving Flask app "myflask" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 301-791-187 

 

웹브라우저로 접속한다.

http://102.168.0.105:8000

 

 

1.PNG

템플릿 사용하기

myhello 폴더를 생성한다.

​templates 폴더를 생성한다.

root@client:~/pyApp# mkdir -p myhello/templates
root@client:~/pyApp# cd myhello/
root@client:~/pyApp/myhello# vi app.py

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
    return render_template('hello.html' )
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000,debug=True)

root@client:~/pyApp/myhello# ls
app.py  templates
root@client:~/pyApp/myhello# cd templates/
root@client:~/pyApp/myhello/templates# vi hello.html
<h1>Hello World!</h1>

root@client:~/pyApp/myhello/templates# ls
hello.html
root@client:~/pyApp/myhello/templates# cd ..

웹어플리케이션을 실행한다.

root@client:~/pyApp/myhello# python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 301-791-187

웹브라우저로 접속한다.

1.PNG

같은 예제를 템플릿으로 바꾼예제 입니다.

템플릿 파일에 변수값 전달하기

​웹어플리케이션 폴더를 생성한다.

root@client:~/pyApp# mkdir -p mydht/templates
root@client:~/pyApp# cd mydht/

소스를 작성한다.

root@client:~/pyApp/mydht# vi app.py
from flask import Flask, render_template
import requests
import json
app = Flask(__name__)
@app.route('/')
def hello():
    rows = dict()
    url = "https://api.thingspeak.com/channels/8888888/feeds.json?api_key=xxxxx"
    try :
        r = requests.get(url)
        if(r.status_code == 200) :
            docs = json.loads(r.content)
            rows = docs["feeds"]
            total = len(rows)
    except :
        pass
    return render_template('list.html' , rows=rows , total=total)
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000,debug=True)

root@client:~/pyApp/mydht# cd templates/
root@client:~/pyApp/mydht/templates# vi list.html
​<center><h1>my dht lists</h1></center>
<table align="center" border="1" cellpadding="0" cellspacing="0">
<tr height=30 align=center>
 <td width=70>번호</td>
 <td width=200>생성일</td>
 <td width=100>온도</td>
 <td width=100>습도</td>
</tr>
{% for i in range(total) %}
<tr height=30 align=center>
 <td >{{rows[i].entry_id}}</td>
 <td >{{rows[i].created_at}}</td>
  <td >{{rows[i].field1}}</td>
  <td >{{rows[i].field1}}</td>
</tr>
{% endfor %}
</table>

root@client:~/pyApp/mydht/templates# cd ..
root@client:~/pyApp/mydht# ls
app.py  templates

웹어플리케이션을 실행한다.


root@client:~/pyApp/mydht# python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 301-791-187

3.PNG

첨부파일

댓글목록


개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.