Skip to content

[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

Closed
T4rk1n opened this issue Aug 12, 2022 · 5 comments · Fixed by #2226
Closed

[BUG] Import from a page without application (For testing purpose) #2193

T4rk1n opened this issue Aug 12, 2022 · 5 comments · Fixed by #2226

Comments

@T4rk1n
Copy link
Contributor

T4rk1n commented Aug 12, 2022

When register_page is called during import, if there is no app defined before it will throw a bare Exception with a message indicating there was no app before the import.

raise Exception("`dash.register_page()` must be called after app instantiation")

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.

@wcheek
Copy link

wcheek commented Sep 5, 2022

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 Exception it means that my_callback never gets imported and I can't test it.

Is there currently no way to test a callback inside a page file?

Edit:
Just some additional information.
I thought maybe I could get around this issue by using app = dash.get_app() in my_page.py but I get another exception doing this:

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.

@T4rk1n
Copy link
Contributor Author

T4rk1n commented Sep 6, 2022

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.

@wcheek
Copy link

wcheek commented Sep 6, 2022

What would be the way to go about doing that? I currently have all of my callbacks being created with @dash.callback together with my page layout. Where should I move these callbacks and what do I do to ensure they get connected with the app/page?

@T4rk1n
Copy link
Contributor Author

T4rk1n commented Sep 7, 2022

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 DASH_TESTING and not throw the error if there is no app?

@alexcjohnson
Copy link
Collaborator

What about mocking dash.register_page and importing the page inside the test?

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 register_page?

from dash.testing import unit_import
with unit_import:
    from pages.my_page import my_callback

@T4rk1n T4rk1n changed the title Bare exception for page register import. [BUG] Import from a page without application (For testing purpose) Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants