flask 下载 扩展名
Last week I was writing a talk to give at Google Developers Bus and I needed to show how to integrate Flask and Google Maps API, as I did not found any extension to Google Maps I decided to create one.
上周,我在Google Developers Bus上发表演讲,我需要展示如何集成Flask和Google Maps API,因为我没有找到我决定创建的Google Maps扩展。
One of the best things in Flask is the way it is extended by Extensions and Blueprints, by the way, Blueprints is one of the best idea I’ve seem in Python web frameworks, once you start working with Blueprints you want to use it everywhere (but unfortunatelly not every framework has an ellegant way to be extended)
Flask最好的事情之一是扩展和蓝图对它的扩展方式,顺便说一句, 蓝图是我在Python Web框架中出现的最好的主意之一,一旦开始使用蓝图,就想在任何地方使用它(但不幸的是,并非每个框架都有一种优雅的方式可以扩展)
I will start showing how the extension works and then I will explain how to build it from scratch.
我将开始展示扩展的工作原理,然后再说明如何从头开始构建它。
烧瓶谷歌地图 (Flask Google Maps)
Template filter and a template global to create Google Maps from latitude and longitude
模板过滤器和全局模板 ,可根据经纬度创建Google地图
正在安装 (Installing)
pip install flask-googlemaps
pip install flask-googlemaps
正在加载您的应用 (Loading in your app)
from flask import Flask from flask.ext.googlemaps import GoogleMaps app = Flask(__name__) GoogleMaps(app)
from flask import Flask from flask.ext.googlemaps import GoogleMaps app = Flask(__name__) GoogleMaps(app)
在模板中使用 (Using in templates)
Parameters:
参数:
- identifier: The name tat will be used to identify your map (you can have multiple maps in one page) - lat: latitude to center the map - lng: Longitude to center the map - markers: a list of tuples, each tuple is a (lat, lng) marker (a pointer in the map) - zoom: percentage of the zoom - maptype: Google map type, TERRAIN or ROADMAP. defaults to ROADMAP - varname: The JS varname to bind the map, defaults to "map" - style: css style to be appended to the < div > - cls: css class to the map < div >
- identifier: The name tat will be used to identify your map (you can have multiple maps in one page) - lat: latitude to center the map - lng: Longitude to center the map - markers: a list of tuples, each tuple is a (lat, lng) marker (a pointer in the map) - zoom: percentage of the zoom - maptype: Google map type, TERRAIN or ROADMAP. defaults to ROADMAP - varname: The JS varname to bind the map, defaults to "map" - style: css style to be appended to the < div > - cls: css class to the map < div >
TODO: In near future it will be possible to pass an address as argument
TODO:在不久的将来,可以将地址作为参数传递
例 (Example)
模板 (The template)
输出 (The output)
![map](http://brunorocha.org/mediafiles/image/2013/11/Flask_Google_Maps_Example_58870.png)
Github (Github)
Screenshots and docs on Github.
Github上的屏幕截图和文档。
https://github.com/rochacbruno/Flask-GoogleMaps
https://github.com/rochacbruno/Flask-GoogleMaps
如何创建烧瓶扩展 (How to create a Flask Extension)
Flask is extendable by two patterns Extension and Blueprint
Flask可通过两种模式扩展:扩展和蓝图
An Extension is something like a complete plugin, a distribution containing models, views, templates, static files, template globals and filters etc and usually an Extension is built of Blueprints, which is an app prototype, it can define the way an app will be when registered, exposing resources, and url rules, also the Blueprint has the capability to access the current running application to contribute with things like config values, template filters, etc.
扩展程序就像一个完整的插件,包含模型,视图,模板,静态文件,模板全局变量和过滤器等的分布,通常,扩展程序是由Blueprints构建的,Blueprints是应用程序原型 ,它可以定义应用程序的开发方式注册后,公开资源和url规则,Blueprint即可访问当前正在运行的应用程序,以提供诸如配置值,模板过滤器之类的功能。
In the Flask docs there is a great explanation on Extensions and Blueprints
扩展手术 (Anathomy of an extension)
An extension is just a Python package following the naming convention Flask_anything, with packages naming like that Flask will automatically find them in the Python PATH via the ext proxy. So instead of from flask_anything import something you can do from flask.ext.anything import something in that way the code will be very clear and explicit and you know you are dealing with a Flask extension.
扩展名只是遵循命名约定Flask_anything的Python软件包,类似Flask的软件包将通过ext代理在Python PATH中自动找到它们。 因此,而不是从flask_anything进口的东西你可以从flask.ext.anything以这种方式进口的东西做的代码会非常清晰和明确的,你知道你正在处理的烧瓶延伸。
烧瓶什么 (Flask-Anything)
root folder |__ flask_anything/ |__ templates/ |__ static/ |__ __init__.py |__ __some_module.py |__ * |__ setup.py
root folder |__ flask_anything/ |__ templates/ |__ static/ |__ __init__.py |__ __some_module.py |__ * |__ setup.py
That is it! you can write really anything you want and it will be available throught from flask.ext.anything.some_module import FooBar
这就对了! 您可以写任何您想要的东西 ,并且可以从flask.ext.anything.some_module import FooBar中获得
里面是什么? (What is inside?)
Usually extensions expose a main Class which will be registered in your app, there is no rule, but there is some conventions, an example:
通常,扩展会公开一个将在您的应用程序中注册的主类, 没有规则 ,但是有一些约定,例如:
By the community convention your extension main class should receive its configurations in
__init__
method and should have a lazy way to init defined as a method calledinit_app
, also is a good practice to create methods for things likecreate_blueprint
,register_blueprint
,get_url_rules
and also arender_template
method inside your Blueprint is usefull. That is because others could extend your class and overwrite them, in example to use Flask-Themes it is usefull to overwrite therender_template
method.按照社区惯例,您的扩展主类应在
__init__
方法中接收其配置,并应具有一种被称为init_app
的方法定义的惰性初始化方法,也是一种为诸如create_blueprint
,register_blueprint
,get_url_rules
和render_template
类的方法创建方法的好习惯蓝图中的方法很有用。 那是因为其他人可以扩展您的类并覆盖它们,例如,使用Flask-Themes覆盖render_template
方法很有用。
使用扩展程序/蓝图 (Using your extension/Blueprint)
# your_app.py from flask import Flask from flask.ext.anything.some_module import Anything app = Flask(__name__) # option 1 Anything(app) # option 2 anything = Anything() anything.init_app(app)
# your_app.py from flask import Flask from flask.ext.anything.some_module import Anything app = Flask(__name__) # option 1 Anything(app) # option 2 anything = Anything() anything.init_app(app)
With the above instantiation/init_app your app will be manipulated by the extension and the views, urls, template filters etc will be available.
使用上面的实例化/ init_app,您的应用将由扩展名操纵 ,并且视图,URL,模板过滤器等将可用。
There is more conventions to follow as state, namespace, resource access etc, but you can find all the information on Flask docs.
翻译自: https://www.pybloggers.com/2013/11/flask-google-maps-plus-how-to-write-a-flask-extension/
flask 下载 扩展名