==Flask==
[[Python][Angular]]
{{amazon|B00XZTYMG6}}
*http://flask.pocoo.org/
*http://flask.pocoo.org/docs/0.12/installation/
*Windows , python3
> > pythoh -m venv flask > > cd flask\Scripts > > activate (flask)> > pip install Flask
===Visual Studio Code===
[[File:0511_flask_vscode_settings.jpg]]
**https://github.com/pppiroto/flask_sample.git
=====ローカルリポジトリを初期化=====
> > git init
=====ローカルリポジトリにコミット=====
> > git add . > > git commit -m "flask lesson init" > > git branch
* master
=====リモートリポジトリの設定=====
> > git remote add origin https://github.com/pppiroto/flask_sample.git
=====リモートリポジトリにpush=====
> > git push origin master
==クイックスタート==
===サーバー起動===
*Visual Studio Code、Powershell
(flask) PS C:\workspaces\vscode\flask_sample> > set-item env:FLASK_APP hello.py (flask) PS C:\workspaces\vscode\flask_sample> > python -m flask run
* Serving Flask app "hello"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[[File:0506_flask_run_server.jpg]]
====デバッグモード====
PS> > set-item env:FLASK_DEBUG 1
===ルーティング===
from flask import Flask
===変数===
@app.route('/user/<<username>>/<<int:age>>')
def show_user_profile(username, age):
return 'User {0} {1}'.format(username,age)
=====<<コンバーター:変数>>=====
{|class="wikitable"
!コンバーター
return 'Hello world!'
@app.route('/user/<<username>>/<<int:age>>')
def show_user_profile(username, age):
return 'User {0} {1}'.format(username,age)
def print_urls():
return '''
<<ol>> <<li>>index() = {0} <<li>>hello_world() = {1} <<li>>show_user_profile('piroto',46) = {2} <</ol>>
'''.format(
url_for('index'),
=====テンプレート利用=====
from flask import Flask, render_template
@app.route('/render_sample/<<param>>')
def render_sample(param=None):
return render_template('render_sample.html',param=param)
=====テンプレート=====
<<!doctype html>> <<title>>Rendering Sample<</title>>
{% if param %}
<<h1>>Parameter : {{ param }}!<</h1>>
{% else %}
<<h1>>No parameter found.<</h1>>
{% endif %}
*安全と確信できる場合、Markupクラスもしくは、 |safeフィルターを使用する
=====テンプレート=====
<<!doctype html>> <<title>>Safe Rendering Sample<</title>> <<p>>{{ param1 }}<</p>> <<p>>{{ param2 }}<</p>> <<p>>{{ param3 | safe}}<</p>>
==========
from flask import Markup
def render_safe_var(param=None):
return render_template('render_safe_var.html',
param1="<<h2>>Sub title<</h2>>", param2=Markup("<<h2>>Mark up Sub title<</h2>>"), param3="<<h2>>Safe filtered Sub title<</h2>>")
{{ref_image flask_safe_var.jpg.jpg}}
===リクエストデータへのアクセス===