![]() |
ノート/flask実験2https://pepper.is.sci.toho-u.ac.jp:443/pepper/index.php?%A5%CE%A1%BC%A5%C8%2Fflask%BC%C2%B8%B3%A3%B2 |
![]() |
python - debug Flask server inside Jupyter Notebook - Stack Overflowによると、
if __name__ == "__main__": ### app.run(debug=True, host='0.0.0.0', port=5000, threaded=True) # <--これを from werkzeug.serving import run_simple #run_simple('localhost', 9000, app) # <-- こうしろと書いてあるのだが、 run_simple('0.0.0.0', 5000, app, threaded=True) # <-- こうした方がよさそうだ
ということ。このwerkzeugパッケージのマニュアルは Werkzeug — Werkzeug Documentation (0.15.x) にある。
この中の、Tutorial に、同じようなサンプルがある。
また、werkzeug.serving.run_simpleの引数については、 Serving WSGI Applications — Werkzeug Documentation (0.15.x) に書いてある。
pandasのDataFrameを、ホームページHTMLに表示する(変換する)には、 df.to_html() が便利に使える。flaskの場合、
df = pd.DataFrame([[1,3,5],[2,4,6],[3,5,9]], index=['A', 'B', 'C'], columns=['P', 'Q', 'R']) table = df.to_html() return render_template("listfiles.html", table=table)
としておいて、listfiles.html側で
{% extends "layout.html" %} {% block body %} {% autoescape False %} {{ table }} {% endautoescape %} {% endblock %}
とすればよい。
表の内容にリンクを含ませたいときには、
df = pd.DataFrame([['<a href="http:uploads/box-chart.png">1</a>',3,5],[2,4,6],[3,5,9]], index=['A', 'B', 'C'], columns=['P', 'Q', 'R']) table = df.to_html(escape=False) # ここのescape=Falseが肝心 return render_template("listfiles.html", table=table)
とする。to_html()としてしまうと、画面上にそのまま<a href=...>と表示される。見ると、 <などがエスケープされて
<td><a ref=http:uploads/box-chart.png>1</a></td>
となっている。
なお、ここで書いたURLはこのままでは不可で、flaskの中でrouteで設定しておかなければならない。