Pythonスクリプトをページ遷移なしでhtmlから呼び出し、戻り値を受け取る方法はありますでしょうか?
Bottleを使って、Webページ上で入力した文字列をpythonスクリプトに渡し、処理結果を返すという内容のWebアプリケーションを作っています。
現在は下記のようにwebページからpostメソッドでスクリプトを呼び出し、遷移先に処理結果を出力するところまでできています。
- main.py
python
1from bottle import route, run, template, request, static_file, url, get, post, response, error 2import sys, codecs, bottle 3def processer(text): 4 res=処理結果 5 return res 6 7@bottle.route("/") 8@bottle.auth_basic(check) 9def hello(): 10 return template("input") 11 12@route('/result', method='POST') 13def go(): 14 value=request.forms.textvalue 15 res=processer(value) 16 return template('result',outputvalue=res) 17 18if __name__ == '__main__': 19 run(host="0.0.0.0", port=8000, debug=True, reloader=True) 20
- input.tpl
html
1<form method="post" action="/result" name="text"> 2 <button type="submit">処理開始</button> 3 <textarea id="box_left" class="diff_value_box1" name="textvalue">処理対象文字列</textarea> 4</form>
- result.tpl
html
1<html> 2<body> 3<p>処理結果:{{outputvalue}}</p> 4</body>
これを、ボタンクリック時にページ遷移させず、input.tplを表示したままmain.pyのprocesserを実行し、下記のように同一ページ内に結果表示させるようにするのは可能でしょうか?
- in-out.tpl
html
1<form method="post" action="/in-out.tpl" name="text"> 2 <button type="submit">処理開始</button> 3 <textarea id="box_input" name="textvalue">処理対象文字列</textarea> 4</form> 5<p id="box_output" >処理結果:処理結果</p>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/12 12:22
2017/04/12 16:53 編集