コード例 #1
0
    def slider(self, label, min, max, value, step=1, unit='', marks=False, on_change=None, id=None, *args, **kwargs):
        """"""
        form = MDCForm()
        label_ = MDCComponent(html.SPAN(f'{label}'))
        label_ .mdc.typography('subtitle1')
        form <= label_
        form <= MDCComponent(html.SPAN(
            f' {self._round(value)} {unit}', id=f'value_{id}')).mdc.typography('caption')
        slider_ = form.mdc.Slider(
            'Slider', min=min, max=max, value=value, step=step, marks=marks, *args, **kwargs)

        if on_change:
            slider_.mdc.listen('MDCSlider:change',
                               lambda evt: on_change(self.widgets[id]))

        if id:
            self.widgets[id] = self._fix_value(value)
            self.component[id] = slider_

            def set_value(id, value):
                self.widgets[id] = self._round(value)
                document.select_one(
                    f'#value_{id}').html = f' {self.get_value(id)} {unit}'

            slider_.mdc.listen('MDCSlider:input', lambda event: set_value(
                id, slider_.mdc.getValue()))

        return form
コード例 #2
0
    def range_slider(self, label, min, max, value_lower, value_upper, step, unit='', on_change=None, id=None, *args, **kwargs):
        """"""
        form = MDCForm()
        label_ = MDCComponent(html.SPAN(f'{label}'))
        label_ .mdc.typography('subtitle1')
        form <= label_
        form <= MDCComponent(html.SPAN(
            f' {self._round(value_lower)}-{self._round(value_upper)} {unit}', id=f'value_{id}')).mdc.typography('caption')
        slider_ = form.mdc.RangeSlider(
            'Slider', min, max, value_lower, value_upper, step, *args, **kwargs)

        if on_change:
            slider_.mdc.listen('MDCSlider:change',
                               lambda evt: on_change(self.widgets[id]))

        if id:
            self.widgets[id] = [self._fix_value(
                value_lower), self._fix_value(value_upper)]
            self.component[id] = slider_

            def set_value(id, value):
                self.widgets[id] = [self._round(
                    value[0]), self._round(value[1])]
                document.select_one(
                    f'#value_{id}').html = f' {self.get_value(id)[0]}-{self.get_value(id)[1]} {unit}'

            slider_.mdc.listen('MDCSlider:input', lambda event: set_value(
                id, [slider_.mdc.getValueStart(), slider_.mdc.getValue()]))

        return form
コード例 #3
0
    def select(self, label, options, value=None, on_change=None, id=None):
        """"""
        label_ = MDCComponent(html.SPAN(f'{label}'))
        label_ .mdc.typography('subtitle1')
        form = MDCForm(formfield_style={
                       'width': '100px', 'min-height': '90px', 'margin-left': '15px'})
        form <= label_
        select_ = form.mdc.Select('', options=options, selected=value)

        if id:
            self.widgets[id] = value
            self.component[id] = select_

            def set_value():
                def wrap(evt):
                    self.widgets[id] = select_.mdc.value
                return wrap

            select_.mdc.listen('MDCSelect:change', set_value())

        if on_change:
            select_.mdc.listen('MDCSelect:change',
                               lambda evt: on_change(select_.mdc.value))

        return form
コード例 #4
0
    def radios(self, label, options, on_change=None, id=None):
        """"""
        label = MDCComponent(html.SPAN(f'{label}'))
        label.mdc.typography('subtitle1')
        radios_ = []
        form = MDCForm(formfield_style={'width': '100px'})
        form <= label

        for i, (radio, value) in enumerate(options):
            radios_.append(
                [form.mdc.Radio(radio, name=id, checked=(i == 0)), value])

        if id:
            self.widgets[id] = options[0][1]
            self.component[id] = radios_

            def set_value(value):
                def wrap(evt):
                    self.widgets[id] = value
                return wrap

            for radio, value in radios_:
                radio.bind('change', set_value(value))

        if on_change:
            for radio, _ in radios_:
                radio.bind('change', lambda evt: on_change())

        return form
コード例 #5
0
    def label(self, text, typo='body1', style={}, id=None, *args, **kwargs):
        """"""
        label = MDCComponent(html.SPAN(f'{text}'), style={
                             **style, **{'width': '100%', 'display': 'flex'}}, *args, **kwargs)
        label.mdc.typography(typo)

        if id:
            self.component[id] = label

        return label
コード例 #6
0
ファイル: main.py プロジェクト: UN-GCPDS/brython-radiant
    def __init__(self, *args, **kwargs):
        """"""
        super().__init__(*args, **kwargs)

        document.select('body')[0] <= html.H1('Hello World')
        submodule_fn()

        document.select('body')[0] <= html.H3(self.MyClass.local_python())

        a, b = 3, 5
        c = self.MyClass.pitagoras(a, b)
        document.select('body')[0] <= html.H3(
            f"Pitagoras: {a=}, {b=}, {c=:.3f}")

        self.add_css_file('custom_styles.css')

        document <= MDCButton("Button", raised=False, unelevated=True)
        document <= MDCButton("Button raised", raised=True, unelevated=True)
        button = MDCButton("Button outlined",
                           raised=False,
                           outlined=True,
                           unelevated=True)
        button.bind('click', self.on_button)

        document <= button

        label = 'SLIDER'
        unit = 'PX'
        id = 'XXXX'

        form = MDCForm()
        label_ = MDCComponent(html.SPAN(f'{label}'))
        label_.mdc.typography('subtitle1')
        form <= label_
        slider_ = form.mdc.Slider('Slider', min=1, max=100, step=5, value=50)

        options = [[f'Option-{o}', f'value-{o}'] for o in range(10)]
        value = 'value-5'
        self.select_ = form.mdc.Select('', options=options, selected=value)

        document.select('body')[0] <= form
コード例 #7
0
    def checkbox(self, label, options, on_change=None, id=None):
        """"""
        label = MDCComponent(html.SPAN(f'{label}'),
                             style={'flex-basis': '100%'})
        label.mdc.typography('subtitle1')
        checkbox_ = []
        form = MDCForm(formfield_style={
            'min-width': 'unset',
            'width': 'unset',
            # 'display': 'inline',
            # 'vertical-align': 'super',
        })
        form <= label
        form <= html.BR()

        for checkbox, checked in options:
            checkbox_.append(
                [form.mdc.Checkbox(checkbox, name=id, checked=checked), checkbox])

        if id:
            self.widgets[id] = [ch[0] for ch in options if ch[1]]
            self.component[id] = checkbox_

            def set_value():
                def wrap(evt):
                    self.widgets[id] = [value for ch,
                                        value in checkbox_ if ch.mdc.checked]
                return wrap

            for checkbox, _ in checkbox_:
                checkbox.bind('change', set_value())

        if on_change:
            for checkbox, _ in checkbox_:
                checkbox.bind('change', lambda evt: on_change())

        return form