Skip to content

Commit eb06c98

Browse files
author
Mark Gibbs
committed
Added admin step to save base state of dash app
1 parent 2f0d77b commit eb06c98

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

demo/demo/templates/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
{%load plotly_dash%}
44
<title>Simple stuff</title>
55
<body>
6-
<div width="300">
6+
<div>
77
Content here
8-
{%plotly_item "simpleexample-1"%}
8+
{%plotly_item "simpleexample-1" ratio=0.2 %}
99
</div>
1010
<div>
1111
Content here
1212
{%plotly_item "SimpleExample"%}
1313
</div>
14-
<div width="800">
14+
<div>
1515
Content here
1616
{%plotly_item "Ex2"%}
1717
</div>

django_plotly_dash/dash_wrapper.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def get_app_instance_by_id(id):
3636
return nd_apps.get(id,None)
3737

3838
def clear_app_instance(id):
39-
del nd_apps[id]
39+
try:
40+
del nd_apps[id]
41+
except:
42+
pass
4043

4144
def get_or_form_app(id, name, **kwargs):
4245
'''
@@ -170,6 +173,22 @@ def augment_initial_layout(self, base_response):
170173
return HttpResponse(response_data,
171174
content_type=base_response.mimetype)
172175

176+
def walk_tree_and_extract(self, data, target):
177+
if isinstance(data, dict):
178+
for key in ['children','props',]:
179+
self.walk_tree_and_extract(data.get(key,None),target)
180+
ident = data.get('id', None)
181+
if ident is not None:
182+
idVals = target.get(ident,{})
183+
for key, value in data.items():
184+
if key not in ['props','options','children','id']:
185+
idVals[key] = value
186+
if len(idVals) > 0:
187+
target[ident] = idVals
188+
if isinstance(data, list):
189+
for element in data:
190+
self.walk_tree_and_extract(element, target)
191+
173192
def walk_tree_and_replace(self, data):
174193
# Walk the tree. Rely on json decoding to insert instances of dict and list
175194
# ie we use a dna test for anatine, rather than our eyes and ears...

django_plotly_dash/models.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,40 @@ def get_app_instance(id):
5757
dd = get_app_by_name(id)
5858
return dd.form_dash_instance()
5959

60+
def _get_base_state(self):
61+
'''
62+
Get the base state of the object, as defined by the app.layout code, as a python dict
63+
'''
64+
# Get base layout response, from a base object
65+
base_app_inst = get_app_instance_by_id(self.app_name)
66+
if not base_app_inst:
67+
base_app = get_app_by_name(self.app_name)
68+
base_app_inst = base_app.form_dash_instance()
69+
70+
base_resp = base_app_inst.locate_endpoint_function('dash-layout')()
71+
72+
base_obj = json.loads(base_resp.data.decode('utf-8'))
73+
74+
# Walk the base layout and find all values; insert into base state map
75+
obj = {}
76+
base_app_inst.walk_tree_and_extract(base_obj, obj)
77+
return obj
78+
79+
def populate_values(self):
80+
'''
81+
Add values from the underlying dash layout configuration
82+
'''
83+
obj = self._get_base_state()
84+
self.base_state = json.dumps(obj)
85+
6086
class DashAppAdmin(admin.ModelAdmin):
61-
list_display = ['app_name','instance_name','slug','creation','update',]
87+
list_display = ['instance_name','app_name','slug','creation','update',]
6288
list_filter = ['app_name','creation','update',]
6389

90+
def _populate_values(self, request, queryset):
91+
for da in queryset:
92+
da.populate_values()
93+
da.save()
94+
_populate_values.short_description = "Populate app"
95+
96+
actions = ['_populate_values',]

django_plotly_dash/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ def update(request, id, **kwargs):
3737
resp = mFunc()
3838
else:
3939
# Use direct dispatch with extra arguments in the argMap
40+
print("Update args")
41+
print(rb)
4042
argMap = {'id':id,
4143
'request':request,
4244
'session':request.session}
4345
resp = app.dispatch_with_args(rb, argMap)
46+
print(resp.data)
4447

4548
return HttpResponse(resp.data,
4649
content_type=resp.mimetype)

0 commit comments

Comments
 (0)