A practical summary of Web application development based on Python

Source: Internet
Author: User
Tags virtual environment virtualenv

Python-based Web application Development Learning Summary Project address

?? This study is based on the flask framework. Develop a personal blogging system based on tutorials. Blog interface.




The whole learning process has gained a lot, the following is the study summary.

1, Virtualenv

Virtualenv is a third-party tool for creating virtual environments. You can create a separate virtual environment for each program, each of which has no impact on the global environment, meaning that different versions of Python and plugins can be used in different virtual environments. Another benefit of using a virtual environment is that you do not need administrator privileges.
The following is the installation and use of virtualenv:

1, check whether the installation of virtualenvvirtualenv --version

2. Install: https://bitbucket.org/pypa/setuptools Download the setuptools script in ez_setup.py , then execute

python ez_setup.pyeasy_install virtualenv

3. Create: Use commands in the project root directory where you need to use the virtual environment

Venv is the name of the virtual environment and can be changed according to individual needs. After successful creation, a subfolder appears under the current folder with the name specified in the above command, and the files associated with the virtual environment are saved in this subfolder.

4, use: The use of virtual environment requires activation, the command is as follows:

venv\Scripts\activate

The command prompt changes to successful activation (venv) $ , and if you need to exit the virtual environment, you can use the command:

deactivate
2. Project Structure

Basic Project Structure:

1, flask programs are generally saved in the package named app:

    • Api_0_1 the package that stores the API blueprint
    • Auth and main are the packages that store the main program blueprint and the certification blueprint.
    • static files such as CSS files are stored
    • Templates for storing web templates

2, Migrations folder contains the database migration script;
3. The files in the requirements list all the dependent packages needed in different development environments, which makes it easy to regenerate the same virtual environment in other computers.
4, config.py storage configuration;
5. manage.py is used to start programs and other program tasks.

?? Changing a folder into a Python module requires only a new __init__.py in that folder. Note: The role of __init__.py is as follows:

    1. Equivalent to the Def init(self) in class: A function that initializes the module.
    2. Treat your directory as a package
    3. More relevant
3. Blueprint

?? Flask uses the blueprint technology to modularize the project. Blueprints are similar to programs, and routes can also be defined. The difference is that the route defined in the blueprint is dormant until the blueprint is registered to the program, and the route is truly part of the program. When using a blueprint that is located in the global scope, the method of defining the route is almost the same as a single-script program.
?? The use of blueprints is divided into two steps: Create and register.

    • Create: You can create a blueprint by instantiating a Blueprint class object:
 #app/main__init__.py from flask import Blueprint    #导入蓝本模块 main = Blueprint(‘main‘, __name__)     # 第一个参数为蓝本的名字,第二个参数为蓝本所在的包或模块 from . import views, errors  #避免循环导入依赖,在末尾导入错误处理程序和路由程序
    • Registration: The calling program comes with its own register_blueprint blueprint registered on the program
#app_init_.pyfrom .main import main as main_blueprintapp.register_blueprint(main_blueprint)
4. Flask Extension

?? The flask extension can be understood as a plug-in, small enough to be called a "micro-frame", but small does not mean less functionality. The core of flask includes basic services, and other functions can be implemented by extension .
?? The use of extensions is also divided into two steps: Installation and import. Generally use the command pip install 扩展名 to install the extension, after the installation is complete, directly in the program from ... import ... with the use of the statement import (specifically for the Flask development of the expansion of the storm leak in the Flask.ext namespace). The following sample code uses the Flask-script extension for the installation:
?? First, install Flask-script on the command line:

 (venv) $ pip install flask-script  

?? To use in a program:

from flask.ext.script import Manager  #导入manager = Manager(app)  # ...  中间无关代码省略if __name__ == ‘__main__‘:manager.run()

?? During the development of this project, many extensions were used, such as Flask-login, Flask-bootstrap, FLASK-WTF and so on.

5. Implementing an API that conforms to the REST architecture

?? Flask is the ideal framework for developing a REST architecture Web service because Flask is inherently lightweight.
?? REST API-related routing is a self-contained subset of programs, so in order to better organize the code, it is best to put these routes into a separate blueprint. Using Flask to create a REST Web service can be applied to the applet backend service, which provides resources for small programs. If time permits, it is intended to introduce the rest architecture Web service separately after developing a small program backend system.

A practical summary of Web application development based on Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.