question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to reference app.ctx from a Blueprint instance?

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vgoklanicommented, Aug 8, 2022

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!

0reactions
ahopkinscommented, Aug 8, 2022

😊

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found