Tehran PyCon 2017
#install virtualenv using pip
pip install virtualenv
# choose a folder
cd my_project_folder
# create the venv
virtualenv venv
# active it
source venv/bin/activate
python3 -m venv /path/to/env
cd path/to/env
source /venv/bin/activate
The venv package is built-in
source ./venv/bin/activate
pip install flask
Switch from os python to venv
#learning-flask/app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'aleyk'
app.run(debug=True)
#learning-flask/app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'aleyk'
@app.route('/foo')
def foo():
return 'foo'
@app.route('/bar')
def bar():
return 'bar'
def aHelperFunction():
return 'some stuff'
@app.route('/baz')
def baz():
return 'baz'
def databaseConnection():
return DB()
def fetchObjects():
return databaseConnection().getObjects()
app.run(debug=True)
@app.route()
# learning-flask/app/__init__.py
from flask import Flask
app = Flask(__name__)
# think of it as a way of executing `views.py`
from app import views
# learning-flask/app/views.py
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello flask"
# learning-flask/run.py
from app import app
app.run(debug=True)
@app.route('/user')
def user():
user = {'name': 'kian'}
return '''
<html>
<head>
<title>PyCon</title>
</head>
<body>
<h1>Hello, ''' + user['name'] + '''</h1>
</body>
</html>
'''
from flask import render_template
@app.route('/user-template')
def user_template():
user = {'name': 'kian'}
title = 'PyCon'
return render_template('user.html', user=user, title=title)
<!-- templates/user.html -->
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello, {{ user.name }}</h1>
</body>
</html>
@app.route('/user-template')
def user_template():
user = {'name': 'kian'}
title = 'PyCon'
posts = [
{
'author': user,
'body': 'a mock post'
},
{
'author': user,
'body': 'another mock post'
}
]
return render_template('user.html',
user=user,
title=title,
posts=posts)
{% if title %}
<title>{{ title }}</title>
{% else %}
<title>Default</title>
{% endif %}
{% for post in posts %}
<div>
<p>{{ post.author.name }} says: <b>{{ post.body }}</b></p>
</div>
{% endfor %}
<body>
<div class="jumbotron" style="margin: 20px; padding:20px;">
<a href="/"> Home </a> -
<a href="/user"> User </a> -
<a href="/user-template"> User-template </a>
</div>
<div class="container">
<hr>
{% block content %}{% endblock %}
</div>
</body>
Master layout file
{% extends "layout.html" %}
{% block content %}
<h1>Hello, {{ user.name }}</h1>
{% for post in posts %}
<div>
<p>{{ post.author.name }} says: <b>{{ post.body }}</b></p>
</div>
{% endfor %}
{% endblock %}
Normal view file
from flask import request
@app.route('/login', methods=['GET', 'POST'])
def login():
print('request',request)
print('method', request.method)
print('form', request.form)
print('args', request.args)
return render_template('login.html')
<form action="/login" method="post">
<div class="form-group">
<input type="text" name="username">
</div>
<div class="form-group">
<input type="password" name="password">
</div>
<input type="submit" value="Submit">
</form>
Let's try and fill in the page with a
?foo=bar&bar=baz
appended to the url
<button type="button" name="button" id="ajax"> Send AJAX </button>
$("#ajax").click(function(e) {
$.post('/login', {foo: 'bar', data: 1}, function(data){
console.log(data)
})
})