[ Flask ] 16 April, 2017 17:28

Flask core functionality includes WSGI(Suggest to use bjoern)
Routing(URL rules) based on Werkzeug
Template engine based on Jinja2
support cookie、sessions、web helpers like JSON, static files etc
and other extensions.

pip install <Flask-Ext>

import Flask extensions(Flask 0.8 and after):
from flask.ext import foo

Some ext introducing:

Flask-Admin: admin interface framework
Flask-Assets: to compress and merge CSS and Javascript files
Flask-Blogging: Markdown blog support
Flask-Bcrypt: bcrypt hashing(password) utilities
Flask-Cache: cache support
Flask-Gravatar: integration gravatar
Flask-Login: User session management
Flask-Locale: i18n/l10n support(CSV or db)
Flask-Mail: sending email
Flask-PageDown: StackOverflow's "PageDown" markdown editor for Flask-WTF
Flask-Principal: Identity management
Flask-Security: security features
Flask-SQLAlchemy: SQLAlchemy support
Flask-Themes: infrastructure for theming Flask ap
Flask-Uploads: upload handling
Flask-User: Customizable User Account Management
Flask-WTF: Simple integration of Flask and WTForms

Flask-API: Browsable web APIs
Flask-Babel: javascript converter
Flask-Script: Scripting support
Flask-RESTful: framework for creating REST APIs
Flask-Sijax: Sijax interface - Python/jQuery library for AJAX

==

Old than Flask 0.8 need activated compatibility module first:

download:
https://github.com/mitsuhiko/flask/raw/master/scripts/flaskext_compat.py

import flaskext_compat
flaskext_compat.activate()
from flask.ext import foo

[ Flask ] 16 April, 2017 03:23

an HTML form with its enctype attribute set to 'multipart/form-data'
request.files[]: fetches file and saves it to the desired location
secure_filename()

app.config[‘UPLOAD_FOLDER’]: Defines path for upload folder
app.config[‘MAX_CONTENT_PATH’]: Specifies maximum size of file yo be uploaded – in bytes

example:

mkdir uploadfd

vi templates/upload.html

<html>
   <body>

      <form action = "/uploadfd" method = "POST"
         enctype = "multipart/form-data">
         <input type = "file" name = "file" />
         <input type = "submit value='upload""/>
      </form>

   </body>
</html>
vi upload.py
from flask import Flask, render_template, request
from werkzeug import secure_filename
ap = Flask(__name__)

@ap.route('/upload')
def upload():
   return render_template('upload.html')

@ap.route('/uploadfd', methods = ['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      f = request.files['file']
      f.save('uploadfs/' + secure_filename(f.filename))
      return 'file uploaded successfully'

if __name__ == '__main__':
   ap.run(host='0.0.0.0')

==

http://code.runnable.com/UiPcaBXaxGNYAAAL/

[ Flask ] 16 April, 2017 00:17
flash(message, category)

message: The actual message to be flashed.
category: ‘error’, ‘info’ or ‘warning’, optional.

get_flashed_messages(with_categories, category_filter)

with_categories: a tuple if received messages are having category.
category_filter: to display only specific messages.

To remove message from session, Both parameters, are optional.

Flashes received messages in a template:
{% with messages = get_flashed_messages() %}
   {% if messages %}
      {% for message in messages %}
         {{ message }}
      {% endfor %}
   {% endif %}
{% endwith %}

example:

vi templates/flash.html

<!doctype html>
<html>

   <head>
      <title>Flask Message flashing</title>
   </head>
   <body>

      {% with messages = get_flashed_messages() %}
         {% if messages %}
            <ul>
               {% for message in messages %}
               <li>{{ message }}</li>
               {% endfor %}
            </ul>
         {% endif %}
      {% endwith %}

      <h1>Flask Message Flashing Example</h1>
      <p>Do you want to <a href = "{{ url_for('login') }}">
         <b>log in?</b></a></p>

   </body>
</html>
vi templates/flash_login.html
<!doctype html>
<html>
   <body>

      <h1>Login</h1>

      {% if error %}
      <p><strong>Error:</strong> {{ error }}
      {% endif %}

      <form action = "/flash_login" method = "post">
         <dl>
            <dt>Username:</dt>

            <dd>
               <input type = "text" name = "username"
                  value = "{{request.form.username }}">
            </dd>

            <dt>Password:</dt>
            <dd><input type = "password" name = "password"></dd>
         </dl>
         <p><input type = "submit" value = "Login"></p>
      </form>

   </body>
</html>
vi flash.py
from flask import Flask, flash, redirect, render_template, request, url_for
ap = Flask(__name__)
ap.secret_key = '<random string>'

@ap.route('/flash')
def flashing():
   return render_template('flash.html')

@ap.route('/flash_login', methods = ['GET', 'POST'])
def login():
   error = None

   if request.method == 'POST':
      if (request.form['username'] != 'admin' or
          request.form['password'] != 'admin'
         ):
         error = 'Invalid username or password. Please try again!'
      else:
         flash('Hi, Admin!')
         return redirect(url_for('flashing'))

   return render_template('flash_login.html', error = error)

if __name__ == "__main__":
   ap.run(host='0.0.0.0')
[ Flask ] 15 April, 2017 19:45
1.
Flask.redirect(location, statuscode, response)

location: URL be redirected
statuscode: defaults to 302
response: used to instantiate response

standardized status code−
    HTTP_300_MULTIPLE_CHOICES
    HTTP_301_MOVED_PERMANENTLY
    HTTP_302_FOUND
    HTTP_303_SEE_OTHER
    HTTP_304_NOT_MODIFIED
    HTTP_305_USE_PROXY
    HTTP_306_RESERVED
    HTTP_307_TEMPORARY_REDIRECT

example:

vi templates/login.html
<html>
   <body>

      <form action = "/check" method = "POST">

         <p>ID <input type = "text" name = "Name"/></p>
         <p><input type = "submit" value = "Login"/></p>

      </form>

   </body>
</html>
vi login.py
from flask import Flask, redirect, url_for, render_template, request

# Initialize the Flask application
ap = Flask(__name__)

@ap.route('/login')
def login():
   return render_template('login.html')

@ap.route('/check', methods = ['POST', 'GET'])
def check():

   if request.method == 'POST' and request.form['Name'] == 'admin':
      return redirect(url_for('success'))

   return redirect(url_for('login'))

@ap.route('/success')
def success():
   return 'logged in successfully'

if __name__ == '__main__':
   ap.run(host='0.0.0.0')
python login.py

2.
abort(code)

Code parameter values-
    400 − for Bad Request
    401 − for Unauthenticated
    403 − for Forbidden
    404 − for Not Found
    406 − for Not Acceptable
    415 − for Unsupported Media Type
    429 − Too Many Requests

example:

#make some modify in login.py
vi login.py
from flask import Flask, redirect, url_for, render_template, request, abort

@ap.route('/check', methods = ['POST', 'GET'])
def check():

   if request.method == 'POST':
      if request.form['Name'] == 'admin' :
         return redirect(url_for('success'))
      else:
         abort(401)

   return redirect(url_for('login'))
[ Flask ] 14 April, 2017 23:53

Session - a dictionary object containing session variables and associated values.

session.pop()
secret_key

vi session.py

from flask import Flask, session, redirect, url_for, escape, request
ap = Flask(__name__)

@ap.route('/sessions')
def sessions():
   if 'username' in session:
      username = session['username']
      return 'Logged in as ' + username + '<br>' + \
         "<b><a href = '/logout'>click here to log out</a></b>"
   return "You are not logged in <br><a href = '/login'></b>" + \
      "click here to log in</b></a>"

@ap.route('/login', methods = ['GET', 'POST'])
def login():
   if request.method == 'POST':
      session['username'] = request.form['username']
      return redirect(url_for('sessions'))
   return \[code\]'

   <form action = "" method = "POST">
      <p><input type = "text" name = "username"/></p>
      <p<<input type = "submit" value = "Login"/></p>
   </form>
   
   \[/code\]'
     
@ap.route('/logout')
def logout():
   # remove the username from the session if it is there
   session.pop('username', None)
   return redirect(url_for('sessions'))

ap.secret_key = '<any random string>'

if __name__ == '__main__':
   ap.run(host= '0.0.0.0')
==

https://www.jjude.com/flask-errors/

 

[ Flask ] 13 April, 2017 03:04

Cookies − dictionary object holding Cookie names and values
def setcookie()
make_response()
get() of request.cookies

example:

vi templates/cookie.html

<html>
   <body>

      <form action = "/setcookie" method = "POST">
         <p><h3>Enter userID</h3></p>
         <p><input type = 'text' name = 'Name'/></p>
         <p><input type = 'submit' value = 'Login'/></p>
      </form>

   </body>
</html>
vi cookie.py
from flask import Flask, render_template, request, make_response
ap = Flask(__name__)

@ap.route('/cookie')
def cookie():
   return render_template('cookie.html')

@ap.route('/setcookie', methods = ['POST', 'GET'])
def setcookie():
   if request.method == 'POST':
       user = request.form['Name']

   resp = make_response(render_template('readcookie.html'))
   resp.set_cookie('userID', user)

   return resp

@ap.route('/getcookie')
def getcookie():
   name = request.cookies.get('userID')
   return '<h1>Hello '+name+'</h1>'

if __name__ == '__main__':
   ap.run(host= '0.0.0.0')
vi templates/readcookie.html
<html>
   <body>

     <a href="http://<ip>:5000/getcookie">Enter</a>

   </body>
</html>
python cookie.py
[ Flask ] 12 April, 2017 00:39

method − current request method
Form − dictionary object form parameters and their values
args − parsed contents of query string which is part of URL after a (?)
Cookies − dictionary object holding Cookie names and values
files − data pertaining to uploaded file

Form:

vi templates/record.html

<!doctype html>
<html>
   <body>

      <table border = 1>
         {% for key, value in input.iteritems() %}

            <tr>
               <th> {{ key }} </th>
               <td> {{ value }} </td>
            </tr>

         {% endfor %}
      </table>

   </body>
</html>
vi templates/friend.html
<html>
   <body>

      <form action = "http://<ip>:5000/record" method = "POST">
         <p>Name <input type = "text" name = "name" /></p>
         <p>Age <input type = "text" name = "age" /></p>
         <p>Higher <input type = "text" name = "higher" /></p>
         <p>Weight <input type ="text" name = "weight" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>

   </body>
</html>
vi friend_rec.py
from flask import Flask, render_template, request
ap = Flask(__name__)

@ap.route('/')
def friend():
   return render_template('friend.html')

@ap.route('/record',methods = ['POST', 'GET'])
def record():
   if request.method == 'POST':
      input = request.form
      return render_template("record.html",input = input)

if __name__ == '__main__':
   ap.run(host= '0.0.0.0')
[ Flask ] 12 April, 2017 00:37

vi static/hello.js

function sayHello() {
   alert("Hello. :)")
}
vi templates/hello.html
<html>
   <head>
      <script type = "text/javascript"
         src = "{{ url_for('static', filename = 'hello.js') }}" ></script>
   </head>

   <body>
      <input type = "button" onclick = "sayHello()" value = "Say: Hello" />
   </body>
</html>
vi hello.py
from flask import Flask, render_template
ap = Flask(__name__)

@ap.route("/hello")
def index():
   return render_template("hello.html")

if __name__ == '__main__':
   ap.run(host='0.0.0.0')
[ Flask ] 10 April, 2017 23:32

cd <flask_root_dir>/<ap_dir>

example_1:
vi templates/hello.html

vi templates/hello.html
<!doctype html>
<html>
   <body>

      <h1>Hello,{{ name }}!</h1>

   </body>
</html>
vi hello.py
from flask import Flask, render_template
ap = Flask(__name__)

@ap.route('/<who>')
def hello(who):
   return render_template('hello.html', name=who)

if __name__ == '__main__':
   ap.run(host = '0.0.0.0')
python hello.py

example_2:
vi templates/allpeople.html
<!doctype html>
<html>
   <body>
  
      <table border = 1>
         {% for key, value in people.iteritems() %}
        
            <tr>
               <th> {{ key }} </th>
               <td> {{ value }} </td>
            </tr>
           
         {% endfor %}
      </table>
     
   </body>
</html>
vi allpeople.py
from flask import Flask, render_template
ap = Flask(__name__)

@ap.route('/allpeople')
def result():
   dict = {'phoebe':1,'range':2}
   return render_template('allpeople.html', people = dict)

if __name__ == '__main__':
   ap.run(host = '0.0.0.0')

python allpeople.py

===

Jinga2 delimiters:
1.{{ Expressions to print to the template output }}
2.{% Statements %}
    1.
    {% if ... %}
    ...
    {% else %}
    ...
    {% endif %}
    2.
    {% for ... %}
    ...
    (% endfor %)
3.# Statements
    # for ... :
    ...
    # endfor
4.{# Comments not included in the template output #}
    {# ...
    ...
    #}
5.## comment
    ## ignore...

http://jinja.pocoo.org/docs/2.9/templates/#synopsis

[ Flask ] 10 April, 2017 01:46

GET: get the information stored on that page and send it
HEAD: get only interested in the headers, not the content of the page
POST: post information to that URL and that the server must ensure the data is stored and only stored once
PUT: server might trigger the store procedure multiple times by overwriting the old values more than once
PATCH: used to update partial resources(e.g. update one field)
DELETE: Remove the information at the given location
OPTIONS: Provides a quick way for a client to figure out which methods are supported by this URL

in HTML4 and XHTML1, the only methods a form can submit to the server are GET and POST,
But with JavaScript and future HTML standards you can use the other methods as well

example:

login.html

<html>

  <body>
      <form action = "http://localhost:5000/login" method = "post">

      <p>Enter Name:</p>
      <p><input type = "text" name = "nm" /></p>
      <p><input type = "submit" value = "submit" /></p>

    </form>
  </body>

</html>
test_login.py
from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route('/success/<name>')
def success(name):
    return 'welcome %s' % name

@app.route('/login', methods = ['POST', 'GET'])
def login():

    if request.method == 'POST':
       user = request.form['nm']
       return redirect(url_for('success', name = user))
    else:
       user = request.args.get('nm')
       return redirect(url_for('success', name = user))

if __name__ == '__main__':
    app.run(host='0.0.0.0')
[ Flask ] 09 April, 2017 19:44

Func style:

    templates/
        home/
        control_panel/
        admin/

    views/
        __init__.py
        home.py
        control_panel.py
        admin.py

Zone Style:

    admin/
        __init__.py
        views.py
        static/
        templates/

    home/
        __init__.py
        views.py
        static/
        templates/

    control_panel/
        __init__.py
        views.py
        static/
        templates/

Example:

ap/views/profile.py

from flask import Blueprint, render_template
profile = Blueprint('profile', __name__,
                    template_folder='templates', #Blueprint with self template
                    static_folder='static') #Blueprint with self static

@profile.route('/<user_url>')
def timeline(user_url):
    ...
    return render_template('profile/timeline.html')

@profile.route('/<user_url>/photos')
def photos(user_url):
    ...
    return render_template('profile/photos.html')

@profile.route('/<user_url>/about')
def about(user_url):
    ...
    return render_template('profile/about.html')

ap/__init__.py

from flask import Flask
from .views.profile import profile

app = Flask(__name__)
app.register_blueprint(profile)

Dynamic Url Prefix

instance:

ap/views/profile.py

from flask import Blueprint, render_template
profile = Blueprint('profile', __name__, url_prefix='/<user_url>')

register:

ap/__init__.py

from flask import Flask
from .views.profile import profile

app = Flask(__name__)
app.register_blueprint(profile, url_prefix='/<user_url>')


example:

ap/views/profile.py

from flask import Blueprint, render_template, g
from ..models import User

# The prefix is defined in ap/__init__.py.
profile = Blueprint('profile', __name__)

@profile.url_value_preprocessor
def get_profile_owner(endpoint, values):
    query = User.query.filter_by(url=values.pop('user_url'))
    g.profile_owner = query.first_or_404()

@profile.route('/')
def timeline():
    return render_template('profile/timeline.html')

@profile.route('/photos')
def photos():
    return render_template('profile/photos.html')

@profile.route('/about')
def about():
    return render_template('profile/about.html')

Dynamic SubDomain

ap.com:

ap/__init__.py

from flask import Flask
from .site import site

app = Flask(__name__)
app.register_blueprint(site, subdomain='<site_subdomain>')

subdn.ap.com:

ap/site1/__init__.py

from flask import Blueprint
from ..models import Site

# Site is module, site is Blueprint
site = Blueprint('site', __name__)

@site.url_value_preprocessor
def get_site(endpoint, values):
    query = Site.query.filter_by(subdomain=values.pop('site_subdn'))
    g.site = query.first_or_404()

#View module need import 'site', so def site before import views.
from . import views

#modify var SERVER_NAME
config.py

SERVER_NAME = 'ap.com'

 

[ Flask ] 09 April, 2017 01:14

<pyenv / dir>

    requirements.txt

pip install <pkg> >> requirements.txt

    main.py

from flask import Flask
app = Flask(__name__)

    config.py

app.config.from_object('config')

    instance/config.py

app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile('config.py')

 #SECRET_KEY in onfig.py

    SECRET_KEY =
    STRIPE_API_KEY =
    SQLALCHEMY_DATABASE_URI=

    ap_home/

                  __init__.py
                  models.py
                  views.py
                  templates/
                  static/ 

    config/<configname>.py

 app.config.from_envvar(‘ap_config_file’) #abs path of config file

#start ap from hosting
start.sh
ap_config_file=/<path>/<configname>.py
python main.py
[ Flask ] 06 April, 2017 23:13

service firewalld status
#if Firewalld not running
service firewalld start
firewall-cmd --zone=public --permanent --add-port=5000/tcp
firewall-cmd --zone=public --list-all

cd <flask_rootdir>
pyenv activate
pip install flask

vi test.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
     return "It's Flask!"

@app.route("/<name>")
def hello(name):
     return "Hello, %s. :)" %name

if __name__ == "__main__":
    app.run(host='0.0.0.0')
python test.py
#open web browser with http://<ip>:5000

===

@app.route('<path>', options)
add_url_rule('<path>, '<name>', func)

@app.route("/xxx/") -> <url>/xxx == <url>/xxx/
@app.route("/xxx") -> <url>/xxx/ -> 404 Not Found

@app
.route('/<name>')
@app
.route('/<path/<int:var>') #type(var) == type(int)
@app
.route('/hello/<float:var>') #type(var) == type(float)

app.run(host, port, debug, options)

url_for():
@app
.route('/admin')

def
hello_admin():

   
return 'Hello Admin'


@app
.route('/guest/<guest>')

def
hello_guest(guest):

   
return 'Hello
Guest, %s.' % guest

@app.route('/user/<name>')
def
hello(name):
   
if name =='admin':
       
return redirect(url_for('hello_admin'))
   
else:
       
return redirect(url_for('hello_guest', guest = name))
[ General ] 06 April, 2017 02:31

git clone https://github.com/yyuu/pyenv.git /home/pyenv
git clone https://github.com/yyuu/pyenv-virtualenv.git /home/pyenv/plugins/pyenv-virtualenv

vi ~/.bashrc

alias ...
##
#pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
##
source ...

source ~/.bashrc

yum install readline readline-devel readline-static -y
yum install openssl openssl-devel openssl-static -y
yum install sqlite-devel -y
yum install bzip2-devel bzip2-libs -y

cd /home/pyenv/versions
pyenv versions
pyenv install 2.7.13
pyenv versions
pyenv rehash
pyenv virtualenv 2.7.13 <vdir>
pyenv virtualenvs

#pyenv global, pyenv local, pyenv shell

cd <vdir>
pyenv activate
pyenv deactivate

[ General ] 04 April, 2017 02:24

 Since I am a third-rate U*ix system admin,

I would like to try to trace Fabric code.

but where to start?

 you can read all the code on github at https://github.com/fabric/fabric.

ok...I try to start from any file of them.

1 2  Next»