Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active December 20, 2015 00:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbx6spp/6043797 to your computer and use it in GitHub Desktop.
Save mbbx6spp/6043797 to your computer and use it in GitHub Desktop.
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':
webapp.run(debug=True)
else:
webapp.run()
"""
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):
pass
def testWelcomePageRendered(self):
resp = self.driver.get('/')
self.assertEqual(resp.status_code, 200)
self.assertRegexpMatches(resp.data, re.compile('<h1>Welcome'))
def testPostAck(self):
resp = self.driver.post('/acks',
data=dict(pr_id=1, repo='mbbx6spp/ruby-procinfo',
comment='This is a vile mess, Susan',
comment_by='mgresko'))
self.assertEqual(resp.status_code, 201)
self.assertRegexpMatches(resp.data, re.compile('"code": 201'))
if __name__ == '__main__':
unittest.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

Assumptions:

  • 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
VIRTUALENVWRAPPER=/path/to/virtualenvwrapper.sh
export WORKON_HOME=/path/you/want/sandboxes/to/live
test -f $VIRTUALENVWRAPPER && source $VIRTUALENVWRAPPER

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.

python acktacular.py

Running Your Tests

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

python acktacular_tests.py

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.

References

Credits

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 -->
<html>
<head>
<title>Acktacular</title>
</head>
<body>
<h1>Welcome to Actacular, PyCU</h1>
<p>
Woot!
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment