Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Last active May 7, 2018 02:40
Show Gist options
  • Select an option

  • Save allenyang79/02ab528273ce56b61899d09fd9870b2c to your computer and use it in GitHub Desktop.

Select an option

Save allenyang79/02ab528273ce56b61899d09fd9870b2c to your computer and use it in GitHub Desktop.
flask custom a context
import datetime
import arrow
from flask import Flask, request, current_app
from werkzeug.test import EnvironBuilder
app = Flask(__name__)
@app.route
def inedx():
return 'index'
# request context
ctx = app.request_context(EnvironBuilder('/','http://localhost/').get_environ())
ctx.push()
try:
print(request.url)
finally:
ctx.pop()
# or less code
with app.request_context(EnvironBuilder('/','http://localhost/').get_environ()) as ctx:
print(request.url)
# app context
with app.app_context():
print(current_app.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment