sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
18.09k stars 1.55k forks source link

how to can get current_app ? #921

Closed Feeeenng closed 7 years ago

Feeeenng commented 7 years ago

Flask get current app is from flask import current_app

How to can get 'current_app' in Sanic ?

CharAct3 commented 7 years ago

ref #913

You can use request.app in a handler function to access it.

Feeeenng commented 7 years ago

@CharAct3
from sanic import request

current_app = request.app

AttributeError: module 'sanic.request' has no attribute 'app'

CharAct3 commented 7 years ago
@app.route("/")
async def test(request):
    current_app = request.app
    return json({"hello": "world"})
Feeeenng commented 7 years ago

@CharAct3 but some function is outside use. not only is inside request . l hope is :

from sanic import request
def test():
     current_app = request.app
     return current_app.id

@app.route('/)
async def index(request):
        id = test()
        return json({"id",id})
yunstanford commented 7 years ago

You can always pass app as function param.

maybe helpful, https://github.com/channelcat/sanic/issues/845

in your example, you can do sth like.

def test(app):
     return app.id

@app.route('/)
async def index(request):
    id = test(request.app)
    return json({"id",id})
Feeeenng commented 7 years ago

@yunstanford thx. if is class .how to can use current_app ? from flask import session . In sanic . how to use the ?

yunstanford commented 7 years ago

Sanic doesn't have builtin session support, but you can use extension, https://github.com/subyraman/sanic_session, similar to https://pythonhosted.org/Flask-Session/

jrocketfingers commented 7 years ago

@r0fls, @seemethere, @messense, @yunstanford, can we close the Q&A types of issues? This is becoming a lot like an IRC log and it's getting harder to track real issues. Hell, if it was gitter/IRC/slack I might even be so inclined to answer those as well.

@yunstanford please don't think I don't appreciate your perseverance in answering all of the questions, though. I'm just thinking you could get paid in SO reputation :)

r0fls commented 7 years ago

Since the question has diverged, I agree we should close this. Please keep questions within issues restricted to Sanic specifics in general.

jrocketfingers commented 7 years ago

I'm not sure that closing questions one by one upon divergence would cut it. Maybe we should redirect them to sanic tag on SO and start watching that? We could end up getting free moderation and editing from the ever present mods there - and just wrapping people's code/output in code segments would be enough.

In the meantime, I'll try to answer some of those so we can start closing them here.

Feeeenng commented 7 years ago

please close question. the question has been resolved .

r0fls commented 7 years ago

@jrocketfingers I think it's ok to ask Sanic related questions in the issues, we have the questions tag for it. However the issues are definitely ballooning, which is a problem.

jrocketfingers commented 7 years ago

Open an issue about issues balooning! :) Jokes aside, yeah, a question label would help tremendously, as I'm just looking for a way not to drown things like found a memory leak in a bunch of questions. Other than that, yeah, I agree that Sanic's is still too small a community to necessitate the split between but tracking and questions.

On Sep 4, 2017 9:02 AM, "Raphael Deem" notifications@github.com wrote:

@jrocketfingers https://github.com/jrocketfingers I think it's ok to ask Sanic related questions in the issues, we have the questions tag for it. However the issues are definitely ballooning, which is a problem.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/channelcat/sanic/issues/921#issuecomment-326881797, or mute the thread https://github.com/notifications/unsubscribe-auth/ACtqPN0HEHf255CYojifgIGCaWAOoMOUks5se6CKgaJpZM4PFxnF .

OuIChien commented 3 years ago

When you instantiate a Sanic instance, that can be retrieved at a later time from the Sanic app registry. This can be useful, for example, if you need to access your Sanic instance from a location where it is not otherwise accessible.

Here you go: # ./path/to/server.py from sanic import Sanic app = Sanic("my_awesome_server") # ./path/to/somewhere_else.py from sanic import Sanic app = Sanic.get_app("my_awesome_server")