Skip to content

Commit 20f14c6

Browse files
author
Mark Gibbs
committed
Tweak django dash wrapper so that jupyter wrapper can also use it
1 parent 7134b2d commit 20f14c6

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ See the source for this project here:
1010
This README file provides a short guide to installing and using the package, and also
1111
outlines how to run the demonstration application.
1212

13-
14-
1513
More detailed information
1614
can be found in the online documentation at
1715
<https://readthedocs.org/projects/django-plotly-dash>

django_plotly_dash/dash_wrapper.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,30 @@ def have_current_state_entry(self, wid, key):
7272
'Do nothing impl - only matters if state present'
7373
pass
7474

75-
def form_dash_instance(self, replacements=None, specific_identifier=None):
75+
def get_base_pathname(self, specific_identifier):
7676
if not specific_identifier:
7777
app_pathname = "%s:app-%s"% (app_name, main_view_label)
78+
ndid = self._uid
7879
else:
7980
app_pathname="%s:%s" % (app_name, main_view_label)
81+
ndid = specific_identifier
82+
83+
try:
84+
full_url = reverse(app_pathname,kwargs={'id':ndid})
85+
except:
86+
full_url = "/%s/" %ndid
87+
88+
return ndid, full_url
89+
90+
def form_dash_instance(self, replacements=None, specific_identifier=None):
91+
92+
ndid, base_pathname = self.get_base_pathname(specific_identifier)
8093

81-
rd = NotDash(name_root=self._uid,
82-
app_pathname=app_pathname,
94+
rd = NotDash(base_pathname=base_pathname,
8395
expanded_callbacks = self._expanded_callbacks,
8496
replacements = replacements,
85-
specific_identifier = specific_identifier)
97+
ndid = ndid)
98+
8699
rd.layout = self.layout
87100

88101
for cb, func in self._callback_sets:
@@ -126,22 +139,21 @@ def run(self,*args,**kwargs):
126139
pass
127140

128141
class NotDash(Dash):
129-
def __init__(self, name_root, app_pathname=None, replacements = None, specific_identifier=None, expanded_callbacks=False, **kwargs):
142+
def __init__(self, base_pathname=None, replacements = None, ndid=None, expanded_callbacks=False, **kwargs):
130143

131-
if specific_identifier is not None:
132-
self._uid = specific_identifier
133-
else:
134-
self._uid = name_root
144+
self._uid = ndid
135145

136146
self._flask_app = Flask(self._uid)
137147
self._notflask = NotFlask()
138-
self._base_pathname = reverse(app_pathname,kwargs={'id':self._uid})
148+
self._base_pathname = base_pathname
139149

140150
kwargs['url_base_pathname'] = self._base_pathname
141151
kwargs['server'] = self._notflask
142152

143153
super(NotDash, self).__init__(**kwargs)
144154

155+
self.css.config.serve_locally = True
156+
145157
self._adjust_id = False
146158
self._dash_dispatch = not expanded_callbacks
147159
if replacements:
@@ -158,17 +170,16 @@ def use_dash_layout(self):
158170

159171
def augment_initial_layout(self, base_response):
160172
if self.use_dash_layout() and False:
161-
return HttpResponse(base_response.data,
162-
content_type=base_response.mimetype)
173+
return base_response.data, base_response.mimetype
163174
# Adjust the base layout response
164175
baseDataInBytes = base_response.data
165176
baseData = json.loads(baseDataInBytes.decode('utf-8'))
166177
# Walk tree. If at any point we have an element whose id matches, then replace any named values at this level
167178
reworked_data = self.walk_tree_and_replace(baseData)
168179
response_data = json.dumps(reworked_data,
169180
cls=PlotlyJSONEncoder)
170-
return HttpResponse(response_data,
171-
content_type=base_response.mimetype)
181+
182+
return response_data, base_response.mimetype
172183

173184
def walk_tree_and_extract(self, data, target):
174185
if isinstance(data, dict):

django_plotly_dash/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.urls import path
22
from django.views.decorators.csrf import csrf_exempt
33

4-
from .views import routes, layout, dependencies, update, main_view
4+
from .views import routes, layout, dependencies, update, main_view, component_suites
55

66
from .app_name import app_name, main_view_label
77

@@ -11,11 +11,13 @@
1111
path('instance/<slug:id>_dash-dependencies', dependencies, name="dependencies"),
1212
path('instance/<slug:id>_dash-update-component', csrf_exempt(update), name="update-component"),
1313
path('instance/<slug:id>', main_view, name=main_view_label),
14+
path('instance/<slug:id>_dash-component-suites/<slug:component>/<resource>', component_suites, name='component-suites'),
1415

1516
path('app/<slug:id>_dash-routes', routes, {'stateless':True}, name="app-routes"),
1617
path('app/<slug:id>_dash-layout', layout, {'stateless':True}, name="app-layout"),
1718
path('app/<slug:id>_dash-dependencies', dependencies, {'stateless':True}, name="app-dependencies"),
1819
path('app/<slug:id>_dash-update-component', csrf_exempt(update), {'stateless':True}, name="app-update-component"),
1920
path('app/<slug:id>', main_view, {'stateless':True}, name='app-%s'%main_view_label),
21+
path('app/<slug:id>_dash-component-suites/<slug:component>/<resource>', component_suites, {'stateless':True}, name='app-component-suites'),
2022
]
2123

django_plotly_dash/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.shortcuts import render, get_object_or_404
2-
from django.http import HttpResponse
2+
from django.http import HttpResponse, HttpResponseRedirect
33

44
import json
55

@@ -22,7 +22,9 @@ def layout(request, id, stateless=False, **kwargs):
2222

2323
mFunc = app.locate_endpoint_function('dash-layout')
2424
resp = mFunc()
25-
return app.augment_initial_layout(resp)
25+
response_data, mimetype = app.augment_initial_layout(resp)
26+
return HttpResponse(response_data,
27+
content_type=mimetype)
2628

2729
def update(request, id, stateless=False, **kwargs):
2830
da, app = DashApp.locate_item(id, stateless)
@@ -59,3 +61,16 @@ def main_view(request, id, stateless=False, **kwargs):
5961
resp = mFunc()
6062
return HttpResponse(resp)
6163

64+
def component_suites(request, resource=None, component=None, **kwargs):
65+
print("Blagging component suites")
66+
print(kwargs)
67+
68+
eBig = request.GET.urlencode()
69+
if len(eBig) > 0:
70+
redone_url = "/STATIC/%s/%s?%s" %(component, resource, eBig)
71+
else:
72+
redone_url = "/STATIC/%s/%s" %(component, resource)
73+
74+
print(redone_url)
75+
76+
return HttpResponseRedirect(redirect_to="/static/someth")

0 commit comments

Comments
 (0)