Skip to content

Commit

Permalink
Add external_js/css_urls to dash ctor, support for external only urls
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Jul 31, 2018
1 parent 54ca4a3 commit 805ee12
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 10 additions & 3 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 or []
self._external_css_urls = external_css_urls or []

self.registered_paths = {}

# urls
Expand Down Expand Up @@ -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([
'<link rel="stylesheet" href="{}">'.format(link)
for link in links
Expand All @@ -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([
'<script src="{}"></script>'.format(src)
Expand Down
24 changes: 23 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 805ee12

Please sign in to comment.