User:Jskladan/Sandbox:Flask FI

From FedoraProject

< User:Jskladan(Difference between revisions)
Jump to: navigation, search
Line 15: Line 15:
 
  cd hello_world
 
  cd hello_world
 
  mkdir static templates
 
  mkdir static templates
  touch app.py config.py
+
  touch config.py
 +
 
 +
== config.py ==
 +
 
 +
<pre>
 +
HOST = '0.0.0.0'
 +
DEBUG = True
 +
</pre>
 +
 
  
 
= Dummy application =
 
= Dummy application =
  
== app.py ==
+
Create new file (app_1.py), and copy/paste the code below.
 +
To start the app, just run it as a regular python script.
 +
 
 +
== app_1.py ==
  
 
<pre>
 
<pre>
Line 35: Line 46:
 
if __name__ == '__main__':                                                                   
 
if __name__ == '__main__':                                                                   
 
     app.run(host = app.config['HOST'], debug = app.config['DEBUG'])  
 
     app.run(host = app.config['HOST'], debug = app.config['DEBUG'])  
</pre>
 
 
== config.py ==
 
 
<pre>
 
HOST = '0.0.0.0'
 
DEBUG = True
 
 
</pre>
 
</pre>

Revision as of 15:36, 5 December 2012

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

Create directory/file structure

mkdir hello_world
cd hello_world
mkdir static templates
touch config.py

config.py

HOST = '0.0.0.0'
DEBUG = True


Dummy application

Create new file (app_1.py), and copy/paste the code below. To start the app, just run it as a regular python script.

app_1.py

from flask import Flask

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