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.

Multi Page Apps: circular reference when importing app

See original GitHub issue

I define my dash app instance in app.py:

server = flask.Flask(__name__)
app = Dash(__name__,
        plugins=[dl.plugins.pages],
        external_stylesheets=external_stylesheets,
        external_scripts=external_scripts, server=server)

By default the pages plug-in iterates over the modules in the ./pages folder. I have a page that needs to reference the dash app instance in order to call the get_asset_url() method.

In my page module, the import:

from app import app

Creates a circular reference. I can fix this with an embedded import but I’d prefer not to. Is there a way to resolve this? Ideally a proxy reference to the current dash instance (dash.current_app) would be one solution.

Cheers.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
AnnMarieWcommented, Feb 9, 2022

@stevej2608, @M4thM4gician To solve the circular reference issue we will be adding the following functions that will be similar to dash.callback where the appobject is not needed. This is on the project plan to be completed before pages/ is moved into Dash.

  • dash.get_relative_path
  • dash.strip_relative_path
  • dash.get_asset_url

This is a good first issue. Here are some tips for getting started:

  • The pull request will be in dash, rather than here in dash-labs
  • The solution will be similar to how dash.callback was created. Here is the pull request

Related issues:

Are there plans to integrate a solution to accessing the cache in callbacks outside the app.py file? For example, I have multipage app that uses a structure similar to the “Caching and Signaling” example in the docs. Once the expensive query is complete, callbacks on every page will be utilizing this same data source and need to access the global cache. Since the cache is tied to the app object, I haven’t been able to make this work on a multipage setup without just putting all my callbacks inside the app.py file. This quickly becomes hard to navigate with a larger multipage app.

2reactions
bigmike36ccommented, Feb 24, 2022

Just to add to this, the same issue occurs with the dashboard engine for enterprise users. The workaround is easy enough by simply registering the dashboard engine page and declaring its layout inside the app.py file. However, for code organization, it would be nice to eventually be able to break this code and the associated callbacks into its own file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing model classes from other apps without causing ...
Running into circular importing means you are fighting against the separation you've created, or you're not writing appropriately decoupled ...
Read more >
Multi-Page Apps and URL Support - Dash Plotly
If you want to build a multi-page app without Pages, see the Multi Page Apps ... the Dash file (usually app.py ), then...
Read more >
Multiple Component Instances - Circular Reference Error
I have been experimenting with Components in order to make some very useful reusable controls. I have created a Component that is basically ......
Read more >
How to Eliminate Circular Dependencies from Your JavaScript ...
Circular dependencies (also known as cyclic dependencies) occur when two or more modules reference each other. ... // file a.ts import { b...
Read more >
Issues · plotly/dash-labs - GitHub
Multi-Page Apps [Feature Request] Prevent ID collisions. #75 opened on Jan 12 by AnnMarieW · Multi Page Apps: circular reference when importing app....
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