前提・実現したいこと
HTMLとpythonを改修したい
HTMLとPythonの改修について質問です。
pythonで作成したif文の結果をHTMLに反映させる方法がわかりません。
具体的には、bmiが~18.5までを瘦せがた18.5~25が普通、それ以上が肥満十いった具合に表示させたいと思っています。
発生している問題・エラーメッセージ
肥満度のところに <function bmi at 0x05A57E30> と表示が出ました。
該当のソースコード
//HTMLのソースコード </head> <body> <nav class="navbar navbar-default navbar-fixed-top"> % include('navbar.html', active='bmi') </nav> <div class="container" style="margin-top:60px;padding:20px 0"> <h1>最新の健康状態</h1> <table class="table"> <tr><th>測定日</th><td>{{latest_date}}</td></tr> <tr><th>体重</th><td>{{weight}}</td></tr> <tr><th>身長</th><td>{{height}} </td></tr> <tr><th>BMI</th><td>{{bmi}}</td></tr> <tr><th>肥満度</th><td>やせ型、普通、太り気味</td></tr> //pythonのソースコード @route('/health-blood','POST') def health_blood_post(): ''' テンプレートのhtmlにグラフ描画のデータをAPIから取得し、 javascriptでグラフ画像を作成するやり方 ''' app_session = request.environ.get('beaker.session') params=read_settings(app_session['account_id']) request_params_key=['datetime','high_pressure','low_pressure','pulse'] # リクエストパラメータの値を取得 for key in request_params_key: params[key]=unicode(request.forms.get(key,''),'utf-8') conn = sqlite3.connect(FILENAME) db=conn.cursor() db.execute('''INSERT INTO blood_data (account_id ,datetime ,high_pressure ,low_pressure ,pulse ) VALUES(?,?,?,?,?)''', (params['account_id'], params['datetime'].replace('/','-') ,params['high_pressure'],params['low_pressure'],params['pulse'],)) conn.commit() db.close() return redirect('/health-blood') @route('/health-bmi', 'GET') def health_bmi(): ''' テンプレートのhtmlにグラフ描画のデータを埋め込み、 javascriptでグラフ画像を作成するやり方 ''' app_session = request.environ.get('beaker.session') params=read_settings(app_session['account_id']) db = sqlite3.connect(FILENAME ).cursor() rows=db.execute( '''SELECT * from bmi_data WHERE account_id=? order by DATE(date)''', (params['account_id'],) ).fetchall() db.close() records='' weight_hist='' params['bmi']='' params['height']='' params['weight']='' params['latest_date']='' for row in rows: latest_bmi=str(bmi(float(row[2]),float(row[3]))) date= sqlite2google(row[1]) records=records+"[new Date("+date+"),25,"+latest_bmi+",22,18.5,],\n\t" weight_hist=weight_hist+"[new Date("+date+"),"+str(row[3])+",],\n\t" params['bmi']=latest_bmi params['height']=str(row[2]) params['weight']=str(row[3]) params['latest_date']=sqlite2fmt(row[1],'/') if len(records)>0: params["records"]=records if len(weight_hist)>0: params["weight_hist"]=weight_hist return template('./views/health-bmi.html', params)
試したこと
(1) HTMLの
<tr><th>肥満度</th><td>やせ型、普通、太り気味</td></tr>
↓
<tr><th>肥満度</th><td>{{result}}</td></tr>
に変更した。
(2)以下のif文を
params['latest_date']=sqlite2fmt(row[1],'/')
の下の行に加えてみた。
result= bmi
if result< 17:
print 'やせ型'
elif 17<= result <18.5:
print '痩せ気味'
elif 18.5<= result < 25:
print '普通'
elif 25<= result < 30:
print '太り気味'
else:
print '肥満'
params['result']=str(result)
(3)サーバーを起動し確認
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。