search
HomeBackend DevelopmentPython TutorialPython的Bottle框架的一些使用技巧介绍

之前对bottle做过不少的介绍,也写过一些文章来说明bottle的缺点,最近发现其实之前有些地方说的不太公平,所以趁此机会也来更正一下。

    bottle是支持类似flask url_for的语法的,具体使用方法在下文介绍
    bottle的request.query之类的参数默认是str类型,也是有原因的,比如我在给google做代理的时候,编码就不一定是utf8的,如果强制转化utf8就会报错
    之前的bug也得到了修正,比如mount(‘/x',app)之后,/x/和/x都可以访问到

OK,现在正式进入主题,我们来介绍一些bottle的一些高级使用


一. 智能创建url

这部分在bottle的文档上是没有介绍的(其实bottle明明实现了很多贴心的功能,不知道为啥都不写在文档上)。
在Bottle类里,有一个成员函数:

def get_url(self, routename, **kargs):
  """ Return a string that matches a named route """
  scriptname = request.environ.get('SCRIPT_NAME', '').strip('/') + '/'
  location = self.router.build(routename, **kargs).lstrip('/')
  return urljoin(urljoin('/', scriptname), location)
 
def get_url(self, routename, **kargs):
  """ Return a string that matches a named route """
  scriptname = request.environ.get('SCRIPT_NAME', '').strip('/') + '/'
  location = self.router.build(routename, **kargs).lstrip('/')
  return urljoin(urljoin('/', scriptname), location)

那么这个routename是哪里来的呢?看 route 装饰器的参数:

def route(self, path=None, method='GET', callback=None, name=None,
     apply=None, skip=None, **config):
 
def route(self, path=None, method='GET', callback=None, name=None,
     apply=None, skip=None, **config):

其中的name参数就是routename(这里不得不说一下,这种方式比flask要好些,要用才指定name,而不需要为了实现url_for,把整个框架都实现的很复杂)

所以看到这里大家也就明白了,bottle的url生成器是绑定在Bottle实例上的,所以跨实例访问默认是做不到的。
而可能由于bottle所推崇的micro化,所以其源码中特意对默认Bottle示例包装出了一个函数:

for name in '''route get post put delete error mount
        hook install uninstall'''.split():
  globals()[name] = make_default_app_wrapper(name)
url = make_default_app_wrapper('get_url')
del name
 
for name in '''route get post put delete error mount
        hook install uninstall'''.split():
  globals()[name] = make_default_app_wrapper(name)
url = make_default_app_wrapper('get_url')
del name

这样做的好处是,如果工程只用到默认的Bottle实例的话,在模板中就可以直接使用url,而不必再多传个Bottle实例进去。

更正一下,bottle的get_url是不能跨app调用的,比如被mount的app调用主app的get_url会错掉,因为此时的SCRIPT_NAME是当前页的path,所以拼装起来会乱掉,所以就不要尝试了。

但是怎么才能让模板能够访问到local变量呢?我们接下来介绍


二. 给模板指定默认的变量

因为笔者用的最多的是jinja2,所以模板相关的介绍都是以jinja2为例子.
由于bottle的很多实例都是使用的代理模式,如request,response,local,所以我们可以放心的将这些变量传入到模板默认变量里去。
代码也很简单:

from bottle import BaseTemplate

BaseTemplate.defaults.update(dict(
  request=request,
  local=local,
  )
)
 
from bottle import BaseTemplate
 
BaseTemplate.defaults.update(dict(
  request=request,
  local=local,
  )
)

有兴趣的话,大家也可以去直接看一下源码,很好懂


三. 给模板增加filters

还是以jinja2为例,直接给出代码如下:

from bottle import BaseTemplate

if 'filters' not in BaseTemplate.settings:
  BaseTemplate.settings['filters'] = {}

filters = BaseTemplate.settings['filters']

def urlencode_filter(params):
  '''
  urlencode
  '''
  from urllib import urlencode

  return urlencode(params)

filters.update(dict(
  urlencode=urlencode_filter,
  )
)
 
from bottle import BaseTemplate
 
if 'filters' not in BaseTemplate.settings:
  BaseTemplate.settings['filters'] = {}
 
filters = BaseTemplate.settings['filters']
 
def urlencode_filter(params):
  '''
  urlencode
  '''
  from urllib import urlencode
 
  return urlencode(params)
 
filters.update(dict(
  urlencode=urlencode_filter,
  )
)

OK,一共就是这些,这里基于的bottle版本是 0.10.9,如果有不相符的地方,请查看bottle版本。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

How do you debug shebang-related issues?How do you debug shebang-related issues?Apr 30, 2025 am 12:17 AM

The methods to debug the shebang problem include: 1. Check the shebang line to make sure it is the first line of the script and there are no prefixed spaces; 2. Verify whether the interpreter path is correct; 3. Call the interpreter directly to run the script to isolate the shebang problem; 4. Use strace or trusts to track the system calls; 5. Check the impact of environment variables on shebang.

How do you remove elements from a Python array?How do you remove elements from a Python array?Apr 30, 2025 am 12:16 AM

Pythonlistscanbemanipulatedusingseveralmethodstoremoveelements:1)Theremove()methodremovesthefirstoccurrenceofaspecifiedvalue.2)Thepop()methodremovesandreturnsanelementatagivenindex.3)Thedelstatementcanremoveanitemorslicebyindex.4)Listcomprehensionscr

What data types can be stored in a Python list?What data types can be stored in a Python list?Apr 30, 2025 am 12:07 AM

Pythonlistscanstoreanydatatype,includingintegers,strings,floats,booleans,otherlists,anddictionaries.Thisversatilityallowsformixed-typelists,whichcanbemanagedeffectivelyusingtypechecks,typehints,andspecializedlibrarieslikenumpyforperformance.Documenti

What are some common operations that can be performed on Python lists?What are some common operations that can be performed on Python lists?Apr 30, 2025 am 12:01 AM

Pythonlistssupportnumerousoperations:1)Addingelementswithappend(),extend(),andinsert().2)Removingitemsusingremove(),pop(),andclear().3)Accessingandmodifyingwithindexingandslicing.4)Searchingandsortingwithindex(),sort(),andreverse().5)Advancedoperatio

How do you create multi-dimensional arrays using NumPy?How do you create multi-dimensional arrays using NumPy?Apr 29, 2025 am 12:27 AM

Create multi-dimensional arrays with NumPy can be achieved through the following steps: 1) Use the numpy.array() function to create an array, such as np.array([[1,2,3],[4,5,6]]) to create a 2D array; 2) Use np.zeros(), np.ones(), np.random.random() and other functions to create an array filled with specific values; 3) Understand the shape and size properties of the array to ensure that the length of the sub-array is consistent and avoid errors; 4) Use the np.reshape() function to change the shape of the array; 5) Pay attention to memory usage to ensure that the code is clear and efficient.

Explain the concept of 'broadcasting' in NumPy arrays.Explain the concept of 'broadcasting' in NumPy arrays.Apr 29, 2025 am 12:23 AM

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

Explain how to choose between lists, array.array, and NumPy arrays for data storage.Explain how to choose between lists, array.array, and NumPy arrays for data storage.Apr 29, 2025 am 12:20 AM

ForPythondatastorage,chooselistsforflexibilitywithmixeddatatypes,array.arrayformemory-efficienthomogeneousnumericaldata,andNumPyarraysforadvancednumericalcomputing.Listsareversatilebutlessefficientforlargenumericaldatasets;array.arrayoffersamiddlegro

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.