import uuid from flask import render_template_string def make_id(): return uuid.uuid4() def make_widget(template, widget_type, sub_topic=None, extra_classes=None, **kwargs): def f(): _classes = ['widget'] + list(extra_classes or []) _attrs = {} if sub_topic: _classes.append('subscriber') _attrs['data-sub-topic'] = sub_topic _classes.append(f'{widget_type}-widget') kwargs['sub_topic'] = sub_topic _attrs['class'] = ' '.join([c for c in _classes if c is not None]) attr_str = " ".join([f'{k}="{v}"' for k, v in _attrs.items()]) widget_template = f'
\n{template}\n
' return render_template_string(widget_template, **kwargs) return f def button_widget(topic, label, pub_value): template = ''' ''' return make_widget(template, 'button', label=label, pub_value=pub_value, topic=topic) def slider_widget(label, topic, sub_topic=None, value_path=None, unit='', min_val=0, max_val=255): id_ = make_id() sub_topic = sub_topic or topic template = '''
- {{unit}}
{{label}}
''' return make_widget(template, 'slider', sub_topic=sub_topic, label=label, min_val=min_val, max_val=max_val, value_path=value_path, topic=topic, unit=unit, id_=id_) def label_widget(topic, label, unit=''): template = '''
- {{unit}}
{{label}}
''' return make_widget(template, 'label', sub_topic=topic, topic=topic, unit=unit, label=label) def log_widget(topic, label): id_ = make_id() template = '''
TimeMessage
{{label}}
''' return make_widget(template, "log", sub_topic=topic, topic=topic, label=label, id_=id_) def row_layout(title, elems): def f(): template = '''
{% if title %}
{{title}}
{% endif %}
{% for w in elems %}
{{w()|safe}}
{% endfor %}
''' return render_template_string(template, title=title, elems=elems) return f def column_layout(title, elems): def f(): template = ''' {% if title %}
{{title}}
{% endif %}
{% for w in elems %}
{{w()|safe}}
{% endfor %}
''' return render_template_string(template, title=title, elems=elems) return f