2017年8月12日土曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 10.(Function Decorators: Wrapping Functions)の SHARPEN YOUR PENCIL(No. 7163) を取り組んでみる。

SHARPEN YOUR PENCIL(No. 7163)

コード(Emacs)

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

from flask import Flask, render_template, request, session
from vsearch import search4letters

app = Flask(__name__)

app.secret_key = 'YouWillNeverGuess'


@app.route('/')
def hello() -> 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 results:'
    results = str(search4letters(phrase, letters))
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_results=results,
                           the_letters=letters)


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


@app.route('/login')
def do_login() -> str:
    session['logged_in'] = True
    return 'You are now logged in'

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

入出力結果(Terminal, IPython)

$ ./vsearch4web.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 873-014-066
127.0.0.1 - - [12/Aug/2017 19:38:07] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2017 19:38:09] "GET /login HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2017 19:38:10] "GET /login HTTP/1.1" 200 -
  C-c C-c$

0 コメント:

コメントを投稿