User:Jskladan/Sandbox:Flask FI

From FedoraProject

< User:Jskladan
Revision as of 18:14, 5 December 2012 by Jskladan (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Setup

Create directory to hold virtualenv

mkdir python_web
cd python_web

Create virtualenv & install flask

wget https://raw.github.com/pypa/virtualenv/master/virtualenv.py
python virtualenv.py flask
source flask/bin/activate
pip install flask flask-sqlalchemy flask-wtf

Hello World

Create directory/file structure

mkdir hello_world
cd hello_world

Dummy App

app_01.py

from flask import Flask

DEBUG = True
HOST = '0.0.0.0'

app = Flask(__name__)                                                                       
app.config.from_object(__name__)                                                            
                                                                                            
@app.route('/')                                                                             
@app.route('/index')                                                                        
def index():                                                                                
    return "Hello, World!"                                                                  
                                                                                            
                                                                                            
if __name__ == '__main__':                                                                  
    app.run(host = app.config['HOST'], debug = app.config['DEBUG']) 

HTML

app_02.py

from flask import Flask

HOST = '0.0.0.0'
DEBUG = True

app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/')
@app.route('/index')
def index():

    args = {'user': 'John Doe'}
    page = """
<html>
  <head>
    <title>Home Page</title>
  </head>
  <body>
    <h1>Hello, %(user)s</h1>
  </body>
</html>
"""

    return page % args


if __name__ == '__main__':
    app.run(host = app.config['HOST'], debug = app.config['DEBUG'])

Templates

http://jinja.pocoo.org/docs/templates/

Create a directory to store templates

mkdir templates