3. Meetup Details
Will be hosted once a month at Mango/Scisys - Methuen Park
Presentations on various topics. If you're interested in doing one
then propose it on the forum
Meetup group
http://www.meetup.com/Chippenham-Tech-Chat/
Google Groups Forum
https://groups.google.com/forum/?fromgroups#!
forum/chippenhamtechchat
5. Overview
● A brief history of web applications
● Past experience
● An overview of lightweight web frameworks
● An introduction to one web framework and
it's features
● I won't go into
6. Web Frameworks - A Brief History
● CGI and Perl - circa 1993
● PHP, Coldfusion - circa 1995
● ASP - circa 1998
● JSP and Servlet Spec - circa 1999
● Struts - circa 2001
● Rails - circa 2005
How many are there today?
8. Used Java Servlets and JSP around 1999/2000.
First introduction - somewhat painful.
Then discovered Struts in 2001/2002 (pre 1.0
release). Vowed never to go back to JSP hell.
In 2005/2006 we migrated our legacy Struts apps
to Struts 2. Better still.
SpringMVC came in around the same time.
Past Experience - Cathedrals
10. Past Experience - Post Java
Then looked at PHP for doing more lightweight
work. Used CakePHP 1.x then looked at
CodeIgniter.
Used Dojo (pre 1.0 release) and Velocity to
build a rich client Javascript application.
11. Past Experience - First One Pager
Then ended up building a rich client interface in
Google Web Toolkit 1.x
Then around 2007 went along to the Vancouver
Ruby/Rails meetup. Talked about Rails/Merb
then someone mentioned Sinatra.
Then picked up Flask, looked at Ratpack in
Geecon this year
14. Here's what we would have have done back in
the day with Apache Struts...
15. 1. Setup web.xml
2. Created an index.jsp to forward on to my app
3. Setup struts-config.xml and added a form
and action mapping detailing my Action class
4. Create an Action & Action Form class
16. 5. Setup my forward on success
6. Put all this in a predefined folder structure
7. Package it all up into a war file
8. Deploy to Tomcat and Start
17. then....
9. Fix the stupid errors
10. Deploy again and see Hello World in my
browser. Maybe.
20. There's others !!
There's a bunch of other lightweight web
frameworks in various languages:
● Flask - Python
● Nancy - .NET
● Ratpack - Groovy/Java
● Berliner - CoffeeScript
● Dancer - Perl
Classification of these...
21. Web Framework Taxonomy
Sinatra, Flask, Berliner, Dancer, Ratpack, Nancy
[Micro Frameworks]
Rails, Django
[Lightweight Frameworks]
Play, Struts 2, Spring MVC
[MOR]
Google Web Toolkit, JSF
[Component Based]
Light
Heavy
28. Python Flask Architecture
Based on Werkzeug so mod_wsgi based
Built in web server
Uses Jinja2 for templating
Hosting available on heroku, webfaction
Celery integration for async task/job queuing
30. What can I use Flask for?
1. Projects with tight deadlines
2. Prototyping
3. In-house internal applications
4. Applications where system resources are
limited, e.g. VM's hosted on Linode.com
32. Flask App - Log Viewer
A lightweight log viewer application, but without
the overhead of indexing. Provides:
● Access to specific application logs for users
instead of them ssh'ing to the server to "less"
them.
● Retrieve the head or tail a log file
● Search in logs (with grep) for an expression
33. Flask 0.9 utilising:
● YAML (Yet Another Markup Language) for
configuration
● Jinja 2 templates for content separation
● The LESS dynamic stylesheet module
Virtualenv - for creating an isolated Python
environment to manage dependencies
Python 2.6.1 (CPython)
System Components
34. Python Modules
Used additional Python wrappers for
Grin - http://pypi.python.org/pypi/grin
● Provides search features (wraps GNU grep)
● Supports regex, before/after context
● File/dir exclusion
Tailer - http://pypi.python.org/pypi/tailer/0.2.1
● Display n lines of the head/tail of a file
● Allows "follow" of a file
41. cache = Cache(app)
cache.init_app(app)
cache = Cache(config={'CACHE_TYPE': 'simple'})
#Use a decorator to cache a specific template with
@cache.cached(timeout=50)
def index():
return render_template('index.html')
Flask Cache
42. man = CouchDBManager()
man.setup(app)
# Create a local proxy to get around the g.couch namespace
couch = LocalProxy(lambda: g.couch)
# Store a document and retrieve
document = dict(title="Hello", content="Hello, world!")
couch[some_id] = document
document = couch.get(some_id)
Flask CouchDB
43. from flask_mail import Message
@app.route("/")
def index():
msg = Message("Hello",
sender="from@example.com",
recipients=["to@example.com"])
Flask Mail
44. from flask import Flask, Response
from flask_principal import Principal, Permission, RoleNeed
app = Flask(__name__)
# load the extension
principals = Principal(app)
# Create a permission with a single Need, in this case a RoleNeed.
admin_permission = Permission(RoleNeed('admin'))
# protect a view with a principal for that need
@app.route('/admin')
@admin_permission.require()
def do_admin_index():
return Response('Only if you are an admin')
Flask Principles