app.config['TEMPLATES_AUTO_RELOAD'] = True
@app.after_request
def add_header(response):
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.headers['Cache-Control'] = 'public, max-age=0'
return response
debug=True
*/5 * * * * cd /your/path/to/app && /your/path/to/app/your_script_name.py
Client-side processing - DOM sourced data: ~5'000 rows.
Client-side processing - Ajax sourced data (ajax): ~50'000 rows.
Server-side processing (serverSide): millions of rows.
ResSeats.query.filter_by(res_id=sessio.film_id).order_by(res_id.desc()).limit(1)
@app.route('/foo')
def foo():
raw = str(request)
return raw
curl 'localhost:5000/foo?r=M&Ms'
<Request 'http://localhost:5000/foo?r=M&Ms' [GET]>
@app.route('/foo')
def foo():
raw = str(request)
print(raw)
p = re.compile(r"""(\?|\&)([^=]+)\=(?P<val>([^']+))""")
r = p.search(raw)
return r.group("val")
import pickle
"""
Сохраняем сложнную структуру в файл
"""
favorite_color = {"lion": "yellow", "kitty": "red", "aaa": [0, 1, 2]}
pickle.dump(favorite_color, open("id.txt", "wb"))
"""
Читаем из файла
"""
favorite_color = pickle.load(open("id.txt", "rb"))
print(favorite_color)
from flask import Flask, jsonify
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route('/')
def default():
result = u"\u20AC"
return jsonify(result=result), 200, {'Content-Type': 'text/css; charset=utf-8'}
if __name__ == "__main__":
app.run(debug=True, port=8767, host='0.0.0.0')
@app.before_request
def before_request():
user_id = session.get('user_id', None)
# Дальше проверяем, есть ли у user_id другие открытые сессии.
# Если сессия есть, то
# session.clear()
<form action="{{ url_for('get_task') }}" method="POST">
@app.route('/tasks/', methods=['POST'])
def get_task():
task_id = request.form['task_id']
from flask import Flask, request
@app.route('/ip/')
def get_user_ip():
ip = request.headers.get('X-Real-IP')
return ip
COOKIE_SECURE = 'Secure'
COOKIE_DURATION = timedelta(days=365)
import json
listok = {'привет': 'медвед'}
data = json.dumps(listok)
print('Json: %s' % data)
new_obj = json.loads(data)
print('Python obj: %s' % new_obj)
Json: {"\u043f\u0440\u0438\u0432\u0435\u0442": "\u043c\u0435\u0434\u0432\u0435\u0434"}
Python obj: {'привет': 'медвед'}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
listok = {'привет': 'медвед'}
data = json.dumps(listok)
print 'Json: %s' % data
new_obj = json.loads(data)
print('Python obj: %s' % new_obj)
for key in new_obj:
print key, new_obj[key]
Json: {"\u043f\u0440\u0438\u0432\u0435\u0442": "\u043c\u0435\u0434\u0432\u0435\u0434"}
Python obj: {u'\u043f\u0440\u0438\u0432\u0435\u0442': u'\u043c\u0435\u0434\u0432\u0435\u0434'}
привет медвед
def __init__(self, text=None, writer=None, key=None):
self.text = text
self.writer = current_user.id
self.key = key
@app.route('/post/<post_id>')
@app.route('/post/<int:post_id>')
@app.before_request
def before_request():
sitename = db = ""
if request.headers['Host'] in 'www.example1.com':
sitename = 'example1.com'
db = 'example1_com'
elif request.headers['Host'] in 'www.example2.com':
sitename = 'example2.com'
db = 'example2_com'
else:
sitename = 'test.com'
db = 'test_com'
# И т.д.
app.jinja_env.globals['sitename'] = sitename