-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[BUG] Import from a page without application (For testing purpose) #2193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm having this exact same issue while trying to test callbacks within a page file. The fix you give on the forum, try:
from pages.my_page import my_callback
except:
pass doesn't work for me. Since the import raises the above Is there currently no way to test a callback inside a page file? Edit: dashboard\pages\my_page.py:25: in <module>
app = dash.get_app()
D:\VENVS\MY_VENV\lib\site-packages\dash\_get_app.py:8: in get_app
raise Exception(
E Exception:
E App object is not yet defined. `app = dash.Dash()` needs to be run
E before `dash.get_app()` is called and can only be used within apps that use
E the `pages` multi-page app feature: `dash.Dash(use_pages=True)`.
E
E `dash.get_app()` is used to get around circular import issues when Python files
E within the pages/` folder need to reference the `app` object. |
Hmm, yea the import fail so it's not available, best you can do rn is put the callback in another file separates from the pages. |
What would be the way to go about doing that? I currently have all of my callbacks being created with |
You can put the callbacks anywhere, just make sure the files are imported in the main app file. @alexcjohnson Maybe we can check an environ variable like |
What about mocking def test_my_page_callbacks(mocker):
mocker.patch("dash.register_page")
from pages.my_page import my_callback
assert my_callback(1, 2) == 3 What bothers me about an env var solution is you can't just run the test file, you need to know how to run it, and if this was used globally (ie including integration tests) we could easily swallow real errors. What if we made a context manager to wrap around the imports? Then you could keep the imports up top where they belong, but dash would know not to even attempt from dash.testing import unit_import
with unit_import:
from pages.my_page import my_callback |
When
register_page
is called during import, if there is no app defined before it will throw a bareException
with a message indicating there was no app before the import.dash/dash/_validate.py
Line 465 in 20b6467
This makes it hard to ignore that particular exception in the case of unit testing callbacks defined within a page file as reported on the forums and should be changed to a custom exception.
The text was updated successfully, but these errors were encountered: