• ALL
  • LEETCODE
  • BEAR
  • RSS
  • Flask Day 2

    2016年2月16日

    To handle web forms we use Flask-WTF . So we need to write a config file (file config.py):

    1
    2
    WTF_CSRF_ENABLED = True
    SECRET_KEY = 'you-will-never-guess'

    And then you need to use this config (file app/__init__.py):

    1
    2
    3
    4
    5
    6
    from flask import Flask

    app = Flask(__name__)
    app.config.from_object('config')

    from app import views

    Let’s build a simple form (file app/forms.app):

    1
    2
    3
    4
    5
    6
    7
    from flask.ext.wtf import Form
    from wtforms import StringField, BooleanField
    from wtforms.validators import DataRequired

    class LoginForm(Form):
    openid = StringField('openid', validators=[DataRequired()])
    remember_me = BooleanField('remember_me', default=False)

    The DataRequired() is a validator that checks the field is empty or not.

    After that, we need a HTML page to show the form (file app/templates/login.html):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!-- extend from base layout -->
    {% extends "base.html" %}

    {% block content %}
    <h1>Sign In</h1>
    <form action="" method="post" name="login">
    {{ form.hidden_tag() }}
    <p>
    Please enter your OpenID:<br>
    {{ form.openid(size=80) }}<br>
    </p>
    <p>{{ form.remember_me }} Remember Me</p>
    <p><input type="submit" value="Sign In"></p>
    </form>
    {% endblock %}

    The final step is to code a view function that renders the template and receiving data from form (file app/views.py):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    from flask import render_template, flash, redirect
    from app import app
    from .forms import LoginForm

    # index view function suppressed for brevity

    app.route('/login', methods=['GET', 'POST'])
    def login():
    form = LoginForm()
    if form.validate_on_submit():
    flash('Login requested for OpenID="%s", remember_me=%s' %
    (form.openid.data, str(form.remember_me.data)))
    return redirect('/index')
    return render_template('login.html',
    title='Sign In',
    form=form)
  • Flask Day 1

    2016年2月15日

    “Hello World” in Flask

    Create a folder named microblog (or whatever you want). Then cd into that folder and run following prompt in terminal:

    1
    $ python3 -m venv flask

    Now you’ll have a folder named flask inside microblog, containing a private version of Python interpreter.

    And you should install flask and extensions by the commands below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ flask/bin/pip install flask
    $ flask/bin/pip install flask-login
    $ flask/bin/pip install flask-openid
    $ flask/bin/pip install flask-mail
    $ flask/bin/pip install flask-sqlalchemy
    $ flask/bin/pip install sqlalchemy-migrate
    $ flask/bin/pip install flask-whooshalchemy
    $ flask/bin/pip install flask-wtf
    $ flask/bin/pip install flask-babel
    $ flask/bin/pip install guess_language
    $ flask/bin/pip install flipflop
    $ flask/bin/pip install coverage

    After that, let’s create the basic structure for our application: app app/static app/templates tmp.

    1. app — where the application package is
    2. static — stores static files like images, javascripts, and css.
    3. templates — where templates will go.

    Then you can start with __init__.py which should put into app folder (file app/__init__.py):

    1
    2
    3
    4
    from flask import Flask

    app = Flask(__name__)
    from app import views

    The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.

    Let’s see what a views function looks like (file app/views.py):

    1
    2
    3
    4
    from flask import Flask

    app = Flask(__name__)
    from app import views

    Finally we should create a script to starts up the web server with our application(file run.py):

    1
    2
    3
    #!flask/bin/python
    from app import app
    app.run(debug=True)

    To indicating that is an executable file you need to run this in terminal:

    1
    $ chmod a+x run.py

    Now the file structure should look like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    microblog\
    flask\
    <virtual environment files>
    app\
    static\
    templates\
    __init__.py
    views.py
    tmp\
    run.py

    Then start to write the template (file app/templates/index.html):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <html>

    <head>
    <title>{{ title }} - microblog</title>
    </head>

    <body>
    <h1>Hello, {{ user.nickname }}!</h1>
    </body>

    </html>

    Now let’s write the view function that uses this template (file app/views.py):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    from flask import render_template
    from app import app

    @app.route('/')
    @app.route('/index')
    def index():
    user = {'nickname': 'ching'} # fake user
    return render_template('index.html',
    title='Home',
    user=user)

    render_template function is what we import from Flask framework to render the template. It uses Jinja2 templating engine.

  • First Post

    2016年2月12日

    This is the very first post I wrote,

    with Typora & Hexo.