Skip to content

Instantly share code, notes, and snippets.

@KenjiTakahashi
Created January 20, 2014 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KenjiTakahashi/8523285 to your computer and use it in GitHub Desktop.
Save KenjiTakahashi/8523285 to your computer and use it in GitHub Desktop.
flask-jitsu, used backslashes, 'cause gist won't let me use proper ones
# Other Stuff
from lib import auth
app = Flask(__name__)
auth.init_app(app)
with app.app_context():
auth.send_request('whatever') # Same instance as below, but different context.
@app.route('/')
def index():
return auth.send_request('nothing') # Works always on the same instance of `auth`.
# Other Stuff
from auth import Auth
auth = Auth()
from random import randint
from flask import current_app
class Auth(object):
def __init__(self):
self.i = randint(0, 666)
def init_app(self, app):
self.api_key = app.config['API_PUBLIC']
self.pri_key = app.config['API_PRIVATE']
def send_request(self, *args, **kwargs):
# DO STUFF
print(self.i, current_app.app_context())
################## OR
class Auth(object):
def __init__(self):
self.i = randint(0, 666)
def send_request(self, *args, **kwargs):
app = current_app
api_key = app.config['API_PUBLIC']
pri_key = app.config['API_PRIVATE']
# DO STUFF
print(self.i, current_app.app_context())
# Some Stuff
from lib import auth
@product_view.route('/products', methods=['GET'])
def prods_get():
# Same instance of `auth` as in app.py, but different context.
auth.send_request('sth')
@KenjiTakahashi
Copy link
Author

Could also be solved by shaping directories structure like this: https://github.com/miguelgrinberg/microblog/tree/version-0.4.

But when you're stuck, you're stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment