I've been playing with Tornado recently, so I'll show you how to use it.
This time, I will use a template. I just tried using it, but I didn't explain much. Expect next time. If you have any questions or requests, we will answer them.
My execution environment is as follows, but I think that there is no problem if it is an environment where Python works.
The template engine is a mechanism that synthesizes a template (template) and input data to create a character string. This time I'll limit myself to the web template engine in Python. In this case, the template engine is a mechanism that partially embeds Python code inside an HTML template and finally generates HTML. If you want to know a little difficult thing, you can refer to the following Wikipedia article or search on Google.
Seeing is believing, so let's start with an example using a template engine. Last time Let's modify the index.html used. The directory structure is the same as that of Last time. It's a very boring example, but let's display the current time.
The changes from the last time are as follows.
{{datetime.now ()}} \ </ p>
<!DOCTYPE html>
<html>
<head>
<title>Hello, world</title>
<link rel="stylesheet" href="{{ static_url("style.css") }}"/>
{% from datetime import datetime %}
</head>
<body>
<div id="container">
<div id="main">
<p>Hello, world</p>
<p>{{datetime.now()}}</p>
</div>
</div>
</body>
</html>
You can get the current time by accessing it with a browser.
In Python, the following code, which is familiar, is simply written directly in HTML by enclosing it in {% and%} (or {{and}}).
#Import datetime from datetime module
from datetime import datetime
# datetime.Get the current time with the now method
datetime.now()
It's late, but last time I used it a lot.
{{ static_url("style.css") }}
Was also a feature of the template engine.
It is replaced with the result of evaluating the character string enclosed in {% and%} (or {{and}}). It's easy. Tornado's template engine basically encloses Python code in {% and%} (or {{and}}) in an HTML file, and the evaluation result is embedded in the HTML.
You can also write control structures such as for, while, if, try-cxcept.
You can also define variables in the template.
I'll summarize it more properly later, but if you want to know more, see the Tornado manual tornado.template — Flexible output generation. Please give me.
From here, I will explain how to receive the calculation result of the server program (server.py). I will explain the post () method and get () method from the next time onward, so I will use it for the time being. Let's write a program that counts the number of characters that will be taken care of during job hunting.
$ tree --charset=x
.
|-- server.py
|-- static
| `-- style.css
`-- templates
|-- index.html
`-- result.html
The result of the POST request issued by index.html is received by the post () method, and the variable len_body is passed to result.html and displayed. It looks like a boring example, but if you can do it so far, you should be able to write Web applications in the heyday of CGI because you can pass calculation results by Python.
server.py
#!/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
def post(self):
body = self.get_argument('body')
len_body = len(body)
self.render("result.html",
len_body = len_body
)
application = tornado.web.Application([
(r"/", MainHandler)
],
template_path=os.path.join(os.getcwd(), "templates"),
static_path=os.path.join(os.getcwd(), "static"),
)
if __name__ == "__main__":
application.listen(8888)
print("Server is up ...")
tornado.ioloop.IOLoop.instance().start()
Delete the style attached to the p tag.
style.css
body {
font-family:'Lucida Grande', 'Hiragino Kaku Gothic ProN', 'Hiragino Kakugo ProN W3', "MS Gothic", sans-serif;
width: 80%;
margin: 0 auto;
}
Display the screen for entering the character string for which you want to calculate the number of characters in index.html.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Character count</title>
<link rel="stylesheet" href="{{ static_url("style.css") }}"/>
</head>
<body>
<div id="container">
<div id="main">
<form method="post" action="/">
<textarea name="body" cols="40" rows="4"></textarea>
<input type="submit">
</form>
</div>
</div>
</body>
</html>
Display the calculation result in result.html.
result.html
<!DOCTYPE html>
<html>
<head>
<title>Character count</title>
<link rel="stylesheet" href="{{ static_url("style.css") }}"/>
</head>
<body>
<div id="container">
<div id="main">
<p>{{len_body}}</p>
</div>
</div>
</body>
</html>
Certainly the number of characters could be counted. (The number of characters may vary depending on the environment.)
Extends the character count. If you feel like it, you may even write a similarity search by TF / IDF.
Recommended Posts