Using blueprints effectively
If you already know what a blueprint is, imagine for a moment that you do not. As we are building out our application and trying to structure our code base in a logical and maintainable pattern, we realize that we need to constantly pass around our app
object:
from some.location import app @app.route("/my/stuff") async def stuff_handler(...): ... @app.route("/my/profile") async def profile_handler(...): ...
This can become very tedious if we need to make changes to our endpoints. You can imagine a scenario where we would need to update a bunch of separate files to duplicate the same change over and over again.
Perhaps more frustratingly, we might end up in a scenario where we have circular imports:
# server.py from user import * app = Sanic(...) # user.py from server import app @app.route("/user") ...
Blueprints solve both of these problems and allow us to abstract...