Last active
May 7, 2018 02:40
-
-
Save allenyang79/02ab528273ce56b61899d09fd9870b2c to your computer and use it in GitHub Desktop.
flask custom a context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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