def _checkbox( self, label: str, value: bool = False, key: Optional[Key] = None, help: Optional[str] = None, on_change: Optional[WidgetCallback] = None, args: Optional[WidgetArgs] = None, kwargs: Optional[WidgetKwargs] = None, *, # keyword-only arguments: disabled: bool = False, ctx: Optional[ScriptRunContext] = None, ) -> bool: key = to_key(key) check_callback_rules(self.dg, on_change) check_session_state_rules( default_value=None if value is False else value, key=key) checkbox_proto = CheckboxProto() checkbox_proto.label = label checkbox_proto.default = bool(value) checkbox_proto.form_id = current_form_id(self.dg) if help is not None: checkbox_proto.help = dedent(help) def deserialize_checkbox(ui_value: Optional[bool], widget_id: str = "") -> bool: return bool(ui_value if ui_value is not None else value) current_value, set_frontend_value = register_widget( "checkbox", checkbox_proto, user_key=key, on_change_handler=on_change, args=args, kwargs=kwargs, deserializer=deserialize_checkbox, serializer=bool, ctx=ctx, ) # This needs to be done after register_widget because we don't want # the following proto fields to affect a widget's ID. checkbox_proto.disabled = disabled if set_frontend_value: checkbox_proto.value = current_value checkbox_proto.set_value = True self.dg._enqueue("checkbox", checkbox_proto) return cast(bool, current_value)
def checkbox(self, label, value=False, key=None, help=None): """Display a checkbox widget. Parameters ---------- label : str A short label explaining to the user what this checkbox is for. value : bool Preselect the checkbox when it first renders. This will be cast to bool internally. key : str An optional string to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key. help : str A tooltip that gets displayed next to the checkbox. Returns ------- bool Whether or not the checkbox is checked. Example ------- >>> agree = st.checkbox('I agree') >>> >>> if agree: ... st.write('Great!') """ checkbox_proto = CheckboxProto() checkbox_proto.label = label checkbox_proto.default = bool(value) checkbox_proto.form_id = current_form_id(self.dg) if help is not None: checkbox_proto.help = help ui_value = register_widget("checkbox", checkbox_proto, user_key=key) current_value = ui_value if ui_value is not None else value return self.dg._enqueue("checkbox", checkbox_proto, bool(current_value))
def checkbox( self, label: str, value: bool = False, key: Optional[Key] = None, help: Optional[str] = None, on_change: Optional[WidgetCallback] = None, args: Optional[WidgetArgs] = None, kwargs: Optional[WidgetKwargs] = None, ) -> bool: """Display a checkbox widget. Parameters ---------- label : str A short label explaining to the user what this checkbox is for. value : bool Preselect the checkbox when it first renders. This will be cast to bool internally. key : str or int An optional string or integer to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. Multiple widgets of the same type may not share the same key. help : str An optional tooltip that gets displayed next to the checkbox. on_change : callable An optional callback invoked when this checkbox's value changes. args : tuple An optional tuple of args to pass to the callback. kwargs : dict An optional dict of kwargs to pass to the callback. Returns ------- bool Whether or not the checkbox is checked. Example ------- >>> agree = st.checkbox('I agree') >>> >>> if agree: ... st.write('Great!') """ key = to_key(key) check_callback_rules(self.dg, on_change) check_session_state_rules( default_value=None if value is False else value, key=key) checkbox_proto = CheckboxProto() checkbox_proto.label = label checkbox_proto.default = bool(value) checkbox_proto.form_id = current_form_id(self.dg) if help is not None: checkbox_proto.help = dedent(help) def deserialize_checkbox(ui_value: Optional[bool], widget_id: str = "") -> bool: return bool(ui_value if ui_value is not None else value) current_value, set_frontend_value = register_widget( "checkbox", checkbox_proto, user_key=key, on_change_handler=on_change, args=args, kwargs=kwargs, deserializer=deserialize_checkbox, serializer=bool, ) if set_frontend_value: checkbox_proto.value = current_value checkbox_proto.set_value = True self.dg._enqueue("checkbox", checkbox_proto) return cast(bool, current_value)