2017年3月12日日曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 6.(Soring & Manipulating Data: Where To Put Your Data) の EXERCISE(No. 4741) を取り組んでみる。

EXERCISE(No. 4741)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, render_template, request

import vsearch

app = Flask(__name__)


@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = 'Here are your result:'
    results = str(vsearch.search4letters(phrase, letters))
    log_request(request, results)
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_letters=letters,
                           the_results=results)


@app.route('/')
@app.route('/entry')
def entr_page() -> 'html':
    return render_template('entry.html',
                           the_title='Welcome to search4letters on the web!')


def log_request(req: 'flask_request', res: str) -> None:
    with open('vsearch.log', 'a') as f:
        print(req, res, file=f)

if __name__ == '__main__':
    app.run(debug=True)

入出力結果(Terminal, IPython)

$ ./hello_flask.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 873-014-066
127.0.0.1 - - [12/Mar/2017 17:18:16] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Mar/2017 17:18:21] "POST /search4 HTTP/1.1" 200 -
  C-c C-c$ cat vsearch.log 
<Request 'http://localhost:5000/search4' [POST]> {'e', 'o', 'i', 'a'}
$

0 コメント:

コメントを投稿

Comments on Google+: