Skip to content

Commit cdfa3c1

Browse files
committed
Adding Flask information, bookdb exercise, etc.
1 parent 5e4c107 commit cdfa3c1

11 files changed

Lines changed: 258 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Minimal Flask + forms demo
3+
4+
Send HTML page that echoes message from HTTP request
5+
To get started, point browser at echo_flask.html
6+
"""
7+
8+
from flask import Flask, render_template, request
9+
import copy
10+
11+
# No need for message page
12+
# Flask converts view function return string to HTML page
13+
14+
app = Flask(__name__)
15+
16+
app.debug = True # development only - remove on production machines
17+
18+
messages = []
19+
20+
# View functions generate HTTP responses including HTML pages and headers
21+
22+
@app.route('/echo_flask.html')
23+
def form():
24+
return render_template('form.html')
25+
# return form_page
26+
27+
@app.route('/echo_flask.py')
28+
def message_page():
29+
# Flask Quickstart suggests request.form should work, but here it is empty
30+
# Flask converts return string to HTML page
31+
messages.insert(0,request.args['message'])
32+
return render_template('response.html', messages=copy.deepcopy(messages))
33+
# return 'Message: %s' % ("<br> Previous Message : ".join(messages))
34+
35+
# No function needed for other routes - Flask will send 404 page
36+
37+
if __name__ == '__main__':
38+
app.run()
39+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Demonstrate Flask test support: test without server
3+
"""
4+
5+
import echo_flask as e
6+
7+
# Flask test support
8+
tc = e.app.test_client()
9+
print tc
10+
print
11+
print
12+
13+
# should respond with 404
14+
r = tc.get('/')
15+
print r
16+
print r.data
17+
print
18+
print
19+
20+
# should respond with form
21+
r = tc.get('/echo_flask.html')
22+
print r
23+
print r.data
24+
print
25+
print
26+
27+
# should respond with message page
28+
r = tc.get('/echo_flask.py?message=Hello')
29+
print r
30+
print r.headers
31+
print r.data
32+
print
33+
print
34+
35+
# This will fail if application isn't working
36+
#assert 'Hello' in r.data # demonstrate success
37+
assert 'Goodbye' in r.data # demonstrate failure
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
flask_lab.txt
2+
+--
3+
4+
Week 7 Lab: Flask
5+
6+
Install Flask on your computer
7+
8+
Revise echo_flask.py to show all the messages received since startup.
9+
Suggestion: experiment with formatting the output page. What if
10+
you return a big string? HTML? Does the templating system help?
11+
12+
To install, consult http://flask.pocoo.org/docs/installation/
13+
-- but I didn't take their advice. I do not recommend virtualenv
14+
I do not have pip installed so I tried sudo easy_install Flask
15+
worked for me, installed Flask 0.8, Jinja2 2.6, Werkzeug 0.8.3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!-- This template is really just a static HTML page -->
2+
<title>Echo request</title>
3+
</head>
4+
<body>
5+
<form method="GET" action="echo_flask.py">
6+
Message: <input type="text" name="message" size="40">
7+
<input type="submit" value="Submit">
8+
</form>
9+
</body>
10+
</html>

WebProgramming/booksPage/__init__.py

Whitespace-only changes.

