posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.32k stars 81 forks source link

Adding attributes to inputs #1730

Open thohan88 opened 1 month ago

thohan88 commented 1 month ago

There may be an easy way of doing this, but all my attempts to add attributes to an input using input_xxx() has failed.

My use case is adding bootstrap validation by setting the required attribute. It would be nice if something like this could work:

ui.input_text("id", "label", {"required":True})
# TypeError: Invalid type for attribute: <class 'dict'>.

I guess it would not be clear whether such attributes applies to the <form>, <label> or <input> tag, so I'm not really sure what a good api looks like here. This is an okay workaround for simple inputs, but more cumbersome for complex inputs (e.g input_select).

ui.tags.input(
  id = "id", 
  label = "label",
  type = "text",
  required = True
).get_html_string
# <input id="id" label="label" type="text" required=""/>

I also believe this workaround requires you to use resolve_id(id) manually to make it work in modules?