2018年10月3日水曜日

開発環境

  • macOS High Sierra - Apple
  • Emacs (Text Editor)
  • Python 3.7 (プログラミング言語)

Head First Python 第2版 ―頭とからだで覚えるPythonの基本 (Paul Barry (著)、嶋田 健志 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(Webアプリケーションの構築 - 現実に目を向ける)、テンプレートマグネット(p. 231)を取り組んでみる。

PythonAnywhere

コード(Emacs)

Python 3

#!/usr/bin/env python3
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():
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = '検索結果'
    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 entry_page() -> str:
    return render_template(
        'entry.html', the_title='Web版のsearch4lettersにようこそ!')


app.run(debug=True)

入出力結果(Terminal, Jupyter(IPython))

$ ./vsearch4web.py 
 * Serving Flask app "vsearch4web" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 178-619-558
127.0.0.1 - - [03/Oct/2018 14:16:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:16:43] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:16:43] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:16:45] "POST /search4 HTTP/1.1" 200 -
 * Detected change in '.../webapp/vsearch4web.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 178-619-558
127.0.0.1 - - [03/Oct/2018 14:17:32] "GET /search4 HTTP/1.1" 405 -
127.0.0.1 - - [03/Oct/2018 14:17:33] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:17:34] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:17:35] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:17:35] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:17:35] "GET /entry HTTP/1.1" 200 -
127.0.0.1 - - [03/Oct/2018 14:17:37] "POST /search4 HTTP/1.1" 200 -
  C-c C-c$ 

0 コメント:

コメントを投稿

関連コンテンツ