Why use bottle? Because simple, just a py file, and other modules are not dependent on more than 3,000 lines of code.
http://www.bottlepy.org/docs/dev/
Now that you have started learning, install it.
PIP3 Install bottle
Ok
First code:
from Import Route,run,template@route ('/hello/<name>') def index (name): return template ('<b>hello {{name}}</b>! ', name=name) run (host='localhost', port=8080)
Run OK
From this code can I see, bottle although small, support is not bad, including route,template, etc., are supported.
And the last line of code run, in effect, is to start a built-in Web server. More than 3,000 lines of code, also including one this, very bad.
For a simple purpose, we follow up on learning, most of the time, using a module-level decorator route () to define the route. This will add routes to a global default program, and when the route () is first called, a process of bottle will be created.
In fact, the code is more like this;
from Import = Bottle () @app. Route ('/hello')def Hello (): return"Hello world! " run (app, host='localhost', port=8080)
Above, the route basically is static, below, we introduce the dynamic route:
Dynamic routing is more extensive than static routing, and matches more URLs at once:
For example/hello/<name> can match:/hello/wcf/hello/hanyu/hello/whoami but does not match:/hello/hello//hello/wcf/ddd
examples are as follows;
@route ('/wiki/<pagename>'# matches/wiki/learning_python def show_wiki_page (PAGENAME): @route ('/<action>/<user>' ) # Matches/follow/defnull def User_api (Action,
Here, you also need to pay attention to the dynamic routing of the filters (I call the match, there are currently 4 kinds, can add more), specifically:
: int matches (signed) digits only and converts the value to integer.
? : Float similar to:int but for decimal numbers.
? :p ath matches all characters including the slash character in a non-greedy a-and can be used-match more
than one path segment.
? : RE allows specify a custom regular expression in the Config field. The matched value is not modified
Example
from Import = Flask (__name__) @app. Route ('/user/<int:user_id>' def User (user_id): return ' hello,%d ' %user_idif__name__'__main__' : app.run (Debug=true)
#Coding:utf-8 fromFlaskImportFlask fromWerkzeug.routingImportBaseconverter#classes that define a regular converterclassRegexconverter (baseconverter):def __init__(self,url_map,*items): Super (Regexconverter, self).__init__(url_map) Self.regex=Items[0]app= Flask (__name__)#instantiation ofapp.url_map.converters['Regex']=Regexconverter@app.route ('/user/<regex ("([a-z]|[ A-z]) {4} "):username>', methods=['POST','GET'])defUser (username):return 'hello,%s'%usernameif __name__=='__main__': App.run (Debug=true)
The code is as follows;
Web Development with Bottle (1): Hello World