def edit_rules(): """Edit rules Edit the rules that process inbound events""" my_rules = rules.get_all_rules() my_rules.append(DEFAULT_RULE) selected_rule_id = select( label="Existing rules", options=[{ "label": rule["name"], "value": rule["id"] } for rule in my_rules], ) # Rules have unique IDs from the database: logging.info(f"selected_rule: {selected_rule_id}") use_rule = [r for r in my_rules if r["id"] == int(selected_rule_id)][0] updated_rule = input_group( "Rule editing", [ input("name", type=TEXT, name="name", value=use_rule["name"], required=True), # Need ttextarea( textarea( "Rule names", name="rule", rows=10, code={ "mode": "python", # code language "theme": "darcula", # Codemirror theme. Visit https://codemirror.net/demo/theme.html#cobalt to get more themes }, value=f"""{use_rule['rule']}\n""", ), actions( "actions", [ # {"label": "test", "value": "test"}, { "label": "save", "value": "save" }, ], name="action", help_text="Save", ), ], ) if updated_rule is not None: rl = dict(updated_rule) if rl["action"] == "save": rule_info = rules.save_rule(rl["name"], rl["rule"], selected_rule_id) put_row(put_text("Rule")) put_row(put_code(pprint.pformat(rule_info, indent=1))) # Use webhook_info's ID to add/update the extractor put_text(f"The rule added is: {updated_rule}")
def put_select(name, options=None, *, label='', multiple=None, value=None, help_text=None, scope=None, position=OutputPosition.BOTTOM, **other_html_attrs) -> Output: """Output a select widget. Refer to: `pywebio.input.select()`""" from pywebio.input import select check_name(name) single_input_return = select(name=name, options=options, label=label, multiple=multiple, value=value, help_text=help_text, **other_html_attrs) return _pin_output(single_input_return, scope, position)
def setting(): data = input_group(inputs=[ select(name='language', label='Language', options=LANGUAGES, value=V.lang, required=True), checkbox( name='check', label='Other settings', options=[{ "label": 'Button [Next] only shows untranslated key', 'value': 'untranslated', 'selected': V.untranslated_only }, { "label": 'Do not fill input with old value (only effect the language you selected)', "value": "clear", "selected": V.clear }]) ]) V.lang = data['language'] V.untranslated_only = True if 'untranslated' in data['check'] else False V.clear = True if 'clear' in data['check'] else False
def edit_webhook(): """Edit webhook and its extractor This should be changed to work with a programattically named webhook that has a randmoly named inbound address. For now, we name it I guess """ my_hooks = extractions.get_all_webhooks() my_hooks.append({ "id": 0, "name": "Your new webhook could go here!", "path": "Enter the path that this will be matched on", "queue_name": "base name for the queue", }) selected_hook_id = select( label="Existing webhooks", options=[{ "label": hook.get("name", "No hook name found"), "value": hook.get("id", 0), } for hook in my_hooks], ) # Rules have unique IDs from the database: logging.info(f"selected_hook: {selected_hook_id}") my_hook = dict() my_extractor = dict() my_hook.update(DEFAULT_HOOK) my_extractor.update(DEFAULT_EXTRACTOR) if selected_hook_id != 0: my_hook = extractions.get_webhook(selected_hook_id) my_extractor = extractions.get_hook_extractor(selected_hook_id) # TODO: update validator to avoid default hook 0 names updated_extractor = input_group( "Hook data and hook extractor", [ input("name", type=TEXT, name="name", value=my_hook["name"], required=True), # Need to get a way to carry around the IDs input("path", type=TEXT, name="path", value=my_hook["path"], required=True), # Need to get a way to carry around the IDs input( "queue_name", type=TEXT, name="queue_name", value=my_hook["queue_name"], required=True, ), # Need to get a way to carry around the IDs textarea( "Example message", name="example", rows=10, code={ "mode": "python", # code language "theme": "darcula", # Codemirror theme. Visit https://codemirror.net/demo/theme.html#cobalt to get more themes }, value=my_extractor["example"], ), textarea( "Edit an extraction rule", name="extractor", rows=10, code={ "mode": "python", # code language "theme": "darcula", # Codemirror theme. Visit https://codemirror.net/demo/theme.html#cobalt to get more themes }, value=my_extractor["extractor"], ), actions( "actions", [ { "label": "test", "value": "test" }, { "label": "save", "value": "save" }, ], name="action", help_text="Save or test", ), ], validate=test_extractor, ) # Pretty much can't be none, but let's follow the example from # https://pywebio.readthedocs.io/en/latest/input.html#pywebio.input.actions if updated_extractor is not None: uex = dict(updated_extractor) if uex["action"] == "save": webhook_info = extractions.save_webhook(selected_hook_id, uex["name"], uex["path"], uex["queue_name"]) extractor_info = extractions.save_extractor( uex["name"], webhook_info["id"], uex["extractor"], uex["example"]) put_row(put_text("Webhook")) put_row(put_code(pprint.pformat(webhook_info, indent=1))) put_row(put_text("Extractor")) put_row(put_code(pprint.pformat(extractor_info, indent=1)))
from pywebio import input, output from time import * output.put_markdown("## Hi </fooders>") output.put_markdown("<p>I am Jayant</p>") output.put_table([["Name", "code"], ['Food', 11], ['Water', 12], ['Sunlight', 13]]) with output.popup("Subscribe to the page"): output.put_text("Join Other Customer!") food = input.select("Choose Your Favorite Food", ['Maggi', 'Noodles']) output.put_text("You Choose", food, "from given list") output.put_processbar('bar') for i in range(1, 11): output.set_processbar('bar', i / 10) sleep(0.1) output.put_markdown("Your Order is ready") if food == "Maggi": with open("maggi.jpg", "rb+") as f: image = f.read() else: with open("noodles.jpg", "rb+") as f: image = f.read() output.put_image(image, height="100", width="100")
def get_inputs(): out = [] old = deep_get(dict_lang[V.lang], f'{V.group}.{V.arg}.{V.key}', 'Key not found!') out.append( input( name=V.lang, label=V.lang, value=None if V.clear else old, help_text=f'{V.group}.{V.arg}.{V.key}', placeholder=old, )) out.append( select(name='group', label='Group', options=V.groups, value=V.group, onchange=lambda g: update_var(group=g), required=True)) out.append( select(name='arg', label='Arg', options=V.args, value=V.arg, onchange=lambda a: update_var(arg=a), required=True)) out.append( select(name='key', label='Key', options=V.keys, value=V.key, onchange=lambda k: update_var(key=k), required=True)) _LANGUAGES = LANGUAGES.copy() _LANGUAGES.remove(V.lang) for L in _LANGUAGES: out.append( input(name=L, label=L, readonly=True, value=deep_get(dict_lang[L], f'{V.group}.{V.arg}.{V.key}', 'Key not found!'))) out.append( actions(name='action', buttons=[ { "label": "Next", "value": 'Next', "type": "submit", "color": "success" }, { "label": "Next without save", "value": 'Skip', "type": "submit", "color": "secondary" }, { "label": "Submit", "value": "Submit", "type": "submit", "color": "primary" }, { "label": "Quit and save", "type": "cancel", "color": "secondary" }, ])) return out