[Question] Access app instance from Router files
See original GitHub issueIn the router files I need access to the app instance to be able to use methods like url_path_for
. However, since in main.py
the routers are imported before the app is created, I cannot import the app there (circular import). Also in the validators for the models I would like to have access to the app.
I see two solutions:
- import the app within the router function (during request handling)
- Add a reference to the router objects to the app
To avoid problems with linting the
__import__
function could be used instead of theimport
statement.
Are there better (standard) solutions to solve this issue?
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Express: How to pass app-instance to routes from a different ...
Use req.app , req.app.get('somekey'). The application variable created by calling express() is set on the request and response objects.
Read more >Different History modes - Vue Router
The history option when creating the router instance allows us to choose among different history modes. Hash Mode #. The hash history mode...
Read more >Router tutorial: tour of heroes - Angular
This tutorial provides an extensive overview of the Angular router. In this tutorial, you build upon a basic router configuration to explore features...
Read more >Boot files - Quasar Framework
Please make sure you understand what problem boot files solve and when it is ... These are the cases where you don't need...
Read more >How Requests are Routed | App Engine standard ...
In the Google Cloud console, you can view the corresponding Instances, ... You can create a dispatch file to override App Engine's URL-based...
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 Free
Top 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
One thing you can do is access the app on the
request
object.This works fine in a simple case at least with the following structure:
Where
main.py
is:I know this piece isn’t covered in the “Bigger Applications” but still worth going through: https://fastapi.tiangolo.com/tutorial/bigger-applications/
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.