Flask
How to build socketio in Flask project : socketio
Flask variable, blueprint and install from requirement.txt, some basic knowledge about WSGI, Nginx and Django.
# Reference
NAME | URL | DESCRIPTION |
---|---|---|
flask_socketio | https://github.com/miguelgrinberg/flack (opens new window) | a project using flask_socketio |
https://flask-socketio.readthedocs.io/en/latest/ | flask_socketio official tutorial | |
blueprint | http://flask.pocoo.org/docs/1.0/blueprints/ | official tutorial (opens new window) |
# SocketIO
pip install flask-socketio
# Flask Structure
application context and request context:
variable | context | description |
---|---|---|
current_app | application context | The application instance for the active application. |
g | ac | |
request | request context | |
session | rc |
from flask import current_app
g
(opens new window) is a special object that is unique for each request. It is used to store data that might be accessed by multiple functions during the request. The connection is stored and reused instead of creating a new connection if get_db
is called a second time in the same request.
# Blueprint
Tutorial : Modular Applications with Blueprints (opens new window)
We use the create_app()
so that the application is created at runtime.
A blueprint is similar to an application in that it can also define routes. The difference is that routes associated with a blueprint are in a dormant state until the blueprint is registered with an application, at which point the routes become part of it.
# app/main/__init__.py
from flask import Blueprint
main = Blueprint('main', __name__)
The constructor for this class takes two required arguments: the blueprint name and the model or package where the blueprint is located(always default __name__
).
# Virtual environment
# installation
Take Linux system as example:
pip install virtualenv
# usage
cd [project directory]
virtualenv venv
该命令执行后,将在当前目录中建立一个 venv 目录,该目录复制了一份完整的当前系统的 python 环境。之后运行 python 时可以直接运行该项目的 bin 文件夹中的命令。
例:在当前虚环境下安装 Tornado 组件:
./venv/bin/pip install tornado
或者在该虚环境中运行 python 程序:
./venv/bin/python xxxx.py
也可以使用 activate
命令启动虚环境,之后不必再显示地调用虚环境 bin 文件夹中的命令:
source ./venv/bin/activate
退出虚拟环境使用 deactive
:
(venv) xxx:~/xxx$ deactivate
# requirement.txt
Install from requirement.txt
:
pip install -r requirements.txt
generate a requirement.txt
from current project:
pip freeze > requirement.txt
# pipenv
目前流行的取代 virtual envirment 的方式就是 pipenv (opens new window)
Install pipenv:
pip install pipenv
pipenv shell
pipenv install package-name
# if you want lock the environment
pipenv lock
退出可以使用 exit
或者 Ctrl+D
.
# Video Streaming with Flask
# Flask Script
# manage.py
import os
from app import create_app, db
from app.models import User, Role
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
# Web Server
目前主流的 Web 服务器包括 Nginx, Apache, lighthttpd, IIS, etc..,Python 服务端程序在 Linux 平台下使用最广泛的是 Nginx。
# WSGI
Web Server Gateway Interface [^2], 为 Python 语言定义 Web 服务器和服务端程序的通用接口规范。
[^2]: WSGI, wiki (opens new window)
# Nginx
sudo apt-get install nginx
sudo service nginx start/status/stop/restart
# Django
# Installation
pip3 install django
测试是否安装成功:
python3
>>> import django
>>> print(django.VERSION)
# Establish application
建立项目:
django-admin startproject [project-name]
建立应用:
python manage.py startapp [app-name]
例如,在当前目录中创建一个项目 my_project, 并且拥有特定的目录结构:
djangp-admin startproject my_project
cd my_project
python3 manage.py startapp my_app
完成之后目录结构类似于:
my_project/
manage.py
my_project/
__init__.py
settings.py
urls.py
wsgi.py
my_app/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
内置 web 服务器运行:
python manage.py runserver 0.0.0.0:8001
生成数据移植文件:
python manage.py makemigrations app
移植到数据库:
python manage.py migrate
# New Words
words | means |
---|---|
PK | primary key |
FK | foregin key |
CRUD | create, retrieve, update and delete |