Skip to content

Commit 9c702c7

Browse files
author
Mark Gibbs
committed
More work around class and identifier generation for html tags
1 parent c290b43 commit 9c702c7

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

django_plotly_dash/dash_wrapper.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,26 @@ def dispatch_with_args(self, body, argMap):
414414

415415
return res
416416

417-
def extra_html_properties(self):
417+
def slugified_id(self):
418+
pre_slugified_id = self._uid
419+
return slugify(pre_slugified_id)
420+
421+
def extra_html_properties(self, prefix=None, posfix=None, template_type=None):
418422
'''
419423
Return extra html properties to allow individual apps to be styled separately.
420424
421425
The content returned from this function is injected unescaped into templates.
422426
'''
423427

424-
pre_slugified_id = self._uid
425-
slugified_id = slugify(pre_slugified_id)
428+
prefix = prefix if prefix else "django-plotly-dash"
429+
430+
post_part = "-%s" % posfix if posfix else ""
431+
template_type = template_type if template_type else "iframe"
432+
433+
slugified_id = self.slugified_id()
426434

427-
return 'class="django-plotly-dash django-plotly-dash-iframe django-plotly-dash-app-%s"' % slugified_id
435+
return "%(prefix)s %(prefix)s-%(template_type)s %(prefix)s-app-%(slugified_id)s%(post_part)s" % {'slugified_id':slugified_id,
436+
'post_part':post_part,
437+
'template_type':template_type,
438+
'prefix':prefix,
439+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<div style="{{dstyle}}" {%autoescape off%}{{app.extra_html_properties}}{%endautoescape%}>
1+
<div style="{{dstyle}}">
22
<iframe src="{{app.base_url}}" style="{{istyle}}"frameborder="{{fbs}}"></iframe>
33
</div>

django_plotly_dash/templatetags/plotly_dash.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,36 @@ def plotly_message_pipe(context, url=None):
7272
'Insert script for providing background websocket connection'
7373
url = url if url else ws_default_url
7474
return locals()
75+
76+
@register.simple_tag()
77+
def plotly_app_identifier(name=None, slug=None, da=None, postfix=None):
78+
if name is not None:
79+
da, app = DashApp.locate_item(name, stateless=True)
80+
81+
if slug is not None:
82+
da, app = DashApp.locate_item(slug, stateless=False)
83+
84+
if not app:
85+
app = da.as_dash_instance()
86+
87+
slugified_id = app.slugified_id()
88+
89+
if postfix:
90+
return "%s-%s" %(slugified_id, postfix)
91+
return slugified_id
92+
93+
@register.simple_tag()
94+
def plotly_class(name=None, slug=None, da=None, prefix=None, postfix=None, template_type=None):
95+
96+
if name is not None:
97+
da, app = DashApp.locate_item(name, stateless=True)
98+
99+
if slug is not None:
100+
da, app = DashApp.locate_item(slug, stateless=False)
101+
102+
if not app:
103+
app = da.as_dash_instance()
104+
105+
return app.extra_html_properties(prefix=prefix,
106+
postfix=postfix,
107+
template_type=template_type)

docs/template_tags.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,59 @@ on the page, and its ordering relative to the ``Dash`` instances using updating
5454
the page footer - to avoid delaying the main page load - along
5555
with other scripts is generally advisable.
5656

57+
The ``plotly_app_identifier`` template tag
58+
-----------------------------------------
59+
60+
This tag provides an identifier for an app, in a form that is suitable for use as a classname or identifier
61+
in HTML:
62+
63+
.. code-block:: jinja
64+
65+
{%load plotly_dash%}
66+
67+
{%plotly_app_identifier name="SimpleExample"%}
68+
69+
{%plotly_app_identifier slug="liveoutput-2" postfix="A"%}
70+
71+
The identifier, if the tag is not passed a ``slug``, is the result of passing the identifier of the app through
72+
the ``django.utils.text.slugify`` function.
73+
74+
The tag arguments are:
75+
76+
:name = None: The name of the application, as passed to a ``DjangoDash`` constructor.
77+
:slug = None: The slug of an existing ``DashApp`` instance.
78+
:da = None: An existing ``django_plotly_dash.models.DashApp`` model instance.
79+
:postfix = None: An optional string; if specified it is appended to the identifier with a hyphen.
80+
81+
The validity rules for these arguments are the same as those for the ``plotly_app`` template tag.
82+
83+
The ``plotly_class`` template tag
84+
-----------------------------------------
85+
86+
Generate a string of class names, suitable for a ``div`` or other element that wraps around ``django-plotly-dash`` template content.
87+
88+
.. code-block:: jinja
89+
90+
{%load plotly_dash%}
91+
92+
<div class="{%plotly_class slug="liveoutput-2" postfix="A"%}">
93+
{%plotly_app slug="liveoutput-2" ratio="0.5" %}
94+
</div>
95+
96+
The identifier, if the tag is not passed a ``slug``, is the result of passing the identifier of the app through
97+
the ``django.utils.text.slugify`` function.
98+
99+
The tag arguments are:
100+
101+
:name = None: The name of the application, as passed to a ``DjangoDash`` constructor.
102+
:slug = None: The slug of an existing ``DashApp`` instance.
103+
:da = None: An existing ``django_plotly_dash.models.DashApp`` model instance.
104+
:prefix = None: Optional prefix to use in place of the text ``django-plotly-dash`` in each class name
105+
:postfix = None: An optional string; if specified it is appended to the app-specific identifier with a hyphen.
106+
:template_type = None: Optional text to use in place of ``iframe`` in the template-specific class name
107+
108+
The tag inserts a string with three class names in it. One is just the ``prefix`` argument, one
109+
has the ``template_type`` appended, and the final one has the app identifier (as generated
110+
by the ``plotly_app_identifier`` tag) and any ``postfix`` appended.
111+
112+
The validity rules for these arguments are the same as those for the ``plotly_app`` template tag.

0 commit comments

Comments
 (0)