Babel 中文文档 Babel 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 指南
    • Babel 是什么?
    • 使用指南
    • 配置 Babel
    • 学习 ES2015
    • 升级到 Babel 7
  • 配置

  • 预设环境

  • 杂项

  • 集成

  • 工具

  • 辅助

flask-bones


An example of a large scale Flask application using blueprints and extensions.

Setup


Quickly run the project using docker and docker-compose :

  1. ``` shell
  2. docker-compose up -d
  3. ```

Create the database and seed it with some data:

  1. ``` shell
  2. docker-compose run --rm app flask create-db
  3. docker-compose run --rm app flask populate-db --num_users 5
  4. ```

Download front-end dependencies with yarn :

  1. ``` shell
  2. yarn install --modules-folder ./app/static/node_modules
  3. ```

Configuration


The following environment variables are optional:

Name Purpose
:--- :---
APP_NAME The name of the application. i.e Flask Bones
MAIL_PORT The port number of an SMTP server.
MAIL_SERVER The hostname of an SMTP server.
MEMCACHED_HOST The hostname of a memcached server.
MEMCACHED_PORT The port number of a memcached server.
POSTGRES_HOST The hostname of a postgres database server.
POSTGRES_PASS The password of a postgres database user.
POSTGRES_PORT The port number of a postgres database server.
POSTGRES_USER The name of a postgres database user.
REDIS_HOST The hostname of a redis database server.
REDIS_PORT The port number of a redis database server.
SECRET_KEY A secret key required to provide authentication.
SERVER_NAME The hostname and port number of the server.

Features


Caching with Memcached


  1. ``` python
  2. from app.extensions import cache

  3. # Cache something
  4. cache.set('some_key', 'some_value')

  5. # Fetch it later
  6. cache.get('some_key')
  7. ```

Email delivery


  1. ``` python
  2. from app.extensions import mail
  3. from flask_mail import Message

  4. # Build an email
  5. msg = Message('User Registration', sender='admin@flask-bones.com', recipients=[user.email])
  6. msg.body = render_template('mail/registration.mail', user=user, token=token)

  7. # Send
  8. mail.send(msg)
  9. ```

Asynchronous job scheduling with RQ


RQ is a simple job queue for python backed by redis.

Define a job:

  1. ``` python
  2. @rq.job
  3. def send_email(msg):
  4.     mail.send(msg)
  5. ```

Start a worker:

  1. ``` shell
  2. flask rq worker
  3. ```

Queue the job for processing:

  1. ``` python
  2. send_email.queue(msg)
  3. ```

Monitor the status of the queue:

  1. ``` shell
  2. flask rq info --interval 3
  3. ```

For help on all available commands:

  1. ``` shell
  2. flask rq --help
  3. ```

Stupid simple user management


  1. ``` python
  2. from app.extensions import login_user, logout_user, login_required

  3. # Login user
  4. login_user(user)

  5. # You now have a global proxy for the user
  6. current_user.is_authenticated

  7. # Secure endpoints with a decorator
  8. @login_required

  9. # Log out user
  10. logout_user()
  11. ```

Password security that can keep up with Moores Law


  1. ``` python
  2. from app.extensions import bcrypt

  3. # Hash password
  4. pw_hash = bcrypt.generate_password_hash('password')

  5. # Validate password
  6. bcrypt.check_password_hash(pw_hash, 'password')
  7. ```

Easily swap between multiple application configurations


  1. ``` python
  2. from app.config import dev_config, test_config

  3. app = Flask(__name__)

  4. class dev_config():
  5.     DEBUG = True

  6. class test_config():
  7.     TESTING = True

  8. # Configure for testing
  9. app.config.from_object(test_config)

  10. # Configure for development
  11. app.config.from_object(dev_config)
  12. ```

Form validation & CSRF protection with WTForms


Place a csrf token on a form:

  1. ``` html
  2. {{ form.csrf_token }}
  3. ```

Validate it:

  1. ``` python
  2. form.validate_on_submit()
  3. ```

Rate-limit routes


  1. ``` python
  2. from app.extensions import limiter

  3. @limiter.limit("5 per minute")
  4. @auth.route('/login', methods=['GET', 'POST'])
  5. def login():
  6.     # ...
  7.     return 'your_login_page_contents'
  8. ```

Automated tests


Run the test suite:

  1. ``` shell
  2. pytest
  3. ```

Use any relational database using the SQLAlchemy ORM


  1. ``` python
  2. from app.user.models import User

  3. # Fetch user by id
  4. user = User.get_by_id(id)

  5. # Save current state of user
  6. user.update()

  7. # Fetch a paginated set of users
  8. users = User.query.paginate(page, 50)
  9. ```

Front-end asset management


Download front-end dependencies with yarn :

  1. ``` shell
  2. yarn install --modules-folder ./app/static/node_modules
  3. ```

Merge and compress them together with Flask-Assets :

  1. ``` shell
  2. flask assets build
  3. ```

Version your database schema


Display the current revision:

  1. ``` shell
  2. flask db current
  3. ```

Create a new migration:

  1. ``` shell
  2. flask db revision
  3. ```

Upgrade the database to a later version:

  1. ``` shell
  2. flask db upgrade
  3. ```

Internationalize the application for other languages (i18n)


Extract strings from source and compile a catalog (.pot ):

  1. ``` shell
  2. pybabel extract -F babel.cfg -o i18n/messages.pot .
  3. ```

Create a new resource (.po) for German translators:

  1. ``` shell
  2. pybabel init -i i18n/messages.pot -d i18n -l de
  3. ```

Compile translations (.mo):

  1. ``` shell
  2. pybabel compile -d i18n
  3. ```

Merge changes into resource files:

  1. ``` shell
  2. pybabel update -i i18n/messages.pot -d i18n
  3. ```
Last Updated: 2023-09-03 17:10:52