iT邦幫忙

0

工程師菜鳥_宇弟的 學習筆記 [Python] 04 - [Python] [Flask] 的應用

jton 2022-10-04 18:25:241253 瀏覽
  • 分享至 

  • xImage
  •  

[Python] 之旅第4天

  • 實際應用例題一
  • 實際應用例題二

練習一:
[python] [flask] 能取值並改 variable 變數 (ex:string to float)

# import main Flask class and request object
from flask import Flask, request
 
# create the Flask app
app = Flask(__name__)
 
 
@app.route('/data')
def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    # if key doesn't exist, returns a 400, bad request error
    CustomerID = request.args['CustomerID']
 
    # if key doesn't exist, returns None
    website = request.args.get('website')
 
    # take input from string to other variable變數
 
    CustomerID_float = float(CustomerID)
    print (CustomerID_float)
 
 
    return '''
              <h1>The name is: {}</h1>
              <h1>The CustomerID is: {}</h1>
              <h1>The CustomerID float is: {}</h1>  
              <h1>The website value is: {}'''.format(name, CustomerID, CustomerID_float, website)
             
 
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

所以如圖示: 原本從URL 取的值直接print 出來的話,只能為string, CustomerID_float = float(CustomerID) 把他改成 float值
https://ithelp.ithome.com.tw/upload/images/20221004/20153034vXtQ0KCakU.png

練習二:
[python] [flask] 能取值並改 variable 變數,能從URL取值,並做運算
假設我們的URL input 是:

http://127.0.0.1:5000/data?num1=4&num2=6

程式碼:

# import main Flask class and request object
from flask import Flask, request
 
# create the Flask app
app = Flask(__name__)
 
 
@app.route('/data')
def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    # if key doesn't exist, returns a 400, bad request error
    num1 = request.args['num1']
    num2 = request.args['num2']
 
   
 
    # take input from string to other variable變數
 
    num1_float = float(num1)
    num2_float = float(num2)
 
    sum = num1_float*num2_float
 
 
    return '''
              <h1>The name is: {}</h1>
              <h1>The num1 float is: {}</h1>
              <h1>The num2 float is: {}</h1>  
              <h1>The sum float is: {}</h1>
              '''.format(name, num1_float,num2_float , sum)
             
 
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)
 

https://ithelp.ithome.com.tw/upload/images/20221004/20153034BXzI6pgT5O.png


其他貼文~~
(變強,就從小小的累積開始)

[Python] 之旅第1天 - python環境
https://ithelp.ithome.com.tw/articles/10296280

[Python] 之旅第2天-用 [Python] 跟 [Flask] 為基礎,把 URL input 抓出來
https://ithelp.ithome.com.tw/articles/10296290

[Python] 之旅第3天- 用 [Python] 跟 [Flask]
https://ithelp.ithome.com.tw/articles/10296965

[Python] 之旅第4天- [Python] [Flask] 的應用
https://ithelp.ithome.com.tw/articles/10303948

[Python] 之旅第5天- [Python] [Flask] 的應用, 日期計算 及日期報錯篇
https://ithelp.ithome.com.tw/articles/10309202

[Python] 之旅第6天- [Python] [Flask] 的應用, 比較有小數點圓的面積大小,簡易請假系統製作
https://ithelp.ithome.com.tw/articles/10309352

[Python] 之旅第7天- [Python] [regular expression] 的應用,找特定資料,或限制輸入的格式
https://ithelp.ithome.com.tw/articles/10309371

[Python] 之旅第8天 - [Python] [regular expression] 的應用, 甚麼是 Json
https://ithelp.ithome.com.tw/articles/10309422

[Python] 之旅第9天 - [Python] [Json] 進階說明
https://ithelp.ithome.com.tw/articles/10309620


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言