Python web framework bottle ultra clear usage example

From , 5 Years ago, written in Python, viewed 188 times.
URL https://pastebin.vip/view/9d86d83f
  1. #coding: utf-8
  2. from bottle import route, error, post, get, run, static_file, abort, redirect, response, request, template
  3.  
  4. @route('/')
  5. @route('/index.html')
  6. def index():
  7.     return '<a href="/hello">Go to Hello World page</a>'
  8.  
  9. @route('/hello')
  10. def hello():
  11.     return '<h1>HELLO WOLRD</h1>'
  12.  
  13. @route('/hello/:name')
  14. def hello_name(name):
  15.     page = request.GET.get('page', '1')
  16.     return '<h1>HELLO %s <br/>(%s)</h1>' % (name, page)
  17.  
  18. @route('/static/:filename')
  19. def serve_static(filename):
  20.     return static_file(filename, root='/home/arthur/workspace/my_python_codes/src/')
  21.  
  22. @route('/raise_error')
  23. def raise_error():
  24.     abort(404, "error...")
  25.  
  26. @route('/redirect')
  27. def redirect_to_hello():
  28.     redirect('/hello')
  29.  
  30. @route('/ajax')
  31. def ajax_response():
  32.     return {'dictionary': 'you will see ajax response right? Content-Type will be "application/json"'}
  33.  
  34. @error(404)
  35. def error404(error):
  36.     return '404 error !!!!!'
  37.  
  38. @get('/upload')
  39. def upload_view():
  40.     return """
  41.        <form action="/upload" method="post" enctype="multipart/form-data">
  42.          <input type="text" name="name" />
  43.          <input type="file" name="data" />
  44.          <input type="submit" name="submit" value="upload now" />
  45.        </form>
  46.        """    
  47.  
  48. @post('/upload')
  49. def do_upload():
  50.     name = request.forms.get('name')
  51.     data = request.files.get('data')
  52.     if name is not None and data is not None:
  53.         raw = data.file.read() # small files =.=
  54.         filename = data.filename
  55.         return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
  56.     return "You missed a field."
  57.  
  58. @route('/tpl')
  59. def tpl():
  60.     return template('test')
  61.  
  62. run(host='localhost', port=8000, reloader=True)
  63. #//python/8187

Reply to "Python web framework bottle ultra clear usage example"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website