Skip to content

Instantly share code, notes, and snippets.

@fransiska
Created May 8, 2020 01:34
Show Gist options
  • Select an option

  • Save fransiska/10ae14211cb66c751b31a6aab60edeec to your computer and use it in GitHub Desktop.

Select an option

Save fransiska/10ae14211cb66c751b31a6aab60edeec to your computer and use it in GitHub Desktop.
Example for flask
from flask import Blueprint
from flask_cache import cache
from datetime import datetime
bp = Blueprint('bp', __name__)
@bp.route("/bp")
@cache.cached(timeout=50)
def index():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
from flask_caching import Cache
cache = Cache()
from flask import Flask, abort, Response, request, session, jsonify
from json import dumps
from flask_cache import cache
from flask_blueprint import bp
app = Flask(__name__)
app.secret_key = 'super secret key'
app.debug = True
app.register_blueprint(bp)
cache.init_app(app, config={'CACHE_TYPE': 'simple'})
def error_message(status_code, body=None, headers={}):
if 'text/html' in request.headers.get("Accept",""):
return "<h4><b>{}</b> {}</h4>".format(status_code, body.get("status"))
else:
return Response(dumps(body), status_code, headers, mimetype=u'application/json')
@app.route('/')
def index():
if 'error' not in session:
session['error'] = {"status": ""}
response = jsonify(session["error"])
response.headers.add('Access-Control-Allow-Headers',
"Origin, X-Requested-With, Content-Type, Accept, x-auth")
return response
@app.route('/error')
def error():
session['error'] = {"status": "has been accessed"}
raise Exception("i am error")
@app.errorhandler(Exception)
def error_exception(error):
return error_message(500, {"status": "{}".format(error)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment