Skip to content

Commit 252680f

Browse files
committed
Add external_js/css_urls to dash ctor, support for external only urls
1 parent 54ca4a3 commit 252680f

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

dash/dash.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262

6363
# pylint: disable=too-many-instance-attributes
64-
# pylint: disable=too-many-arguments
64+
# pylint: disable=too-many-arguments, too-many-locals
6565
class Dash(object):
6666
def __init__(
6767
self,
@@ -75,6 +75,8 @@ def __init__(
7575
compress=True,
7676
meta_tags=None,
7777
index_string=_default_index,
78+
external_script_urls=None,
79+
external_css_urls=None,
7880
**kwargs):
7981

8082
# pylint-disable: too-many-instance-attributes
@@ -128,6 +130,10 @@ def _handle_error(error):
128130
# static files from the packages
129131
self.css = Css()
130132
self.scripts = Scripts()
133+
134+
self._external_scripts_urls = external_script_urls
135+
self._external_css_urls = external_css_urls
136+
131137
self.registered_paths = {}
132138

133139
# urls
@@ -302,7 +308,8 @@ def _relative_url_path(relative_package_path='', namespace=''):
302308
def _generate_css_dist_html(self):
303309
links = self._collect_and_register_resources(
304310
self.css.get_all_css()
305-
)
311+
) + self._external_css_urls
312+
306313
return '\n'.join([
307314
'<link rel="stylesheet" href="{}">'.format(link)
308315
for link in links
@@ -324,7 +331,7 @@ def _generate_scripts_html(self):
324331
self.scripts._resources._filter_resources(
325332
dash_renderer._js_dist
326333
)
327-
)
334+
) + self._external_scripts_urls
328335

329336
return '\n'.join([
330337
'<script src="{}"></script>'.format(src)

tests/test_integration.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,26 @@ def will_raise():
434434
self.assertTrue('{%config%}' in exc_msg)
435435
self.assertTrue('{%scripts%}' in exc_msg)
436436
time.sleep(0.5)
437-
print('invalid index string')
437+
438+
def test_external_files_init(self):
439+
js_files = [
440+
'https://www.google-analytics.com/analytics.js',
441+
'https://cdn.polyfill.io/v2/polyfill.min.js'
442+
]
443+
css_files = [
444+
'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css',
445+
'https://codepen.io/chriddyp/pen/bWLwgP.css'
446+
]
447+
448+
app = dash.Dash(
449+
external_script_urls=js_files, external_css_urls=css_files)
450+
451+
app.layout = html.Div()
452+
453+
self.startServer(app)
454+
time.sleep(0.5)
455+
456+
for fmt, url in itertools.chain(
457+
(("//script[@src='{}']", x) for x in js_files),
458+
(("//link[@href='{}']", x) for x in css_files)):
459+
self.driver.find_element_by_xpath(fmt.format(url))

0 commit comments

Comments
 (0)