GibbsConsulting / django-plotly-dash

Expose plotly dash apps as django tags
MIT License
546 stars 124 forks source link

append_script and serve_locally=True #296

Open sdementen opened 3 years ago

sdementen commented 3 years ago

To add a local script, I am using app.scripts.append_script(dict(external_url=app.get_asset_static_url("script.js"))) with serve_locally=True and I get the warning

...\dash\resources.py:60: UserWarning: You have set your config to `serve_locally=True` but A local version of /static/dpd/assets/simulation/dashapps/script.js is not available.
If you added this file with `app.scripts.append_script` or `app.css.append_css`, use `external_scripts` or `external_stylesheets` instead.

with the file not being included in the html via the {% plotly_footer %}.

It appears that the append_script approach is to be replaced by the use of external_scripts in Dash. Does django-plotly-dash support this ?

GibbsConsulting commented 3 years ago

Can you insert the script using the app.get_asset_url function and the local assets functionality? Note that this requires the staticfiles finders to be configured.

sdementen commented 3 years ago

I have replaced app.scripts.append_script(dict(external_url=app.get_asset_static_url("script.js"))) by app.scripts.append_script(dict(external_url=app.get_asset_url("script.js"))) as you asked and I get the same error message (I had already configured my django to use locally served files by following https://django-plotly-dash.readthedocs.io/en/latest/local_assets.html#local-assets). This issue is in https://github.com/plotly/dash/blob/dev/dash/resources.py#L46 where specifying an "external_url" while serving locally is not supported.

dnace commented 3 years ago

Unfortunately look like external url doesn't work when serve_locally is true, so i go workaround I try to explain my solution for serve font-awesome locally, i hope this could help you:

    app.css.append_css(
         {
          'asset_path': STATIC_URL + 'font-awesome/css/all.css',
          'filepath': STATIC_ROOT + 'font-awesome/css/all.css',   # don't forget collectstatic files 
          })
    app.scripts.append_script(
         {
          'asset_path': STATIC_URL + 'font-awesome/js/all.js',
          'filepath': STATIC_ROOT + 'font-awesome/js/all.js',
          })

also i'll unsuccessfully try to setup asset path, but can't find the way using the view static files finders were configured but it is doesn't help so i write new hardcoded redirect rule in urls.py re_path(r'^static/dpd/assets/my_app/my_view/(?P.*)$', RedirectView.as_view(url=settings.ROOT_URL + '%(path)s')),

it work, but is the way make it more flexible?

sdementen commented 3 years ago

I have just commented the second part of the condition in https://github.com/plotly/dash/blob/dev/dash/resources.py#L46 and got it working with external_url but patching dash like this doesn't sound clean. In the exception message, Dash explicitly tells to use external_scripts so I guess the cleaner way is for django-plotly-dash to support it.

sdementen commented 3 years ago

with #312 , I can write:

app = DjangoDash(
    "myapp",
    external_scripts=[
        static_asset_path(__name__, "my-local-script.js"),
    ],
    external_stylesheets=[
        "https://unpkg.com/tailwindcss@1.9.6/dist/tailwind.min.css",
        static_asset_path(__name__,"my-local-css.css"),
        dbc.themes.BOOTSTRAP,
    ],
)

and it works

GibbsConsulting commented 3 years ago

@sdementen can this issue now be closed?

sdementen commented 3 years ago

Maybe still a need to update the docs around the use of append_script?