Support two output formats from plotly.plot.

This commit is contained in:
Christopher C. Wells 2019-05-29 09:47:21 -07:00
parent c8611fac39
commit bff17dd2a0
3 changed files with 16 additions and 16 deletions

View File

@ -25,6 +25,5 @@
{% block javascript %}
<script src="{% static "babybuddy/js/graph.js" %}"></script>
{{ js_graph|safe }}
{{ js_listener|safe }}
{{ js|safe }}
{% endblock %}

View File

@ -79,10 +79,15 @@ def rangeselector_date():
def split_graph_output(output):
"""
Split out of a Plotly graph in to html and javascript.
This function accounts for two output formats from different Plotly
versions: one with a single <script> tag and one with two <script> tags.
:param output: a string of html and javascript comprising the graph.
:returns: a tuple of the the graph's html and javascript.
"""
html, js_graph, js_listener = output.split('<script')
js_graph = '<script' + js_graph
js_listener = '<script' + js_listener
return html, js_graph, js_listener
try:
html, js_graph, js_listener = output.split('<script')
js = js_graph + js_listener
except ValueError:
html, js = output.split('<script')
js = '<script' + js
return html, js

View File

@ -21,7 +21,7 @@ class DiaperChangeLifetimesChildReport(PermissionRequired403Mixin, DetailView):
child = context['object']
changes = models.DiaperChange.objects.filter(child=child)
if changes and changes.count() > 1:
context['html'], context['js_graph'], context['js_listener'] = \
context['html'], context['js'] = \
graphs.diaperchange_lifetimes(changes)
return context
@ -40,7 +40,7 @@ class DiaperChangeTypesChildReport(PermissionRequired403Mixin, DetailView):
child = context['object']
changes = models.DiaperChange.objects.filter(child=child)
if changes:
context['html'], context['js_graph'], context['js_listener'] = \
context['html'], context['js'] = \
graphs.diaperchange_types(changes)
return context
@ -64,8 +64,7 @@ class FeedingDurationChildReport(PermissionRequired403Mixin, DetailView):
child = context['object']
instances = models.Feeding.objects.filter(child=child)
if instances:
context['html'], context['js_graph'], context['js_listener'] = \
graphs.feeding_duration(instances)
context['html'], context['js'] = graphs.feeding_duration(instances)
return context
@ -88,8 +87,7 @@ class SleepPatternChildReport(PermissionRequired403Mixin, DetailView):
child = context['object']
instances = models.Sleep.objects.filter(child=child).order_by('start')
if instances:
context['html'], context['js_graph'], context['js_listener'] = \
graphs.sleep_pattern(instances)
context['html'], context['js'] = graphs.sleep_pattern(instances)
return context
@ -112,8 +110,7 @@ class SleepTotalsChildReport(PermissionRequired403Mixin, DetailView):
child = context['object']
instances = models.Sleep.objects.filter(child=child).order_by('start')
if instances:
context['html'], context['js_graph'], context['js_listener'] = \
graphs.sleep_totals(instances)
context['html'], context['js'] = graphs.sleep_totals(instances)
return context
@ -131,6 +128,5 @@ class WeightWeightChildReport(PermissionRequired403Mixin, DetailView):
child = context['object']
objects = models.Weight.objects.filter(child=child)
if objects:
context['html'], context['js_graph'], context['js_listener'] = \
graphs.weight_weight(objects)
context['html'], context['js'] = graphs.weight_weight(objects)
return context