[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge pull request plotly#2771 from plotly/fix/namespace-package-map
Browse files Browse the repository at this point in the history
Dynamic loading: map namespace to package name in case they differ.
  • Loading branch information
T4rk1n committed Mar 1, 2024
2 parents 330a2c4 + ba17593 commit e580ff0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions dash/_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def insert_callback(
"output": output,
"raw_inputs": inputs,
"manager": manager,
"allow_dynamic_callbacks": dynamic_creator,
}
callback_list.append(callback_spec)

Expand Down
15 changes: 14 additions & 1 deletion dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,10 @@ def serve_reload_hash(self):
)

def serve_dist(self):
libraries = flask.request.get_json()
libraries = [
ComponentRegistry.namespace_to_package.get(lib, lib)
for lib in flask.request.get_json()
]
dists = []
for dist_type in ("_js_dist", "_css_dist"):
resources = ComponentRegistry.get_resources(dist_type, libraries)
Expand Down Expand Up @@ -1282,6 +1285,8 @@ def long_callback(
def dispatch(self):
body = flask.request.get_json()

nlibs = len(ComponentRegistry.registry)

g = AttributeDict({})

g.inputs_list = inputs = body.get( # pylint: disable=assigning-non-slot
Expand Down Expand Up @@ -1311,6 +1316,7 @@ def dispatch(self):

try:
cb = self.callback_map[output]
_allow_dynamic = cb.get("allow_dynamic_callbacks", False)
func = cb["callback"]
g.background_callback_manager = (
cb.get("manager") or self._background_manager
Expand Down Expand Up @@ -1362,6 +1368,7 @@ def dispatch(self):
except KeyError as missing_callback_function:
msg = f"Callback function not found for output '{output}', perhaps you forgot to prepend the '@'?"
raise KeyError(msg) from missing_callback_function

ctx = copy_context()
# noinspection PyArgumentList
response.set_data(
Expand All @@ -1375,6 +1382,12 @@ def dispatch(self):
)
)
)

if not _allow_dynamic and nlibs != len(ComponentRegistry.registry):
print(
"Warning: component library imported during callback, move to top-level for full support.",
file=sys.stderr,
)
return response

def _setup_server(self):
Expand Down
9 changes: 6 additions & 3 deletions dash/development/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ComponentRegistry:

registry = OrderedSet()
children_props = collections.defaultdict(dict)
namespace_to_package = {}

@classmethod
def get_resources(cls, resource_name, includes=None):
Expand All @@ -44,10 +45,12 @@ def __new__(mcs, name, bases, attributes):
# as it doesn't have the namespace.
return component

_namespace = attributes.get("_namespace", module)
ComponentRegistry.namespace_to_package[_namespace] = module
ComponentRegistry.registry.add(module)
ComponentRegistry.children_props[attributes.get("_namespace", module)][
name
] = attributes.get("_children_props")
ComponentRegistry.children_props[_namespace][name] = attributes.get(
"_children_props"
)

return component

Expand Down

0 comments on commit e580ff0

Please sign in to comment.