This is a memo for myself, the details are as follows.
Don't think it's difficult, first try until Hello World is displayed in the browser.
#pip install to import
$ pip install bottle
# -*- coding:utf-8 -*-
from bottle import route, run
@route('/hello')
def hello():
return "Hello World!"
run(host='localhost', port=8080, debug=True)
Run it in PyCharm and open it in your browser. http://localhost:8080/hello
Now you can see Hello World in your browser !! Amazing Bottle
# -*- coding:utf-8 -*-
from bottle import route, run
@route('/hello/')
@route('/hello/<user>')
def hello(user="taro"):
return "Hello {user}".format(user=user)
@route('/date/<month:re:[a-z]+>/<day:int>/<path:path>')
def date(month, day, path):
return "{month}/{day} {path}".format(month=month, day=day, path=path)
run(host='localhost', port=8080, debug=True)
By defining the default in the argument of the method to be executed, it can be displayed even when user is not passed. http://localhost:8080/hello/
http://localhost:8080/hello/masaibar
Simple value constraints can be given.
http://localhost:8080/date/april/29//dev/null
It seems that there are two ways to write annotations such as GET and POST. @get ('/ hoge') and @route ('/ hoge', method ='GET') have the same behavior.
The passed value is received by referring to the following variables.
In addition, you can receive various data such as headers and request body. Reference
# -*- coding:utf-8 -*-
from bottle import route, run
from bottle import get, post, request
@route('/login', method='GET') # or @get('/login')
def login():
username = request.query.get('user')
password = request.query.get('pass')
#Username when nothing is passed in GET,Don't put anything in password
username = "" if username is None else username
password = "" if password is None else password
return '''
<form action="/login" method="post">
Username: <input name="username" type="text" value="{username}"/>
Password: <input name="password" type="password" value="{password}"/>
<input value="Login" type="submit" />
</form>
'''.format(username=username, password=password)
@route('/login', method='POST') # or @post('/post')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
return "{username} {password}".format(username=username, password=password)
run(host='localhost', port=8080, debug=True)
GET http://localhost:8080/login
http://localhost:8080/login?user=hoge&pass=fuga
POST
# -*- coding:utf-8 -*-
from bottle import route, run
from bottle import error
@route('/hello')
def hello():
return "Hello World!"
@error(404)
def error404(error):
return "Nothing here sorry {error}".format(error=error)
run(host='localhost', port=8080, debug=True)
I deliberately step on a URL that doesn't exist. http://localhost:8080/hellow
To be honest, I was surprised that it moved easier than I expected. This time, I focused on moving it without thinking about templates, but when I looked it up, I felt that there were quite a few people using only the template engine. (Is Jinja2 popular?)
Tutorial — Bottle 0.13-dev documentation : http://bottlepy.org/docs/dev/tutorial.html Easy introduction of 6 kinds of Python web frameworks --Mojilog: http://mojix.org/2013/04/13/python-six-wafs Master the Request / Response object for Bottle-Qiita: http://qiita.com/tomotaka_ito/items/62fc4d58d1be7867a158 Comparison of None in Python is using is not the == - is Hello Hello monmon! : http://monmon.hateblo.jp/entry/20110214/1297710749
Recommended Posts