Skip to content

Commit 1d973dd

Browse files
author
Mark Gibbs
committed
Improve code based on linter report
1 parent 82ae4e3 commit 1d973dd

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

demo/demo/dash_apps.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
TODO attribution here
44
'''
55

6+
# The linter doesn't like the members of the html and dcc imports (as they are dynamic?)
7+
#pylint: disable=no-member
8+
69
import dash
710
import dash_core_components as dcc
811
import dash_html_components as html
@@ -25,16 +28,16 @@
2528
# I was using e.g. Bootstrap inconsistenyly across the dash layout. Staying consistent worked fine for me.
2629
dash_example1.layout = html.Div(id='main',
2730
children=[
28-
html.Div([dcc.Dropdown(id='my-dropdown1',
29-
options=[{'label': 'New York City', 'value': 'NYC'},
30-
{'label': 'Montreal', 'value': 'MTL'},
31-
{'label': 'San Francisco', 'value': 'SF'}
32-
],
33-
value='NYC',
34-
className='col-md-12',
35-
),
36-
html.Div(id='test-output-div')
37-
]),
31+
html.Div([dcc.Dropdown(id='my-dropdown1',
32+
options=[{'label': 'New York City', 'value': 'NYC'},
33+
{'label': 'Montreal', 'value': 'MTL'},
34+
{'label': 'San Francisco', 'value': 'SF'}
35+
],
36+
value='NYC',
37+
className='col-md-12',
38+
),
39+
html.Div(id='test-output-div')
40+
]),
3841

3942
dcc.Dropdown(
4043
id='my-dropdown2',
@@ -49,7 +52,7 @@
4952

5053
html.Div(id='test-output-div2')
5154

52-
]) # end of 'main'
55+
]) # end of 'main'
5356

5457
@dash_example1.expanded_callback(
5558
dash.dependencies.Output('test-output-div', 'children'),
@@ -78,7 +81,7 @@ def callback_test(*args, **kwargs): #pylint: disable=unused-argument
7881

7982
fig = dict(data=data, layout=layout)
8083
line_graph = dcc.Graph(id='line-area-graph2', figure=fig, style={'display':'inline-block', 'width':'100%',
81-
'height':'100%;'} )
84+
'height':'100%;'})
8285
children = [line_graph]
8386

8487
return children
@@ -88,6 +91,7 @@ def callback_test(*args, **kwargs): #pylint: disable=unused-argument
8891
dash.dependencies.Output('test-output-div2', 'children'),
8992
[dash.dependencies.Input('my-dropdown2', 'value')])
9093
def callback_test2(*args, **kwargs):
94+
'Callback to exercise session functionality'
9195

9296
print(args)
9397
print(kwargs)

django_plotly_dash/dash_wrapper.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from plotly.utils import PlotlyJSONEncoder
3737

3838
from .app_name import app_name, main_view_label
39+
from .middleware import EmbeddedHolder
3940

4041
uid_counter = 0
4142

@@ -466,6 +467,7 @@ def index(self, *args, **kwargs): # pylint: disable=unused-argument
466467
metas = self._generate_meta_html()
467468
title = getattr(self, 'title', 'Dash')
468469
if self._favicon:
470+
import flask
469471
favicon = '<link rel="icon" type="image/x-icon" href="{}">'.format(
470472
flask.url_for('assets.static', filename=self._favicon))
471473
else:
@@ -484,7 +486,7 @@ def index(self, *args, **kwargs): # pylint: disable=unused-argument
484486

485487
return index
486488

487-
def interpolate_index(self, **kwargs):
489+
def interpolate_index(self, **kwargs): #pylint: disable=arguments-differ
488490

489491
if not self._return_embedded:
490492
resp = super(WrappedDash, self).interpolate_index(**kwargs)
@@ -497,6 +499,9 @@ def interpolate_index(self, **kwargs):
497499
return kwargs['app_entry']
498500

499501
def set_embedded(self, embedded_holder=None):
502+
'Set a handler for embedded references prior to evaluating a view function'
500503
self._return_embedded = embedded_holder if embedded_holder else EmbeddedHolder()
504+
501505
def exit_embedded(self):
506+
'Exit the embedded section after processing a view'
502507
self._return_embedded = False

django_plotly_dash/middleware.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,34 @@
44
This middleware enables the collection of items from templates for inclusion in the header and footer
55
'''
66

7+
#pylint: disable=too-few-public-methods
8+
79
class EmbeddedHolder(object):
10+
'Hold details of embedded content from processing a view'
811
def __init__(self):
912
self.css = ""
1013
self.config = ""
1114
self.scripts = ""
1215
def add_css(self, css):
16+
'Add css content'
1317
if css:
1418
self.css = css
1519
def add_config(self, config):
20+
'Add config content'
1621
if config:
1722
self.config = config
1823
def add_scripts(self, scripts):
24+
'Add js content'
1925
if scripts:
2026
self.scripts = scripts
2127

2228
class ContentCollector:
29+
'''
30+
Collect content during view processing, and substitute in response by finding magic strings.
31+
32+
This enables view functionality, such as template tags, to introduce content such as css and js
33+
inclusion into the header and footer.
34+
'''
2335
def __init__(self):
2436
self.header_placeholder = "DJANGO_PLOTLY_DASH_HEADER_PLACEHOLDER"
2537
self.footer_placeholder = "DJANGO_PLOTLY_DASH_FOOTER_PLACEHOLDER"
@@ -28,6 +40,7 @@ def __init__(self):
2840
self._encoding = "utf-8"
2941

3042
def adjust_response(self, response):
43+
'Locate placeholder magic strings and replace with content'
3144

3245
c1 = self._replace(response.content,
3346
self.header_placeholder,
@@ -48,6 +61,7 @@ def _encode(self, string):
4861
return string.encode(self._encoding)
4962

5063
class BaseMiddleware:
64+
'Django-plotly-dash middleware'
5165

5266
def __init__(self, get_response):
5367
self.get_response = get_response

django_plotly_dash/tests.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import pytest
3131

32+
#pylint: disable=bare-except
33+
3234
def test_dash_app():
3335
'Test the import and formation of the dash app orm wrappers'
3436

@@ -176,11 +178,11 @@ def test_injection_updating(client):
176178
have_thrown = False
177179

178180
try:
179-
response2 = client.post(url, json.dumps({'output':{'id':'test-output-div2', 'property':'children'},
180-
'inputs':[{'id':'my-dropdown2',
181-
'property':'value',
182-
'value':'TestIt'},
183-
]}), content_type="application/json")
181+
client.post(url, json.dumps({'output':{'id':'test-output-div2', 'property':'children'},
182+
'inputs':[{'id':'my-dropdown2',
183+
'property':'value',
184+
'value':'TestIt'},
185+
]}), content_type="application/json")
184186
except:
185187
have_thrown = True
186188

0 commit comments

Comments
 (0)