summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--static/hf.css71
-rw-r--r--templates/base.html12
-rw-r--r--templates/entry.html17
-rw-r--r--templates/results.html17
-rw-r--r--templates/viewlog.html22
-rw-r--r--vsearch4web.py68
6 files changed, 207 insertions, 0 deletions
diff --git a/static/hf.css b/static/hf.css
new file mode 100644
index 0000000..b02d621
--- /dev/null
+++ b/static/hf.css
@@ -0,0 +1,71 @@
+body {
+ font-family: Verdana, Geneva, Arial, sans-serif;
+ font-size: medium;
+ background-color: tan;
+ margin-top: 5%;
+ margin-bottom: 5%;
+ margin-left: 10%;
+ margin-right: 10%;
+ padding: 10px 10px 10px 10px;
+}
+a {
+ text-decoration: none;
+ font-weight: 600;
+}
+a:hover {
+ text-decoration: underline;
+}
+a img {
+ border: 0;
+}
+h2 {
+ font-size: 150%;
+}
+table {
+ margin-left: 20px;
+ margin-right: 20px;
+ caption-side: bottom;
+ border-collapse: collapse;
+}
+td, th {
+ padding: 5px;
+ text-align: left;
+}
+.copyright {
+ font-size: 75%;
+ font-style: italic;
+}
+.slogan {
+ font-size: 75%;
+ font-style: italic;
+}
+.confirmentry {
+ font-weight: 600;
+}
+
+/*** Tables ***/
+
+table {
+font-size: 1em;
+background-color: #fafcff;
+border: 1px solid #909090;
+color: #2a2a2a;
+padding: 5px 5px 2px;
+border-collapse: collapse;
+}
+
+td, th {
+border: thin dotted gray;
+}
+
+/*** Inputs ***/
+input[type=text] {
+ font-size: 115%;
+ width: 30em;
+}
+input[type=submit] {
+ font-size: 125%;
+}
+select {
+ font-size: 125%;
+}
diff --git a/templates/base.html b/templates/base.html
new file mode 100644
index 0000000..98eef27
--- /dev/null
+++ b/templates/base.html
@@ -0,0 +1,12 @@
+<!doctype html>
+<html>
+ <head>
+ <title>{{ the_title }}</title>
+ <link rel="stylesheet" href="static/hf.css" />
+ </head>
+ <body>
+ {% block body %}
+
+ {% endblock %}
+ </body>
+</html>
diff --git a/templates/entry.html b/templates/entry.html
new file mode 100644
index 0000000..c54a766
--- /dev/null
+++ b/templates/entry.html
@@ -0,0 +1,17 @@
+{% extends 'base.html' %}
+
+{% block body %}
+
+<h2>{{ the_title }}</h2>
+
+<form method='POST' action='/search4'>
+<table>
+<p>Use this form to submit a search request:</p>
+<tr><td>Phrase:</td><td><input name='phrase' type='TEXT' width='60'></td></tr>
+<tr><td>Letters:</td><td><input name='letters' type='TEXT' value='aeiou'></td></tr>
+</table>
+<p>When you're ready, click this button:</p>
+<p><input value='Do it!' type='SUBMIT'></p>
+</form>
+
+{% endblock %}
diff --git a/templates/results.html b/templates/results.html
new file mode 100644
index 0000000..462bfb0
--- /dev/null
+++ b/templates/results.html
@@ -0,0 +1,17 @@
+{% extends 'base.html' %}
+
+{% block body %}
+
+<h2>{{ the_title }}</h2>
+
+<p>You submitted the following data:</p>
+<table>
+<tr><td>Phrase:</td><td>{{ the_phrase }}</td></tr>
+<tr><td>Letters:</td><td>{{ the_letters }}</td></tr>
+</table>
+
+<p>When "{{the_phrase }}" is searched for "{{ the_letters }}", the following
+results are returned:</p>
+<h3>{{ the_results }}</h3>
+
+{% endblock %}
diff --git a/templates/viewlog.html b/templates/viewlog.html
new file mode 100644
index 0000000..8a47ccc
--- /dev/null
+++ b/templates/viewlog.html
@@ -0,0 +1,22 @@
+{% extends 'base.html' %}
+
+{% block body %}
+
+<h2>{{ the_title }}</h2>
+
+<table>
+ <tr>
+ {% for row_title in the_row_titles %}
+ <th>{{row_title}}</th>
+ {% endfor %}
+ </tr>
+ {% for log_row in the_data %}
+ <tr>
+ {% for item in log_row %}
+ <td>{{item}}</td>
+ {% endfor %}
+ </tr>
+ {% endfor %}
+</table>
+
+{% endblock %}
diff --git a/vsearch4web.py b/vsearch4web.py
new file mode 100644
index 0000000..3eed663
--- /dev/null
+++ b/vsearch4web.py
@@ -0,0 +1,68 @@
+from flask import Flask, render_template, request, escape
+from vsearch import search4letters
+
+from DBcm import UseDatabase
+
+app = Flask(__name__)
+
+app.config['dbconfig'] = {'host': '127.0.0.1',
+ 'user': 'vsearch',
+ 'password': 'vsearchpasswd',
+ 'database': 'vsearchlogDB', }
+
+
+def log_request(req: 'flask_request', res: str) -> None:
+ """Log details of the web request and the results."""
+
+ with UseDatabase(app.config['dbconfig']) as cursor:
+ _SQL = """insert into log
+ (phrase, letters, ip, browser_string, results)
+ values
+ (%s, %s, %s, %s, %s)"""
+ cursor.execute(_SQL, (req.form['phrase'],
+ req.form['letters'],
+ req.remote_addr,
+ req.user_agent.browser,
+ res, ))
+
+
+@app.route('/search4', methods=['POST'])
+def do_search() -> 'html':
+ """Extract the posted data; perform the search; return results."""
+ phrase = request.form['phrase']
+ letters = request.form['letters']
+ title = 'Here are your results:'
+ results = str(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 entry_page() -> 'html':
+ """Display this webapp's HTML form."""
+ return render_template('entry.html',
+ the_title='Welcome to search4letters on the web!')
+
+
+@app.route('/viewlog')
+def view_the_log() -> 'html':
+ """Display the contents of the log file as a HTML table."""
+ with UseDatabase(app.config['dbconfig']) as cursor:
+ _SQL = """select phrase, letters, ip, browser_string, results
+ from log"""
+ cursor.execute(_SQL)
+ contents = cursor.fetchall()
+ titles = ('Phrase', 'Letters', 'Remote_addr', 'User_agent', 'Results')
+ return render_template('viewlog.html',
+ the_title='View Log',
+ the_row_titles=titles,
+ the_data=contents,)
+
+
+if __name__ == '__main__':
+ app.run(debug=True)