There are a number of Python frameworks available to help you create Web applications more easily. These frameworks organize the modules together, making it easier to build applications without having to worry about details (such as sockets and protocols), so everything is in the framework. We’ll look at the different options next.
Python was invented in the late 1980s. The developer is Guido van Rossum of Centrum Wiskunde & Informatica, a math and computer science research center in the Amsterdam Science Park in the Netherlands. Van Rossum has been an influential figure in Python development ever since. In fact, community members gave him an honorary title: Benevolent Dictator for Life (BDFL).
After humble beginnings, Python has become one of the most popular server-side programming languages on the Internet. According to W3Techs, it is used on more high-traffic sites than ColdFusion, PHP, and ASP.NET. More than 98% of these sites are running Python 2.0, and just over 1% are running 3.0.
Frameworks make development easier
Today, there are a number of Python frameworks that make web application development much easier. These frameworks integrate different modules together, allowing you to build applications faster without having to worry about details (such as sockets and protocols), and provide all the functionality you need.
As part one of this two-part series, we’ll cover some of the most popular Python frameworks. While most modern Web frameworks run on the server side, some are beginning to experiment with client-side code that can run on the client side (Skulpt and Trinket, for example). Python frameworks are generally divided into full-stack and non-full-stack frameworks. Full-stack frameworks design all technologies from user experience to database, while non-full-stack frameworks do not cover the entire technology of development.
There is also a microframework that is not a full-stack framework, but is more lightweight. In some cases, microframes are appropriate, and in other cases, full-stack frames are appropriate. In the second part of this article we will make a comparison between full stack frameworks and microframeworks.
Django
Django is probably the most representative Python framework. It is an open source framework that follows the MMVC architectural pattern. It takes its name from Django Reinhardt, a French composer and guitarist who is considered by many to be the greatest guitarist in history. At Lawrence Journal-World in Lawrence city, Kansas, two programmers, Adrian Holovaty and Simon Willison, created Django in 2003 to create Web applications for newspapers.
Django comes with a template engine built in, as well as OOTB support for the popular Jinja2 engine. It also supports rege-based URL distribution, allowing complex mappings to be accomplished with simple urls.
One of the advantages of Django is that you only need a separate installation package to install it. Other similar frameworks require downloading many components to get started. Also, Django has good documentation to keep up to date, which is often a disadvantage for open source projects. It is a robust framework that integrates many plug-ins and extensions from the community. The community behind the project also seems to be well organized, as evidenced by its excellent documentation and tutorials.
The first framework I learned was Django. It was easy to get started and quick to get started. Personally, Djangos Model is difficult to use, mainly because of the unfriendly cross-library link-table. Django’s official website
Create a project:
$ pip install django
$ django-admin startproject djdemo
cdDjdemo $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin $django.admin Migrations │ │ └ ─ ─ just set py │ ├ ─ ─ models. Py │ ├ ─ ─ tests. Py │ └ ─ ─ views. Py ├ ─ ─ djdemo │ ├ ─ ─ just set py │ ├ ─ ─ __pycache__ │ │ ├ ─ ─ just set retaining - 37. Pyc │ │ └ ─ ─ Settings. Retaining - 37. Pyc │ ├ ─ ─ Settings. Py │ ├ ─ ─ urls. Py │ └ ─ ─ Wsgi. Py └ ─ ─ the manage. PyCopy the code
Here I give a Django directory structure for one of my projects. Djangos classic directory structure:
Django is very simple, django ecosystem is very complete, based on Django development, general need function, you can find already existing wheels. Development efficiency is high. Development with Django is about filling code into the framework. The other thing is it’s not flexible enough, it’s too heavy.
Django has a lot of handy scaffolding
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Copy the code
Django excellent resources
TurboGears
TurboGears is a framework built on several well-known Python projects, such as SQLAlchemy, WebOb, Repoze, and Genshi. In a sense, TurboGears is gluing together multiple established open platforms. Like Django, it uses an MVC architecture. It also recently included a “minimal mode” that allows it to act as a microframework.
TurboGears was developed by Kevin Dangoor in 2005. He released it as an open source project in September of that year. In 2013, the project developers moved to Support Python 3, abandoning the Pylons code they had written. TurboGears benefits include:
- Support the aggregation
- Powerful object-relational mapper
- Transaction systems support transactions between multiple databases
- Multiple database support
- Use reusable code snippets as templates
- There is a lot of flexibility for interfacing with non-standard components
- Support the shard
- The template system makes the designer’s design easier
TurboGears
The TurboGears website
The installation
$ pip install tg.devtools
$ gearbox --help
usage: gearbox [--version] [-v | -q] [--log-file LOG_FILE] [-h] [--debug]
[--relative]
TurboGears2 Gearbox toolset
optional arguments:
--version show program's version number and exit -v, --verbose Increase verbosity of output. Can be repeated. -q, --quiet Suppress output except warnings and errors. --log-file LOG_FILE Specify a file to log output. Disabled by default. -h, --help Show this help message and exit. --debug Show tracebacks on errors. --relative Load plugins and applications also from current path. Commands: help print detailed help for another command makepackage Creates a basic python package patch Patches files by replacing, appending or deleting text. quickstart Creates a new TurboGears2 project scaffold Creates a new file from a scaffold template serve Serves a web application that uses a PasteDeploy configuration file setup-app Setup an application, given a config file tgext Creates a tgext.* packageCopy the code
Create a project
$gearbox QuickStart tgdemo // Generates a lot of files. ├── Manifest.In ├── readme.txt ├── __pycache__ │ ├── cpython-37.pyc ├─ Development. Ini ├ ─ ─ migration │ ├ ─ ─ env. Py │ ├ ─ ─ script. Py. Mako │ └ ─ ─ versions │ └ ─ ─ the empty. TXT ├ ─ ─ setup. The CFG ├ ─ ─ Setup. Py ├ ─ ─ test. Ini ├ ─ ─ tgdemo │ ├ ─ ─ just set py │ ├ ─ ─ the config │ │ ├ ─ ─ just set py │ │ ├ ─ ─ app_cfg. Py │ │ ├ ─ ─ Environment. Py │ │ └ ─ ─ middleware. Py │ ├ ─ ─ controllers │ │ ├ ─ ─ just set py │ │ ├ ─ ─ controller. Py. Template │ │ ├ ─ ─ Error. Py │ │ ├ ─ ─ root. Py │ │ └ ─ ─ secure. Py │ ├ ─ ─ i18n │ │ └ ─ ─ ru │ ├ ─ ─ lib │ │ ├ ─ ─ just set py │ │ ├ ─ ─ app_globals. Py │ │ ├ ─ ─ base. Py │ │ └ ─ ─ helpers. Py │ ├ ─ ─ model │ │ ├ ─ ─ just set py │ │ ├ ─ ─ auth. Py │ │ └ ─ ─ model. Py. Template │ ├ ─ ─ the public │ │ ├ ─ ─ CSS │ │ ├ ─ ─ the favicon. Ico │ │ ├ ─ ─ fonts │ │ ├ ─ ─ img │ │ └ ─ ─ javascript │ ├ ─ ─ templates │ │ ├ ─ ─ just set py │ │ ├ ─ ─ __pycache__ │ │ ├ ─ ─ the about. XHTML │ │ ├ ─ ─ data. The XHTML │ │ ├ ─ ─ environ. XHTML │ │ ├ ─ ─ the error. The XHTML │ │ ├ ─ ─ index. The XHTML │ │ ├ ─ ─ the login. XHTML │ │ ├ ─ ─ master. XHTML │ │ └ ─ ─ the template. The XHTML. Template │ ├ ─ ─ tests │ │ ├ ─ ─ just set py │ │ ├ ─ ─ the functional │ │ └ ─ ─ models │ └ ─ ─ websetup │ ├ ─ ─ just set py │ ├ ─ ─ the bootstrap. Py │ └ ─ ─ schema. Py └ ─ ─ tgdemo. An egg - info ├ ─ ─ PKG - info ├ ─ ─ SOURCES. TXT ├ ─ ─ dependency_links. TXT ├ ─ ─ entry_points. TXT ├ ─ ─ not zip - safe ├ ─ ─ the requires the. TXT └ ─ ─ top_level. TXT 22 directories, 48 filesCopy the code
Above 👆 is the code form generated in python package form, which is easy to package and distribute.
The user only needs to modify the code in the directory below
TurboGears example use
from wsgiref.simple_server import make_server
from tg import expose, TGController, AppConfig
class RootController(TGController):
@expose()
def index(self):
return "<h1>Hello World</h1>"
config = AppConfig(minimal=True, root_controller=RootController())
print "Serving on port 8080..."
httpd = make_server(' ', 8080, config.make_wsgi_app())
httpd.serve_forever()
Copy the code
web2py
Web2py is an open source framework that allows developers to quickly create dynamic, interactive websites. It is designed to eliminate the many repetitive programming tasks that slow development, such as creating basic tables. It was originally developed as a tool. It was later imitated by Django and Ruby on Rails, a Ruby framework. Like TurboGears, it uses an MVC architecture.
The original source code was released by Massimo DiPierro in 2007. At the time, it was called the Enterprise Web Framework(EWF). It changed its name several times due to naming conflicts before settling on the current web2py in version 1.16. Applications developed using Web2py include Movuca, a content management system, NoobMusic, a web diagnostic tool called LinkFindr, and Instant Press, a blogging platform. In 2011, Web2py was named the best open source development software, winning the Bossie Award. The following year, it won InfoWorld’s Technology of the Year award.
Like Django, Web2py has extensive documentation. New and advanced developers can download its full development manual for free.
Some of web2py’s benefits include:
- Easy to use – as a robust full-stack framework, it works without dependencies, is easy to learn and deploy, doesn’t require any configuration files to install, and is ready to use once downloaded and installed. Developers get a database, a Web-based IDE, a Web server, and a powerful API with multiple core objects.
- Excellent security – Web2py’s templating language reduces the risk of hackers using cross-site scripting, the abstraction layer checks form field validity when creating forms, avoids SQL injection, and prevents cross-site request forgery attacks (CSRF attacks). Sessions are stored on the server, preventing bad actors from messing up browser cookies, and each password is hashed.
Some disadvantages of Web2py include:
- It is not easy to use the administrative interface on a regular basis
- The managed interface does not have permissions
- There is no built-in unit test support
- Development is fast, all functions have default behavior, forms are automatically generated, high-level widgets and application grids are built in
The directory structure
project/ README LICENSE VERSION > this web2py version web2py.py > the startup script anyserver.py > to run with third party servers ... > other handlers and example files gluon/ > the core libraries packages/ > web2py submodules dal/ contrib/ > third party libraries tests/ > unittests applications/ > are the apps admin/ > web based IDE ... examples/ > examples, docs, links ... welcome/ > the scaffolding app (they all copy it) ABOUT LICENSE models/ views/ controllers/ sessions/ errors/ cache/ static/ uploads/ modules/ cron/ tests/ ... > your own apps examples/ > example config files, mv .. and customize extras/ > other fileswhich are required for building web2py
scripts/ > utility and installation scripts
handlers/
wsgihandler.py > handler to connect to WSGI
... > handlers for Fast-CGI, SCGI, Gevent, etc
site-packages/ > additional optional modules
logs/ > log files will go in there
deposit/ > a place where web2py stores apps temporarily
Copy the code
Flask
Flask is a Python microframework based on Jinja2 and Werkzeug. Like other frameworks, it is BSD licensed, a free software license with a few restrictions. Flask’s sites include LinkedIN and Pinterest. Flask has the following characteristics:
- Built-in unit testing support
- The template uses Jinjia2
- A large number of documents
- The client session uses security cookies
- Development server and debugger
- A Restful request
- Compatibility with WSGI 1.0
- Based on unicode
- Lots of extensions
Flask is a young framework, born in 2010. The goal of Flask is not to impose restrictions on programmers, and to allow you to use your own database object relational mapping, template engine, session middleware, and other components you need for your projects, which I think is the purpose of this micro-framework.
I would say that frameworks like Flask are more suitable for experienced developers and not necessary for small scale applications, of course, if you just want to make a simple REST API, then Flask is great.
Flask I personally use a lot of. Flask is a micro-framework that is flexible and suitable for small projects.
The sample code
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello(a):
return 'Hello, World! '
Copy the code
Flask’s ecology is also full and the community is very active, with resources available at 👇 below. The document
Flask: A collection of excellent resources
Bottle
Like Flask, Bottle is a server Gateway Interface (WSGI) networking framework. As a file, it does not depend on any libraries outside the Python standard library. When Marcel Hellkamp wrote it in 2009, it started with minimal tools that included templates, routing, and a WSGI abstraction layer. This small and powerful framework is great for programmers looking for flexibility and basic functionality, building simple applications and websites, and creating a Web API.
Its advantages include:
- Built-in fast templatingengine and support for Jinja2,Mako, and Cheetah
- Lots of tools to access uploads,cookies, form data, titles, and other metadata
- Support for fAPWS3,Google App Engine, cherrypaste built-in HTTP development server
- Support dynamic URLs
Another advantage of Bottle is that it is compact and easy to embed in a larger application without worrying about system dependencies. If you want to create small applications with a simple, clean and fast framework that doesn’t have too much redundancy, Bottle is for you.
Sample code:
from bottle import route, run, template
@route('/hello/<name>')
def index(name):
return template('Hello {{name}}! ', name=name)
run(host='localhost', port=8080)
Copy the code
The document
Many frameworks
Hard as it may be to believe, these are just a few dozen developers who can use the Python framework. Python.org reports that Django,TurboGears, and Web2py are the most popular full options. To provide a comprehensive look at framework optimization, here we have added two advanced forms of micro-frameworks. Other frameworks with an enthusiastic following include Pyramid, Web. py, Bobo, Albatross, and CherryPy.
Find the right fit
The choice of the right framework depends on the size of the project, its communication requirements, whether it is a stand-alone application, the level of customization requirements, overhead, and many other factors. Equally important, it depends on which framework fits your personal way of working. Review these projects and download their latest version. Before planning a major trip, take a test drive to make sure you’re in the right vehicle.