User:Jskladan/Sandbox:Flask FI

From FedoraProject

< User:Jskladan(Difference between revisions)
Jump to: navigation, search
 
Line 10: Line 10:
 
  source flask/bin/activate
 
  source flask/bin/activate
 
  pip install flask flask-sqlalchemy flask-wtf
 
  pip install flask flask-sqlalchemy flask-wtf
 +
 +
= Hello World =
  
 
Create directory/file structure
 
Create directory/file structure
 
  mkdir hello_world
 
  mkdir hello_world
 
  cd hello_world
 
  cd hello_world
mkdir static templates
 
touch config.py
 
  
== config.py ==
+
== Dummy App ==
 
+
<pre>
+
HOST = '0.0.0.0'
+
DEBUG = True
+
</pre>
+
 
+
 
+
= Hello World =
+
 
+
Create new file (app_01.py), and copy/paste the code below.
+
To start the app, just run it as a regular python script.
+
 
+
== app_01.py ==
+
  
 +
''app_01.py''
 
<pre>
 
<pre>
 
from flask import Flask
 
from flask import Flask
 +
 +
DEBUG = True
 +
HOST = '0.0.0.0'
  
 
app = Flask(__name__)                                                                       
 
app = Flask(__name__)                                                                       
app.config.from_object('config')                                                             
+
app.config.from_object(__name__)                                                             
 
                                                                                              
 
                                                                                              
 
@app.route('/')                                                                             
 
@app.route('/')                                                                             
Line 47: Line 38:
 
     app.run(host = app.config['HOST'], debug = app.config['DEBUG'])  
 
     app.run(host = app.config['HOST'], debug = app.config['DEBUG'])  
 
</pre>
 
</pre>
 +
 +
== HTML ==
 +
 +
''app_02.py''
 +
<pre>
 +
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'])
 +
</pre>
 +
 +
== Templates ==
 +
 +
http://jinja.pocoo.org/docs/templates/
 +
 +
Create a directory to store templates
 +
mkdir templates

Latest revision as of 18:14, 5 December 2012

Contents

[edit] 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

[edit] Hello World

Create directory/file structure

mkdir hello_world
cd hello_world

[edit] 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']) 

[edit] 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'])

[edit] Templates

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

Create a directory to store templates

mkdir templates