Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve_locally by default #722

Merged
merged 5 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
### Changed
- [#722](https://github.com/plotly/dash/pull/722) Assets are served locally by default. Both JS scripts and CSS files are affected. This improves robustness and flexibility in numerous situations, but in certain cases initial loading could be slowed. To restore the previous CDN serving, set `app.scripts.config.serve_locally = False` (and similarly with `app.css`, but this is generally less important).

## [0.43.0] - 2019-04-25
### Changed
- Bumped dash-core-components version from 0.47.0 to [0.48.0](https://github.com/plotly/dash-core-components/blob/master/CHANGELOG.md#0480---2019-05-15)
Expand Down
5 changes: 4 additions & 1 deletion dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,9 +1376,12 @@ def enable_dev_tools(self,
self._watch_thread.daemon = True
self._watch_thread.start()

if debug and self._dev_tools.serve_dev_bundles:
if (debug and self._dev_tools.serve_dev_bundles and
not self.scripts.config.serve_locally):
# Dev bundles only works locally.
self.scripts.config.serve_locally = True
print('WARNING: dev bundles requested with serve_locally=False.\n'
'This is not supported, switching to serve_locally=True')

return debug

Expand Down
21 changes: 9 additions & 12 deletions dash/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ def get_all_resources(self, dev_bundles=False):
return self._filter_resources(all_resources, dev_bundles)


# pylint: disable=too-few-public-methods
class _Config:
def __init__(self, infer_from_layout, serve_locally):
self.infer_from_layout = infer_from_layout
self.serve_locally = serve_locally


class Css:
def __init__(self, layout=None):
self._resources = Resources('_css_dist', layout)
self._resources.config = self.config
self._resources.config = self.config = _Config(True, True)

def _update_layout(self, layout):
self._resources.layout = layout
Expand All @@ -80,16 +87,11 @@ def append_css(self, stylesheet):
def get_all_css(self):
return self._resources.get_all_resources()

# pylint: disable=no-init, too-few-public-methods
class config:
infer_from_layout = True
serve_locally = False


class Scripts:
def __init__(self, layout=None):
self._resources = Resources('_js_dist', layout)
self._resources.config = self.config
self._resources.config = self.config = _Config(True, True)

def _update_layout(self, layout):
self._resources.layout = layout
Expand All @@ -99,8 +101,3 @@ def append_script(self, script):

def get_all_scripts(self, dev_bundles=False):
return self._resources.get_all_resources(dev_bundles)

# pylint: disable=no-init, too-few-public-methods
class config:
infer_from_layout = True
serve_locally = False
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we did this before, despite the fact that Scripts was instantiated, Scripts.config was a singleton. Mostly this is OK, dunno if there will ever be a reason to put two Scripts instances in one Python session, but it caused our test cases to be coupled to each other. I had to change this to an instance when I took out all the otherwise superfluous overrides in our test cases.

5 changes: 2 additions & 3 deletions tests/IntegrationTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ def tearDown(s):
s.server_process.terminate()
time.sleep(2)

def startServer(s, dash):
def startServer(s, app):
def run():
dash.scripts.config.serve_locally = True
dash.run_server(
app.run_server(
port=8050,
debug=False,
processes=4,
Expand Down
5 changes: 1 addition & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def callback2(value):
output2 = self.wait_for_element_by_id('output2')

# callback1 runs 4x (initial page load and 3x through send_keys)
self.assertEqual(callback1_count.value, 4)
wait_for(lambda: callback1_count.value == 4)

# callback2 is never triggered, even on initial load
self.assertEqual(callback2_count.value, 0)
Expand Down Expand Up @@ -562,7 +562,6 @@ def create_layout():

def test_multi_output(self):
app = Dash(__name__)
app.scripts.config.serve_locally = True

app.layout = html.Div([
html.Button('OUTPUT', id='output-btn'),
Expand Down Expand Up @@ -663,7 +662,6 @@ def overlapping_multi_output(n_clicks):

def test_multi_output_no_update(self):
app = Dash(__name__)
app.scripts.config.serve_locally = True

app.layout = html.Div([
html.Button('B', 'btn'),
Expand Down Expand Up @@ -696,7 +694,6 @@ def show_clicks(n):

def test_no_update_chains(self):
app = Dash(__name__)
app.scripts.config.serve_locally = True

app.layout = html.Div([
dcc.Input(id='a_in', value='a'),
Expand Down
5 changes: 4 additions & 1 deletion tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_external(self):
)
app.layout = dcc.Markdown()
app.scripts.config.serve_locally = False
app.scripts.config.xxx = 123
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hun?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Senator, I do not recall.
c096aa1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it at the time and had assumed it was to test for acceptance of randomly-named config keys :)


with mock.patch('dash.dash.os.stat', return_value=StatMock()):
resource = app._collect_and_register_resources(
Expand All @@ -67,7 +68,9 @@ def test_internal(self):
assets_ignore='load_after.+.js'
)
app.layout = dcc.Markdown()
app.scripts.config.serve_locally = True

self.assertEqual(app.scripts.config.serve_locally, True)
self.assertEqual(app.css.config.serve_locally, True)

with mock.patch('dash.dash.os.stat', return_value=StatMock()):
with mock.patch('dash.dash.importlib.import_module',
Expand Down