Show via live coding how to get a small web app done with Flask, without any fuss at all.
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 by just passing the web app "main" module to python
, e.g.
python acktacular.py
Run your tests by just passing the test "main" module to python
, e.g.
python acktacular_tests.py
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! :)