bottleの@routeの特殊な使い方。
ex3.py
# bottleの基本3
from bottle import *
import re
html='''
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<div class="container body-content">%s</div>
</body>
</html>'''
Carousel='''<div id="demo" class="carousel slide" data-ride="carousel" style="margin-top:1em;">
<div class="carousel-inner">
%s
</div>
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
'''
@route('/')
def home():
return html%"<h1>Hello World</h1>"
# イメージファイルの読み込み
@route('/<name:re:.*\.(jpg|png|bmp)>')
def image(name):
return static_file(name, root='./static/image')
# アイコンファイルの読み込み
@route('/favicon.ico')
def favcon():
return static_file('favicon.ico', root='./static')
# 静的ファイルの読み込み
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='./static')
HOST,PORT='localhost',8080
if __name__ =='__main__':
run(host=HOST,port=PORT)
sample.py
route('/<name:re:.*\.(jpg|png|bmp)>')
def image(name):
return static_file(name, root='./static/image')
上記の例は、正規化表現を用い(jpg|png|bmp)の拡張子を持つファイルを/staic/imageににルーティングするスニペットです。
http://localhost:8080/img0.jpgのurlでアクセスすると/static/image/img0.jpgをアクセスします。
static
├─image
│ img0.jpg http://localhost:8080/img0.jpg
│ img1.jpg http://localhost:8080/img1.jpg
│ img2.jpg http://localhost:8080/img2.jpg
│ img3.jpg http://localhost:8080/img3.jpg
│ img4.jpg http://localhost:8080/img4.jpg
│ img5.jpg http://localhost:8080/img5.jpg
@route('/favicon.ico')
def favcon():
return static_file('favicon.ico', root='./static')
このスニペットは、favicon.icoを/staicフォルダ内のfavicon.icoにルーティングします。
@route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='./static')
スニペットは、cssやjsファイルを/staticフォルダ以下のファイルパスにルーティングします。
<link rel="stylesheet" type="text/css" href="/static/content/bootstrap.min.css" />
<link href="static/content/jumbotron.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="/static/content/site.css" />
<script src="/static/scripts/modernizr-2.6.2.js"></script>
<script src="/static/scripts/jquery-1.10.2.min.js"></script>
<script src="/static/scripts/bootstrap.min.js"></script>
<script src="/static/scripts/respond.min.js"></script>
<script src="/static/scripts/mindmup-editabletable.js"></script>
上記の例は、cssは、/static/content以下にjsは、/static/scriptsにそれぞれルーティングします。