Skip to content

Meteorix/flaskweb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flaskweb

An easy to start yet full-featured web framework

Installation

pip install -U flaskweb

# or git clone and install; git pull for later updates
git clone git@github.com:Meteorix/flaskweb.git
pip install -e flaskweb

Quickstart

基本语法与flask几乎一样,几行代码即可构建一个web服务器

from flaskweb.app import create_app, gevent_run
from flask import render_template

app = create_app("debug")

@app.route("/")
def index():
    return render_template("debug.html")

if __name__ == "__main__":
    gevent_run(app)

不一样的地方在于,上面的web服务器自带:

  • 用户登录系统
  • orm/migrate
  • db管理页面
  • swaggerui
  • gevent服务器
main !login
!api !admin

try it:

cd simple
# 初始化数据库,会保存到simple/app.db
flask db init
flask db upgrade
flask db migrate
# 运行gevent服务器
python -u app.py

then visit http://127.0.0.1:5000/

A more sophisticated example

Features

  • flask
  • sqlalchemy
  • config
  • logger
  • user login
  • db admin
  • restful api with swagger ui
  • gevent
  • use as a 3rd library
  • frontend with bootstrap/jquery
  • setup.py for distribution
  • cythonize
  • gunicorn

more todos

  • celery
  • nginx
  • more samples: tensorflow/pytorch webapp
  • jwt
  • pyinstaller
  • dockerfile
  • performance/benchmark

Tutorial

10分钟速成版

假设你有python和flask基础,介绍如何利用flaskweb速成

  1. 创建新的项目

    example目录copy到你的项目中,作为启动模板

  2. 定义url,返回默认html页面

    @app.route("/")
    def index():
        return render_template(debug.html)

    启动服务器,访问 http://127.0.0.1:5000/

    python wsgi.py
  3. 创建sqlite数据库,使用内置的用户登录系统

    cd simple
    # 初始化数据库,会保存到simple/app.db
    flask db init
    flask db migrate
    flask db upgrade

    重启服务器,访问登陆页面 http://127.0.0.1:5000/login

  4. 当然你需要先注册用户

    在注册页面上注册好之后,debug模式运行会自动激活账号,直接登录

    debug模式需要找管理员激活账号:管理员访问admin页面 http://127.0.0.1:5000/admin/user,勾选用户的active属性

  5. 定义数据库orm,使用admin页面管理

    参考 example/models.py

  6. 定义restful api,使用swagger ui调试

    python代码参考 example/views.py, api spec参考OpenAPI

    访问 http://127.0.0.1:5000/apidocs/ 调试API,调试好就可以给前端工程师开发前端了

  7. 前端工程师输出前端代码

    • html文件放到templates目录
    • 其他文件放到static目录
  8. 10分钟差不多都学会啦

Serious python web developer version 👽

  1. python basics
  2. flask quickstart
  3. sql基础
  4. html/css/js基础
  5. restful api规范
  6. ...
  7. http/tcp协议《http权威指南》
  8. linux知识《APUE》
  9. ...

Development

Develop flaskweb framework

virtualenv venv
source venv/bin/activate
pip install -r requirements

Build and upload to pypi

python setup.py build
python setup.py sdist
pip install twine  # for pypi upload
python -m twine upload dist/...

Distribution

Distribute example project using setup.py

cd example
python setup.py build
python setup.py sdist --formats=zip

Or cythonize and dist as wheel for better encryption

python setup.py bdist_wheel

todo: pyinstaller

Thanks