From Fedora Project Wiki
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
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 | ||
== Dummy App == | |||
''app_01.py'' | |||
<pre> | |||
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']) | |||
</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
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