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()]) details_link = ''' {% if details_url %} {%endif%} {% if details_url %} {% endif %} ''' widget_template = f'''
{details_link}
{template}
''' return render_template_string(widget_template, **kwargs) return f def button_widget(topic, label, pub_value, release_value=None, width=None): template = ''' ''' return make_widget(template, 'button', label=label, pub_value=pub_value, release_value=release_value, width=width, topic=topic, repr=repr) def square_button_widget(*args, size="4em", **kwargs): return button_widget(*args, width=size, **kwargs) def slider_widget(label, topic, sub_topic=None, value_path=None, unit='', min_val=0, max_val=255, step=1, retain=False): 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, step=step, value_path=value_path, topic=topic, unit=unit, retain=retain, id_=id_) def label_widget(topic, label, unit='', **widget_args): id_ = make_id() template = '''
{{label}}
- {{unit}}
''' return make_widget(template, 'label', sub_topic=topic, topic=topic, unit=unit, label=label, id_=id_, **widget_args) def log_widget(topic, label, **widget_args): id_ = make_id() template = '''
TimeMessage
{{label}}
''' return make_widget(template, "log", sub_topic=topic, topic=topic, label=label, id_=id_, **widget_args) 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