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

Standalone dash renderer (for custom hooks) - March 1 #367

Merged
merged 37 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ef0fbeb
Use DashRenderer as a standalone library
valentijnnieman Sep 4, 2018
938e466
Cleanup code
valentijnnieman Sep 4, 2018
d916765
Update tests with renderer tag
valentijnnieman Sep 5, 2018
456a090
Fix linting error
valentijnnieman Sep 5, 2018
1e957c7
Refactor generate_renderer function
valentijnnieman Sep 5, 2018
e776e8e
Refactor _generate_renderer
valentijnnieman Sep 5, 2018
3edaa12
Fixed pylint line too long error
valentijnnieman Sep 5, 2018
41fdc76
Fixed pylint trailing whitespace error
valentijnnieman Sep 5, 2018
6c3469f
Added test for custom renderer in index string
valentijnnieman Sep 5, 2018
b46f0df
Use updated dash-renderer tarball for now
valentijnnieman Sep 5, 2018
bd8c9fb
Also use dash-renderer tarball in dev-requirements.txt
valentijnnieman Sep 5, 2018
d4b0418
Update dev-requirements-py37
valentijnnieman Sep 5, 2018
d0120db
dash-renderer tarball in dev-requirements-py37
valentijnnieman Sep 5, 2018
421cd2d
Merged with master & resolved merge conflicts
valentijnnieman Sep 7, 2018
0fe6d64
Add renderer to interpolate_index
valentijnnieman Sep 14, 2018
09a0511
application/json -> application/javascript
valentijnnieman Sep 14, 2018
ec95f0b
Add renderer to interpolate_index
valentijnnieman Sep 14, 2018
2a0c922
Add (broken) test for interpolate_index renderer
valentijnnieman Sep 14, 2018
92e6bd8
Fixed typo
valentijnnieman Sep 14, 2018
827e1a3
Add wait_for_text_to_equal and use it in renderer tests
valentijnnieman Sep 14, 2018
2486dcb
Merge branch 'master' of https://github.com/plotly/dash into standalo…
valentijnnieman Feb 15, 2019
01b3216
Try dash-renderer egg instead of tarball :egg:
valentijnnieman Feb 15, 2019
f3a85ca
Add egg to dev-req files too :egg:
valentijnnieman Feb 15, 2019
ea2b623
Use egg of custom_hooks branch of dash-renderer in circleci config to…
valentijnnieman Feb 15, 2019
1358dbe
Use ActionChain instead of .clear() to fix tests
valentijnnieman Feb 21, 2019
f9002e1
Add correct interpolated index test & fix wildcard test
valentijnnieman Feb 21, 2019
0150977
Remove print from test :see_no_evil:
valentijnnieman Feb 21, 2019
bf651ff
Fix formatting
valentijnnieman Feb 21, 2019
ba95dc6
Remove unused code
valentijnnieman Feb 21, 2019
b94ca9a
Remove unused requirements files
valentijnnieman Feb 27, 2019
11d98a1
Changed renderer param example string
valentijnnieman Feb 27, 2019
caf21a2
Add check for new DashRenderer
valentijnnieman Feb 27, 2019
d15f0ba
const -> var
valentijnnieman Feb 27, 2019
4797b2a
Missing > in script tag
valentijnnieman Feb 27, 2019
7fc8a80
Remove unused wait_for methods
valentijnnieman Feb 27, 2019
5fbadf1
Revert renderer scripts check
valentijnnieman Feb 27, 2019
1c59379
Merge branch 'master' of https://github.com/plotly/dash into standalo…
valentijnnieman Feb 28, 2019
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
19 changes: 16 additions & 3 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from ._utils import get_asset_path as _get_asset_path
from . import _configs


