From 252680fd11ceffee76cadc1d97f0a8816ff30345 Mon Sep 17 00:00:00 2001 From: Philippe Duval Date: Thu, 26 Jul 2018 12:14:59 -0400 Subject: [PATCH] Add `external_js/css_urls` to dash ctor, support for external only urls --- dash/dash.py | 13 ++++++++++--- tests/test_integration.py | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index 44fad1efee..1baf2cac6c 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -61,7 +61,7 @@ # pylint: disable=too-many-instance-attributes -# pylint: disable=too-many-arguments +# pylint: disable=too-many-arguments, too-many-locals class Dash(object): def __init__( self, @@ -75,6 +75,8 @@ def __init__( compress=True, meta_tags=None, index_string=_default_index, + external_script_urls=None, + external_css_urls=None, **kwargs): # pylint-disable: too-many-instance-attributes @@ -128,6 +130,10 @@ def _handle_error(error): # static files from the packages self.css = Css() self.scripts = Scripts() + + self._external_scripts_urls = external_script_urls + self._external_css_urls = external_css_urls + self.registered_paths = {} # urls @@ -302,7 +308,8 @@ def _relative_url_path(relative_package_path='', namespace=''): def _generate_css_dist_html(self): links = self._collect_and_register_resources( self.css.get_all_css() - ) + ) + self._external_css_urls + return '\n'.join([ ''.format(link) for link in links @@ -324,7 +331,7 @@ def _generate_scripts_html(self): self.scripts._resources._filter_resources( dash_renderer._js_dist ) - ) + ) + self._external_scripts_urls return '\n'.join([ ''.format(src) diff --git a/tests/test_integration.py b/tests/test_integration.py index 770b9152cb..aef8f05ffd 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -434,4 +434,26 @@ def will_raise(): self.assertTrue('{%config%}' in exc_msg) self.assertTrue('{%scripts%}' in exc_msg) time.sleep(0.5) - print('invalid index string') + + def test_external_files_init(self): + js_files = [ + 'https://www.google-analytics.com/analytics.js', + 'https://cdn.polyfill.io/v2/polyfill.min.js' + ] + css_files = [ + 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css', + 'https://codepen.io/chriddyp/pen/bWLwgP.css' + ] + + app = dash.Dash( + external_script_urls=js_files, external_css_urls=css_files) + + app.layout = html.Div() + + self.startServer(app) + time.sleep(0.5) + + for fmt, url in itertools.chain( + (("//script[@src='{}']", x) for x in js_files), + (("//link[@href='{}']", x) for x in css_files)): + self.driver.find_element_by_xpath(fmt.format(url))