lost and found ( for me ? )

Python web framework Flask

just referred to http://flask.pocoo.org/

root@note:~# tail -1 /etc/lsb-release ;uname -ri
DISTRIB_DESCRIPTION="Ubuntu 13.10"
3.11.0-13-generic x86_64

install flask. ( system-wide install, not using virtual env )
root@note:~# apt-get install python-flask

sample python script ( hello.py )
__author__ = 'root'

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
   return "Hello Python Flask"

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

run the script
/usr/bin/python2.7 /root/PycharmProjects/test02/.idea/hello.py
* Running on http://127.0.0.1:5000/
127.0.0.1 - - [25/Nov/2013 22:17:25] "GET / HTTP/1.1" 200 -







- rendering tempaltes

python scripts
root@ubuntu:~/PycharmProjects/test02/.idea# cat application.py
__author__ = 'root'

from flask import Flask
from flask import render_template

# make an instance
app = Flask(__name__)

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
   return render_template('hello.html', name=name)

if __name__ == '__main__':
   app.run()root@ubuntu:~/PycharmProjects/test02/.idea# cat ./application.py
__author__ = 'root'

from flask import Flask
from flask import render_template

# make an instance
app = Flask(__name__)

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
   return render_template('hello.html', name=name)

html file ( jinja2 format. http://jinja.pocoo.org/docs/ to generate HTML )
# cat ./templates/hello.html
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
 <h1>Hello {{ name }}!</h1>
{% else %}
 <h1>Hello World!</h1>
{% endif %}

store jinja file under templates directory like this:

test/application.py
test/templates/hello.html





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.