Skip to content

Instantly share code, notes, and snippets.

Last active Dec 20, 2015
What would you like to do?
Summary of the code and commands of my last minute @pycu short talk tonight.
Main module for the acktacular webapp
from flask import Flask, render_template
import os
__all__ = ['webapp']
webapp = Flask(__name__)
@webapp.route('/', methods=['GET'])
def welcomePage():
Spits out Welcome page
return render_template('welcome.html')
@webapp.route('/acks', methods=['POST'])
def postAck():
Post an Ack to the API
# TODO Persist or whatever you want to do here
return '{"status": "Created", "code": 201, "id": 1}', 201
if __name__ == '__main__':
if os.environ.get('FLASK_ENV') == 'dev':
Main tests module for the acktacular webapp
import acktacular
import unittest
import re
class AcktacularTestCase(unittest.TestCase):
Test all the things (in the Acktacular webapp)
def setUp(self):
self.driver = acktacular.webapp.test_client()
def tearDown(self):
def testWelcomePageRendered(self):
resp = self.driver.get('/')
self.assertEqual(resp.status_code, 200)
self.assertRegexpMatches(, re.compile('<h1>Welcome'))
def testPostAck(self):
resp ='/acks',
data=dict(pr_id=1, repo='mbbx6spp/ruby-procinfo',
comment='This is a vile mess, Susan',
self.assertEqual(resp.status_code, 201)
self.assertRegexpMatches(, re.compile('"code": 201'))
if __name__ == '__main__':

Getting It Done With Flask

Show via live coding how to get a small web app done with Flask, without any fuss at all.

Setup Your Sandbox


  • You have Python 2.7+ installed
  • You have pip installed

Note: if you don't, Google it for your OS, it's there :)

If you haven't already install virtualenvwrapper via pip:

pip install virtualenvwrapper

Now in your profile script (e.g. .bash_profile or .zshrc or similar for your preferred shell) you should add the following:

# This definitely works in bash, may not work in zsh, can't remember, so please customize for your shell
export WORKON_HOME=/path/you/want/sandboxes/to/live

Now setup your project sandbox:

mkdir acktacular
cd acktacular
git init
mkvirtualenv acktacular
pip install Flask
pip install bpython # optional, but you will not be bright to not install this! :)
# pip install whatever else you need for you app to work
# Next step only needed if you want to share your project with others
pip freeze > requirements.txt

Once you have this in place then on to Python code. See .py files here.

Run Your Server

Run your server by just passing the web app "main" module to python, e.g.


Running Your Tests

Run your tests by just passing the test "main" module to python, e.g.


Pros / Cons

Flask is really simple. That can be good or bad. Don't misuse it.

If you need to get started really quickly, without the overhead of a full stack web framework, Flask (in the Python world) provides a very efficient foundation to get started. This is ideally suited to those that prefer building microservice architectures over larger, more structured monolithic projects.



Props to my coworker Matt Gresko for helping me update my rusty Python knowledge recently. He suggested virtualenvwrapper (I was just using virtualenv) and bpython, which I still think is cheating, but also awesome! :)

<!-- Move me to templates/ dir for the app to work -->
<h1>Welcome to Actacular, PyCU</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment