[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[ノート/flask実験]]
***Jupyter Notebookの中でflaskを起動する [#k2badf1c]
[[python - debug Flask server inside Jupyter Notebook - Stack Overflow:https://stackoverflow.com/questions/41831929/debug-flask-server-inside-jupyter-notebook]]によると、
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):https://werkzeug.palletsprojects.com/en/0.15.x/]] にある。
この中の、[[Tutorial:https://werkzeug.palletsprojects.com/en/0.15.x/tutorial/#step-2-the-base-structure]] に、同じようなサンプルがある。
また、werkzeug.serving.run_simpleの引数については、
[[Serving WSGI Applications — Werkzeug Documentation (0.15.x):https://werkzeug.palletsprojects.com/en/0.15.x/serving/]] に書いてある。
***表のHTMLレンダリング(dataframe.to_html()) にリンクを入れたい [#m4fd35d5]
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で設定しておかなければならない。
終了行:
[[ノート/flask実験]]
***Jupyter Notebookの中でflaskを起動する [#k2badf1c]
[[python - debug Flask server inside Jupyter Notebook - Stack Overflow:https://stackoverflow.com/questions/41831929/debug-flask-server-inside-jupyter-notebook]]によると、
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):https://werkzeug.palletsprojects.com/en/0.15.x/]] にある。
この中の、[[Tutorial:https://werkzeug.palletsprojects.com/en/0.15.x/tutorial/#step-2-the-base-structure]] に、同じようなサンプルがある。
また、werkzeug.serving.run_simpleの引数については、
[[Serving WSGI Applications — Werkzeug Documentation (0.15.x):https://werkzeug.palletsprojects.com/en/0.15.x/serving/]] に書いてある。
***表のHTMLレンダリング(dataframe.to_html()) にリンクを入れたい [#m4fd35d5]
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で設定しておかなければならない。
ページ名: