flask 下载 扩展名_Flask Google Maps(以及:如何编写Flask扩展名)

news/2024/7/10 1:53:45 标签: python, java, vue, 设计模式, linux

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

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

在Flask文档中,对扩展和蓝图有很好的解释

扩展手术 (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 called init_app, also is a good practice to create methods for things like create_blueprint, register_blueprint, get_url_rules and also a render_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 the render_template method.

按照社区惯例,您的扩展主类应在__init__方法中接收其配置,并应具有一种被称为init_app的方法定义的惰性初始化方法,也是一种为诸如create_blueprintregister_blueprintget_url_rulesrender_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.

状态名称空间资源访问等还有更多约定,但您可以在Flask文档中找到所有信息。

翻译自: https://www.pybloggers.com/2013/11/flask-google-maps-plus-how-to-write-a-flask-extension/

flask 下载 扩展名


http://www.niftyadmin.cn/n/983522.html

相关文章

库、表、超级表是什么?怎么用?60后大叔抽丝剥茧讲清TDengine的数据建模

视频教程第二弹&#xff0c;快速理清TDengine中的抽象概念&#xff0c;并学会规划生产场景中的数据模型。 点击链接&#xff0c;获取视频教程。 欢迎来到物联网的数据世界 在典型的物联网场景中&#xff0c;一般有多种不同类型的采集设备&#xff0c;采集多种不同的物理量&a…

性能优化总结(六):预加载、聚合SQL应用实例

前面已经把原理都讲了一遍&#xff0c;这篇主要是给出一个应用的实例。该实例取自GIX4&#xff0c;比较复杂。 领域模型&#xff1a; 领域模型间的关系&#xff0c;如下&#xff1a; 右边模型链的具体关系在《第二篇》中已经描述过&#xff0c;不再赘述。 本次重点在于红线框住…

只会Java不用愁,Connector来拯救,一本正经演示如何用好 taos-jdbcdriver

TDengine 为了方便 Java 应用使用&#xff0c;提供了遵循 JDBC 标准(3.0)API 规范的 taos-jdbcdriver 实现。目前可以通过 Sonatype Repository 搜索并下载。 https://search.maven.org/artifact/com.taosdata.jdbc/taos-jdbcdriver 由于 TDengine 是使用 C 语言开发的&#…

Windows 服务器使用FTP出现“当前的安全设置不允许从该位置下载文件 警告

为了安全&#xff0c;Windows服务器默认禁用了从网上下载文件的安全选项。此错误即是出现在这里&#xff0c;所以打开这个选项即可&#xff1a; 控制面板 —— Internet选项 ——安全 —— 找到“自定义级别” —— 找到“下载”项&#xff0c;其中有“文件下载”勾选框&…

Android Guts: Intro to Loopers and Handlers

One of the reasons I love Android API is because it contains so many useful little things. Many of them are not even specific to the platform and I really miss them in Java SE. Today I’d like to give a short introduction to two nice classes – Looper and …

Perl 实现简单的html 标签筛选

此程序提供简单的获取html 页面代码并筛选出以下标签和一些基本属性&#xff1a; <script> : 属性 src&#xff0c; type <a> : 属性 href <img>: 属性 src 后续会添加一些更有用的功能&#xff0c;并逐步完善命令行接口。 使用方法&#xff1a; perl…

django管理界面美化_非人员用户的Django管理界面

django管理界面美化A while back I had a Django application in which I needed registered users able to view, create, update and delete objects in my system. These objects were instances of only a subset of all the Django models.Model subclasses I had defined…

涛思数据与优锘科技达成战略合作,可视化携手大数据引领物联网科技创新

12月17日&#xff0c;涛思数据与北京优锘科技有限公司&#xff08;以下简称“优锘科技”&#xff09;在京举行战略合作签约仪式。未来&#xff0c;双方将发挥各自技术优势&#xff0c;在产品创研、业务开发、产业推动等方面展开全面合作&#xff0c;共同推动在电力能源、智慧园…