python3: How to use bottle (3)

How to use template.

Folder structure

$ tree
.
├── template.py
└── views
    └── sample.html

template.py


#! /usr/bin/python
#
#	template.py
#
#						Nov/12/2020
#
# ------------------------------------------------------------------
from bottle import run, route, template

# ------------------------------------------------------------------
@route("/")
def index():
    username = 'Natsume Soseki'
    return template('sample', username=username)
# ------------------------------------------------------------------
@route('/hello/<name>')
def hello(name="Stranger"):
    return template("Hello, {{ name }}.  How are you?", name=name)
# ------------------------------------------------------------------
if __name__ == "__main__":
    run(host='localhost', port=8080, reloader=True, debug=True)
# ------------------------------------------------------------------

views/sample.html


<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<title>Template engine</title>
</head>
<body>
<h2>Hello: {{ username }}</h2>
Nov/12/2020<br />
</body>
</html>

Server execution

$ ./template.py 
Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Access by client http://localhost:8080/ template01.png

http://localhost:8080/hello/太郎 template02.png

Recommended Posts

python3: How to use bottle (2)
python3: How to use bottle (3)
python3: How to use bottle
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
How to use Python bytes
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
Python: How to use pydub (playback)
How to use PubChem in Python
[Python] How to use Typetalk API
How to use xml.etree.ElementTree
How to use Python-shell
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to install and use pandas_datareader [Python]
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to install python
How to use partial
How to use Bio.Phylo
[Python] How to use import sys sys.argv
How to use x-means
How to use WikiExtractor.py
How to use IPython
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
How to use virtualenv
[Python2.7] Summary of how to use unittest
How to use Matplotlib
How to use iptables
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use TokyoTechFes2015
How to use dictionary {}
How to use Pyenv
How to use list []
How to use "deque" for Python data
How to use python-kabusapi
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
How to use OptParse
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use return