WebProgramming/booksPage/bookdb.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
From Brian Dorsey's Internet Programming in Python, Winter 2011
3+
"""
4+
5+
class BookDB():
6+
def titles(self):
7+
titles = [dict(id=id, title=database[id]['title']) for id in database.keys()]
8+
return titles
9+
10+
def title_info(self, id):
11+
return database[id]
12+
13+
14+
# let's pretend we're getting this information from a database somewhere
15+
database = {
16+
'id1' : {'title' : 'CherryPy Essentials: Rapid Python Web Application Development',
17+
'isbn' : '978-1904811848',
18+
'publisher' : 'Packt Publishing (March 31, 2007)',
19+
'author' : 'Sylvain Hellegouarch',
20+
},
21+
'id2' : {'title' : 'Python for Software Design: How to Think Like a Computer Scientist',
22+
'isbn' : '978-0521725965',
23+
'publisher' : 'Cambridge University Press; 1 edition (March 16, 2009)',
24+
'author' : 'Allen B. Downey',
25+
},
26+
'id3' : {'title' : 'Foundations of Python Network Programming',
27+
'isbn' : '978-1430230038',
28+
'publisher' : 'Apress; 2 edition (December 21, 2010)',
29+
'author' : 'John Goerzen',
30+
},
31+
'id4' : {'title' : 'Python Cookbook, Second Edition',
32+
'isbn' : '978-0-596-00797-3',
33+
'publisher' : 'O''Reilly Media',
34+
'author' : 'Alex Martelli, Anna Ravenscroft, David Ascher',
35+
},
36+
'id5' : {'title' : 'The Pragmatic Programmer: From Journeyman to Master',
37+
'isbn' : '978-0201616224',
38+
'publisher' : 'Addison-Wesley Professional (October 30, 1999)',
39+
'author' : 'Andrew Hunt, David Thomas',
40+
},
41+
}
42+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
From Brian Dorsey's Internet Programming in Python, Winter 2011
3+
"""
4+
5+
import bookdb
6+
7+
def test_list_books():
8+
books = bookdb.BookDB()
9+
titles = books.titles()
10+
assert len(titles) > 1
11+
print titles
12+
for title in titles:
13+
assert 'title' in title
14+
assert 'id' in title
15+
16+
def test_get_book_info():
17+
books = bookdb.BookDB()
18+
titles = books.titles()
19+
id = titles[0]['id']
20+
print id
21+
info = books.title_info(id)
22+
print info
23+
assert 'title' in info
24+
assert info['title'] == titles[0]['title']
25+
assert 'publisher' in info
26+
assert 'isbn' in info
27+
assert 'author' in info
28+

WebProgramming/booksPage/books.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Minimal Flask + forms demo
3+
4+
Send HTML page that echoes message from HTTP request
5+
To get started, point browser at echo_flask.html
6+
"""
7+
8+
from flask import Flask, render_template, request
9+
from bookdb import BookDB
10+
11+
# No need for message page
12+
# Flask converts view function return string to HTML page
13+
14+
app = Flask(__name__)
15+
16+
app.debug = True # development only - remove on production machines
17+
18+
booksDB = BookDB ()
19+
20+
# View functions generate HTTP responses including HTML pages and headers
21+
22+
@app.route('/books.html')
23+
def bookList():
24+
return render_template('bookslist.html',titles = booksDB.titles())
25+
# return form_page
26+
27+
@app.route('/abook/<book_id>')
28+
def bookInfo(book_id):
29+
return render_template('book.html', thisBook=booksDB.title_info(book_id))
30+
31+
# return from_page
32+
#===============================================================================
33+
# @app.route('/echo_flask.py')
34+
# def message_page():
35+
# # Flask Quickstart suggests request.form should work, but here it is empty
36+
# # Flask converts return string to HTML page
37+
# messages.insert(0,request.args['message'])
38+
# return render_template('response.html', messages=copy.deepcopy(messages))
39+
# # return 'Message: %s' % ("<br> Previous Message : ".join(messages))
40+
#===============================================================================
41+
42+
# No function needed for other routes - Flask will send 404 page
43+
44+
if __name__ == '__main__':
45+
app.run()
46+

WebProgramming/booksPage/books.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
books.txt
2+
+--
3+
4+
Week 7 Assignment: Books web site
5+
6+
- Write a multi page website, using bookdb.py
7+
- Index page is a list of books, link to a detail page per book
8+
- Each detail page displays info about one book
9+
- Use any Python tech you’d like
10+
11+
(from Brian Dorsey's Internet Programming in Python, Winter 2011)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- This template is really just a static HTML page -->
2+
<title>Book List</title>
3+
</head>
4+
<body>
5+
<h1>{{ thisbook['title] }} : </h1>
6+
<div style="word-wrap: break-word; margin:20px">
7+
<ul id="navigation">
8+
{% for item in thisBook %}
9+
<li>{{ thisBook['item'] }} </li>
10+
{% endfor %}
11+
</ul>
12+
</div>
13+
</form>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)