Skip to content

Commit fbe45df

Browse files
delsimGibbsConsulting
authored andcommitted
Improve docs and other fixes (#151)
* Minor doc tweaks, and improve static substitution location * Demo should use latest static substitution logic * Refine static use and fix demo session app * Add renderer script to directly rendered apps * Make pipe demo run on a completely local basis * Remove unwanted print statements * Update dpd version * Add in coveralls package to dev requirements
1 parent 773ed08 commit fbe45df

File tree

11 files changed

+39
-19
lines changed

11 files changed

+39
-19
lines changed

demo/demo/bootstrap_app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ def session_demo_alert_callback(n_clicks, session_state=None, **kwargs):
7272
raise NotImplementedError("Cannot handle a missing session state")
7373
csf = session_state.get('bootstrap_demo_state', None)
7474
if not csf:
75-
csf = dict(clicks=0)
76-
session_state['bootstrap_demo_state'] = csf
75+
csf = dict(clicks=0, overall=0)
7776
else:
7877
csf['clicks'] = n_clicks
78+
if n_clicks is not None and n_clicks > csf.get('overall_max',0):
79+
csf['overall_max'] = n_clicks
80+
session_state['bootstrap_demo_state'] = csf
7981
return "Button has been clicked %s times since the page was rendered" %n_clicks
8082

demo/demo/plotly_apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def callback_c(*args, **kwargs):
118118
return "Args are [%s] and kwargs are %s" %(",".join(args), str(kwargs))
119119

120120
liveIn = DjangoDash("LiveInput",
121-
serve_locally=False,
121+
serve_locally=True,
122122
add_bootstrap_links=True)
123123

124124
liveIn.layout = html.Div([

dev_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
coveralls>=1.6.0
12
channels>=2.0
23
channels-redis
34
daphne
45
Django>=2.0,<2.3
56
django-bootstrap4
67
django-redis
7-
dpd-static-support
8+
dpd-static-support>=0.0.4
89
grip
910
pandas
1011
pylint

django_plotly_dash/dash_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def dispatch(self):
453453
'Perform dispatch, using request embedded within flask global state'
454454
import flask
455455
body = flask.request.get_json()
456-
return self. dispatch_with_args(body, argMap=dict())
456+
return self.dispatch_with_args(body, argMap=dict())
457457

458458
#pylint: disable=too-many-locals
459459
def dispatch_with_args(self, body, argMap):

django_plotly_dash/middleware.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
2626
'''
2727

28-
from .util import serve_locally
28+
from .util import serve_locally, static_path
2929

3030
#pylint: disable=too-few-public-methods
3131

@@ -46,7 +46,7 @@ def add_config(self, config):
4646
def add_scripts(self, scripts):
4747
'Add js content'
4848
if scripts:
49-
self.scripts = scripts
49+
self.scripts += scripts
5050

5151
class ContentCollector:
5252
'''
@@ -103,9 +103,12 @@ def __call__(self, request):
103103

104104

105105
# Bootstrap4 substitutions, if available
106+
dpd_substitutions = []
106107
try:
107-
from dpd_static_support.mappings import substitutions as dpd_ss_substitutions
108-
substitutions += dpd_ss_substitutions
108+
from dpd_static_support.mappings import substitutions as dpd_direct_substitutions
109+
dpd_substitutions += dpd_direct_substitutions
110+
from dpd_static_support.mappings import static_substitutions as dpd_ss_substitutions
111+
dpd_substitutions += [(x, static_path(y)) for x, y in dpd_ss_substitutions]
109112
except Exception as e:
110113
pass
111114

@@ -119,7 +122,7 @@ def __init__(self, get_response):
119122
substitutions = []
120123

121124
if serve_locally():
122-
substitutions += dpd_ss_substitutions
125+
substitutions += dpd_substitutions
123126

124127
self._encoding = "utf-8"
125128

django_plotly_dash/templatetags/plotly_dash.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ def plotly_direct(context, name=None, slug=None, da=None):
9797

9898
# Load embedded holder inserted by middleware
9999
eh = context.request.dpd_content_handler.embedded_holder
100+
101+
# Need to add in renderer launcher
102+
renderer_launcher = '<script id="_dash-renderer" type="application/javascript">var renderer = new DashRenderer();</script>'
103+
100104
app.set_embedded(eh)
101105
try:
102106
resp = view_func()
103107
finally:
108+
eh.add_scripts(renderer_launcher)
104109
app.exit_embedded()
105110

106111
return locals()

django_plotly_dash/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
from django.conf import settings
2929
from django.core.cache import cache
30-
from django.contrib.staticfiles.templatetags.staticfiles import static
3130

3231
def _get_settings():
3332
try:
@@ -107,7 +106,14 @@ def full_asset_path(module_name, asset_path):
107106
return path_contrib
108107

109108
def static_asset_path(module_name, asset_path):
110-
return static(full_asset_path(module_name, asset_path))
109+
return static_path(full_asset_path(module_name, asset_path))
111110

112111
def serve_locally():
113112
return _get_settings().get('serve_locally', settings.DEBUG)
113+
114+
def static_path(relative_path):
115+
try:
116+
static_url = settings.STATIC_URL
117+
except:
118+
static_url = '/static/'
119+
return "%s%s" %(static_url, relative_path)

django_plotly_dash/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
2424
'''
2525

26-
__version__ = "0.9.10"
26+
__version__ = "0.9.11"

django_plotly_dash/views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from django.shortcuts import redirect
3131

3232
from .models import DashApp
33-
from .util import get_initial_arguments
33+
from .util import get_initial_arguments, static_path
3434

3535
def routes(*args, **kwargs):
3636
'Return routes'
@@ -122,9 +122,9 @@ def component_suites(request, resource=None, component=None, extra_element="", *
122122

123123
get_params = request.GET.urlencode()
124124
if get_params and False:
125-
redone_url = "/static/dash/component/%s/%s%s?%s" %(component, extra_element, resource, get_params)
125+
redone_url = static_path("dash/component/%s/%s%s?%s" %(component, extra_element, resource, get_params))
126126
else:
127-
redone_url = "/static/dash/component/%s/%s%s" %(component, extra_element, resource)
127+
redone_url = static_path("dash/component/%s/%s%s" %(component, extra_element, resource))
128128

129129
return HttpResponseRedirect(redirect_to=redone_url)
130130

@@ -133,9 +133,9 @@ def app_assets(request, **kwargs):
133133
get_params = request.GET.urlencode()
134134
extra_part = ""
135135
if get_params:
136-
redone_url = "/static/dash/assets/%s?%s" %(extra_part, get_params)
136+
redone_url = static_path("dash/assets/%s?%s" %(extra_part, get_params))
137137
else:
138-
redone_url = "/static/dash/assets/%s" % extra_part
138+
redone_url = static_path("dash/assets/%s" % extra_part)
139139

140140
return HttpResponseRedirect(redirect_to=redone_url)
141141

docs/configuration.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ and also providing a list of components used
7272
7373
# django-plotly-dash components
7474
'dpd_components',
75+
# static support if serving local assets
76+
'dpd_static_support',
7577
7678
# Other components, as needed
7779
'dash_bootstrap_components',

docs/installation.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ can be safely added now even if not used.
2626

2727
If assets are being served locally through the use of the global ``serve_locally`` or on a per-app basis, then
2828
``django_plotly_dash.middleware.ExternalRedirectionMiddleware`` should be added, along with the ``whitenoise`` package whose
29-
middleware should also be added as per the instructions for that package.
29+
middleware should also be added as per the instructions for that package. In addition, ``dpd_static_support`` should be
30+
added to the ``INSTALLED_APPS`` setting.
3031

3132
The application's routes need to be registered within the routing structure by an appropriate ``include`` statement in
3233
a ``urls.py`` file::

0 commit comments

Comments
 (0)