Skip to content

Support replacing resources #655

Closed
Closed
@mjclawar

Description

@mjclawar

Background

As raised in plotly/dash-core-components#462, some of the default included JS libraries are not the latest/much larger than needed by some users. For example, there is no need to include the full plotly.js library for an application only using a scatter plot.

Potential implementation

@alexcjohnson laid out a potential function that would allow a user to swap out a resource on a per-namespace basis, dash.replace_resource: plotly/dash-core-components#462 (comment)

dash.replace_resource(dcc, 'plotly.js', {'external_url': 'https://cdn.plot.ly/plotly-basic-1.44.4.js'})
# or even just put the new file in your assets/ so it's loaded automatically,
# and tell us to ignore the default one:
dash.replace_resource(dcc, 'plotly.js', None)

This issue would need to cover implementing this function, which might look something like below:

def replace_resource(namespace, resource_name, replacement_resource):
    """Replace the metadata for resource_name in namespace with replacement_resource"""
    def _process_resource(resource):
        if 'resource_name' in resource and resource['resource_name'] == resource_name:
            return replacement_resource
        else:
            return resource

    namespace._js_dist = [
        _process_resource(r) for r in namespace._js_dist
    ]

replace_resource(
    namespace=dcc,
    resource_name='plotly.js',
    replacement_resource={
        'external_url': 'https://cdn.plot.ly/plotly-cartesian-1.45.2.min.js',
        'asset_path': 'plotly-cartesian-1.45.2.min.js',
        'filepath': 'assets/plotly-cartesian-1.45.2.min.js',
    }
)

In order to support this generically, future Dash component packages would need to include a resource_name field when specifying, e.g., _js_dist. Alternatively, it could also be set up using a lambda that returns True/False (i.e., a function passed to a filter) given a generic resource specification.

There's likely a better way to handle this using the Resources objects for both css and for js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions