Sinatra Like Framework: :Flask

Ruby has this God-Like framework called Sinatra and I like it so much because of its potability and lightness. I used sinatra to make prototying, light-weight APIs and simple web pages. Sinatra is great and much lighter then rails in some cases. But I found Flask in a github conservation and came to test it. Wow execellent speed and great concurrency factor.

Flask is a micro framework in Python programming. The concepts and behaviors are much like Sinatra although both has different usabilities and unique propositions.

Writing something with Flask is as easy as with Sinatra and here is a quick way into learning Flask.

PYTHON

You need to have Python 2 or above installed in your machine. There is something like rbenv for python , pyenv.

1
2
3
$ brew install pyenv
$ pyenv install 2.7.6
$ pyenv global 2.7.6

Above command sequence is about to install pyenv and python version 2.7.6 , then set it as the default python executable for the OS. You can have many python versions and use [pyenv local version] to set the executable path locally right under your project folder.

We are going to use VirtualEnv for our project because it is better to have python packages contained right under our project folder. By this means, we can control the necessary packages locally and redistrubute to others without any difficulty despite of their python working pattern.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ mkdir flask
$ cd flask
$ pip install virtualenv
$ virtualenv flask
Downloading/unpacking flask
  Downloading Flask-0.10.1.tar.gz (544kB): 544kB downloaded
  Running setup.py (path:/Users/tal/projects/flask/flask/build/flask/setup.py) egg_info for package flask

Downloading/unpacking Werkzeug>=0.7 (from flask)
  Downloading Werkzeug-0.9.4.tar.gz (1.1MB): 1.1MB downloaded
  Running setup.py (path:/Users/tal/projects/flask/flask/build/Werkzeug/setup.py) egg_info for package Werkzeug

Downloading/unpacking Jinja2>=2.4 (from flask)
  Downloading Jinja2-2.7.2.tar.gz (378kB): 378kB downloaded
  Running setup.py (path:/Users/tal/projects/flask/flask/build/Jinja2/setup.py) egg_info for package Jinja2


Downloading/unpacking itsdangerous>=0.21 (from flask)
  Downloading itsdangerous-0.24.tar.gz (46kB): 46kB downloaded
  Running setup.py (path:/Users/tal/projects/flask/flask/build/itsdangerous/setup.py) egg_info for package itsdangerous


Downloading/unpacking markupsafe (from Jinja2>=2.4->flask)
  Downloading MarkupSafe-0.21.tar.gz
  Running setup.py (path:/Users/tal/projects/flask/flask/build/markupsafe/setup.py) egg_info for package markupsafe

Installing collected packages: flask, Werkzeug, Jinja2, itsdangerous, markupsafe
  Running setup.py install for flask
  Running setup.py install for Werkzeug
  Running setup.py install for Jinja2
  Running setup.py install for itsdangerous
  Running setup.py install for markupsafe
    building 'markupsafe._speedups' extension
    clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c markupsafe/_speedups.c -o build/temp.macosx-10.9-x86_64-2.7/markupsafe/_speedups.o
    clang -bundle -undefined dynamic_lookup -L/usr/local/lib -L/usr/local/opt/sqlite/lib build/temp.macosx-10.9-x86_64-2.7/markupsafe/_speedups.o -o build/lib.macosx-10.9-x86_64-2.7/markupsafe/_speedups.so
Successfully installed flask Werkzeug Jinja2 itsdangerous markupsafe
Cleaning up...

Done. Lets start putting things into a piece of working block. Create a file whtevrucall.py in the root of the project folder.

1
2
$ touch whtevrucall.py
$ vi whtevrucall.py

Lets import the flask package and create an instance.

1
2
from flask import Flask
app = Flask(__name__)

Add a route and return a string , usually “Hello , World”.

1
2
3
4
5
6
7
8
9
from falask import Flask
app = Flas(__name__)

@app.route('/')
def main_route():
  return 'Hello, World'

if __name__ == '__main__':
  app.run()

Ok lets run our code. Just easy as Sinatra.

1
2
3
$ python whtevrucall.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

Now you can have a very simple working application which return Hello, World from the root. All I have to do is just to share something nice and introduce about it. The rest is yours.

Have Fun

Copyright © 2016 - Tin Aung Linn. Powered by Octopress