Possible to reference app.ctx from a Blueprint instance?
See original GitHub issueI’ve added a redis_connection
to my app.ctx
in the main file:
app.ctx.redis_connection = aioredis.from_url(...)
and I’m able to reference it via a request object:
request.app.ctx.redis_connection...
However, I also need to access the redis_connection in my blueprint, outside of the request object, which is located in a separate file.
blueprint_instance = Blueprint("MyBlueprint", url_prefix="/blueprint_url")
I started looking at this: https://github.com/sanic-org/sanic/blob/main/sanic/blueprints.py#L110 but I’m not sure how it’s referencing the global app.ctx.
I tried this:
bluprint_instance.apps.ctx.redis_connection
and got this error:
sanic.exceptions.SanicException: <Blueprint blueprint_instance> has not yet been registered to an app
How do I get the main app.ctx from the blueprint? Thanks!
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Possible to reference app.ctx from a Blueprint instance? - PullAnswer
and I'm able to reference it via a request object: request.app.ctx.redis_connection... However, I also need to access the redis_connection in my blueprint ......
Read more >Blueprints — Sanic 22.9.1 documentation - Read the Docs
A helper method to register class instance or functions as a handler to the application url routes. Parameters. handler (Callable[[...], Coroutine[Any, Any, ...
Read more >The Application Context — Flask Documentation (2.2.x)
The Flask application object has attributes, such as config , that are useful to access within views and CLI commands. However, importing the...
Read more >API — Flask Documentation (1.1.x) - Renaud Detry
A blueprint is an object that allows defining application functions without requiring an application object ahead of time. It uses the same decorators...
Read more >HTMLCanvasElement.getContext() - Web APIs | MDN
Chrome Edge
getContext Full support. Chrome1. Toggle history Full support. Edge12. Tog...
2d context Full support. Chrome1. Toggle history Full support. Edge12. Tog...
options.alpha parameter Full...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ha! I did not realize that the blueprints had access to the listeners!!!
Thank you, this is the correct the solution, and yes it’s very elegant!
Thanks for maintaining this library, it’s by far my favorite web-framework!
😊