Web Development with Bottle (1): Hello World

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.