開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
- パッケージ
- PostgreSQL (ORDBMS(object-relational database management system))
Head First Python 第2版 ―頭とからだで覚えるPythonの基本 (Paul Barry (著)、嶋田 健志 (監修)、木下 哲也 (翻訳)、オライリージャパン)の9章(コンテキストマネジメントプロトコル - with文を使う)、自分で考えてみよう(p. 355)を取り組んでみる。
コード(Emacs)
Python 3
from flask import Flask, render_template, request, escape
import vsearch
import psycopg2 as sql
from DBcm import UseDatabase
app = Flask(__name__)
dbconfig = {'host': '127.0.0.1',
'database': 'vsearchlogdb'}
def log_request(req: 'flask_request', res: str) -> None:
with UseDatabase(dbconfig) as cursor:
_sql = '''
select phrase, letters, ip, browser_string, result
from log
'''
cursor.execute(_sql)
contents = cursor.fetchall()
titles = ('phrase', 'letters', 'ユーザーエージェント', '結果')
return render_template('viewlog.html',
the_title='ログの閲覧',
the_row_titles=titles,
the_data=contents,)
conn.close()
@app.route('/')
@app.route('/entry')
def entry_page() -> str:
return render_template(
'entry.html',
the_title='Welcom to search4letters on the web!')
@app.route('/search4', methods=['POST'])
def do_search() -> str:
phrase = request.form['phrase']
letters = request.form['letters']
results = str(vsearch.search4letters(phrase, letters))
log_request(request, results)
title = '検索結果'
return render_template(
'results.html',
the_title=title,
the_phrase=phrase,
the_letters=letters,
the_results=results)
@app.route('/viewlog')
def view_the_log() -> str:
with UseDatabase(dbconfig) as cursor:
_sql = '''
select phrase, letters, ip, browser_string, results
from log
'''
cursor.execute(_sql)
contents = cursor.fetchall()
titles = ('phrase', 'letters', 'ユーザーエージェント', '結果')
return render_template('viewlog.html',
the_title='ログの閲覧',
the_row_titles=titles,
the_data=contents,)
if __name__ == '__main__':
app.run(debug=True)
入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))
$ python3 hello_flask.py
* Serving Flask app "hello_flask" (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 - - [10/Dec/2018 12:40:19] "GET /viewlog HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:40:20] "GET /favicon.ico HTTP/1.1" 404 -
* Detected change in '.../hello_flask.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 178-619-558
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog HTTP/1.1" 500 -
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File ".../hello_flask.py", line 59, in view_the_log
cursor.execute(_sql)
psycopg2.ProgrammingError: column "result" does not exist
LINE 2: select phrase, letters, ip, browser_string, result
^
HINT: Perhaps you meant to reference the column "log.results".
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:41:39] "GET /viewlog?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
* Detected change in '.../hello_flask.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 178-619-558
127.0.0.1 - - [10/Dec/2018 12:42:26] "GET /viewlog HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2018 12:42:26] "GET /static/hf.css HTTP/1.1" 404 -
C-c C-c$
0 コメント:
コメントを投稿