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.

[Question] Access app instance from Router files

See original GitHub issue

In 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 the import statement.

Are there better (standard) solutions to solve this issue?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
falkbencommented, Feb 24, 2021

One thing you can do is access the app on the request object.

request.app.url_path_for("logout")

This works fine in a simple case at least with the following structure:

├── __init__.py
├── main.py
├── routerA.py
└── routerB.py

Where main.py is:

from fastapi import FastAPI

from experiments.multi_file_app.routerA import router as routerA
from experiments.multi_file_app.routerB import router as routerB

app = FastAPI()

app.include_router(routerB)
app.include_router(routerA)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)

I know this piece isn’t covered in the “Bigger Applications” but still worth going through: https://fastapi.tiangolo.com/tutorial/bigger-applications/

0reactions
github-actions[bot]commented, Nov 25, 2022

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.

Read more comments on GitHub >

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

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