_default_index = '''
<!DOCTYPE html>
<html>
Expand All @@ -42,6 +41,7 @@
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
Expand Down Expand Up @@ -217,6 +217,9 @@ def add_url(name, view_func, methods=('GET',)):
self._cached_layout = None
self.routes = []

# default renderer string
self.renderer = 'const renderer = new DashRenderer();'
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved

@property
def layout(self):
return self._layout
Expand Down Expand Up @@ -378,6 +381,13 @@ def _generate_config_html(self):
'</script>'
).format(json.dumps(self._config()))

def _generate_renderer(self):
return (
'<script id="_dash-renderer" type"application/json">'
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
'{}'
'</script'
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
).format(self.renderer)

def _generate_meta_html(self):
has_ie_compat = any(
x.get('http-equiv', '') == 'X-UA-Compatible'
Expand Down Expand Up @@ -431,6 +441,7 @@ def index(self, *args, **kwargs): # pylint: disable=unused-argument
css = self._generate_css_dist_html()
config = self._generate_config_html()
metas = self._generate_meta_html()
renderer = self._generate_renderer()
title = getattr(self, 'title', 'Dash')
if self._favicon:
favicon = '<link rel="icon" type="image/x-icon" href="{}">'.format(
Expand All @@ -440,7 +451,8 @@ def index(self, *args, **kwargs): # pylint: disable=unused-argument

index = self.interpolate_index(
metas=metas, title=title, css=css, config=config,
scripts=scripts, app_entry=_app_entry, favicon=favicon)
scripts=scripts, app_entry=_app_entry, favicon=favicon,
renderer=renderer)

checks = (
(_re_index_entry_id.search(index), '#react-entry-point'),
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -462,7 +474,7 @@ def index(self, *args, **kwargs): # pylint: disable=unused-argument

def interpolate_index(self,
metas='', title='', css='', config='',
scripts='', app_entry='', favicon=''):
scripts='', app_entry='', favicon='', renderer=''):
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
"""
Called to create the initial HTML string that is loaded on page.
Override this method to provide you own custom HTML.
Expand Down Expand Up @@ -506,6 +518,7 @@ def interpolate_index(self, **kwargs):
config=config,
scripts=scripts,
favicon=favicon,
renderer=renderer,
app_entry=app_entry)

def dependencies(self):
Expand Down
Binary file added dash_renderer-0.13.1.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dev-requirements-py37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dash_core_components>=0.27.2
dash_html_components>=0.12.0
dash-flow-example==0.0.3
dash-dangerously-set-inner-html
dash_renderer
dash_renderer-0.13.1.tar.gz
percy
selenium
mock
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dash_core_components>=0.27.2
dash_html_components>=0.12.0rc3
dash_flow_example==0.0.3
dash-dangerously-set-inner-html
dash_renderer
dash_renderer-0.13.1.tar.gz
percy
selenium
mock
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ appnope==0.1.0
backports.shutil-get-terminal-size==1.0.0
click==6.7
dash-core-components==0.3.3
dash_renderer-0.13.1.tar.gz
dash-html-components==0.12.0
dash-renderer==0.2.9
dash.ly==0.14.0
decorator==4.0.11
enum34==1.1.6
Expand Down
82 changes: 82 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def test_index_customization(self):
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
<div id="custom-footer">My custom footer</div>
<script>
Expand Down Expand Up @@ -379,6 +380,7 @@ def test_assets(self):
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
Expand Down Expand Up @@ -494,6 +496,7 @@ def test_external_files_init(self):
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
Expand Down Expand Up @@ -532,3 +535,82 @@ def create_layout():

self.startServer(app)
time.sleep(0.5)

def test_with_custom_renderer(self):
app = dash.Dash(__name__)

app.index_string = '''
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
</head>
<body>
<div>Testing custom DashRenderer</div>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
<script id="_dash-renderer" type"application/json">
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
console.log('firing up a custom renderer!')
const renderer = new DashRenderer({
request_pre: () => {
var output = document.getElementById('output-pre')
if(output) {
output.innerHTML = 'request_pre changed this text!';
}
},
request_post: () => {
var output = document.getElementById('output-post')
if(output) {
output.innerHTML = 'request_post changed this text!';
}
}
})
</script>
</footer>
<div>With request hooks</div>
</body>
</html>
'''

app.layout = html.Div([
dcc.Input(
id='input',
value='initial value'
),
html.Div(
html.Div([
html.Div(id='output-1'),
html.Div(id='output-pre'),
html.Div(id='output-post')
])
)
])

call_count = Value('i', 0)

@app.callback(Output('output-1', 'children'), [Input('input', 'value')])
def update_output(value):
call_count.value = call_count.value + 1
return value
call_count = Value('i', 0)
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved

self.startServer(app)

input1 = self.wait_for_element_by_id('input')
input1.clear()

input1.send_keys('fire request hooks')

output1 = self.wait_for_element_by_id('output-1')
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
output_pre = self.wait_for_element_by_id('output-pre')
output_post = self.wait_for_element_by_id('output-post')

self.assertEqual('fire request hooks', output1.text)
self.assertEqual('request_pre changed this text!', output_pre.text)
self.assertEqual('request_post changed this text!', output_post.text)
self.percy_snapshot(name='request-hooks')