Friday, November 23, 2012

Python, Flask, MongoDB - Part 1a

The past few months I've been working quite a bit with Flask and MongoDB... and I've found it easy to use and quickly setup a database driven website.   Most of the tutorials and such out there assume some sort of knowledge or experience with Python or Javascript or both--and leave out quite a few details or are just plain outdated.
If you're not familiar with Flask, well... Python Flask is a web micro-framework designed to make creating web services and sites easy and even fun.  You can read all about that here:  http://flask.pocoo.org/ and you can skim through its Quickstart to get a feel for what Flask is about.
Now I'm sure what I'm writing will be outdated in a few months, but as of now, in early November 2012, it's current.  And with enough work collegues and geek friends asking me "how did you get going with Flask and/ or Python and/or Mongo" I might as well share these steps.  So let's go.

Goals for today

These should be enough to get going.
1a. Install Flask and write a Hello World.
1b. Install MongoDB and add a database with a single collection with three records
1c. Get Flask to connect to MongoDB and display the three records
A word about environments: I'll be using a Mac for this, and these steps should work with anything later than MacOS 10.6.  Many of the examples below were done in a terminal window on Mac OS, so you'll need to adjust the commands if you're using another platform.

Environments and prep work

A word about environments: I'll be using a Mac for this, and these steps should work with anything later than MacOS 10.6.  Many of the examples below were done in a terminal window on Mac OS, so you'll need to adjust the commands if you're using another platform.
To develop, I use a bit of vim and a bit more of PyCharm as the IDE.  More on PyCharm in a later post, but if you'd like to download a 30-day trial version you can do that here: http://www.jetbrains.com/pycharm/
Other things that aren't covered in this particular post will be virtual environments, Flask templates, or styling--it's more important to do something than do everything all at once.  Also, there won't be any manipulation of the Mongo data, such as creating, updating, and deleting records.  Those would both be additional posts.
For this first post, it's just straight up Python with globally install Python packages and the like.

Step 1 - Install Flask and write Hello World

Figure out your Python version

Let's Start by figuring out what version of Python you have.  These steps work with Python 2.6 or higher.  At a command line (open up a Terminal window--I use iTerm2 instead) type the following to make sure you've got Python installed:
python --version
That should return something like Python 2.6.6 or Python 2.7.3.  Both are fine.
If you don't have Python 2.6 or 2.7, install it.  Don't install Python 3 for this tutorial.

Install flask and make the first app

Install flask You might have to sudo these.
easy_install pip
pip install flask
Create the flask directory and application
mkdir ~/Desktop/flask
vim ~/Desktop/flask/hello.py
Add these lines:
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
You can also just create the hello.py using a text editor, but it's important to note that in Python the indents are significant--the return and app.run() statements should be indented four spaces.  This will make copying and pasting code difficult if you're not aware of it.
Finally, run it:
cd ~/Desktop/flask
python hello.py
 * Running on http://127.0.0.1:5000/
Now, in a web browser, go to http://127.0.0.1:5000/
And you'll see Hello World.

3 comments: