plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.19k stars 2.04k forks source link

Can't define components in submodules #2236

Open daviddavo opened 2 years ago

daviddavo commented 2 years ago

Describe your context

dash==2.6.1
dash-bootstrap-components==1.2.1
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-table==5.0.0

Describe the bug

I have an application that is deployed using a namespace package. It has multiple parts, and each of them is a different package, but all of them in the same namespace:

my_namespace.scripts holds data retrieval scripts, while my_namespace.web holds the dash application to show the data. I also created a new package to hold the components, called my_namespace.components. This means that there's no __init__.py inside the my_namespace folder.

I can import components like from my_namespace.components import MyComponent, but when I visit the webpage it raises an Error: <my components namespace name> was not found. This is because the script needed is not loaded. And the script is not loaded because the module my_namespace.components is not added to the registry (see line 38).

https://github.com/plotly/dash/blob/0df861f212cbd4a55dcbf77f64c1a1bffedb0bc7/dash/development/base_component.py#L33-L50

Expected behavior

To be able to define my components in a namespace package, like dash does with dash.html and dash.dcc

T4rk1n commented 2 years ago

You just have to put what we put in the regular boilerplate __init__.py in your namespace.components.__init__.py

alexcjohnson commented 2 years ago

@T4rk1n Are you sure that'll work? Ever since moving html, dcc, and dash_table into dash (not a namespace package, but they are submodules) we need special handling of their files:

https://github.com/plotly/dash/blob/0df861f212cbd4a55dcbf77f64c1a1bffedb0bc7/dash/dash.py#L804-L812

Would be great if we could avoid that, and maybe it would address @daviddavo's use case as well? ie if these were registered like regular components they'd be picked up by get_all_scripts:

https://github.com/plotly/dash/blob/0df861f212cbd4a55dcbf77f64c1a1bffedb0bc7/dash/dash.py#L800

T4rk1n commented 2 years ago

Hmm, it's missing something to handle namespace.sub.bundle.js in both frontend (needs a single namespace without dots for the resolution) and backend (requires the dot/slash for serving the bundle).

T4rk1n commented 2 years ago

@alexcjohnson Maybe we can add something like "resource_path": "package/subpackage" and join the relative_package_path from there if that key is present in the dist dict's ?

daviddavo commented 2 years ago

Yes, I've been trying a couple of hours multiple combinations of changing namespace names, using folders, etc. But there was no way...

Another option would be to explore the "package path" until we found the _js_dist, but I don't know if it would break some implementations.