2017年3月10日金曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 5.(Building a Webapp: Getting Real) の TEMPLATE MAGNETS(No. 4411) を取り組んでみる。

TEMPLATE MAGNETS(No. 4411)

コード(Emacs)

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

from flask import Flask, render_template, request

import vsearch

app = Flask(__name__)


@app.route('/')
def hell() -> str:
    return 'Hello world from Flask!'


@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))
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_letters=letters,
                           the_results=results)


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

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 - - [11/Mar/2017 01:33:02] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2017 01:33:04] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2017 01:33:04] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2017 01:33:44] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2017 01:33:53] "POST /search4 HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2017 01:34:01] "POST /search4 HTTP/1.1" 200 -
  C-c C-c$  

0 コメント:

コメントを投稿