Python的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版本。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
